Exemple #1
0
def test_readDicom():
    dicomImg1 = imgHandler.readDicomFromFile(dicomFile)
    vol1 = imgHandler.parseDicomVolume(dicomImg1, 64)
    assert vol1 is not None

    with open(dicomFile, 'rb') as fp:
        data = fp.read()
    dicomImg2 = imgHandler.readDicomFromBuffer(data)
    vol2 = imgHandler.parseDicomVolume(dicomImg2, 64)
    assert vol2 is not None
    assert (vol1 == vol2).all()

    # if dataInterface is not initialized with allowedDirs or allowedFileTypes it should fail
    dataInterface = DataInterface()
    with pytest.raises(ValidationError):
        dataInterface.initWatch(dicomDir, '*.dcm', 0)

    # Now allow all dirs and file types
    dataInterface = DataInterface(allowedDirs=['*'], allowedFileTypes=['*'])
    dataInterface.initWatch(dicomDir, '*.dcm', 0)
    dicomImg3 = imgHandler.readRetryDicomFromDataInterface(
        dataInterface, dicomFile)
    vol3 = imgHandler.parseDicomVolume(dicomImg3, 64)
    assert vol3 is not None
    assert (vol1 == vol3).all()

    # read in a truncated file, should fail and return None.
    trucatedDicomFile = os.path.join(dicomDir, test_dicomTruncFile)
    dicomImg4 = imgHandler.readRetryDicomFromDataInterface(
        dataInterface, trucatedDicomFile)
    assert dicomImg4 is None

    # Test convert to nifti
    niftiObject = dicomreaders.mosaic_to_nii(dicomImg3)
    assert niftiObject is not None

    dataInterface.fileWatcher.__del__()
    dataInterface.fileWatcher = None

    # Test anonymization of sensitive patient fields
    def countUnanonymizedSensitiveAttrs(dicomImg):
        sensitiveAttrs = 0
        for attr in imgHandler.attributesToAnonymize:
            if hasattr(dicomImg, attr) and getattr(dicomImg, attr) != "":
                sensitiveAttrs += 1
        return sensitiveAttrs

    dicomImg5 = imgHandler.readDicomFromFile(dicomFile)
    assert countUnanonymizedSensitiveAttrs(dicomImg5) >= 1

    imgHandler.anonymizeDicom(dicomImg5)
    assert countUnanonymizedSensitiveAttrs(dicomImg5) == 0
Exemple #2
0
def test_readDicom():
    dicomDir = os.path.join(os.path.dirname(__file__), 'test_input')
    dicomFile = os.path.join(dicomDir, test_dicomFile)
    dicomImg1 = rd.readDicomFromFile(dicomFile)
    vol1 = rd.parseDicomVolume(dicomImg1, 64)
    assert vol1 is not None

    with open(dicomFile, 'rb') as fp:
        data = fp.read()
    dicomImg2 = rd.readDicomFromBuffer(data)
    vol2 = rd.parseDicomVolume(dicomImg2, 64)
    assert vol2 is not None
    assert (vol1 == vol2).all()

    fileInterface = FileInterface()
    fileInterface.initWatch(dicomDir, '*.dcm', 0)
    dicomImg3 = rd.readRetryDicomFromFileInterface(fileInterface, dicomFile)
    vol3 = rd.parseDicomVolume(dicomImg3, 64)
    assert vol3 is not None
    assert (vol1 == vol3).all()

    # read in a truncated file, should fail and return None.
    trucatedDicomFile = os.path.join(dicomDir, test_dicomTruncFile)
    dicomImg4 = rd.readRetryDicomFromFileInterface(fileInterface,
                                                   trucatedDicomFile)
    assert dicomImg4 is None

    # Test convert to nifti
    niftiObject = dicomreaders.mosaic_to_nii(dicomImg3)
    assert niftiObject is not None

    fileInterface.fileWatcher.__del__()
    fileInterface.fileWatcher = None
Exemple #3
0
def readLocalDicomIncremental(volIdx, entities):
    # read the incremental locally for test comparison
    dicomPath = os.path.join(test_sampleProjectDicomPath, "001_000013_{TR:06d}.dcm".format(TR=volIdx))
    dicomImg = readDicomFromFile(dicomPath)
    dicomMetadata = getDicomMetadata(dicomImg)
    dicomMetadata.update(entities)
    niftiImg = convertDicomImgToNifti(dicomImg)
    localIncremental = BidsIncremental(niftiImg, dicomMetadata)
    return localIncremental
Exemple #4
0
def dicomImage(dicomMetadataSample) -> pydicom.dataset.Dataset:
    dicom = readDicomFromFile(os.path.join(os.path.dirname(__file__),
                                           test_dicomPath))
    assert dicom is not None

    # Test a sampling of fields to ensure proper read
    for field, value in dicomMetadataSample.items():
        assert getattr(dicom, field) == value

    return dicom
Exemple #5
0
def test_nifti():
    with tempfile.TemporaryDirectory() as tmpDir:
        niftiFilename = os.path.join(tmpDir, 'nifti1.nii')
        imgHandler.convertDicomFileToNifti(dicomFile, niftiFilename)
        niftiImg1 = imgHandler.readNifti(niftiFilename)

        dcmImg = imgHandler.readDicomFromFile(dicomFile)
        niftiImgFromDcm = imgHandler.convertDicomImgToNifti(dcmImg)
        assert niftiImg1.header == niftiImgFromDcm.header
        assert np.array_equal(np.array(niftiImg1.dataobj),
                              np.array(niftiImgFromDcm.dataobj))
def checkingDicomNiftiConversion(cfg):
    """
    Purpose: check the nibabel nifti/dicom conversion method BEFORE using it in
    real-time.
    Here we're assuming you have raw dicoms and the corresponding converted nifti that you
    used in pilot data collection.

    STEPS:
        0. set up config
        1. get number of TRs from dicom path and nifti path
        2. go through each TR in dicom, convert them each to niftis and save
        3. load in each nifti into new data matrix
        4. compare this to loaded offline nifti file
    """
    complete_path = os.path.join(cfg.dicomDir,cfg.dicomNamePattern).format('*')
    all_dicoms = glob.glob(complete_path)
    sorted_dicom_list = sorted(all_dicoms, key=lambda x: int("".join([i for i in x if i.isdigit()])))
    target_orientation, dicom_orientation = getAxesForTransform(sorted_dicom_list[0],cfg)
    cfg.axesTransform = getTransform(target_orientation, dicom_orientation)
    nTRs_dicom = len(sorted_dicom_list)
    nifti_image = nib.load(cfg.niftiFile)
    nifti_data = nifti_image.get_fdata()
    nTRs_nifti = np.shape(nifti_data)[-1]
    image_shape = np.shape(nifti_data)
    # check that the number of TRs are the same
    print('Step 1: Verifying dicom and nifti data match # TRs')
    if nTRs_dicom != nTRs_nifti:
        print('WARNING: number of dicoms don''t match! Check your files again!')
    else:
        nTR = nTRs_dicom
        print('TR numbers match!')
    # now read in each dicom
    print('Step 2: Saving dicoms as nifti files')
    full_nifti_name = [None]*nTR
    for trIndex in np.arange(nTR):
        print(trIndex)
        dicomData = readDicomFromFile(sorted_dicom_list[trIndex])
        expected_dicom_name = sorted_dicom_list[trIndex].split('/')[-1]
        full_nifti_name[trIndex] = saveAsNiftiImage(dicomData, expected_dicom_name, cfg, cfg.ref_BOLD)
    # now load in each nifti and create new matrix
    print('Step 3: Reading in the saved dicoms')
    dicom_data = np.zeros(image_shape)
    for trIndex in np.arange(nTR):
        dicom_object = nib.load(full_nifti_name[trIndex])
        dicom_data[:,:,:,trIndex] = dicom_object.get_fdata()
    # now check if there's any value that's different
    print('Step 4: Verifying the saved dicoms match the offline Nifti created')
    different_values = np.argwhere(dicom_data != nifti_data)
    if len(different_values) == 0:
        PASSED = 1
    elif len(different_values) > 0:
        PASSED = 0
    return PASSED
Exemple #7
0
def readFileData(filename):
    data = None
    fileExtension = Path(filename).suffix
    if fileExtension == '.dcm':
        # Anonymize Dicom files
        dicomImg = readDicomFromFile(filename)
        dicomImg = anonymizeDicom(dicomImg)
        data = writeDicomToBuffer(dicomImg)
    else:
        with open(filename, 'rb') as fp:
            data = fp.read()
    return data
Exemple #8
0
def dicomToBidsinc(dicomFile, requiredMetadata: {}) -> BidsIncremental:
    # TODO(spolcyn): Do this all in memory -- dicom2nifti is promising
    # Put extra metadata in sidecar JSON file
    #
    # NOTE: This is not the final version of this method.
    # The conversion from DICOM to BIDS-I and gathering all required metadata
    # can be complex, as DICOM doesn't necessarily have the metadata required
    # for BIDS in it by default. Thus, another component will handle the logic
    # and error handling surrounding this.
    dicomImg = readDicomFromFile(dicomFile)
    niftiImage = convertDicomImgToNifti(dicomImg)
    #logger.debug("Nifti header after conversion is: %s", niftiImage.header)
    publicMeta, privateMeta = getDicomMetadata(dicomImg)

    publicMeta.update(privateMeta)  # combine metadata dictionaries
    publicMeta.update(requiredMetadata)
    return BidsIncremental(niftiImage, requiredMetadata, publicMeta)
Exemple #9
0
    def test_getFile(self, dicomTestFilename):
        print("test_getFile")
        global fileData
        assert Web.wsDataConn is not None
        # Try to initialize file watcher with non-allowed directory
        cmd = projUtils.initWatchReqStruct('/', '*', 0)
        response = Web.sendDataMsgFromThread(cmd)
        # we expect an error because '/' directory not allowed
        assert response['status'] == 400

        # Initialize with allowed directory
        cmd = projUtils.initWatchReqStruct(testDir, '*.dcm', 0)
        response = Web.sendDataMsgFromThread(cmd)
        assert response['status'] == 200

        dcmImg = readDicomFromFile(dicomTestFilename)
        anonDcm = anonymizeDicom(dcmImg)
        data = writeDicomToBuffer(anonDcm)
        # with open(dicomTestFilename, 'rb') as fp:
        #     data = fp.read()

        cmd = projUtils.watchFileReqStruct(dicomTestFilename)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        # import pdb; pdb.set_trace()
        assert responseData == data

        # Try compressed version
        cmd = projUtils.watchFileReqStruct(dicomTestFilename, compress=True)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        cmd = projUtils.getFileReqStruct(dicomTestFilename)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        # Try compressed version
        cmd = projUtils.getFileReqStruct(dicomTestFilename, compress=True)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        cmd = projUtils.getNewestFileReqStruct(dicomTestFilename)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        # Try to get a non-allowed file
        cmd = projUtils.getFileReqStruct('/tmp/file.nope')
        try:
            responseData = handleDataRequest(cmd)
        except RequestError as err:
            # Expecting a status not 200 error to be raised
            assert 'status' in str(err)
        else:
            self.fail('Expecting RequestError')

        # try from a non-allowed directory
        cmd = projUtils.getFileReqStruct('/nope/file.dcm')
        try:
            responseData = handleDataRequest(cmd)
        except RequestError as err:
            # Expecting a status not 200 error to be raised
            assert 'status' in str(err)
        else:
            self.fail('Expecting RequestError')

        # Test putTextFile
        testText = 'hello2'
        textFileName = os.path.join(tmpDir, 'test2.txt')
        cmd = projUtils.putTextFileReqStruct(textFileName, testText)
        response = Web.sendDataMsgFromThread(cmd)
        assert response['status'] == 200

        # Test putBinaryData function
        testData = b'\xFE\xED\x01\x23'
        dataFileName = os.path.join(tmpDir, 'test2.bin')
        cmd = projUtils.putBinaryFileReqStruct(dataFileName)
        for putFilePart in projUtils.generateDataParts(testData,
                                                       cmd,
                                                       compress=True):
            response = Web.sendDataMsgFromThread(putFilePart)
        assert response['status'] == 200
        # read back an compare to original
        cmd = projUtils.getFileReqStruct(dataFileName)
        response = Web.sendDataMsgFromThread(cmd)
        responseData = b64decode(response['data'])
        assert responseData == testData