livecellx.core.SingleCellStatic.write_single_cells_json¶
- static SingleCellStatic.write_single_cells_json(single_cells, path, dataset_dir=None, return_list=False)¶
Write a JSON file containing a list of single cells.
- Parameters:
single_cells (List of SingleCellStatic) – The list of single cells to be written to the JSON file.
path (Union[str, Path]) – The path to the JSON file.
dataset_dir (str, optional) – The path to the dataset directory, by default None.
return_list (bool, optional) – Whether to return the list of single cell JSON dictionaries, by default False.
- Returns:
The list of single cell JSON dictionaries, if return_list is True.
- Return type:
List of dict
- Raises:
TypeError – If there is an error writing the JSON file due to non-serializable attributes.
Notes
This function writes a JSON file containing a list of single cells. Each single cell is converted to a JSON dictionary using the to_json_dict method of the SingleCellStatic class. The include_dataset_json parameter of to_json_dict is set to False, and the dataset_json_dir parameter is set to the dataset_dir argument passed to this function.
If dataset_dir is not provided, it is derived from the parent directory of the path argument by appending “/datasets”.
The img_dataset and mask_dataset attributes of each single cell are expected to be instances of classes that have a write_json method. The write_json method is called on the img_dataset and mask_dataset objects to write their respective JSON files to the dataset_dir.
If return_list is True, the function returns the list of single cell JSON dictionaries.
If there is an error writing the JSON file due to non-serializable attributes, a TypeError is raised.
Example
single_cells = [single_cell1, single_cell2, single_cell3] path = “/path/to/single_cells.json” dataset_dir = “/path/to/datasets” write_single_cells_json(single_cells, path, dataset_dir, return_list=True)