Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect import in sima #351

Merged
merged 6 commits into from
Aug 13, 2024
Merged
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
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
Loading