Coverage for src/cell_abm_pipeline/flows/calculate_neighbors.py: 0%
45 statements
« prev ^ index » next coverage.py v7.1.0, created at 2024-06-05 19:14 +0000
« prev ^ index » next coverage.py v7.1.0, created at 2024-06-05 19:14 +0000
1"""
2Workflow for calculating neighbor connections.
4Working location structure:
6.. code-block:: bash
8 (name)
9 ├── data
10 │ └── data.LOCATIONS
11 │ └── (name)_(key)_(seed).LOCATIONS.tar.xz
12 └── calculations
13 └── calculations.NEIGHBORS
14 └── (name)_(key)_(seed)_(tick).NEIGHBORS.csv
16Data from **data.LOCATIONS** are used to calculate neighbors, which are saved to
17**calculations.NEIGHBORS**.
18"""
20from dataclasses import dataclass
22import pandas as pd
23from abm_colony_collection import get_depth_map, get_neighbors_map, make_voxels_array
24from arcade_collection.output import extract_tick_json
25from io_collection.keys import make_key
26from io_collection.load import load_tar
27from io_collection.save import save_dataframe
28from prefect import flow
31@dataclass
32class ParametersConfig:
33 """Parameter configuration for calculate neighbors flow."""
35 key: str
36 """Simulation key to calculate."""
38 seed: int
39 """Simulation random seed to calculate."""
41 tick: int
42 """Simulation tick to calculate."""
45@dataclass
46class ContextConfig:
47 """Context configuration for calculate neighbors flow."""
49 working_location: str
50 """Location for input and output files (local path or S3 bucket)."""
53@dataclass
54class SeriesConfig:
55 """Series configuration for calculate neighbors flow."""
57 name: str
58 """Name of the simulation series."""
61@flow(name="calculate-neighbors")
62def run_flow(context: ContextConfig, series: SeriesConfig, parameters: ParametersConfig) -> None:
63 """Main calculate neighbors flow."""
65 data_key = make_key(series.name, "data", "data.LOCATIONS")
66 calc_key = make_key(series.name, "calculations", "calculations.NEIGHBORS")
67 series_key = f"{series.name}_{parameters.key}_{parameters.seed:04d}"
69 locations_key = make_key(data_key, f"{series_key}.LOCATIONS.tar.xz")
70 locations_tar = load_tar(context.working_location, locations_key)
71 locations_json = extract_tick_json(locations_tar, series_key, parameters.tick, "LOCATIONS")
73 array = make_voxels_array(locations_json)
75 neighbors_map = get_neighbors_map(array)
76 depth_map = get_depth_map(array, neighbors_map)
77 center_map = {location["id"]: location["center"] for location in locations_json}
79 attributes = {"KEY": parameters.key, "SEED": parameters.seed, "TICK": parameters.tick}
80 all_neighbors = []
82 for voxel_id, voxel_neighbors in neighbors_map.items():
83 voxel_neighbors = {
84 "ID": voxel_id,
85 "GROUP": voxel_neighbors["group"],
86 "NEIGHBORS": voxel_neighbors["neighbors"],
87 "CX": center_map[voxel_id][0],
88 "CY": center_map[voxel_id][1],
89 "CZ": center_map[voxel_id][2],
90 "DEPTH": depth_map[voxel_id],
91 }
92 voxel_neighbors.update(attributes)
93 all_neighbors.append(voxel_neighbors)
95 neighbors_dataframe = pd.DataFrame(all_neighbors)
96 neighbors_key = make_key(calc_key, f"{series_key}_{parameters.tick:06d}.NEIGHBORS.csv")
97 save_dataframe(context.working_location, neighbors_key, neighbors_dataframe, index=False)