◆ Reference · Python
Reference › Python
Python
The milkflow package (PyO3, one abi3 wheel for CPython ≥ 3.9) builds the JSON input, calls the core and returns the result. No dependencies; accepts pandas and polars data frames when installed. The GIL is released during the computation.
pip install milkflow
Example
import milkflow
nodes = [
{"id": "centro", "x": 0, "y": 0, "label": "Centro"},
{"id": "porto", "x": 3200, "y": -1400, "label": "Porto"},
{"id": "serra", "x": -3800, "y": 3600, "label": "Serra"},
{"id": "olaria", "x": -400, "y": -3900, "label": "Olaria"},
]
flows = [
{"origin": "serra", "destination": "centro", "value": 410},
{"origin": "porto", "destination": "centro", "value": 260},
{"origin": "olaria", "destination": "centro", "value": 180},
{"origin": "centro", "destination": "porto", "value": 90},
{"origin": "serra", "destination": "porto", "value": 35},
]
r = milkflow.flowmap(nodes, flows, k=2)
r.save("mapa.svg")
print(r.spec["meta"]["accounting"])
With data frames and other column names:
r = milkflow.flowmap(zones_df, trips_df, id="code", x="easting", y="northing",
origin="from", destination="to", value="trips")
milkflow.flowmap(nodes, flows, *, …) -> Result
| Argument | Effect |
|---|---|
nodes, flows | Lists of dicts, or any object with to_dict("records") (pandas) or to_dicts() (polars). Extra columns are ignored; empty cells (None, NaN) in optional columns count as missing; integer ids become decimal text. |
skeleton, boundary, crs, units, additive=True | The rest of the input. |
group=False | True: aggregates by the group column (node_group). The name of a level ("municipio"): aggregates by the group at that level, read from the node's dict of levels (node_groups) or, when it lacks the level, from the column of that name. One table of nodes gives the figure at every level; a level no node has is refused. |
k=3, mu=4.0, max_stretch=None | Pipeline options. |
filter=None | The name of a filter ("dominant") or a dict with kind and its parameters ({"kind": "disparity", "alpha": 0.01}). See Filters. |
skeleton_source="delaunay" | The skeleton option ("input" uses skeleton). |
represent="all", schematic=False, schematic_options=None | Pipeline options; schematic_options is a dict (fields). |
html=False, tikz=False | Also the interactive page (Result.html) and the TikZ figures (Result.tikz, Result.schematic_tikz). |
lines="auto", max_categories=8 | The lines mode, when there is a category column (Lines). |
style=None | A dict with the fields of Style. |
id, x, y, label, weight, node_group, node_groups="groups" | Column names in nodes (None ignores an optional column); node_groups is the column of the dicts {level: group}. |
origin, destination, value, category | Column names in flows. |
Result
.spec (dict), .svg (str), .schematic_spec, .schematic_svg, .schematic_error, .tikz, .schematic_tikz, .html (None unless asked for); .save(path, schematic=False) writes the figure (.svg) or the spec (.json); in Jupyter the result shows itself as SVG.
Other functions
milkflow.flowmap_json(input_json, options_json="") -> str: the raw door, JSON text in and out, byte for byte the native output. milkflow.__version__. Library errors raise ValueError with its message; wrong types, TypeError.
Planar coordinates: project first (pyproj, GeoDataFrame.to_crs).