Beispiel #1
0
def DICOM2ASCII(in_dir, out_dir, out_prefix, subwindow):
    import dicom
    import DicomIO
    import bfitASCII_IO
    import numpy as np
    import os
    import shutil

    outdir = out_dir + '/' + out_prefix + '_' + in_dir + '_ASCII.txt'
    if os.path.isfile(outdir):
        os.remove(outdir)

    # Resolve new frame list
    dcmio = DicomIO.DicomIO()
    frame_list = dcmio.ReadDICOM_frames(experiment_dir + '/' + out_prefix +
                                        '/' + in_dir)
    slice_1st = frame_list[0][0]
    xdim = slice_1st.Columns
    ydim = slice_1st.Rows
    zdim = slice_1st.NumberOfSlices
    tdim = slice_1st.NumberOfTimeSlices

    ROI_No = 0

    bset = []
    for frame_i in range(len(frame_list)):
        bset.append(int(frame_list[frame_i][0].FrameReferenceTime) / 1000)

    ROIslice = []
    for slice_i in range(len(frame_list[0])):
        ROIslice.append(slice_i + 1)

    name = in_dir
    # Save in data in order z,y,x
    SIs = []
    for z_i in range(zdim):
        for y_i in range(offset, ydim - offset):
            for x_i in range(offset, xdim - offset):
                SI = []
                for t_i in range(tdim):
                    SI.append(frame_list[t_i][z_i].pixel_array[y_i, x_i] *
                              frame_list[t_i][z_i].RescaleSlope +
                              frame_list[t_i][z_i].RescaleIntercept)
                SIs.append(SI)
        print str(z_i + 1) + '/' + str(zdim)
    print "total SIs:" + str(len(SIs))
    data = {
        'subwindow': subwindow,
        'ROI_No': ROI_No,
        'bset': bset,
        'ROIslice': ROIslice,
        'name': name,
        'SIs': SIs
    }
    ASCIIio = bfitASCII_IO.bfitASCII_IO()
    print("Writing " + outdir)
    ASCIIio.Write3D(outdir, data)

    return outdir
def DICOM2ASCII(out_prefix, sub_dir, out_dir, subwindow):
    import dicom
    import DicomIO
    import bfitASCII_IO
    import numpy as np
    import os
    import shutil

    outfile = out_dir + os.sep + out_prefix + '_' + sub_dir + '_ASCII.txt'
    if os.path.isfile(outfile):
        os.remove(outfile)

    # Resolve new frame list
    dcmio = DicomIO.DicomIO()
    frame_list = dcmio.ReadDICOM_frames(experiment_dir + os.sep + out_prefix + os.sep + sub_dir)
    slice_1st = frame_list[0][0]
    xdim = slice_1st.Columns
    ydim = slice_1st.Rows
    zdim = slice_1st.NumberOfSlices
    tdim = slice_1st.NumberOfTimeSlices

    ROI_No = 0

    bset = []
    for frame_i in range(len(frame_list)):
        bset.append(int(frame_list[frame_i][0].FrameReferenceTime)/1000)

    ROIslice = []
    for slice_i in range(len(frame_list[0])):
        ROIslice.append(slice_i+1)

    name = sub_dir
    # Save in data in order z,y,x
    SIs = []
    for z_i in range(zdim):
        print type(frame_list[0][z_i].pixel_array[0, 0])
        print float(frame_list[0][z_i].pixel_array[0, 0])
        pixel_array3 = frame_list[0][z_i].pixel_array
        pixel_array3 = pixel_array3  * frame_list[0][z_i].RescaleSlope + frame_list[0][z_i].RescaleIntercept
        print pixel_array3[0, 0]
        print "Mean:" + str(np.mean(pixel_array3)) + " Min:" + str(np.amin(pixel_array3)) + " Max:" + str(np.amax(pixel_array3))
        for y_i in range(ydim):
            for x_i in range(xdim):
                SI = []
                for t_i in range(tdim):
                    SI.append(frame_list[t_i][z_i].pixel_array[y_i, x_i] * frame_list[t_i][z_i].RescaleSlope + frame_list[t_i][z_i].RescaleIntercept)
                SIs.append(SI)
        print str(z_i+1) + '/' + str(zdim)
    print "total SIs:" + str(len(SIs))
    data = { 'subwindow': subwindow, 'number': ROI_No, 'bset': bset, 'ROIslice': ROIslice, 'name': name, 'SIs': SIs }
    ASCIIio = bfitASCII_IO.bfitASCII_IO()
    print ("Writing " + outfile)
    ASCIIio.Write3D(outfile, data)

    return outfile
if __name__ == "__main__":
    #    parser = ArgumentParser()
    #    parser.add_argument("--dicomdir", dest="dicomdir", help="dicomdir", required=True)
    #    args = parser.parse_args()

    errors = 0

    # Create output directory if it does not exist
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    filenames_raw = glob.glob((original_raw_path + os.sep + '*ASCII.txt'))
    filenames_orig = glob.glob((original_path + os.sep + '*_results.txt'))
    filenames_corr = glob.glob((corrected_path + os.sep + '*_results.txt'))

    ASCIIio = bfitASCII_IO.bfitASCII_IO()
    print filenames_raw
    for fname_raw in filenames_raw:
        splitted_raw = fname_raw.split(os.sep)
        splitted_raw = splitted_raw[1].split('_')
        splitted_raw = splitted_raw[:-2]
        patient_name = ''.join([item + '_' for item in splitted_raw])
        patient_name = patient_name[0:-1]
        print 'Patient name ' + patient_name
        if not patient_name.endswith('Set2'):
            continue
        # Reach for non-corrected fitted file
        found_fname_orig = ''
        for fname_orig in filenames_orig:
            splitted_orig = fname_orig.split(os.sep)
            splitted_orig = splitted_orig[1].split('_')
if __name__ == "__main__":
#    parser = ArgumentParser()
#    parser.add_argument("--dicomdir", dest="dicomdir", help="dicomdir", required=True)
#    args = parser.parse_args()

    errors = 0

    # Create output directory if it does not exist
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    filenames_raw = glob.glob((original_raw_path + os.sep + '*ASCII.txt'))
    filenames_orig = glob.glob((original_path + os.sep + '*_results.txt'))
    filenames_corr = glob.glob((corrected_path + os.sep + '*_results.txt'))

    ASCIIio = bfitASCII_IO.bfitASCII_IO()
    print filenames_raw
    for fname_raw in filenames_raw:
        splitted_raw = fname_raw.split(os.sep)
        splitted_raw = splitted_raw[1].split('_')
        splitted_raw = splitted_raw[:-2]
        patient_name = ''.join([item + '_' for item in splitted_raw])
        patient_name = patient_name[0:-1]
        print 'Patient name ' + patient_name
        if not patient_name.endswith('Set2'):
            continue
        # Reach for non-corrected fitted file
        found_fname_orig = ''
        for fname_orig in filenames_orig:
            splitted_orig = fname_orig.split(os.sep)
            splitted_orig = splitted_orig[1].split('_')
Beispiel #5
0
def ASCII2DICOM(in_refdir, in_file, out_prefix, out_basedir, bounds):
    import dicom
    import DicomIO
    import bfitASCII_IO
    import numpy as np
    import os
    import shutil
    import copy

    outdir_basename =  out_basedir + out_prefix

    # Resolve new frame list
    dcmio = DicomIO.DicomIO()
    frame_list = dcmio.ReadDICOM_frames(in_refdir + '/' + out_prefix + '/' + 'DICOMconverted')
    slice_1st = frame_list[0][0]
    xdim = slice_1st.Columns
    ydim = slice_1st.Rows
    zdim = slice_1st.NumberOfSlices
    tdim = slice_1st.NumberOfTimeSlices
    print "Original data dimensions:" + str([xdim, ydim, zdim, tdim])
    sample_frame = frame_list[0]
    del frame_list

    # Read data = { 'subwindow': subwindow, 'ROI_No': ROI_No, 'bset': bset, 'ROIslice': ROIslice, 'name': name, 'SIs': SIs }
    ASCIIio = bfitASCII_IO.bfitASCII_IO()
    print ("Reading " + in_file)
    data = ASCIIio.Read(in_file, False)
    pmap_subwindow = data['subwindow']
    pmap_SIs = data['data']
    pmap_names = data['parameters']
    pmap_names = [s.strip('[],\'') for s in pmap_names]
    pmap_slices = data['ROIslice']
    pmap_slices = [s-1 for s in pmap_slices]
    # Save in data in order z,y,x
    out_dirs = []
    for p_i in range(len(pmap_names)):
        SI_i = 0
        outvolume = copy.deepcopy(sample_frame)
        out_dir = outdir_basename + '_' + pmap_names[p_i]
        print "Writing " + pmap_names[p_i] + ' to ' + out_dir
        for z_i in range(zdim):
            # Initialize slice intensity values
            pixel_array = np.zeros([xdim, ydim])
            # Place data into slice subregion
            for x_i in range(pmap_subwindow[0], pmap_subwindow[1]+1):
                for y_i in range(pmap_subwindow[2], pmap_subwindow[3]+1):
                    pixel_array[y_i, x_i] = pmap_SIs[SI_i,p_i]
                    SI_i = SI_i + 1
            pixel_array = pixel_array.T
            intercept = np.amin(pixel_array)
            pixel_array = pixel_array - intercept
            slope = np.amax(pixel_array)-np.amin(pixel_array)
            slope = slope/65535.0
            pixel_array = np.round(pixel_array/slope)
            pixel_array = pixel_array.astype(np.uint16)
            # Place data into slice
            outvolume[z_i].RescaleSlope = slope
            outvolume[z_i].RescaleIntercept = intercept
            outvolume[z_i].PixelData = pixel_array.tostring()
            outvolume[z_i].Columns = xdim
            outvolume[z_i].Rows = ydim
            outvolume[z_i].NumberOfSlices = zdim
            outvolume[z_i].NumberOfTimeSlices = 1
            outvolume[z_i].ImageIndex = z_i+1
        # Create output directory if it does not exist
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        else:
            shutil.rmtree(out_dir)
            os.makedirs(out_dir)
        # Write output DICOM
        filenames = dcmio.WriteDICOM_frames(out_dir, [outvolume], 'IM')
        out_dirs.append(out_dir)
    return out_dirs
def DICOM2ASCII_ROI(in_dir, out_prefix, DICOMpath, bounds):
    import dicom
    import DicomIO
    import bfitASCII_IO
    import numpy as np
    import os
    import shutil
    
    splitted = DICOMpath.split(os.sep)

    outdir = outputASCIIdir + os.sep + splitted[-1] + '_' + in_dir + '_ASCII.txt'
    print 'Output directory will be ' + outdir
    if os.path.isfile(outdir):
        os.remove(outdir)

    # Resolve new frame list
    dcmio = DicomIO.DicomIO()
    print 'Reading ' + (experiment_dir + '/' + out_prefix + '/' + in_dir)
    frame_list = dcmio.ReadDICOM_frames(experiment_dir + '/' + out_prefix + '/' + in_dir)
    slice_1st = frame_list[0][0]
    xdim = slice_1st.Columns
    ydim = slice_1st.Rows
    zdim = slice_1st.NumberOfSlices
    tdim = slice_1st.NumberOfTimeSlices
    print 'Dimensions:' + str((xdim, ydim, zdim, tdim))

    offset = 0
    subwindow_dimensions = [xdim-(2*offset), ydim-(2*offset), zdim, tdim]
    subwindow = [bounds[0]+offset, bounds[0]+xdim-offset, bounds[2]+offset, bounds[2]+ydim-offset]
    print "subwindow dimensions:" + str(subwindow_dimensions)
    print "subwindow:" + str(subwindow)


    # Load ROI mask data
    print 'Reading ' + (experiment_dir + '/' + out_prefix + '/' + in_dir)
    DICOM_ROIdata = dcmio.ReadDICOM_frames(DICOMpath)
    ROIimg = DICOM_ROIdata[0]
    ROIxdim = ROIimg[0].Columns
    ROIydim = ROIimg[0].Rows
    ROIzdim = ROIimg[0].NumberOfSlices
    ROItdim = ROIimg[0].NumberOfTimeSlices
    print 'ROI Dimensions:' + str((ROIxdim, ROIydim, ROIzdim, ROItdim))

    ROImin, ROImax = np.min(ROIimg[11].pixel_array), np.max(ROIimg[11].pixel_array)
    print str((ROImin, ROImax))

    # Save in data in order z,y,x
    SIs = []
    ROIslice = []
    for z_i in range(zdim):
        slice = ROIimg[z_i].pixel_array.astype(float)
        ROImin, ROImax = np.min(slice), np.max(slice)
        non_zero = np.nonzero(slice)
        for non_zero_xy_i in range(len(non_zero[0])):
            y_i = non_zero[0][non_zero_xy_i]-subwindow[2]
            x_i = non_zero[1][non_zero_xy_i]-subwindow[0]
            if not (z_i+1) in ROIslice:
                ROIslice.append(z_i+1)
            SI = []
            for t_i in range(tdim):
                SI.append(frame_list[t_i][z_i].pixel_array[y_i, x_i] * frame_list[t_i][z_i].RescaleSlope + frame_list[t_i][z_i].RescaleIntercept)
            SIs.append(SI)
        print str(z_i+1) + '/' + str(zdim)

    # Write data into ASCII file for fittings
    name = (splitted[-1] + '_' + in_dir)
    print 'Name:' + name
    bset = []
    for frame_i in range(len(frame_list)):
        bset.append(int(frame_list[frame_i][9].FrameReferenceTime)/1000)
    print 'Bset:' + str(bset)
    print "total SIs:" + str(len(SIs))
    ROI_No = 0
    print 'ROI_No:' + str(ROI_No)
    ROIslice.sort()
    print 'ROIslice:' + str(ROIslice)
    subwindow = [0, 0, 0, 0]
    print 'Subwindow:' + str(subwindow)
    data = { 'subwindow': subwindow, 'ROI_No': ROI_No, 'bset': bset, 'ROIslice': ROIslice, 'name': name, 'SIs': SIs }
    ASCIIio = bfitASCII_IO.bfitASCII_IO()
    print ("Writing " + outdir)
    ASCIIio.Write3D(outdir, data)

    return outdir