Skip to content

Commit

Permalink
Merge pull request #32 from sandip-shah/patch-2
Browse files Browse the repository at this point in the history
Update mcd_file.py
  • Loading branch information
Milad4849 authored Sep 9, 2024
2 parents ccc08a4 + 57ba081 commit 19a5794
Showing 1 changed file with 23 additions and 12 deletions.
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)

0 comments on commit 19a5794

Please sign in to comment.