# vvinuv/pymorph architecture

> Architecture diagram of the GitHub repository vvinuv/pymorph, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vvinuv/pymorph
- Repository: https://github.com/vvinuv/pymorph

## Overview

#### Purpose and entry point
PyMorph is a Python galaxy-morphology batch pipeline. It combines SExtractor detections/measurements with GALFIT parametric decomposition, plus in-process non-parametric morphology calculations. The installed CLI is `pymorph_bin`; orchestration is centered in `src/pymorph/pymorph.py`, with a working-directory `config.ini` as the operational contract.

#### Input modes and configuration boundary
The pipeline supports full astronomical frames or pre-cut postage stamps, selected by configuration and modes such as target-catalog fitting or discovery-and-fit. Inputs are FITS science images, optional RMS/weight maps, object catalogs, SExtractor catalogs, and one or more PSFs. Representative runnable datasets are `examples/full_image/config.ini` and `examples/postage_stamps/config.ini`; package defaults live at `src/pymorph/config.ini`.

#### Detection and initial-parameter stage
SExtractor is an external executable invoked to detect/measure sources and establish fit initialization, unless an appropriate catalog is supplied. PyMorph maintains SExtractor templates and convolution/filter assets, separating external-program configuration from Python orchestration:
- `src/pymorph/runsexfunc.py`
- `src/pymorph/get_params.py`
- `src/pymorph/SEx/default.sex`

#### Per-target image preparation
For each selected galaxy, the pipeline resolves target position/catalog data, creates or uses a cutout, handles weight-image semantics, estimates background, and builds neighbor masks. This is the important boundary between frame-scale survey data and isolated per-object fitting/measurement products:
- `src/pymorph/weightimage.py`
- `src/pymorph/bkgdfunc.py`
- `src/pymorph/maskfunc.py`

#### PSF selection
PSF handling chooses an individual, listed, or nearest suitable PSF for each target; selection can use coordinate-bearing filenames and SExtractor star/galaxy probability. The selected PSF becomes an explicit input to GALFIT and affects model convolution:
- `src/pymorph/psffunc.py`
- `examples/full_image/psf_1447218+0838538.fits`
- `examples/postage_stamps/psflist.list`

#### Parametric decomposition
GALFIT is the second required external executable. PyMorph generates GALFIT control files from SExtractor-derived initial values and configured component models (bulge, disk, bar), invokes GALFIT, and parses fit logs/results. Detailed mode can explore randomized initial configurations:
- `src/pymorph/galfit_config_run.py`
- `src/pymorph/detailed_galfit.py`
- `src/pymorph/read_fitlog.py`

#### Non-parametric measurements and derived quantities
An optional Python-native branch computes CASGM-style morphology metrics: concentration, asymmetry, clumpiness, Gini, and moment-related values. Cosmological conversion utilities support physical derived parameters:
- `src/pymorph/casgm.py`
- `src/pymorph/cosmocal.py`
- `src/pymorph/bar_length.py`

#### Results, diagnostics, and dependencies
Fit/measurement results flow into CSV/database writers, optional plots, and HTML reporting. Quality flags and fit-diagnosis thresholds govern result classification. Runtime infrastructure is local filesystem plus the configured SExtractor and GALFIT binaries; no network service or web runtime is indicated.
- `src/pymorph/writecsv.py`
- `src/pymorph/writedbfunc.py`
- `src/pymorph/writehtml.py`

Main entry points: [`src/pymorph/weightimage.py`](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/weightimage.py) (Cutout & weight preparation), [`src/pymorph/galfit_config_run.py`](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/galfit_config_run.py) (GALFIT control generation), [`src/pymorph/get_params.py`](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/get_params.py) (Catalog parameter extraction), [`src/pymorph/pymorph.py`](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/pymorph.py) (Pipeline orchestrator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inputs["Inputs &amp; Configuration"]
  node_cli{{"pymorph_bin CLI<br/>entry point"}}
  node_orchestrator["Pipeline orchestrator<br/>Python workflow<br/>[pymorph.py]"]
  node_config["Working config.ini<br/>operational config<br/>[config.ini]"]
  node_survey_inputs["FITS, catalogs &amp; maps<br/>astronomy inputs"]
end

subgraph group_detection["Detection &amp; Preparation"]
  node_sextractor{{"SExtractor<br/>external executable"}}
  node_sex_runner["SExtractor runner<br/>Python adapter<br/>[runsexfunc.py]"]
  node_initial_params["Catalog parameter extraction<br/>initialization<br/>[get_params.py]"]
end

subgraph group_analysis["Per-Target Analysis"]
  node_target_prep["Cutout &amp; weight preparation<br/>per-target preparation<br/>[weightimage.py]"]
  node_background["Background estimation<br/>image analysis<br/>[bkgdfunc.py]"]
  node_neighbor_mask["Neighbor masking<br/>image analysis<br/>[maskfunc.py]"]
  node_psf_selection["PSF selection<br/>per-target input selection<br/>[psffunc.py]"]
  node_galfit_control["GALFIT control generation<br/>model configuration"]
  node_galfit{{"GALFIT<br/>external executable"}}
  node_fit_parser["Fit log parser<br/>result extraction<br/>[read_fitlog.py]"]
  node_casgm["CASGM measurements<br/>Python morphology analysis<br/>[casgm.py]"]
end

subgraph group_outputs["Results &amp; Diagnostics"]
  node_result_writers[("CSV &amp; database writers<br/>result persistence<br/>[writecsv.py]")]
  node_html_report["HTML reporting<br/>diagnostic output<br/>[writehtml.py]"]
end

node_cli -->|"starts"| node_orchestrator
node_config -->|"controls"| node_orchestrator
node_survey_inputs -->|"provides targets"| node_orchestrator
node_orchestrator -->|"runs when catalog needed"| node_sex_runner
node_sex_runner -->|"invokes"| node_sextractor
node_sextractor -->|"source catalog"| node_initial_params
node_survey_inputs -.->|"supplied catalog"| node_initial_params
node_orchestrator -->|"per selected target"| node_target_prep
node_initial_params -->|"target position"| node_target_prep
node_survey_inputs -->|"image and weights"| node_target_prep
node_target_prep -->|"cutout"| node_background
node_target_prep -->|"cutout"| node_neighbor_mask
node_initial_params -->|"source coordinates"| node_psf_selection
node_survey_inputs -->|"PSF candidates"| node_psf_selection
node_initial_params -->|"initial values"| node_galfit_control
node_target_prep -->|"fit image"| node_galfit_control
node_background -->|"sky estimate"| node_galfit_control
node_neighbor_mask -->|"mask"| node_galfit_control
node_psf_selection -->|"selected PSF"| node_galfit_control
node_galfit_control -->|"control file"| node_galfit
node_galfit -->|"fit logs and models"| node_fit_parser
node_target_prep -.->|"prepared image"| node_casgm
node_neighbor_mask -.->|"mask"| node_casgm
node_fit_parser -->|"parametric results"| node_result_writers
node_casgm -.->|"optional metrics"| node_result_writers
node_result_writers -->|"classified results"| node_html_report

click node_orchestrator "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/pymorph.py"
click node_config "https://github.com/vvinuv/pymorph/blob/master/examples/full_image/config.ini"
click node_sex_runner "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/runsexfunc.py"
click node_initial_params "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/get_params.py"
click node_target_prep "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/weightimage.py"
click node_background "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/bkgdfunc.py"
click node_neighbor_mask "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/maskfunc.py"
click node_psf_selection "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/psffunc.py"
click node_galfit_control "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/galfit_config_run.py"
click node_fit_parser "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/read_fitlog.py"
click node_casgm "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/casgm.py"
click node_result_writers "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/writecsv.py"
click node_html_report "https://github.com/vvinuv/pymorph/blob/master/src/pymorph/writehtml.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_orchestrator,node_config,node_survey_inputs toneBlue
class node_sextractor,node_sex_runner,node_initial_params toneAmber
class node_target_prep,node_background,node_neighbor_mask,node_psf_selection,node_galfit_control,node_galfit,node_fit_parser,node_casgm toneMint
class node_result_writers,node_html_report toneRose
```

## Components

### Inputs & Configuration

- **pymorph_bin CLI**: entry point · Launches batch orchestration.
- [Pipeline orchestrator](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/pymorph.py): Python workflow · Coordinates configured target processing. · `src/pymorph/pymorph.py`
- [Working config.ini](https://github.com/vvinuv/pymorph/blob/master/examples/full_image/config.ini): operational config · Selects modes, inputs, models, and thresholds. · `examples/full_image/config.ini`
- **FITS, catalogs & maps**: astronomy inputs · Science frames or stamps with optional weights and catalogs.

### Detection & Preparation

- **SExtractor**: external executable · Detects and measures sources when needed.
- [SExtractor runner](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/runsexfunc.py): Python adapter · `src/pymorph/runsexfunc.py`
- [Catalog parameter extraction](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/get_params.py): initialization · Derives target and fit starting values. · `src/pymorph/get_params.py`

### Per-Target Analysis

- [Cutout & weight preparation](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/weightimage.py): per-target preparation · Bridges survey-scale inputs to object products. · `src/pymorph/weightimage.py`
- [Background estimation](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/bkgdfunc.py): image analysis · `src/pymorph/bkgdfunc.py`
- [Neighbor masking](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/maskfunc.py): image analysis · `src/pymorph/maskfunc.py`
- [PSF selection](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/psffunc.py): per-target input selection · Chooses individual, listed, or nearest PSF. · `src/pymorph/psffunc.py`
- [GALFIT control generation](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/galfit_config_run.py): model configuration · Builds component-model control files. · `src/pymorph/galfit_config_run.py`
- **GALFIT**: external executable · Performs PSF-convolved parametric decomposition.
- [Fit log parser](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/read_fitlog.py): result extraction · `src/pymorph/read_fitlog.py`
- [CASGM measurements](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/casgm.py): Python morphology analysis · Optional non-parametric metric branch. · `src/pymorph/casgm.py`

### Results & Diagnostics

- [CSV & database writers](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/writecsv.py): result persistence · `src/pymorph/writecsv.py`
- [HTML reporting](https://github.com/vvinuv/pymorph/blob/master/src/pymorph/writehtml.py): diagnostic output · `src/pymorph/writehtml.py`

## Connections

- pymorph_bin CLI → Pipeline orchestrator: starts
- Working config.ini → Pipeline orchestrator: controls
- FITS, catalogs & maps → Pipeline orchestrator: provides targets
- Pipeline orchestrator → SExtractor runner: runs when catalog needed
- SExtractor runner → SExtractor: invokes
- SExtractor → Catalog parameter extraction: source catalog
- FITS, catalogs & maps → Catalog parameter extraction: supplied catalog
- Pipeline orchestrator → Cutout & weight preparation: per selected target
- Catalog parameter extraction → Cutout & weight preparation: target position
- FITS, catalogs & maps → Cutout & weight preparation: image and weights
- Cutout & weight preparation → Background estimation: cutout
- Cutout & weight preparation → Neighbor masking: cutout
- Catalog parameter extraction → PSF selection: source coordinates
- FITS, catalogs & maps → PSF selection: PSF candidates
- Catalog parameter extraction → GALFIT control generation: initial values
- Cutout & weight preparation → GALFIT control generation: fit image
- Background estimation → GALFIT control generation: sky estimate
- Neighbor masking → GALFIT control generation: mask
- PSF selection → GALFIT control generation: selected PSF
- GALFIT control generation → GALFIT: control file
- GALFIT → Fit log parser: fit logs and models
- Cutout & weight preparation → CASGM measurements: prepared image
- Neighbor masking → CASGM measurements: mask
- Fit log parser → CSV & database writers: parametric results
- CASGM measurements → CSV & database writers: optional metrics
- CSV & database writers → HTML reporting: classified results
