-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new file format for zipped hologram tif files
- Loading branch information
1 parent
4e51a2d
commit bbe11a8
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import io | ||
import functools | ||
import zipfile | ||
|
||
from .dataset import SeriesData | ||
from .single_tif_holo import SingleTifHolo | ||
|
||
|
||
class SeriesZipTifHolo(SeriesData): | ||
storage_type = "hologram" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(SeriesZipTifHolo, self).__init__(*args, **kwargs) | ||
self._files = None | ||
self._dataset = None | ||
|
||
def __len__(self): | ||
return len(self.files) | ||
|
||
def _get_dataset(self, idx): | ||
if self._dataset is None: | ||
self._dataset = [None] * len(self) | ||
if self._dataset[idx] is None: | ||
# Use ``zipfile.ZipFile.open`` to return an open file | ||
zf = zipfile.ZipFile(self.path) | ||
pt = zf.open(self.files[idx]) | ||
fd = io.BytesIO(pt.read()) | ||
self._dataset[idx] = SingleTifHolo(path=fd, | ||
meta_data=self.meta_data, | ||
holo_kw=self.holo_kw) | ||
return self._dataset[idx] | ||
|
||
@staticmethod | ||
@functools.lru_cache(maxsize=32) | ||
def _index_files(path): | ||
"""Search zip file for tif files""" | ||
with zipfile.ZipFile(path) as zf: | ||
names = sorted(zf.namelist()) | ||
names = [nn for nn in names if nn.endswith(".tif")] | ||
phasefiles = [] | ||
for name in names: | ||
with zf.open(name) as pt: | ||
fd = io.BytesIO(pt.read()) | ||
if SingleTifHolo.verify(fd): | ||
phasefiles.append(name) | ||
return phasefiles | ||
|
||
@property | ||
def files(self): | ||
if self._files is None: | ||
self._files = SeriesZipTifHolo._index_files(self.path) | ||
return self._files | ||
|
||
def get_qpimage_raw(self, idx): | ||
"""Return QPImage without background correction""" | ||
ds = self._get_dataset(idx) | ||
return ds.get_qpimage_raw() | ||
|
||
@staticmethod | ||
def verify(path): | ||
"""Verify hologram zip tif file format""" | ||
valid = False | ||
try: | ||
zf = zipfile.ZipFile(path) | ||
except (zipfile.BadZipfile, IsADirectoryError): | ||
pass | ||
else: | ||
names = sorted(zf.namelist()) | ||
names = [nn for nn in names if nn.endswith(".tif")] | ||
for name in names: | ||
with zf.open(name) as pt: | ||
fd = io.BytesIO(pt.read()) | ||
if SingleTifHolo.verify(fd): | ||
valid = True | ||
break | ||
zf.close() | ||
return valid |