Skip to content
YIZE
P.006 / PROJECT2024

自适应可视多平台主成分分析软件Adaptive Multi-Platform PCA for Very Wide Matrices

Adaptive Multi-Platform PCA for Very Wide Matrices自适应可视多平台主成分分析软件

能在 GWAS 量级的基因型矩阵上活着跑完的主成分分析:分块读取、CPU/GPU 双模式、三种主成分数确定方式,计算过程全程可视。

Principal component analysis that survives GWAS-scale genotype matrices: block-wise reading, CPU or GPU execution, three ways to choose the number of components, and a visual trace of the computation while it runs.

角色Role
开发成员Developer
类型Type
软件Software
年份Year
2024
  • Python
  • GPU computing
  • Blocked algorithms
  • PCA
Software Screenshot
P.006-A
Software Screenshot · placeholder
目录Contents

问题:数据先撞上内存

各领域面对的数据量都大,遗传学尤其如此。GWAS 里的基因型矩阵动辄上万行乘上十万列,常规做法是把它整个读进内存再算——而在普通电脑上,第一步读取就会失败。这不是算法不够快的问题,是数据根本进不来。

另外,常见的主成分分析工具还有四个门槛:门槛高(要写代码、配环境)、缺少过程反馈(点完之后只能等)、缺少可视化(降完维要另外画图)、主成分数的选择不够灵活。这四点让工具在会用的人手里是工具,在其他人手里是黑箱。

这个软件针对的就是这两件事:让大数据能算,以及让算的过程看得见。登记号 2024SR1708999,著作权人为中国农业大学。

功能

  1. 判断文件格式是否正确,并分块读取数据;
  2. 读取与处理可选 CPU 或 GPU 模式(GPU 走 CuPy);
  3. 创建多任务,对文件的不同列分别做主成分处理;
  4. 主成分数的设定有三种:按方差比例、按最大似然估计、或自选;
  5. 对处理过程与结果做可视化跟进;
  6. 以同格式存储降维后的数据;
  7. 呈递过程中的错误信息。

分块是怎么用的

读入时用 python 读 CSV,按用户的选择导入 numpy 或 cupy,然后按块读。安全模式下先做两项检查:文件是不是规范的 CSV(第一行是列名、以逗号分隔);以及所选的那些列是不是真的能拿去做主成分分析的浮点数。第二项检查看起来多余,直到你遇到一份混着 ID 列、日期列和空缺的表格。

列名行如果少于五万个,就全部显示在选择框里;超过五万个只显示前五万个,以保证界面性能——第五万个之后的列默认恒被最后一个任务选中。这不是一个优雅的规则,是一条为了不让界面卡死的规则,所以写在这里。

一次读取,多个任务

程序用 Python 的生成器方法做到在一次读取的前提下同时对多个任务做主成分分析。任务可以逐个增加,每个任务从选择框里挑自己要处理的列——不同任务不允许选同一列,因为同一列进两个任务,降维结果就无法对照。选择框上方有搜索和全选,列很多的时候这是必需的而不是便利。

三种主成分数的确定方式,分别对应三种知道自己想要什么的人:

  • 按方差比例:输入方差阈值与检测分块数,程序读前若干个分块,算出达到该阈值所需的最小主成分数。分块数填 0 表示一直检测到文件末尾。
  • 按最大似然估计:输入检测分块数,由最大似然估计理论算出主成分数。
  • 自选:直接填一个数。

进度与可视化

进度条分三个阶段,不是为了好看,而是因为三个阶段失败的原因完全不同:

| 阶段 | 进度 | 在做什么 | | --- | --- | --- | | 预处理 | 0–30% | 安全模式下检查各任务所选列是否为浮点数,并按设定算出各任务的主成分数 | | 主成分分析 | 30–50% | 按各任务所选列与主成分数,逐块读到 CPU/GPU 上做 fit,每块之后把当前的主成分情况同步到前端 | | 数据转换 | 50–100% | 用分析结果把原始数据转换过去,输出到同目录 |

过程中向 ECharts 传参,实时画图——所以「缺少过程反馈」这一条是被正面解决掉的,而不是被一句「请耐心等待」绕过去的。

结构

前后端架构:前端用 Vue + Vite + naive-ui 负责界面、交互与输入;后端用 Python + PyQt 负责算法与通讯。主文件同时运行前后端,前端的每个按钮都对应到后端的一次动作——比如「导入文件」实际上打开的是后端的选择框;用户点了开始处理之后,前端收集的信息会送到后端执行。

这一页不写的

界面截图。软件在中国版权保护中心有登记,登记号列在上面即可核验;截图不增加可核对性。

目录Contents

The problem: the data hits the memory first

Every field faces large data; genetics faces more of it than most. A GWAS genotype matrix is routinely tens of thousands of rows by hundreds of thousands of columns, and the usual approach — read it all into memory, then compute — fails at the first step on an ordinary machine. That is not a matter of the algorithm being slow. The data does not fit.

Common PCA tooling adds four more barriers: a high floor to entry (write code, set up an environment), no feedback while it runs, no visualisation (the reduction finishes, then you plot it separately), and rigid handling of how many components to keep. Those four turn a tool into a black box for anyone who did not build it.

This software addresses exactly two things: making large data computable, and making the computation visible. Registered as 2024SR1708999, rights holder 中国农业大学.

What it does

  1. Checks that the file's format is valid, and reads it in blocks.
  2. Runs on CPU or GPU (GPU via CuPy).
  3. Creates multiple tasks, each running PCA over a different set of columns.
  4. Chooses the number of components three ways: by variance ratio, by maximum-likelihood estimation, or manually.
  5. Traces the computation and its result visually, while it runs.
  6. Writes the reduced data back in the same format.
  7. Reports errors as they happen.

How the block reading is used

The file is read with Python, numpy or cupy is imported according to the user's choice, and the data is read block by block. Safety mode runs two checks first: is the file a proper CSV (column names on the first row, comma-separated), and are the chosen columns actually floats that can be reduced? The second check looks redundant until the first table that mixes an ID column, a date column and some gaps — which is most of them.

A column-name row shorter than fifty thousand entries is shown in full; beyond that only the first fifty thousand are displayed, to keep the interface responsive, and everything after the fifty-thousandth counts as always selected by the last task. That is not an elegant rule. It is a rule that stops the window from freezing, which is why it is written down here.

One read, many tasks

The program uses Python generators to run PCA for several tasks within a single read of the file. Tasks are added one at a time and each one picks its own columns from the selector; no two tasks may take the same column, because a column reduced twice cannot be compared against itself. Search and select-all sit above the selector, which for a wide matrix is a requirement rather than a convenience.

The three ways to fix the number of components correspond to three kinds of person who know different things:

  • Variance ratio — enter a variance threshold and a number of blocks to inspect; the program reads that many blocks and computes the smallest number of components that reaches the threshold. Zero means read to the end of the file.
  • Maximum likelihood — enter how many blocks to inspect; the number of components follows from the maximum-likelihood estimate.
  • Manual — type a number.

Progress, and why it is staged

The progress bar has three stages, and not for decoration: each stage fails for a different reason.

| Stage | Progress | What is happening | | --- | --- | --- | | Preprocessing | 0–30% | In safety mode, check that each task's columns are floats; compute each task's component count from its setting | | PCA | 30–50% | Read block by block onto CPU or GPU and fit; after each block the current components are pushed to the front end | | Transform | 50–100% | Apply the fitted result to the original data and write the output beside it |

Parameters are handed to ECharts during the run, so the charts update live. "Missing process feedback" is answered directly rather than being waved away with a please-wait message.

Structure

A front end and a back end. The front end — Vue, Vite and naive-ui — holds the interface, the interaction and the input; the back end — Python and PyQt — holds the algorithm and the messaging. One entry file runs both. Every button on the front end corresponds to an action at the back: "import file" really does open the back end's file dialog, and once the user starts the run, what the front end collected is handed over for computation.

What is not on this page

Screenshots. The software is registered with the Copyright Protection Center of China, and the registration number above is what makes it checkable; a picture of the interface adds nothing to that.