Skip to content

mecfs_bio.figures.figure_exporter

Classes:

  • AbstractFigureExporter
  • FigureExporter

    Responsible for invoking the build system to generate Assets corresponding to figures, then copying those assets to the figure directory.

Functions:

Attributes:

ValidFigureMeta module-attribute

ValidFigureMeta = (
    GWASPlotFileMeta
    | GWASPlotDirectoryMeta
    | MarkdownFileMeta
    | GWASLabManhattanQQPlotMeta
)

logger module-attribute

logger = get_logger()

AbstractFigureExporter

Bases: ABC

Methods:

export abstractmethod

export(to_export: Sequence[Task], fig_dir: Path) -> None
Source code in mecfs_bio/figures/figure_exporter.py
@abstractmethod
def export(self, to_export: Sequence[Task], fig_dir: Path) -> None:
    pass

FigureExporter

Bases: AbstractFigureExporter

Responsible for invoking the build system to generate Assets corresponding to figures, then copying those assets to the figure directory.

Methods:

Attributes:

runner instance-attribute

runner: Callable[[Sequence[Task]], Mapping[AssetId, Asset]]

tracer instance-attribute

tracer: Tracer

export

export(to_export: Sequence[Task], fig_dir: Path) -> None
Source code in mecfs_bio/figures/figure_exporter.py
def export(self, to_export: Sequence[Task], fig_dir: Path) -> None:
    fig_dir.mkdir(parents=True, exist_ok=True)
    meta_list = []
    for task in to_export:
        assert isinstance(task.meta, ValidFigureMeta)
        meta_list.append(task.meta)
    result = self.runner(
        to_export,
    )
    for task, meta in zip(to_export, meta_list):
        asset = result[task.asset_id]
        dst = get_figure_destination(meta=meta, fig_dir=fig_dir)
        if isinstance(meta, GWASPlotDirectoryMeta):
            assert isinstance(asset, DirectoryAsset)
            shutil.copytree(asset.path, dst, dirs_exist_ok=True)
            logger.debug(f"Directory figure asset {task.asset_id} copied to {dst}.")
        else:
            assert isinstance(asset, FileAsset)
            shutil.copy(asset.path, dst)
            logger.debug(f"Figure asset {task.asset_id} copied to {dst}.")

get_fig_dir_meta

get_fig_dir_meta(
    meta: GWASPlotDirectoryMeta, fig_dir: Path
) -> Path
Source code in mecfs_bio/figures/figure_exporter.py
def get_fig_dir_meta(meta: GWASPlotDirectoryMeta, fig_dir: Path) -> Path:
    return fig_dir / meta.id

get_fig_file_path

get_fig_file_path(
    meta: GWASPlotFileMeta | GWASLabManhattanQQPlotMeta,
    fig_dir: Path,
) -> Path
Source code in mecfs_bio/figures/figure_exporter.py
def get_fig_file_path(
    meta: GWASPlotFileMeta | GWASLabManhattanQQPlotMeta, fig_dir: Path
) -> Path:
    if isinstance(meta, GWASLabManhattanQQPlotMeta):
        return fig_dir / (str(meta.asset_id) + ".png")
    return fig_dir / (meta.asset_id + meta.extension)

get_figure_destination

get_figure_destination(
    meta: ValidFigureMeta, fig_dir: Path
) -> Path

Single source of truth for "where in the figure directory does the figure produced by a task with this meta land?".

For file-emitting metas this is an exact file path; for GWASPlotDirectoryMeta this is the destination directory (the task's output files all live underneath it).

Source code in mecfs_bio/figures/figure_exporter.py
def get_figure_destination(meta: ValidFigureMeta, fig_dir: Path) -> Path:
    """
    Single source of truth for "where in the figure directory does the figure
    produced by a task with this meta land?".

    For file-emitting metas this is an exact file path; for
    GWASPlotDirectoryMeta this is the destination directory (the task's
    output files all live underneath it).
    """
    if isinstance(meta, GWASPlotFileMeta | GWASLabManhattanQQPlotMeta):
        return get_fig_file_path(meta=meta, fig_dir=fig_dir)
    if isinstance(meta, GWASPlotDirectoryMeta):
        return get_fig_dir_meta(meta=meta, fig_dir=fig_dir)
    if isinstance(meta, MarkdownFileMeta):
        return get_md_fig_file_path(meta=meta, fig_dir=fig_dir)
    raise ValueError(f"Unknown meta type {type(meta)}")

get_md_fig_file_path

get_md_fig_file_path(
    meta: MarkdownFileMeta, fig_dir: Path
) -> Path
Source code in mecfs_bio/figures/figure_exporter.py
def get_md_fig_file_path(meta: MarkdownFileMeta, fig_dir: Path) -> Path:
    return (
        fig_dir / (meta.asset_id + ".mdx")
    )  # Use .mdx instead of .md to prevent mkdocs from auto-generating a documentation page