Browse Source

add a check that filename passed to nd2reader has an extension .nd2 because the current implementation raises InvalidVersionError if the passed file is non-nd2 type, which is misleading

add an exception InvalidFileType

add a test for the new behavior of ND2Reader constructor
zolfa-add_slices_loading
Ulugbek Abdullaev 5 years ago
parent
commit
9059a15781
3 changed files with 17 additions and 2 deletions
  1. +8
    -0
      nd2reader/exceptions.py
  2. +5
    -1
      nd2reader/reader.py
  3. +4
    -1
      tests/test_reader.py

+ 8
- 0
nd2reader/exceptions.py View File

@ -1,3 +1,11 @@
class InvalidFileType(Exception):
"""Non .nd2 extension file.
File does not have an extension .nd2.
"""
pass
class InvalidVersionError(Exception): class InvalidVersionError(Exception):
"""Unknown version. """Unknown version.


+ 5
- 1
nd2reader/reader.py View File

@ -1,7 +1,7 @@
from pims import Frame from pims import Frame
from pims.base_frames import FramesSequenceND from pims.base_frames import FramesSequenceND
from nd2reader.exceptions import EmptyFileError
from nd2reader.exceptions import EmptyFileError, InvalidFileType
from nd2reader.parser import Parser from nd2reader.parser import Parser
import numpy as np import numpy as np
@ -15,6 +15,10 @@ class ND2Reader(FramesSequenceND):
def __init__(self, filename): def __init__(self, filename):
super(ND2Reader, self).__init__() super(ND2Reader, self).__init__()
if not filename.endswith(".nd2"):
raise InvalidFileType(f"The file {filename} you want to read with nd2reader does not have extension .nd2.")
self.filename = filename self.filename = filename
# first use the parser to parse the file # first use the parser to parse the file


+ 4
- 1
tests/test_reader.py View File

@ -4,12 +4,15 @@ import struct
from pims import Frame from pims import Frame
from nd2reader.artificial import ArtificialND2 from nd2reader.artificial import ArtificialND2
from nd2reader.exceptions import EmptyFileError
from nd2reader.exceptions import EmptyFileError, InvalidFileType
from nd2reader.reader import ND2Reader from nd2reader.reader import ND2Reader
from nd2reader.parser import Parser from nd2reader.parser import Parser
class TestReader(unittest.TestCase): class TestReader(unittest.TestCase):
def test_invalid_file_extension(self):
self.assertRaises(InvalidFileType, lambda: ND2Reader('test_data/invalid_extension_file.inv'))
def test_extension(self): def test_extension(self):
self.assertTrue('nd2' in ND2Reader.class_exts()) self.assertTrue('nd2' in ND2Reader.class_exts())


Loading…
Cancel
Save