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

Update mcd_file.py #32

Merged
merged 2 commits into from
Sep 9, 2024
Merged
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
35 changes: 23 additions & 12 deletions readimc/mcd_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def read_acquisition(
img[:, ys, xs] = np.transpose(data[:, 3:])
return img

def read_slide(self, slide: Slide) -> Optional[np.ndarray]:
def read_slide(
self, slide: Slide, raw: bool = False
) -> Union[np.ndarray, bytes, None]:
"""Reads and decodes a slide image as numpy array using the ``imageio``
package.

Expand Down Expand Up @@ -206,15 +208,17 @@ def read_slide(self, slide: Slide) -> Optional[np.ndarray]:
)
try:
return self._read_image(
data_start_offset, data_end_offset - data_start_offset
data_start_offset, data_end_offset - data_start_offset, raw
)
except Exception as e:
raise IOError(
f"MCD file '{self.path.name}' corrupted: "
f"cannot read image for slide {slide.id}"
) from e

def read_panorama(self, panorama: Panorama) -> np.ndarray:
def read_panorama(
self, panorama: Panorama, raw: bool = False
) -> Union[np.ndarray, bytes, None]:
"""Reads and decodes a panorama image as numpy array using the
``imageio`` package.

Expand All @@ -229,6 +233,8 @@ def read_panorama(self, panorama: Panorama) -> np.ndarray:
f"MCD file '{self.path.name}' corrupted: "
f"cannot locate image data for panorama {panorama.id}"
) from e
if data_start_offset == data_end_offset == 0:
return None
data_start_offset += 161
if data_start_offset >= data_end_offset:
raise IOError(
Expand All @@ -237,7 +243,7 @@ def read_panorama(self, panorama: Panorama) -> np.ndarray:
)
try:
return self._read_image(
data_start_offset, data_end_offset - data_start_offset
data_start_offset, data_end_offset - data_start_offset, raw
)
except Exception as e:
raise IOError(
Expand All @@ -246,8 +252,8 @@ def read_panorama(self, panorama: Panorama) -> np.ndarray:
) from e

def read_before_ablation_image(
self, acquisition: Acquisition
) -> Optional[np.ndarray]:
self, acquisition: Acquisition, raw: bool = False
) -> Union[np.ndarray, bytes, None]:
"""Reads and decodes a before-ablation image as numpy array using the
``imageio`` package.

Expand Down Expand Up @@ -278,7 +284,7 @@ def read_before_ablation_image(
)
try:
return self._read_image(
data_start_offset, data_end_offset - data_start_offset
data_start_offset, data_end_offset - data_start_offset, raw
)
except Exception as e:
raise IOError(
Expand All @@ -288,8 +294,8 @@ def read_before_ablation_image(
) from e

def read_after_ablation_image(
self, acquisition: Acquisition
) -> Optional[np.ndarray]:
self, acquisition: Acquisition, raw: bool = False
) -> Union[np.ndarray, bytes, None]:
"""Reads and decodes a after-ablation image as numpy array using the
``imageio`` package.

Expand Down Expand Up @@ -320,7 +326,7 @@ def read_after_ablation_image(
)
try:
return self._read_image(
data_start_offset, data_end_offset - data_start_offset
data_start_offset, data_end_offset - data_start_offset, raw
)
except Exception as e:
raise IOError(
Expand Down Expand Up @@ -358,12 +364,17 @@ def _read_schema_xml(
data = mm.read(end_index + len(end_sub_encoded) - start_index)
return data.decode(encoding=encoding)

def _read_image(self, data_offset: int, data_size: int) -> np.ndarray:
def _read_image(
self, data_offset: int, data_size: int, raw: bool = False
) -> Union[np.ndarray, bytes]:
if self._fh is None:
raise IOError(f"MCD file '{self.path.name}' has not been opened")
self._fh.seek(data_offset)
data = self._fh.read(data_size)
return imread(data)
if raw:
return data
else:
return imread(data)

def __repr__(self) -> str:
return str(self._path)
Loading