Skip to content

anbeime/skill / skills/data-flow-skill/data-flow-skill

data-flow-skill

Verified

面向数据分析任务的一站式 Agent Skill,覆盖数据类型检测、数据理解、预处理、统计分析、可视化图表生成、正式报告和幻灯片输出。适用于课程作业、论文实验、业务分析、SEO/GEO 数据报告和可追溯数据分析工作流。

FileKindSize
README.mdmarkdown1.8 KB
SKILL.mdskill md7.7 KB
references/data-types.mdmarkdown907 B
references/reporting.mdmarkdown816 B
references/slides.mdmarkdown895 B
references/validation.mdmarkdown1.1 KB
references/visualization.mdmarkdown797 B
references/workflow.mdmarkdown875 B
requirements.txtmanifest97 B
scripts/README.mdmarkdown1.0 KB
scripts/analysis/__init__.pyscript35 B
scripts/analysis/analyzer_data_understanding.pyscript5.2 KB
scripts/analysis/analyzer_dataset_detection.pyscript7.6 KB
scripts/analysis/analyzer_findings_generation.pyscript57.1 KB
scripts/analysis/analyzer_preprocessing.pyscript1.9 KB
scripts/analysis/analyzer_statistical_analysis.pyscript934 B
scripts/analysis/analyzer_strategy_registry.pyscript3.4 KB
scripts/analysis/analyzer_time_series_analysis.pyscript2.0 KB
scripts/image_gen/image_generator.pyscript6.4 KB
scripts/mermaid/__init__.pyscript202 B
scripts/mermaid/cli.pyscript4.8 KB
scripts/mermaid/echarts_export.pyscript6.6 KB
scripts/mermaid/flowchart.pyscript9.4 KB
scripts/visualization/matplotlib/bar_memevolve.pyscript4.7 KB
scripts/visualization/matplotlib/bar_spice.pyscript6.0 KB
scripts/visualization/matplotlib/box_plot.pyscript4.4 KB
scripts/visualization/matplotlib/bubble_chart.pyscript5.8 KB
scripts/visualization/matplotlib/calendar_heatmap.pyscript8.2 KB
scripts/visualization/matplotlib/line_aime.pyscript3.0 KB
scripts/visualization/matplotlib/line_loss_inset.pyscript5.4 KB
scripts/visualization/matplotlib/line_selfdistill.pyscript7.0 KB
scripts/visualization/matplotlib/parallel_coordinates.pyscript6.7 KB
scripts/visualization/matplotlib/radar_dora.pyscript4.7 KB
scripts/visualization/matplotlib/scatter_break.pyscript5.8 KB
scripts/visualization/matplotlib/scatter_tsne.pyscript4.4 KB
scripts/visualization/matplotlib/stacked_bar.pyscript4.5 KB
scripts/visualization/matplotlib/violin_plot.pyscript5.3 KB

scripts/mermaid/flowchart.py

View on GitHub
"""Build and render Mermaid flowcharts."""

from __future__ import annotations

import base64
import json
import re
import shutil
import subprocess
import zlib
import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterable


def _slugify(value: str) -> str:
    normalized = re.sub(r"[^0-9A-Za-z\u4e00-\u9fff]+", "_", value.strip())
    normalized = normalized.strip("_")
    return normalized or "node"


def _escape_mermaid_text(value: str) -> str:
    return value.replace('"', "'").replace("\n", "<br/>")


@dataclass(slots=True)
class FlowStep:
    """Single step in a Mermaid flowchart."""

    text: str
    node_id: str | None = None
    shape: str = "rect"
    metadata: dict[str, str] = field(default_factory=dict)

    def resolved_node_id(self, index: int) -> str:
        if self.node_id:
            return self.node_id
        return f"step_{index}_{_slugify(self.text)}"


class MermaidFlowchart:
    """Composable Mermaid flowchart builder."""

    _SHAPE_TEMPLATES = {
        "rect": '[{label}]',
        "round": '({label})',
        "stadium": '([{label}])',
        "subroutine": '[[{label}]]',
        "cylindrical": '[( {label} )]',
        "circle": '(({label}))',
        "diamond": '{{{label}}}',
        "hexagon": '{{{{{label}}}}}',
        "parallelogram": '[/ {label} /]',
    }

    def __init__(
        self,
        title: str,
        direction: str = "TD",
        class_definitions: Iterable[str] | None = None,
    ) -> None:
        self.title = title
        self.direction = direction
        self.class_definitions = list(class_definitions or [])
        self.steps: list[FlowStep] = []
        self.links: list[tuple[str, str, str | None]] = []

    def add_step(self, text: str, *, node_id: str | None = None, shape: str = "rect") -> FlowStep:
        step = FlowStep(text=text, node_id=node_id, shape=shape)
        self.steps.append(step)
        return step

    def add_link(self, source_id: str, target_id: str, label: str | None = None) -> None:
        self.links.append((source_id, target_id, label))

    @classmethod
    def from_steps(
        cls,
        title: str,
        steps: Iterable[str],
        *,
        direction: str = "TD",
        first_shape: str = "round",
        last_shape: str = "stadium",
    ) -> "MermaidFlowchart":
        flowchart = cls(title=title, direction=direction)
        step_items = [step for step in steps if step.strip()]
        for index, step_text in enumerate(step_items):
            if index == 0:
                shape = first_shape
            elif index == len(step_items) - 1:
                shape = last_shape
            else:
                shape = "rect"
            flowchart.add_step(step_text, shape=shape)
        return flowchart

    def to_mermaid(self) -> str:
        lines = [
            "---",
            f"title: {self.title}",
            "---",
            f"flowchart {self.direction}",
        ]

        resolved_steps: list[tuple[str, FlowStep]] = []
        for index, step in enumerate(self.steps, start=1):
            node_id = step.resolved_node_id(index)
            label = _escape_mermaid_text(step.text)
            template = self._SHAPE_TEMPLATES.get(step.shape, self._SHAPE_TEMPLATES["rect"])
            lines.append(f"    {node_id}{template.format(label=label)}")
            resolved_steps.append((node_id, step))

        if self.links:
            for source_id, target_id, label in self.links:
                connector = f' -->|{_escape_mermaid_text(label)}| ' if label else " --> "
                lines.append(f"    {source_id}{connector}{target_id}")
        else:
            for (source_id, _), (target_id, _) in zip(resolved_steps, resolved_steps[1:]):
                lines.append(f"    {source_id} --> {target_id}")

        lines.extend(f"    {class_definition}" for class_definition in self.class_definitions)
        return "\n".join(lines) + "\n"

    def write(self, output_path: str | Path) -> Path:
        path = Path(output_path)
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(self.to_mermaid(), encoding="utf-8")
        return path


def render_mermaid_file(
    input_path: str | Path,
    output_path: str | Path,
    *,
    theme: str = "default",
    background_color: str = "white",
    scale: int = 2,
    width: int | None = None,
    height: int | None = None,
    cli_timeout_seconds: int = 45,
    prefer_remote: bool = False,
) -> Path:
    input_file = Path(input_path)
    output_file = Path(output_path)
    output_file.parent.mkdir(parents=True, exist_ok=True)
    diagram_text = input_file.read_text(encoding="utf-8")

    if prefer_remote:
        _render_via_mermaid_ink(
            diagram_text,
            output_file,
            theme=theme,
            background_color=background_color,
            scale=scale,
            width=width,
            height=height,
        )
        return output_file

    if shutil.which("npx") is None:
        _render_via_mermaid_ink(
            diagram_text,
            output_file,
            theme=theme,
            background_color=background_color,
            scale=scale,
            width=width,
            height=height,
        )
        return output_file

    config = {
        "theme": theme,
        "flowchart": {"curve": "basis"},
        "fontFamily": "PingFang SC, Hiragino Sans GB, Microsoft YaHei, sans-serif",
    }
    config_path = output_file.with_suffix(".mermaid-config.json")
    config_path.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8")

    command = [
        "npx",
        "-y",
        "@mermaid-js/mermaid-cli",
        "-i",
        str(input_file),
        "-o",
        str(output_file),
        "-t",
        theme,
        "-b",
        background_color,
        "-s",
        str(scale),
        "-c",
        str(config_path),
    ]
    if width is not None:
        command.extend(["-w", str(width)])
    if height is not None:
        command.extend(["-H", str(height)])

    try:
        subprocess.run(
            command,
            check=True,
            capture_output=True,
            text=True,
            timeout=cli_timeout_seconds,
        )
    except FileNotFoundError as exc:
        _render_via_mermaid_ink(
            diagram_text,
            output_file,
            theme=theme,
            background_color=background_color,
            scale=scale,
            width=width,
            height=height,
        )
        return output_file
    except subprocess.TimeoutExpired:
        _render_via_mermaid_ink(
            diagram_text,
            output_file,
            theme=theme,
            background_color=background_color,
            scale=scale,
            width=width,
            height=height,
        )
        return output_file
    except subprocess.CalledProcessError as exc:
        stderr = (exc.stderr or "").strip()
        stdout = (exc.stdout or "").strip()
        local_error = stderr or stdout or "未知错误"
        try:
            _render_via_mermaid_ink(
                diagram_text,
                output_file,
                theme=theme,
                background_color=background_color,
                scale=scale,
                width=width,
                height=height,
            )
            return output_file
        except RuntimeError as remote_exc:
            raise RuntimeError(f"Mermaid 导出失败。本地错误: {local_error};远程错误: {remote_exc}") from exc
    finally:
        config_path.unlink(missing_ok=True)

    return output_file


def _render_via_mermaid_ink(
    diagram_text: str,
    output_file: Path,
    *,
    theme: str,
    background_color: str,
    scale: int,
    width: int | None,
    height: int | None,
) -> None:
    extension = output_file.suffix.lower().lstrip(".")
    if extension not in {"png", "svg", "pdf"}:
        raise RuntimeError(f"远程渲染暂不支持输出格式: {output_file.suffix or '<none>'}")

    payload = json.dumps(
        {"code": diagram_text, "mermaid": {"theme": theme}},
        ensure_ascii=False,
        separators=(",", ":"),
    ).encode("utf-8")
    encoded = "pako:" + urllib.parse.quote(
        base64.urlsafe_b64encode(zlib.compress(payload, 9)).decode("ascii").rstrip("="),
        safe=":_-",
    )

    if extension == "svg":
        endpoint = "svg"
        query_params: dict[str, str] = {}
    elif extension == "png":
        endpoint = "img"
        query_params = {"type": "png"}
    else:
        endpoint = "pdf"
        query_params = {"fit": ""}

    if background_color:
        query_params["bgColor"] = f"!{background_color}" if background_color.isalpha() else background_color
    if width is not None:
        query_params["width"] = str(width)
    if height is not None:
        query_params["height"] = str(height)
    if width is not None or height is not None:
        query_params["scale"] = str(max(1, min(scale, 3)))

    query_string = urllib.parse.urlencode(query_params, doseq=False)
    url = f"https://mermaid.ink/{endpoint}/{encoded}"
    if query_string:
        url = f"{url}?{query_string}"

    request = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    try:
        with urllib.request.urlopen(request, timeout=30) as response:
            payload = response.read()
    except urllib.error.URLError as exc:
        raise RuntimeError(f"远程渲染服务不可用: {exc}") from exc

    output_file.write_bytes(payload)
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 468kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 321kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 321kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 321kUpdated Claude CodeCodexVerifiedInstall