backend
Submodules
Classes
An object representing a motile tracking run. Contains a name, |
|
The set of solver parameters supported in the motile plugin. |
Functions
|
Get a tracking solution for the given segmentation and parameters. |
Package Contents
- class backend.MotileRun(graph: networkx.DiGraph, segmentation: numpy.ndarray | None, run_name: str, time_attr: str = NodeAttr.TIME.value, pos_attr: str | tuple[str] | list[str] = NodeAttr.POS.value, scale: list[float] | None = None, solver_params: backend.solver_params.SolverParams | None = None, input_segmentation: numpy.ndarray | None = None, input_points: numpy.ndarray | None = None, time: datetime.datetime | None = None, gaps: list[float] | None = None, status: str = 'done')
Bases:
motile_plugin.data_model.SolutionTracks
An object representing a motile tracking run. Contains a name, parameters, time of creation, information about the solving process (status and list of solver gaps), and optionally the input and output segmentations and tracks. Mostly used for passing around the set of attributes needed to specify a run, as well as saving and loading.
- run_name
- solver_params
- input_segmentation
- input_points
- gaps
- status
- time
- _make_id() str
Combine the time and run name into a unique id for the run
- Returns:
str: A unique id combining the timestamp and run name
- static _unpack_id(_id: str) tuple[datetime.datetime, str]
Unpack a string id created with _make_id into the time and run name
- Args:
_id (str): The id to unpack into time and run name
- Raises:
ValueError: If the provided id is not in the expected format
- Returns:
tuple[datetime, str]: A tuple of time and run name
- save(base_path: str | pathlib.Path) pathlib.Path
Save the run in the provided directory. Creates a subdirectory from the timestamp and run name and stores one file for each element of the run in that subdirectory.
- Args:
base_path (str | Path): The directory to save the run in.
- Returns:
(Path): The Path that the run was saved in. The last part of the path is the directory that was created to store the run.
- classmethod load(run_dir: pathlib.Path | str, output_required: bool = True)
Load a run from disk into memory.
- Args:
- run_dir (Path | str): A directory containing the saved run.
Should be the subdirectory created by MotileRun.save that includes the timestamp and run name.
- output_required (bool): If the model outputs are required.
If true, will raise an error if the output files are not found. Defualts to True.
- Returns:
MotileRun: The run saved in the provided directory.
- _save_params(run_dir: pathlib.Path)
Save the run parameters in the provided run directory. Currently dumps the parameters dict into a json file.
- Args:
run_dir (Path): A directory in which to save the parameters file.
- static _load_params(run_dir: pathlib.Path) backend.solver_params.SolverParams
Load parameters from the parameters json file in the provided directory.
- Args:
run_dir (Path): The directory in which to find the parameters file.
- Raises:
- FileNotFoundError: If the parameters file is not found in the
provided directory.
- Returns:
SolverParams: The solver parameters loaded from disk.
- _save_array(run_dir: pathlib.Path, filename: str, array: numpy.ndarray)
Save a segmentation as a numpy array using np.save. In the future, could be changed to use zarr or other file types.
- Args:
run_dir (Path): The directory in which to save the segmentation filename (str): The filename to use array (np.array): The array to save
- static _load_array(run_dir: pathlib.Path, filename: str, required: bool = True) numpy.ndarray | None
Load an array from file using np.load. In the future, could be lazy loading from a zarr.
- Args:
run_dir (Path): The base run directory containing the array filename (str): The name of the file to load required (bool, optional): If true, will fail if the array
file is not present. If false, will return None if the file is not present. Defaults to True.
- Raises:
- FileNotFoundError: If the array file is not found, and
it was required.
- Returns:
- np.ndarray | None: The array, or None if the file was
not found and not required.
- _save_attrs(directory: pathlib.Path)
Save the time_attr, pos_attr, and scale in a json file in the given directory.
- Args:
directory (Path): The directory in which to save the attributes
- _save_list(list_to_save: list | None, run_dir: pathlib.Path, filename: str)
- static _load_list(run_dir: pathlib.Path, filename: str, required: bool = True) list[float]
- delete(base_path: str | pathlib.Path)
Delete this run from the file system. Will look inside base_path for the directory corresponding to this run and delete it.
- Args:
- base_path (str | Path): The parent directory where the run is saved
(not the one created by self.save).
- class backend.SolverParams(/, **data: Any)
Bases:
pydantic.BaseModel
The set of solver parameters supported in the motile plugin. Used to build the UI as well as store parameters for runs.
- max_edge_distance: float
- max_children: int
- edge_selection_cost: float | None
- appear_cost: float | None
- division_cost: float | None
- distance_cost: float | None
- iou_cost: float | None
- backend.solve(solver_params: backend.solver_params.SolverParams, input_data: numpy.ndarray, on_solver_update: collections.abc.Callable | None = None, scale: list | None = None) networkx.DiGraph
Get a tracking solution for the given segmentation and parameters.
Constructs a candidate graph from the segmentation, a solver from the parameters, and then runs solving and returns a networkx graph with the solution. Most of this functionality is implemented in the motile toolbox.
- Args:
- solver_params (SolverParams): The solver parameters to use when
initializing the solver
- input_data (np.ndarray): The input segmentation or points list to run
tracking on. If 2D, assumed to be a list of points, otherwise a segmentation.
- on_solver_update (Callable, optional): A function that is called
whenever the motile solver emits an event. The function should take a dictionary of event data, and can be used to track progress of the solver. Defaults to None.
- Returns:
- nx.DiGraph: A solution graph where the ids of the nodes correspond to
the time and ids of the passed in segmentation labels. See the motile_toolbox for exact implementation details.