Skip to content
Snippets Groups Projects
Commit eb00e2b2 authored by pswain's avatar pswain
Browse files

feature(signal): make check on lineage assignment by validate_lineage optional

parent fbc1d8f2
No related branches found
No related tags found
No related merge requests found
...@@ -142,15 +142,15 @@ class Signal(BridgeH5): ...@@ -142,15 +142,15 @@ class Signal(BridgeH5):
with h5py.File(self.filename, "r") as f: with h5py.File(self.filename, "r") as f:
if lineage_location not in f: if lineage_location not in f:
lineage_location = "postprocessing/lineage" lineage_location = "postprocessing/lineage"
tile_mo_da = f[lineage_location] traps_mothers_daughters = f[lineage_location]
if isinstance(tile_mo_da, h5py.Dataset): if isinstance(traps_mothers_daughters, h5py.Dataset):
lineage = tile_mo_da[()] lineage = traps_mothers_daughters[()]
else: else:
lineage = np.array( lineage = np.array(
( (
tile_mo_da["trap"], traps_mothers_daughters["trap"],
tile_mo_da["mother_label"], traps_mothers_daughters["mother_label"],
tile_mo_da["daughter_label"], traps_mothers_daughters["daughter_label"],
) )
).T ).T
return lineage return lineage
...@@ -249,6 +249,7 @@ class Signal(BridgeH5): ...@@ -249,6 +249,7 @@ class Signal(BridgeH5):
dataset: str or t.List[str], dataset: str or t.List[str],
in_minutes: bool = True, in_minutes: bool = True,
lineage: bool = False, lineage: bool = False,
run_lineage_check: bool = True,
**kwargs, **kwargs,
) -> pd.DataFrame or t.List[pd.DataFrame]: ) -> pd.DataFrame or t.List[pd.DataFrame]:
""" """
...@@ -262,6 +263,8 @@ class Signal(BridgeH5): ...@@ -262,6 +263,8 @@ class Signal(BridgeH5):
If True, convert column headings to times in minutes. If True, convert column headings to times in minutes.
lineage: boolean lineage: boolean
If True, add mother_label to index. If True, add mother_label to index.
run_lineage_check: boolean
If True, raise exception if a likely error in the lineage assignment.
""" """
try: try:
if isinstance(dataset, str): if isinstance(dataset, str):
...@@ -279,6 +282,7 @@ class Signal(BridgeH5): ...@@ -279,6 +282,7 @@ class Signal(BridgeH5):
lineage, lineage,
indices=np.array(df.index.to_list()), indices=np.array(df.index.to_list()),
how="daughters", how="daughters",
run_lineage_check=run_lineage_check,
) )
mother_label[valid_indices] = lineage[ mother_label[valid_indices] = lineage[
valid_lineage, 1 valid_lineage, 1
......
...@@ -5,7 +5,10 @@ i_dtype = {"names": ["trap_id", "cell_id"], "formats": [np.int64, np.int64]} ...@@ -5,7 +5,10 @@ i_dtype = {"names": ["trap_id", "cell_id"], "formats": [np.int64, np.int64]}
def validate_lineage( def validate_lineage(
lineage: np.ndarray, indices: np.ndarray, how: str = "families" lineage: np.ndarray,
indices: np.ndarray,
how: str = "families",
run_lineage_check: bool = True,
): ):
""" """
Identify mother-bud pairs both in lineage and a Signal's indices. Identify mother-bud pairs both in lineage and a Signal's indices.
...@@ -28,6 +31,9 @@ def validate_lineage( ...@@ -28,6 +31,9 @@ def validate_lineage(
If "mothers", matches indicate mothers from mother-bud pairs; If "mothers", matches indicate mothers from mother-bud pairs;
If "daughters", matches indicate daughters from mother-bud pairs; If "daughters", matches indicate daughters from mother-bud pairs;
If "families", matches indicate mothers and daughters in mother-bud pairs. If "families", matches indicate mothers and daughters in mother-bud pairs.
run_lineage_check: bool
If True, check for errors in the lineage assignment such as a daughter
being assigned two mothers.
Returns Returns
------- -------
...@@ -85,24 +91,25 @@ def validate_lineage( ...@@ -85,24 +91,25 @@ def validate_lineage(
valid_indices = index_isin(indices, selected_lineages[:, c_index, :]) valid_indices = index_isin(indices, selected_lineages[:, c_index, :])
flat_valid_indices = valid_indices.flatten() flat_valid_indices = valid_indices.flatten()
# test for mismatch # test for mismatch
if how == "families": if run_lineage_check:
test_mismatch = ( if how == "families":
indices[flat_valid_indices, :].size test_mismatch = (
!= np.unique( indices[flat_valid_indices, :].size
lineage[flat_valid_lineage, :].reshape(-1, 2), axis=0 != np.unique(
).size lineage[flat_valid_lineage, :].reshape(-1, 2), axis=0
) ).size
else: )
test_mismatch = ( else:
indices[flat_valid_indices, :].size test_mismatch = (
!= lineage[flat_valid_lineage, c_index, :].size indices[flat_valid_indices, :].size
) != lineage[flat_valid_lineage, c_index, :].size
if test_mismatch: )
# all unique indices in valid_lineages should be in valid_indices if test_mismatch:
raise Exception( # all unique indices in valid_lineages should be in valid_indices
"Error in validate_lineage: " raise Exception(
"lineage information is likely not unique." "Error in validate_lineage: "
) "lineage information is likely not unique."
)
return flat_valid_lineage, flat_valid_indices return flat_valid_lineage, flat_valid_indices
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment