Skip to content

Commit

Permalink
Protect import in sima (#351)
Browse files Browse the repository at this point in the history
* safe import for sima

* protect sima extractor

* changelog

* Update src/roiextractors/extractors/simaextractor/simasegmentationextractor.py

Co-authored-by: Cody Baker <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Paul Adkisson <[email protected]>

---------

Co-authored-by: Cody Baker <[email protected]>
Co-authored-by: Paul Adkisson <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent 28b843f commit da6d791
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit da6d791

Please sign in to comment.