Skip to content

Commit

Permalink
Update mrc test
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Mar 24, 2024
1 parent d2bf024 commit 2b36439
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
8 changes: 7 additions & 1 deletion elf/io/mrc_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import warnings
from collections.abc import Mapping

import numpy as np

try:
Expand Down Expand Up @@ -71,8 +73,12 @@ def __init__(self, path, mode="r"):
else: # Other kind of error -> try to open without mmap.
try:
self._f = mrcfile.open(self.path, self.mode)
except ValueError:
except ValueError as e:
self._f = mrcfile.open(self.path, self.mode, permissive="True")
warnings.warn(
f"Opening mrcfile {self.path} failed with unknown error {e} without permissive opening."
"The file will still be opened but the contents may be incorrect."
)


def __getitem__(self, key):
Expand Down
34 changes: 12 additions & 22 deletions test/io_tests/test_mrc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

@unittest.skipIf(mrcfile is None, "Needs mrcfile")
class TestMrcWrapper(unittest.TestCase):
tmp_dir = './tmp'
out = './tmp/data.mrc'
out_old = './tmp/data_old.mrc'
out_compressed = './tmp/data_compressed.mrc'
tmp_dir = "./tmp"
out = "./tmp/data.mrc"
out_old = "./tmp/data_old.mrc"
out_compressed = "./tmp/data_compressed.mrc"

def setUp(self):
os.makedirs(self.tmp_dir)
shape = (16, 32, 64)
self.data = np.random.rand(*shape).astype('float32')
self.data = np.random.rand(*shape).astype("float32")

# change axes order to fit MRC convention
data0 = np.swapaxes(self.data, 0, -1)
Expand All @@ -30,7 +30,7 @@ def setUp(self):
with mrcfile.new(self.out) as f:
f.set_data(self.out_data)

with mrcfile.new(self.out_compressed, compression='gzip') as f:
with mrcfile.new(self.out_compressed, compression="gzip") as f:
f.set_data(self.out_data)

def tearDown(self):
Expand Down Expand Up @@ -63,37 +63,27 @@ def test_dataset_compressed(self):
def test_file(self):
from elf.io.mrc_wrapper import MRCFile
with MRCFile(self.out) as f:
ds = f['data']
ds = f["data"]
self.check_dataset(ds)

def test_file_compressed(self):
from elf.io.mrc_wrapper import MRCFile
with MRCFile(self.out_compressed) as f:
ds = f['data']
ds = f["data"]
self.check_dataset(ds)

def test_old_serialem(self):
a = mrcfile.open(self.out_old, 'w+')
a.header.machst = [11, 0, 0, 0]
a.close()

with self.assertRaises(ValueError):
from elf.io.mrc_wrapper import MRCFile
with MRCFile(self.out_old) as f:
__ = f['data']

os.remove(self.out_old)
from elf.io.mrc_wrapper import MRCFile

a = mrcfile.open(self.out_old, 'w+')
a = mrcfile.open(self.out_old, "w+")
a.header.machst = [68, 0, 0, 0]
a.set_data(self.out_data)
a.close()

from elf.io.mrc_wrapper import MRCFile
with MRCFile(self.out_old) as f:
ds = f['data']
ds = f["data"]
self.check_dataset(ds)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit 2b36439

Please sign in to comment.