Metadata-Version: 2.5
Name: pelago
Version: 0.2.0
Summary: Helpers for Ocean / Pontus-X compute-to-data algorithms: inputs, consumer parameters, outputs, HDF5 sections and PDF reports.
Project-URL: Homepage, https://git.ift.tuwien.ac.at/pub/pypi/pelago
Project-URL: Source, https://git.ift.tuwien.ac.at/pub/pypi/pelago
Project-URL: Issues, https://git.ift.tuwien.ac.at/pub/pypi/pelago/-/issues
Author: TU Wien, IFT
License: MIT
License-File: LICENSE
Keywords: compute-to-data,dataspace,hdf5,ocean protocol,pontus-x
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Provides-Extra: all
Requires-Dist: h5py>=3.8; extra == 'all'
Requires-Dist: markdown>=3.4; extra == 'all'
Requires-Dist: matplotlib>=3.7; extra == 'all'
Requires-Dist: numpy>=1.24; extra == 'all'
Requires-Dist: pandas>=2.0; extra == 'all'
Requires-Dist: xhtml2pdf>=0.2.11; extra == 'all'
Provides-Extra: build
Requires-Dist: stickytape>=0.2.1; extra == 'build'
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == 'dev'
Requires-Dist: h5py>=3.8; extra == 'dev'
Requires-Dist: markdown>=3.4; extra == 'dev'
Requires-Dist: matplotlib>=3.7; extra == 'dev'
Requires-Dist: numpy>=1.24; extra == 'dev'
Requires-Dist: pandas>=2.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: stickytape>=0.2.1; extra == 'dev'
Requires-Dist: tables>=3.8; extra == 'dev'
Requires-Dist: twine>=6.2; extra == 'dev'
Requires-Dist: xhtml2pdf>=0.2.11; extra == 'dev'
Provides-Extra: hdf5
Requires-Dist: h5py>=3.8; extra == 'hdf5'
Requires-Dist: numpy>=1.24; extra == 'hdf5'
Provides-Extra: report
Requires-Dist: markdown>=3.4; extra == 'report'
Requires-Dist: matplotlib>=3.7; extra == 'report'
Requires-Dist: xhtml2pdf>=0.2.11; extra == 'report'
Description-Content-Type: text/markdown

# pelago

Helpers for compute-to-data algorithms on Ocean / Pontus-X: input files, consumer parameters and output files, plus
reading sections of HDF5 files and writing PDF reports. The same script runs on your machine and on the node.

## Install

```bash
pip install pelago                # Job and Param, standard library only
pip install "pelago[hdf5]"        # + H5File (h5py, numpy)
pip install "pelago[report]"      # + Report and plots (matplotlib, markdown, xhtml2pdf)
pip install "pelago[all]"         # everything, including pandas for H5File.frame()
pip install "pelago[build]"       # + the `pelago build` command (stickytape)
```

Python 3.10 or newer; `pelago build` needs 3.11.

## Usage

```python
from pelago import Job, Param

job = Job(
    Param.number("window", default=10000),
    Param.text("keyword", required=False),
    Param.select("mode", ["fast", "full"], default="fast"),
    Param.boolean("pdf", default=True),
    Param.text("api_token", required=False, secret=True),   # masked in the log and in reports
)

for path in job.inputs:
    ...

job.write_json("summary.json", {"files": len(job.inputs), "window": job.params.window})
```

- `job.inputs`: all dataset files of the job, sorted by name. `job.inputs_matching("*.csv")`, `job.input("*.csv")`,
  `job.require_inputs(n)` and `job.hdf5_inputs` (HDF5 by extension or file signature) select from them.
- `job.params`: the parameter values, converted to the declared type. If the consumer did not set a value, the
  default is used. `job.params.sources` tells where each value came from. The log (`algorithm.log`, which the
  consumer gets back) and `Report.params` show `****` for a parameter declared with `secret=True` and only the name
  of an undeclared one.
- `job.output(name)`, `job.write_json`, `job.write_text`, `job.write_csv`, `job.save_figure`: write to the output
  folder. Paths outside it are refused.
- `job.log(msg)` writes to the job log, `job.fail(msg)` stops the job with exit code 2.
- An invalid or missing required parameter stops the job with exit code 2 and one line in the log.

Start from [examples/minimal](examples/minimal/src/main.py).

## HDF5

`H5File` reads the structure and attributes without touching the data, then only the rows and columns you ask for.
It does not assume any layout; compound tables written by PyTables or pandas (`format="table"`) work too.

```python
from pelago import H5File

with H5File(path) as h5:
    print(h5.describe())                      # tree with shapes, types and fields
    h5.tree()                                 # {path: {"kind", "shape", "dtype", "fields", "attrs"}}
    h5.attrs("signals/acceleration")          # attributes; bytes decoded, JSON strings parsed
    part = h5.read("signals/acceleration", rows=slice(0, 10_000), columns=["time", "x"])
    for chunk in h5.chunks("signals/acceleration", size=100_000, columns=["x"]):
        ...
    df = h5.frame("signals/acceleration", rows=slice(0, 1000))    # pandas DataFrame
    h5.find("*image*")                        # paths matching a glob
    h5.save("pictures/img0", job.output("img0.png"))              # bytes or string dataset to a file
```

- `read` returns numpy arrays: a structured array for several fields of a compound dataset, a plain array for one
  field. For 2-D datasets `columns` are indexes of the second axis. Strings come back as `str`.
- `save` and `raw` handle bytes, strings, uint8 arrays and opaque values; `data:...;base64,` strings are decoded.
  `index=` picks one element of an array of images.
- A wrong path raises `KeyError` with the closest existing paths.

## Reports

```python
from pelago import Report
from pelago.plots import bar, line, heatmap

(Report("Summary")
    .text("## Result\n\nAll files passed.")          # Markdown
    .params(job.params)
    .table(rows, title="Files")                       # list of dicts, rows + columns, or a DataFrame
    .figure(line(t, {"x": x, "y": y}, xlabel="time [s]"), "Signal")
    .image("photo.png", "Setup")
    .save(job.output("report.pdf")))                  # .pdf via xhtml2pdf, anything else writes HTML
```

The style is built in. Change a few variables with `Report(title, style={"font": "Times-Roman", "accent": "#b91c1c",
"page": "A4 landscape"})` (also `font_size`, `margin`, `image_width`, colours `ink`, `muted`, `rule`, `shade`), or
add CSS with `Report(title, css="h1 { color: black; }")`. `.html()` returns the whole page; figures and images are
embedded. `pelago.plots` has `bar`, `line` and `heatmap`; each returns a matplotlib figure and needs no display.
A PDF takes only embedded images: links to files or URLs (e.g. in Markdown from dataset text) are left out. Markdown
keeps its HTML; put text from a dataset into `note()` or `table()`, which escape it.

## Run locally

```bash
python main.py --inputs test-data --out outputs --param window=500 --param pdf=false
python main.py --describe        # consumerParameters for the algorithm's DDO
python main.py --describe --full # the same with min, max and secret
```

An `algoCustomData.json` in the input folder is read the same way as on the node. `--params file.json` and
`--param name=value` override it, in that order. Without options the paths come from `PELAGO_INPUTS` and
`PELAGO_OUTPUTS` (default `/data/inputs`, `/data/outputs`); `--log` or `PELAGO_LOG` set the log level.

## How Ocean passes data to the job

| | path in the container |
|---|---|
| datasets | `/data/inputs/<file name>` |
| parameters | `/data/inputs/algoCustomData.json` |
| algorithm | `/data/transformations/algorithm` |
| outputs | `/data/outputs`, returned as `outputs.tar` |
| log | stdout, returned as `algorithm.log` |

`Job` also reads the per-DID layout (`DIDS` set, files in `/data/inputs/<did>/0`); `job.inputs_by_did` groups them.
The node does not apply the parameter defaults from the DDO. If the consumer sets nothing, `algoCustomData.json` is
`{}`. Some clients send numbers and booleans as strings. `Job` handles both.

## Publish an algorithm

`pelago build` bundles the script, its local modules and pelago into one file. It also writes an app definition
next to it:

```bash
pelago build                     # in the project folder: dist/<name>.py and dist/<name>.dsapp.json
pelago check dist/<name>.dsapp.json
pelago describe                  # print the definition, write nothing
```

The definition holds the name, version and description, the container image and entrypoint, and the full
consumerParameters. It also holds the bundle's sha256 and size. Upload the bundle and the definition together.
Trident recognises the definition and fills the algorithm service from it: type, image, entrypoint, parameters and
version. It adds the image when it is not registered yet. The format is described in
[docs/DSAPP.md](docs/DSAPP.md), and its JSON Schema is
[src/pelago/schema/dsapp-1.json](src/pelago/schema/dsapp-1.json).

The settings come from `pelago.toml` in the project folder, or from `[tool.pelago]` in `pyproject.toml`. In
`pyproject.toml`, the name, version, description, authors and license are taken from `[project]`. A dynamic version
is read from `__version__` in the file that `[tool.hatch.version]` names:

```toml
name = "my-algorithm"
version = "1.0.0"
title = "My algorithm"
description = "What it computes."
license = "MIT"
main = "src/main.py"                          # default: main.py or src/main.py
paths = ["../shared"]                         # more folders with modules to bundle
image = "ttrau/datascience-base:3.13.4-slim"  # the default
entrypoint = "python $ALGO"                   # the default
requirements = ["pelago[hdf5]>=0.2"]          # what the bundle needs from the image (informational)
inputs = { min = 1, formats = ["hdf5"] }
outputs = [{ path = "report.pdf", description = "the report" }]
```

The default image `ttrau/datascience-base` has pelago, numpy, h5py, matplotlib and the report libraries installed.
`--image name:tag` sets another one. `pelago build` runs the bundle once with `--describe --full` to read the
parameters, so the Python running it needs the modules the script imports before it creates its `Job`. Use
`--python PATH` to pick another interpreter.

Examples:
- [examples/minimal](examples/minimal) is the template for a new algorithm;
- [examples/multi_asset_text](examples/multi_asset_text) handles several datasets;
- [examples/hdf5_sections](examples/hdf5_sections) reads sections of HDF5 inputs;
- [examples/fedavg](examples/fedavg) trains a model over several data owners with federated averaging.

The `build.sh` of multi_asset_text and fedavg runs `pelago build`, then tests the bundle locally and in the
container.

## Development

```bash
python3 -m venv .venv && . .venv/bin/activate
make init install
make test
```

`make clean` removes build output, caches and example outputs.

## Release

The version is set in one place: `__version__` in `src/pelago/__init__.py`. Use semantic versions: raise the last
number for fixes, the middle one for new features.

1. Set `__version__`, commit and push.
2. `make release`. It stops if the version is already on PyPI, if there are uncommitted changes, or if a test fails.
   Otherwise it builds, uploads to PyPI (credentials from `~/.pypirc` or a PyPI token) and then pushes the tag
   `v<version>`.
3. Raise `pelago>=<version>` in the requirements of `datascience-base-docker` and build a new image.

`make version` prints the current version. If the upload worked but the tag push failed, push the tag by hand:
`git tag v<version> && git push origin v<version>`.

## License

MIT, see [LICENSE](LICENSE).
