diff --git a/CHANGELOG.md b/CHANGELOG.md index 66131e81..a846c99e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ * The `Suite2PSegmentationExtractor` now produces an error when a required sub-file is missin: [#330](https://github.com/catalystneuro/roiextractors/pull/330) * Added `_image_mask` initialization in `BaseSegmentationExtractor`; combined `abstractmethod`s into top of file: [#327](https://github.com/catalystneuro/roiextractors/pull/327) * Optimize parsing of xml with `lxml` library for Burker extractors: [#346](https://github.com/catalystneuro/roiextractors/pull/346) +* Protect sima and dill export [#351](https://github.com/catalystneuro/roiextractors/pull/351) * Improve error message when `TiffImagingExtractor` is not able to form memmap [#353](https://github.com/catalystneuro/roiextractors/pull/353) ### Testing diff --git a/src/roiextractors/extractors/simaextractor/simasegmentationextractor.py b/src/roiextractors/extractors/simaextractor/simasegmentationextractor.py index 71905a2c..f4310090 100644 --- a/src/roiextractors/extractors/simaextractor/simasegmentationextractor.py +++ b/src/roiextractors/extractors/simaextractor/simasegmentationextractor.py @@ -16,14 +16,6 @@ from ...extraction_tools import PathType from ...segmentationextractor import SegmentationExtractor -try: - import sima - import dill - - HAVE_SIMA = True -except ImportError: - HAVE_SIMA = False - class SimaSegmentationExtractor(SegmentationExtractor): """A segmentation extractor for Sima. @@ -34,7 +26,6 @@ class SimaSegmentationExtractor(SegmentationExtractor): """ extractor_name = "SimaSegmentation" - installed = HAVE_SIMA # check at class level if installed or not is_writable = False mode = "file" # error message when not installed @@ -51,6 +42,16 @@ def __init__(self, file_path: PathType, sima_segmentation_label: str = "auto_ROI sima_segmentation_label: str name of the ROIs in the dataset from which to extract all ROI info """ + sima_spec = importlib.util.find_spec("sima") + dill_spec = importlib.util.find_spec("dill") + if sima_spec is not None and dill_spec is not None: + import sima + import dill + + HAVE_SIMA = True + else: + HAVE_SIMA = False + assert HAVE_SIMA, self.installation_mesg SegmentationExtractor.__init__(self) self.file_path = file_path @@ -79,6 +80,8 @@ def _convert_sima(old_pkl_loc): old_pkl_loc: str Path of the pickle file to be converted """ + import dill + # Make a name for the new pickle old_pkl_loc = old_pkl_loc + "/" for dirpath, dirnames, filenames in os.walk(old_pkl_loc): @@ -108,6 +111,8 @@ def _convert_sima(old_pkl_loc): def _file_extractor_read(self): """Read the sima file and return the sima.ImagingDataset object.""" + import sima + _img_dataset = sima.ImagingDataset.load(self.file_path) _img_dataset._savedir = self.file_path return _img_dataset