modules.filters 模块帮助
本章节包含 modules.filters 包中常用「数据筛选与过滤」模块的使用说明和示例,例如:
按条件筛选行(范围过滤、条件过滤等)
按字段值过滤、去重等操作
TableSeriesSelector
模块简介与适用场景
TableSeriesSelector从TableData或TableCollection中选择一列,输出TableSeries。典型适用场景:后续模块或绘图仅需某一字段(如深度、孔号),或从多表集合中按「表 + 字段」抽取一列;
select_field配置见「参数说明」。
端口说明
输入端口 -
InputTable:输入表数据(TableData)或表集合(TableCollection)。输出端口 -
OutputTableSeries:选取到的一列(TableSeries);当输入为空/表为空/字段不存在时为None。
快速上手示例:从单表选择一列
from gdisdk.modules.filters import TableSeriesSelector
selector = TableSeriesSelector(mname="SelectSeries")
selector.select_field = "depth" # 列名(也可以写列标题)
# selector.InputTable = table # TableData
selector.execute()
series = selector.OutputTableSeries.data
快速上手示例:从多表集合选择一列
from gdisdk.modules.filters import TableSeriesSelector
selector = TableSeriesSelector(mname="SelectSeriesFromCollection")
selector.select_field = ("剖面数据表", "x_coordinate") # (表名/表标题, 字段名/字段标题)
# selector.InputTable = tables # TableCollection
selector.execute()
series = selector.OutputTableSeries.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
选择的字段配置:当输入为 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import TableSeriesSelector
pipe = PipeLine(app_name="SelectSeriesDemo", app_title="选择一列示例")
selector = TableSeriesSelector(mname="SelectSeries")
selector.select_field = "depth"
# links = upstream.OutputTable >> selector.InputTable
# pipe.add_links(links)
# pipe.run()
# series = selector.OutputTableSeries.data
更多信息
TableFieldsSelector
模块简介与适用场景
TableFieldsSelector从TableData或TableCollection中**选择或删除若干列**,输出仍为TableData或 ``TableCollection``(结构与输入一致)。字段可用**列名**或**列标题**指定;
select_fields支持list``(统一字段列表)或 ``dict``(按表分别配置),详见「参数说明」与「``select_fields配置说明」。典型场景:GDIM 读入宽表后只保留绘图/统计所需列;从多表集合中批量裁剪各表字段;删除敏感或冗余列后再写入下游模块。
注意:
operation='select'且只保留一列时,输出仍是TableData``(不是 ``TableSeries);若只需单列且后续要TableSeries,请用TableSeriesSelector。
端口说明
输入端口 -
InputTable:输入表(TableData)或表集合(TableCollection)。输出端口 -
OutputTable:字段操作后的表或表集合;当输入为空、表为空或select_fields为None时为None。
快速上手示例:从单表选择若干列
from gdisdk.modules.filters import TableFieldsSelector
selector = TableFieldsSelector(mname="PickColumns")
selector.operation = "select"
selector.select_fields = ["depth", "x_coordinate"] # 列名或列标题
# selector.InputTable = table # TableData
selector.execute()
out_table = selector.OutputTable.data
快速上手示例:从单表删除若干列
from gdisdk.modules.filters import TableFieldsSelector
selector = TableFieldsSelector(mname="DropColumns")
selector.operation = "remove"
selector.select_fields = ["备注", "internal_id"]
# selector.InputTable = table
selector.execute()
out_table = selector.OutputTable.data
快速上手示例:从多表集合按表分别选列
from gdisdk.modules.filters import TableFieldsSelector
selector = TableFieldsSelector(mname="PickFieldsFromCollection")
selector.operation = "select"
selector.select_fields = {
"剖面数据表": ["x_coordinate", "y_coordinate"],
"钻孔表": ["孔号", "孔深"],
} # 键为表名或表标题
# selector.InputTable = tables # TableCollection
selector.execute()
out_tables = selector.OutputTable.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
要选取或删除的字段(列名/列标题)。 |
|
|
|
|
|
|
|
可选,重命名**输出**表或集合壳层的 |
|
|
|
仅当输入为 |
``select_fields`` 配置说明
输入类型 |
|
行为摘要 |
|---|---|---|
|
|
|
|
|
至少一个键须等于该表的 |
|
|
对**每个**嵌套表应用同一字段列表;未知标签在 |
|
|
键为表 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import TableFieldsSelector
pipe = PipeLine(app_name="SelectFieldsDemo", app_title="选择/删除列示例")
selector = TableFieldsSelector(mname="PickColumns")
selector.operation = "select"
selector.select_fields = ["depth", "x_coordinate"]
# links = upstream.OutputTable >> selector.InputTable
# pipe.add_links(links)
# pipe.run()
# out_table = selector.OutputTable.data
更多信息
TableSelector
模块简介与适用场景
TableSelector从TableCollection中选择一张表,输出TableData。典型适用场景:多表读取后只处理其中一张表;在 UI 中动态选择表(模块会根据输入集合生成可选项)。
table_name/table_idx见「参数说明」。
端口说明
输入端口 -
InputTables:输入表集合(TableCollection)。输出端口 -
OutputTable:选中的表(TableData);当输入为空/未命中table_name且未设置table_idx时为None。
快速上手示例:按表名/表标题选择
from gdisdk.modules.filters import TableSelector
selector = TableSelector(mname="SelectTable")
selector.table_name = "剖面数据表" # 表名或表标题;设置后会忽略 table_idx
# selector.InputTables = tables # TableCollection
selector.execute()
table = selector.OutputTable.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
要选择的表名或表标题;若不为 |
|
|
|
按索引选择(从 0 开始);仅在 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import TableSelector
pipe = PipeLine(app_name="SelectTableDemo", app_title="选择表示例")
selector = TableSelector(mname="SelectTable")
selector.table_name = "剖面数据表"
# links = upstream.OutputTables >> selector.InputTables
# pipe.add_links(links)
# pipe.run()
# table = selector.OutputTable.data
更多信息
TableCollectionSelector
模块简介与适用场景
TableCollectionSelector对TableCollection进行选取或剔除,输出新的 ``TableCollection``(保留主子表关系)。典型适用场景:从多表集合中只保留或排除指定表,避免后续模块误处理;
operation、table_names等见「参数说明」。
端口说明
输入端口 -
InputTables:输入表集合(TableCollection)。输出端口 -
OutputTables:处理后的表集合(TableCollection),且保留了已有的主子表关系。
快速上手示例:只保留指定表
from gdisdk.modules.filters import TableCollectionSelector
selector = TableCollectionSelector(mname="PickTables")
selector.operation = "select"
selector.table_names = ["剖面数据表", "钻孔表"] # 支持表名或表标题
# selector.InputTables = tables # TableCollection
selector.execute()
out_tables = selector.OutputTables.data
快速上手示例:移除指定表
from gdisdk.modules.filters import TableCollectionSelector
selector = TableCollectionSelector(mname="RemoveTables")
selector.operation = "remove"
selector.table_names = ["不需要的表"]
# selector.InputTables = tables
selector.execute()
out_tables = selector.OutputTables.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
要选取/移除的表名或表标题列表;若不为 |
|
|
|
要选取/移除的表索引列表;越界索引会被忽略。 |
|
|
|
|
注意
当
table_names与table_idxs都为None时:operation="select":输出None;operation="remove":直接返回**输入**的TableCollection引用。
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import TableCollectionSelector
pipe = PipeLine(app_name="SelectTablesDemo", app_title="选择/移除表集合示例")
selector = TableCollectionSelector(mname="SelectTables")
selector.operation = "select"
selector.table_names = ["剖面数据表"]
# links = upstream.OutputTables >> selector.InputTables
# pipe.add_links(links)
# pipe.run()
# tables = selector.OutputTables.data
更多信息
TablesQuery
模块简介与适用场景
TablesQuery使用 pandasDataFrame.query风格表达式过滤TableData或TableCollection,输出类型与输入一致。支持
query_template模板变量(UI 动态改值)及主表/子表级联过滤;表达式语法见下文「查询模板语法说明」,参数见「参数说明」。
端口说明
输入端口 -
InputTables:输入表(TableData)或表集合(TableCollection)。输出端口 -
OutputTables:过滤后的表(TableData)或表集合(TableCollection);当输入为空、query_template为空、或模板变量校验失败(如变量值为None且none_error_type不为"ignore")时为None。
快速上手示例:无模板变量(手写常量条件)
from gdisdk.modules.filters import TablesQuery
q = TablesQuery(mname="QueryTables")
q.query_template = "`年份` == 2007 and `国家` == 'US'" # 字符串常量需要自己加引号
# q.InputTables = table_or_tables
q.execute()
out = q.OutputTables.data
快速上手示例:使用模板变量(UI 可改值)
from gdisdk.modules.filters import TablesQuery
from gdisdk.pipeline.pipeData import TemplateVariableConfig
q = TablesQuery(mname="QueryWithTpl")
q.query_template = "`年份` == {tpl_year} and `国家` == {tpl_country}"
q.template_variables = {
"tpl_year": TemplateVariableConfig(
title="年份",
default=2007,
value_type="int",
schema_type="auto_select",
),
"tpl_country": TemplateVariableConfig(
title="国家",
default="US",
value_type="str", # 字符串会自动加引号,无需写成 '{tpl_country}'
schema_type="auto_select",
),
}
# q.InputTables = table_or_tables
q.tpl_country = "CN" # 运行前可通过属性直接改模板变量
q.execute()
out = q.OutputTables.data
查询模板语法说明(常见写法速查)
query_template基于 pandas 的DataFrame.query表达式语法(逻辑运算:and/or/not,比较:== != > >= < <=)。推荐把字段名/字段标题写在反引号里(例如
\`年份\`),可避免绝大多数 “字段名不是合法标识符” 导致的解析错误。字符串常量需要用引号包裹(例如
'US');但当你使用模板变量并设置value_type="str"时,字符串会**自动加引号**(无需写成'{tpl_xxx}')。
常见模板示例
# 1) 数值等值 / 范围
"`年份` == 2007"
"`深度` >= 10 and `深度` < 30"
# 2) 字符串等值(手写常量时需要引号)
"`国家` == 'US'"
# 3) in / not in(列表常量)
"`类型` in ['A', 'B', 'C']"
"`类型` not in ['废弃', '删除']"
# 4) 空值判断(None/NaN)
"`备注` == ''" # 空字符串
"`备注`.isnull()" # 为空(None/NaN)
"`备注`.notnull()" # 非空
# 5) 字符串包含/前后缀(pandas 字符串方法)
"`名称`.str.contains('岩', na=False)"
"`名称`.str.startswith('ZK', na=False)"
"`名称`.str.endswith('号', na=False)"
# 6) 多条件组合(括号控制优先级)
"(`状态` == '有效' and `深度` > 10) or (`状态` == '复核')"
什么时候需要使用反引号 ````…````(非常重要)
pandas 的 query 要求 “字段名像 Python 变量一样可解析”。当字段名不满足这个条件时,必须用反引号包裹字段名(即 \`列名\`)。以下情况**建议/需要**用反引号:
字段名包含中文、空格、短横线等特殊字符(如
工程名称、x coordinate、x-coordinate)。字段名以数字开头或包含点号等(如
2020年、a.b)。字段名与 Python 关键字冲突(如
class、lambda)或包含不可作为标识符的字符。你在模板里使用的是 “字段标题” 而非字段名时(标题常包含中文/空格,强烈建议用反引号)。
为了减少踩坑,本文档的示例统一使用 \`字段名/字段标题\` 形式;这也是最推荐的写法。
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
查询表达式模板(pandas 查询语法);支持 |
|
|
|
模板变量配置字典;所有变量名必须以 ``tpl_`` 开头,并可通过 |
|
|
|
级联过滤模式下的主表名/表标题;不填时会尝试从输入 |
|
|
|
级联过滤模式下要跟随过滤的子表列表;不填时会尝试从 |
|
|
|
主表与子表关联的键字段名/字段标题;不填时会尝试从 |
|
|
|
是否启用“主表过滤 → 按键值过滤子表”的级联逻辑;为 |
|
|
|
为 |
|
|
|
当模板变量存在 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import TablesQuery
pipe = PipeLine(app_name="TablesQueryDemo", app_title="表格查询示例")
q = TablesQuery(mname="Query")
q.query_template = "`年份` >= 2020"
# links = upstream.OutputTables >> q.InputTables
# pipe.add_links(links)
# pipe.run()
# out = q.OutputTables.data
更多信息
DropDuplicateRows
模块简介与适用场景
DropDuplicateRows对TableData按指定字段去重,输出TableData。典型适用场景:业务数据按编号去重,或在重复行中合并指定字符串列(
join_string_columns);详见「参数说明」。
端口说明
输入端口 -
InputTable:输入表(TableData)。输出端口 -
OutputTable:去重后的表(TableData);当输入为空时为None。
快速上手示例:按字段去重
from gdisdk.modules.filters import DropDuplicateRows
dropdup = DropDuplicateRows(mname="DropDuplicate")
dropdup.subset = ["bore_number"] # 可写字段名或字段标题
# dropdup.InputTable = table # TableData
dropdup.execute()
out_table = dropdup.OutputTable.data
快速上手示例:重复行合并字符串列(需要 subset)
from gdisdk.modules.filters import DropDuplicateRows
dropdup = DropDuplicateRows(mname="DropDuplicateMergeText")
dropdup.subset = ["bore_number"]
dropdup.join_string_columns = ["remark"] # 需要合并的字符串列
dropdup.join_separator = "\n"
# dropdup.InputTable = table
dropdup.execute()
out_table = dropdup.OutputTable.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
用于识别重复行的列;为 |
|
|
|
是否保留包含空字符串 |
|
|
|
是否保留包含 |
|
|
|
当发现重复行(基于 |
|
|
|
合并字符串列时使用的分隔符。 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import DropDuplicateRows
pipe = PipeLine(app_name="DropDuplicateDemo", app_title="去重示例")
dropdup = DropDuplicateRows(mname="DropDuplicate")
dropdup.subset = ["bore_number"]
# links = upstream.OutputTable >> dropdup.InputTable
# pipe.add_links(links)
# pipe.run()
# out_table = dropdup.OutputTable.data
更多信息
GdimAppDataSelector
模块简介与适用场景
GdimAppDataSelector从ResultModel``(通常来自 :class:`~gdisdk.modules.readers.GdimAppDataReader` 的 ``OutputResultModel)中**选取一个字段**,输出到端口OutputData``(类型为 ``General,实际值可能是TableData、标量等,取决于上游保存的内容)。模块根据你配置的
name与module_name自动拼接查找键,规则与save_data_to_db()落库时的键名一致:name以"Output"开头 → 视为**输出端口名**,必须同时提供module_name,键为"{module_name}@{name}";name不以"Output"开头且提供了module_name→ 视为**模块参数**,键为"{module_name}#{name}";name不以"Output"开头且module_name为None→ 视为 Pipeline 本体属性,键为"pipeline@{name}"``(须与 ``save_data_to_db(..., data_type="pipeline")使用的属性名一致)。
若键不存在,执行时会抛出
KeyError,并提示当前ResultModel中可用的键(便于核对上游配置)。典型适用场景:报告类 Pipeline 中,在
GdimAppDataReader之后为每个下游模块分别接一个GdimAppDataSelector,将表格、图片、参数等拆成独立连线。
端口说明
输入端口 -
InputResultModel:GdimAppDataReader产出的ResultModel。输出端口 -
OutputData:选中字段的值;输入或name为空时为None。
快速上手示例:选取上游模块的输出表
from gdisdk.modules.filters import GdimAppDataSelector
sel = GdimAppDataSelector(mname="PickTable")
sel.module_name = "CorrosionCompute"
sel.name = "OutputTable" # 端口名须以 Output 开头
# sel.InputResultModel = reader.OutputResultModel
sel.execute()
table = sel.OutputData.data
快速上手示例:选取 Pipeline 本体属性(如 workspace)
from gdisdk.modules.filters import GdimAppDataSelector
sel = GdimAppDataSelector(mname="PickWorkspace")
sel.module_name = None
sel.name = "workspace"
sel.execute()
value = sel.OutputData.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
端口名(须以 |
|
|
|
选取端口或模块参数时必填;选取 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules import GdimAppDataReader, GdimAppDataSelector
pipe = PipeLine(app_name="ReportFromSavedApp", app_title="读取上游结果写报告")
reader = GdimAppDataReader(mname="ReadApp")
reader.app_title = "水腐蚀性分析"
pick = GdimAppDataSelector(mname="PickResult")
pick.module_name = "AnalysisModule"
pick.name = "OutputTable"
links = reader.OutputResultModel >> pick.InputResultModel
pipe.add_links(links)
# pipe.run()
# table = pick.OutputData.data
更多信息
modules.readers 模块帮助 中的
GdimAppDataReader。
MarkdownSectionFilter
模块简介与适用场景
MarkdownSectionFilter按 Markdown 标题层级(#/##/###…)解析章节树,并按规则筛选内容;输出过滤后的 Markdown(OutputMarkdown)及结构化ResultModel``(``OutputResultModel)。若需写入文件,请在下游使用 writers 帮助 中的
TextWriter。典型适用场景:长报告按章节裁剪供 LLM/RAG 使用,过滤目录等噪声,或控制表格长度;筛选与表格策略参数见「参数说明」。
端口说明
InputMarkdown:输入 Markdown 文本或文件路径;端口有值时覆盖markdown参数。OutputMarkdown:过滤后的 Markdown 文本。OutputResultModel:MarkdownSectionFilterResult,含filtered_markdown_key命名的 Markdown 字段、章节元数据(selected_sections/dropped_sections)、字符/ token 统计及filter_rules摘要。多实例并行时建议设置不同的 ``filtered_markdown_key``(见「参数说明」)。
快速上手示例:按章节号筛选并压缩表格
from gdisdk.modules.filters import MarkdownSectionFilter
f = MarkdownSectionFilter(mname="FilterMarkdown")
f.markdown = "report.md" # 也可以直接传入 markdown 字符串
f.include_number_prefixes = ["2", "4", "5"] # 例如:场地条件/评价/结论
f.drop_toc = True
f.tables_mode = "truncate_rows"
f.table_truncate_rows = 5
f.execute()
out_md = f.OutputMarkdown.data
快速上手示例:自定义 ResultModel 中 Markdown 字段名(避免冲突)
from gdisdk.modules.filters import MarkdownSectionFilter
f = MarkdownSectionFilter(mname="FilterMarkdown")
f.filtered_markdown_key = "site_conditions_md"
f.markdown = "report.md"
f.execute()
out_md = f.OutputMarkdown.data
# out_md 与 f.OutputMarkdown.data 一致(过滤后的 Markdown 字符串)
# f.OutputResultModel.data.site_conditions_md 同为过滤后的 markdown
# 其它元数据:f.OutputResultModel.data.selected_sections 等
快速上手示例:按标题模式筛选(正则)并排除目录/图件
from gdisdk.modules.filters import MarkdownSectionFilter
f = MarkdownSectionFilter(mname="FilterByTitle")
f.markdown = "report.md"
f.include_title_patterns = ["场地.*地质条件", "地层岩性", "结论"]
f.exclude_title_patterns = ["图件", "目.*录"]
f.execute()
out_md = f.OutputMarkdown.data
参数说明
参数名 |
类型 |
默认值 |
说明 |
|---|---|---|---|
|
|
|
输入 Markdown 文本或文件路径;若 |
|
|
|
|
|
|
|
需要包含的章节编号前缀(如 |
|
|
|
需要包含的标题模式(正则或子串);标题命中则包含对应章节。 |
|
|
|
需要剔除的章节编号前缀(优先级高于 include)。 |
|
|
|
需要剔除的标题模式(正则)。 |
|
|
|
为 |
|
|
|
是否丢弃第一个标题之前的 “前言/封面” 等非章节内容。 |
|
|
|
是否尝试删除目录块(TOC)。 |
|
|
|
表格处理策略:保留/删除/仅保留表题/截断行数(保留表头 + 前 N 行数据)。 |
|
|
|
当 |
在 pipeline 中的使用方式
from gdisdk.pipeline import PipeLine
from gdisdk.modules.filters import MarkdownSectionFilter
pipe = PipeLine(app_name="MarkdownFilterDemo", app_title="Markdown 章节过滤示例")
f = MarkdownSectionFilter(mname="MarkdownFilter")
f.include_number_prefixes = ["2", "4"]
f.drop_toc = True
# links = upstream.OutputMarkdown >> f.InputMarkdown
# pipe.add_links(links)
# pipe.run()
# out_md = f.OutputMarkdown.data
# meta = f.OutputResultModel.data
# 写文件可将 OutputMarkdown 接到 TextWriter 等模块
更多信息