moloney / dcmstack

DICOM to Nifti conversion with meta data preservation
Other
72 stars 51 forks source link

dcmstack fails to convert certain sets of DICOM files #10

Closed chrisgorgo closed 11 years ago

chrisgorgo commented 11 years ago

I'm trying to convert this dataset - https://dl.dropbox.com/u/412959/dicoms.tar.gz Unfortunately I get an error:

---------------------------------------------------------------------------
InvalidDicomError                         Traceback (most recent call last)
/Users/filo/<ipython-input-10-9c30552a7407> in <module>()
      2 for src_path in glob.glob("*.dcm"):
      3     print src_path
----> 4     src_dcm = dicom.read_file(src_path)
      5     stack.add_dcm(src_dcm)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/dicom/filereader.pyc in read_file(fp, defer_size, stop_before_pixels, force)
    623     try:
    624         dataset = read_partial(fp, stop_when, defer_size=defer_size,
--> 625                                             force=force)
    626     finally:
    627         if not caller_owns_file:

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/dicom/filereader.pyc in read_partial(fileobj, stop_when, defer_size, force)
    529     """
    530     # Read preamble -- raise an exception if missing and force=False

--> 531     preamble = read_preamble(fileobj, force)
    532     file_meta_dataset = Dataset()
    533     # Assume a transfer syntax, correct it as necessary

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/dicom/filereader.pyc in read_preamble(fp, force)
    502             fp.seek(0)
    503         else:
--> 504             raise InvalidDicomError("File is missing 'DICM' marker. "
    505                                     "Use force=True to force reading")
    506     else:

InvalidDicomError: File is missing 'DICM' marker. Use force=True to force reading

But when I try to force conversion I get the another error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/Users/filo/<ipython-input-11-11b6ef0c3691> in <module>()
      3     print src_path
      4     src_dcm = dicom.read_file(src_path, force=True)
----> 5     stack.add_dcm(src_dcm)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/dcmstack-0.6.1-py2.7.egg/dcmstack/dcmstack.pyc in add_dcm(self, dcm, meta)
    525         nii_wrp = None
    526         if not is_dummy:
--> 527             nii_wrp = NiftiWrapper.from_dicom_wrapper(dw, meta)
    528             if self._ref_input is None:
    529                 #We don't have a reference input yet, use this one

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/dcmstack-0.6.1-py2.7.egg/dcmstack/dcmmeta.pyc in from_dicom_wrapper(klass, dcm_wrp, meta_dict)
   1487 
   1488         #The Nifti patient space flips the x and y directions

-> 1489         affine = np.dot(np.diag([-1., -1., 1., 1.]), dcm_wrp.get_affine())
   1490 
   1491         #Make 2D data 3D

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/nibabel-1.3.0-py2.7.egg/nibabel/nicom/dicomwrappers.pyc in get_affine(self)
    282         # direction cosine for changes in row index, column 1 is

    283         # direction cosine for changes in column index

--> 284         orient = self.rotation_matrix
    285         # therefore, these voxel sizes are in the right order (row,

    286         # column, slice)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/nibabel-1.3.0-py2.7.egg/nibabel/onetime.pyc in __get__(self, obj, type)
     41            return self.getter
     42 
---> 43        val = self.getter(obj)
     44        #print "** setattr_on_read - loading '%s'" % self.name  # dbg

     45        setattr(obj, self.name, val)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/nibabel-1.3.0-py2.7.egg/nibabel/nicom/dicomwrappers.pyc in rotation_matrix(self)
    165         assert np.allclose(np.eye(3),
    166                            np.dot(R, R.T),
--> 167                            atol=1e-6)
    168         return R
    169 

AssertionError: 

At the same time dcm2nii is able to convert this dataset with the following result https://dl.dropbox.com/u/412959/20101222_131954BTfMRIcontrolB4s004a1001.nii.gz I hope this is something minor since I love using dcmstack. Thanks in advance for any help!

moloney commented 11 years ago

The rotation matrix generated from the ImageOrientationPatient tag was failing the test for orthogonality in nibabel.nicom.dicomwrappers. Its close enough (~1e-5) that its probably not worth normalizing. The referenced pull request reduces the threshold from 1e-6 to 2e-5.

chrisgorgo commented 11 years ago

Thanks!