Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions djimaging/tables/core/roi_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,8 @@ def _add_field_roi_masks(
print('No ROI masks found for field:', field_key)
return

if len(self & field_key) == 0:
main_exists = len(self & field_key) > 0
if not main_exists:
# Find preferred file that should be used as main key.
mask_alias, highres_alias = (self.userinfo_table & field_key).fetch1("mask_alias", "highres_alias")
keys_masks_files = [
Expand Down Expand Up @@ -727,6 +728,11 @@ def _add_field_roi_masks(
main_roi_mask = load_array((self.RoiMaskPresentation & main_pres_key).fetch1('roi_mask'))
main_file, main_source = (self.RoiMaskPresentation & main_pres_key).fetch1(
'roi_mask_file', 'roi_mask_source')
# A fetched <filepath> is an ObjectRef, which cannot be inserted again (e.g. via auto_fill_pres_keys)
main_file = relative_store_path(
local_file_path(main_file, self._filepath_store), self._filepath_store)
if verboselvl > 0:
print(f'Adding {len(data_pairs)} presentation ROI masks to existing field:', field_key)

roi_mask_pres_keys = []

Expand All @@ -747,12 +753,13 @@ def _add_field_roi_masks(
else:
raise ValueError(f'No ROI mask found for key={pres_key} but `auto_fill_pres_keys` is False')

main_key = {**field_key, **main_pres_key, "roi_mask": main_roi_mask,
"roi_mask_file": main_file, "roi_mask_source": main_source}
if not main_exists:
main_key = {**field_key, **main_pres_key, "roi_mask": main_roi_mask,
"roi_mask_file": main_file, "roi_mask_source": main_source}

if add_primary_keys is not None:
main_key = {**main_key, **add_primary_keys}
self.insert1(main_key, skip_duplicates=True)
if add_primary_keys is not None:
main_key = {**main_key, **add_primary_keys}
self.insert1(main_key, skip_duplicates=True)

for roi_mask_pres_key in roi_mask_pres_keys:
if add_primary_keys is not None:
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/test_tutorial_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sklearn.dummy import DummyClassifier

from djimaging.tables.classifier_v2.rgc_classifier_v2 import save_classifier_to_file
from djimaging.utils.dj_storage import load_array
from tests.fixtures.random_utils import numpy_seed


Expand All @@ -15,6 +16,30 @@
)


def _check_rescan_adds_presentations_to_existing_field(tutorial_schema, key):
"""Rescanning with only_new_fields=False adds new presentations of fields that already have a RoiMask.

Simulates a newly added stimulus by removing one non-main presentation mask. The existing main entry
must not be re-inserted from fetched values (a fetched <filepath> is an ObjectRef and cannot be inserted).
"""
roi_mask = tutorial_schema.RoiMask()
main_before = (roi_mask & key).proj("roi_mask_source").fetch1()
main_mask_before = load_array((roi_mask & key).fetch1("roi_mask"))
main_pres_key = (roi_mask.RoiMaskPresentation.proj() & (roi_mask & key)).fetch1()
pres_keys = (roi_mask.RoiMaskPresentation & key).keys()
new_pres_key = next(pres_key for pres_key in pres_keys if pres_key != main_pres_key)
new_pres_mask = load_array((roi_mask.RoiMaskPresentation & new_pres_key).fetch1("roi_mask"))
(roi_mask.RoiMaskPresentation & new_pres_key).delete_quick()

roi_mask.rescan_filesystem(restrictions=key, verboselvl=0, roi_mask_dir="AutoROIs", only_new_fields=False)

assert len(roi_mask.RoiMaskPresentation & key) == len(pres_keys)
np.testing.assert_array_equal(
load_array((roi_mask.RoiMaskPresentation & new_pres_key).fetch1("roi_mask")), new_pres_mask)
assert (roi_mask & key).proj("roi_mask_source").fetch1() == main_before
np.testing.assert_array_equal(load_array((roi_mask & key).fetch1("roi_mask")), main_mask_before)


def test_tutorial_pipeline(tutorial_schema, tutorial_data_dir, dj_test_stores):
experiment_key = {"experimenter": "synthetic"}

Expand Down Expand Up @@ -78,6 +103,7 @@ def test_tutorial_pipeline(tutorial_schema, tutorial_data_dir, dj_test_stores):
verboselvl=0,
roi_mask_dir="AutoROIs",
)
_check_rescan_adds_presentations_to_existing_field(tutorial_schema, experiment_key)
tutorial_schema.Roi().populate(experiment_key, display_progress=False)

tutorial_schema.Traces().populate(experiment_key, display_progress=False)
Expand Down
Loading