def is_dicom(file): """Boolean specifying if file is a proper DICOM file. This function is a pared down version of read_preamble meant for a fast return. The file is read for a proper preamble ('DICM'), returning True if so, and False otherwise. This is a conservative approach. Parameters ---------- file : str The path to the file. See Also -------- pydicom.filereader.read_preamble pydicom.filereader.read_partial """ is_valid_file(file, raise_error=True) fp = open(file, 'rb') preamble = fp.read(0x80) prefix = fp.read(4) if prefix == b"DICM": return True else: return False
def __init__(self, filepath=None): """ Parameters ---------- filepath : None, str If None, image must be loaded later. If a str, path to the image file. """ if filepath is not None and is_valid_file(filepath): self.image = Image.load(filepath) else: self.image = np.zeros((1,1))
def test_invalid_file_str(self): not_a_file = "file" self.assertFalse(is_valid_file(not_a_file, raise_error=False)) self.assertRaises(FileExistsError, is_valid_file, not_a_file)
def test_valid_file(self): real_file = __file__ self.assertTrue(is_valid_file(real_file)) real_file_obj = open(__file__) self.assertTrue(is_valid_file(real_file_obj))