Skip to content

feat(datatype): 数据类型感知绘图 + prompt 领域知识注入#2

Open
Cmochance wants to merge 9 commits into
mainfrom
codex/datatype-aware-plotting
Open

feat(datatype): 数据类型感知绘图 + prompt 领域知识注入#2
Cmochance wants to merge 9 commits into
mainfrom
codex/datatype-aware-plotting

Conversation

@Cmochance

Copy link
Copy Markdown
Owner

背景

nature-figure 此前只能通过 prompt + 选图型进行生图,缺乏对科学数据类型的领域感知。借鉴 Sci-Data-Analyzer(私有)的数据导入和识别模块,为 nature-figure 新增可选的数据类型选择能力。

架构决策

借鉴 Sci-Data-Analyzer 时做了选择性取舍:

  • 保留:数据类型描述、典型轴标签/单位、列名关键词等静态元数据
  • 转成 Rust:数据文件解析(CSV/TXT → 数值列提取),适应 Tauri 技术栈
  • 放弃:自动识别评分引擎(16个规则类)— 本项目用户手动选择,不需要系统猜;LLM 兜底识别 — codex 自身就是 LLM

多级结构化数据类型注册

```
src-tauri/src/datatypes/
├── mod.rs # 入口: DataTypeDescriptor 结构 + registry + Tauri 命令
├── general.rs # 通用 / 时间序列
├── spectroscopy.rs # FTIR / Raman / UV-Vis / XRD / XPS / PL
├── thermal.rs # TGA / DSC
├── electrochem.rs # CV / EIS
└── reader.rs # 纯 Rust CSV/TXT 解析器
```

新增大类只需加一个子模块文件 + 在 `all_types()` 注册一行,避免单文件膨胀。首批覆盖 4 大类 12 个数据类型。

数据流

```
用户选数据类型(FTIR等) → 前端加载该类型的 prompt_fragment

注入 instruction 的【数据类型】区块

codex 获得领域知识(X轴方向惯例/典型范围/标注建议)

生成符合该数据类型学术惯例的图
```

变更

  • Rust 后端: datatypes/ 模块(5个文件) + 2个 Tauri 命令 + csv crate 依赖
  • Skill: manifest.yaml 新增 data_type axis(可选,11个选项)
  • 前端: DynamicForm 中文标签映射 + App.tsx prompt 注入 + mock 适配

验证

  • TypeScript `tsc --noEmit` 零错误
  • Rust `cargo check` 编译通过
  • CI / AI review

借鉴 Sci-Data-Analyzer 的 chart_renderer 架构,为 nature-figure 新增
参数化绘图微调层,实现不经过 LLM 的本地 matplotlib 实时渲染。

整条链路三阶段:
- 阶段一:移植 Python 绘图模板(line/bar/heatmap/dual_y)+ Rust preview_plot
  命令,复用 uv 隔离 Python 环境在子进程渲染,返回 base64 图片
- 阶段二:ChartEditor 组件,左侧参数面板(配色/字号/线宽/图例/边框/序列)
  + 右侧防抖(400ms)实时预览 + SVG/PNG 导出
- 阶段三:修改 figure skill 导出 plot_spec/plot_data 参数契约,codex 生成
  图后前端自动读取参数进入本地编辑器

同时新增 Vite alias mock 层,支持纯浏览器预览前端界面
- plot.ts: 新增 PlotGrid 接口(mode/values/cmap/origin/z_label)和
  CMAP_PRESETS 常量(16 个常用 matplotlib colormap)
- ChartEditor: heatmap 专属编辑面板,含 colormap 下拉、origin 选择、
  colorbar 标签输入、values 二维矩阵编辑器(行列增删 + 单元格数值编辑)
- 渲染时注入默认 grid(用户切入 heatmap 但无数据时不报错)
- mock: heatmap 色块矩阵预览(viridis 近似色阶)
- CSS: 矩阵编辑器样式

同时修正: 后端技术栈确认为 Rust(Tauri),Python 仅作 Rust spawn 的
子进程执行绘图模板,非独立后端
P1: renderer.rs 打包路径修正 — Tauri 打包后资源保留 resources/ 前缀,
原来少了这层导致发布版找不到模板(崩溃)。补上 resources/ 前缀。
P2: dual_y_plot.py 添加 visible 检查 — 编辑器隐藏序列时双 Y 轴模板
仍然渲染,现在与 line/bar 模板行为一致。
P2: renderer.rs Python 环境就绪检测 — 用 is_ready()(检查 python 存在
+ marker 版本匹配)替代仅检查路径存在,避免依赖安装中途渲染失败
ChartEditor 防抖预览存在竞态:用户快速连续调整参数时多个渲染请求
同时在途,旧的请求可能比新的晚返回并覆盖预览,导致显示过期参数
的结果。

修复:用递增 reqIdRef 跟踪每次请求,响应返回时检查是否仍为最新,
过期响应直接丢弃。
1. ChartEditor 竞态修复不完整: reqIdRef 递增应在参数变化时立即执行,
   而非等 400ms 防抖后,否则旧请求仍有窗口通过 ID 检查。
2. App.tsx 旧参数未清除: 任务失败或切换工作目录时不清除
   loadedSpec/loadedData,编辑器仍渲染过期数据。现在 catch 和
   launch 开始时都清除。
3. renderer.rs HOME 覆盖: 回退系统 python3 时覆盖 HOME 导致
   pip install --user 的包不可见。改用 MPLCONFIGDIR/XDG_CACHE_HOME
   隔离缓存,保留 HOME。
借鉴 Sci-Data-Analyzer 的数据导入和识别模块,保留 nature-app 已有的
prompt+图型选择流程,新增可选的数据类型选择能力。

后端(Rust):
- 多级结构化数据类型注册模块 datatypes/:mod.rs 入口 + 四个大类文件
  (spectroscopy/thermal/electrochem/general),12 个数据类型,含描述、
  典型轴标签/单位、列名关键词。新增大类只需加一个子模块文件。
- 纯 Rust 数据文件解析器 reader.rs:读取 CSV/TXT,自动检测分隔符,
  跳过表头,提取数值列,计算 min/max/单调性
- 两个 Tauri 命令:get_data_types(返回类型列表)、resolve_data(解析文件)
- 删除原系统的自动识别评分引擎和 LLM 兜底(本项目用户手动选择)

Skill:
- manifest.yaml 新增 data_type axis(可选,非 blocking gate)
- 11 个数据类型选项(FTIR/Raman/UV-Vis/XRD/XPS/PL/TGA/DSC/CV/EIS/通用)

前端:
- DynamicForm 适配 data_type 选择项 + axisValueLabels 中文标签映射
- App.tsx prompt 注入:选了数据类型时,在 instruction 里注入领域知识
  描述区块(如 FTIR 的 X 轴方向惯例、典型波数范围、官能团标注建议)
- DynamicFormResult 新增 axisSelections 字段
- mock 适配 get_data_types / resolve_data

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6832603c6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

r: static/fragments/backend/r.md
python: static/fragments/backend/python.md
r: static/fragments/backend/r.md
multi: false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Align multi under the backend axis

With only three leading spaces, this multi key is neither inside backend nor aligned with the other axes entries, which makes the manifest invalid YAML. When the catalog loads, serde_yaml::from_str in parse_skill fails and silently falls back to manifestNoAxes, so nature-figure loses the required backend/data_type chips and all on-demand references.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant