Ejemplo n.º 1
0
def ec_select(mrs_data):
    """ 
    """
    try:
        ## work out whether eddy current correction is necessary based on TE
        if mrs_data.te < 288:
            root = Tkinter.Tk()
            root.geometry('0x0+0+0')
            root.attributes("-topmost", 1)
            root.withdraw()
            mrs_data.eddy = tkMessageBox.askyesno(
                'ECC', 'Perform an eddy current correction?', parent=root)
            if mrs_data.eddy == True:
                try:
                    # get path to water directory for eddy current correction
                    mrs_data.h2o_dir = tkFileDialog.askopenfilename(
                        parent=root, initialdir=mrs_data.dcm_dir)
                    water_data = mrs_struct(dicom.read_file(mrs_data.h2o_dir))


#                    mrs_data.ecc_cx=mrs_data.raw_cx*np.exp(-1.0j*np.angle(water_data.raw_cx))
                except:
                    mrs_data.eddy = 0
            else:
                mrs_data.eddy = 0

            root.attributes("-topmost", 0)
            root.destroy()

        else:
            mrs_data.eddy = 0

        return (mrs_data, water_data)
    except:
        print("Error selecting water file for eddy current correction!")
Ejemplo n.º 2
0
def water_t2_corr_csf():

    # Make a top-level instance and hide in top left corner, get filepath
    root = Tkinter.Tk()
    root.geometry('0x0+0+0')
    root.attributes("-topmost", 1)
    root.withdraw()
    water_list = tkFileDialog.askopenfilenames(parent=root)
    water_dir = os.path.split(water_list[0])[0]
    os.chdir(water_dir)
    root.attributes("-topmost", 0)
    root.destroy()

    water_data = {}
    tr_list = np.zeros(shape=len(water_list))
    te_list = np.zeros(shape=len(water_list))
    amp_list = np.zeros(shape=len(water_list))

    fig1, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)

    for w in range(0, len(water_list)):
        water_data[w] = mrs_struct(dicom.read_file(water_list[w]))
        tr_list[w] = water_data[w].tr
        te_list[w] = water_data[w].te
        amp_list[w] = np.mean(
            np.abs(
                hlsvdquant(water_data[w], nc_w,
                           np.arange(0, water_data[w].nsa))[0]))
        ax1.plot(np.arange(0, water_data[w].pts),
                 np.abs(water_data[w].raw_cx[0, :]))
    ax1.set_ylim(bottom=0)
    ax1.set_xlim(left=0)
    #    fig2, ax2 = plt.subplots()
    ax2.scatter(te_list, amp_list)

    m0_1_est, t2_1_est, m0_2_est = np.square(
        curve_fit(t2_curve_biexp,
                  te_list,
                  amp_list,
                  p0=[
                      np.sqrt(np.max(amp_list)), 10.0,
                      np.sqrt(np.max(amp_list / 100.0))
                  ])[0])

    ax2.plot(
        np.arange(0, 1500),
        t2_curve_biexp(np.arange(0, 1500), np.sqrt(m0_1_est),
                       np.sqrt(t2_1_est), np.sqrt(m0_2_est)))
    ax2.set_ylim(bottom=0)
    ax2.set_xlim(left=0)
    return (m0_1_est, t2_1_est, m0_2_est)
Ejemplo n.º 3
0
def water_t2_corr_phantom():

    # Make a top-level instance and hide in top left corner, get filepath
    root = Tkinter.Tk()
    root.geometry('0x0+0+0')
    root.attributes("-topmost", 1)
    root.withdraw()
    water_list = tkFileDialog.askopenfilenames(parent=root)
    water_dir = os.path.split(water_list[0])[0]
    os.chdir(water_dir)
    root.attributes("-topmost", 0)
    root.destroy()

    water_data = {}
    tr_list = np.zeros(shape=len(water_list))
    te_list = np.zeros(shape=len(water_list))
    amp_list = np.zeros(shape=len(water_list))

    for w in range(0, len(water_list)):
        water_data[w] = mrs_struct(dicom.read_file(water_list[w]))
        tr_list[w] = water_data[w].tr
        te_list[w] = water_data[w].te
        amp_list[w] = np.mean(
            np.abs(
                hlsvdquant(water_data[w], nc_w,
                           np.arange(0, water_data[w].nsa))[0]))

    plt.scatter(te_list, amp_list)

    m0_est, t2_est = np.square(
        curve_fit(t2_curve_mono,
                  te_list,
                  amp_list,
                  p0=[np.sqrt(np.max(amp_list)), 50])[0])

    plt.plot(
        np.arange(0, 1500),
        t2_curve_mono(np.arange(0, 1500), np.sqrt(m0_est), np.sqrt(t2_est)))

    axes = plt.gca()
    axes.set_xlim(0, np.max(te_list))

    return (m0_est, t2_est)
Ejemplo n.º 4
0
def mrs_save(mrs_data):
    """ SAVE SELECTED RAW SPECTRA TO A NEW DICOM & PROCESSED (FILTERED) DATA TO .RAW FILES
        Both of these are placed in a folder as a 'sister directory' i.e. the 
        _MC_PROCESSED folder is in the parent directory of the DICOM file
    """
    try:

        ## work out whether eddy current correction is necessary based on TE
        if mrs_data.te < 288:
            mrs_data.eddy = tkMessageBox.askyesno(
                'ECC', 'Perform an eddy current correction?')
            if mrs_data.eddy == True:
                try:
                    # get path to water directory for eddy current correction
                    mrs_data.h2o_dir = tkFileDialog.askopenfilename(
                        initialdir=mrs_data.dcm_dir)
                    water_data = mrs_struct(mrs_data.h2o_dir)

                    ##Create a new window to display the effects of water filtering
                    frame = Tkinter.Toplevel()
                    ecc_select_GUI(frame, water_data)
                    ### EDDY DIALOGUE BOX TO SELECT AVERAGES ###

                    ### EDDY DIALOGUE BOX TO SELECT AVERAGES ###

                    ### EDDY DIALOGUE BOX TO SELECT AVERAGES ###

                    ### EDDY DIALOGUE BOX TO SELECT AVERAGES ###

                    mrs_data.ecc_cx = mrs_data.raw_cx * np.exp(
                        -1.0j * np.angle(water_data.raw_cx))
                except:
                    mrs_data.eddy = 0
            else:
                mrs_data.eddy = 0
        else:
            mrs_data.eddy = 0

        ## save data as raw files

    except AttributeError:
        print('PROBLEM SAVING FILE!')
Ejemplo n.º 5
0
    def dicom_master(self):
        ##Hide any old plots and clear them from memory
        for sub in range(0, numpy.size(self.plotlist)):
            self.plotlist[sub].set_visible(
                False)  #otherwise 'burned' onto canvas
        self.plotlist = []
        print('---CLEARED PREVIOUS CASES FROM ANALYSIS WINDOW---')
        ##Read the new DICOM file dataset
        dcm_dir = tkFileDialog.askopenfilename(parent=self.master)
        dcm = dicom.read_file(dcm_dir, force=True)
        os.chdir(os.path.split(dcm_dir)[0])
        print('CURRENTLY ANALYSING ----->        ' + os.path.split(dcm_dir)[1])

        ##Create an mrs data structure as defined in mrs_format module
        self.mrs_data = mrs_struct(dcm)
        self.mrs_data.dcm_dir = dcm_dir

        ##Repopulate the list of plots with the new dataset
        for sub in range(0, self.mrs_data.nsa):
            l, = self.splt.plot(
                self.mrs_data.ppmscale,
                numpy.real(
                    numpy.fft.fftshift(
                        numpy.fft.fft(
                            self.mrs_data.raw_cx[sub, :] *
                            numpy.exp(1j * self.stheta.get() -
                                      self.sapod.get() * self.mrs_data.fscale /
                                      self.mrs_data.bw)))),
                lw=2,
                visible=False)
            self.plotlist.insert(sub, l)

        ##Repopulate the listbox with the subspectra labels
        self.listbox.delete(0, Tkinter.END)
        for item in range(0, self.mrs_data.nsa):
            self.listbox.insert(Tkinter.END, str(item))

        ##Clear the current canvas to blank
        self.fig.canvas.draw()
Ejemplo n.º 6
0
def water_t1_corr():

    # Make a top-level instance and hide in top left corner, get filepath
    root = Tkinter.Tk()
    root.geometry('0x0+0+0')
    root.attributes("-topmost", 1)
    root.withdraw()
    water1 = tkFileDialog.askopenfilename(
        parent=root, title='CHOOSE WATER DICOM WITH TR=1500')
    water_dir = os.path.split(water1)[0]
    os.chdir(water_dir)
    res_dir = water_dir + '_Results'
    water2 = tkFileDialog.askopenfilename(
        parent=root, title='CHOOSE WATER DICOM WITH TR=5000')
    root.attributes("-topmost", 0)
    root.destroy()

    if not os.path.exists(res_dir):
        os.makedirs(res_dir)

    water_data = {}
    td_list = np.zeros(shape=2)
    amp_list = np.zeros(shape=2)

    water_data[0] = mrs_struct(dicom.read_file(water1))
    water_data[1] = mrs_struct(dicom.read_file(water2))

    frame1 = Tkinter.Toplevel()
    water_select_GUI(frame1, water_data[0])
    frame1.mainloop()
    frame2 = Tkinter.Toplevel()
    water_select_GUI(frame2, water_data[1])
    frame2.mainloop()

    for w in range(0, 2):
        amp_list[w] = np.sum(
            np.abs(hlsvdquant(water_data[w], nc_w,
                              water_data[w].vals)[0])) / np.size(
                                  water_data[w].vals)


#        print(amp_list[w])
    td_list = [1470, 4970]

    plt.scatter(td_list, amp_list)

    m0_est, t1_est = np.square(
        curve_fit(t1_curve,
                  td_list,
                  amp_list,
                  p0=[np.max(amp_list), np.sqrt(1500)])[0])

    plt.plot(np.arange(0, 10000),
             t1_curve(np.arange(0, 10000), np.sqrt(m0_est), np.sqrt(t1_est)))

    axes = plt.gca()
    axes.set_xlim(0, 10000)
    axes.set_ylim(0, m0_est)
    axes.set_ylabel('Water Signal Intensity')
    axes.set_xlabel('Repetition Time (ms)')
    axes.set_title('Water Relaxation Curve')
    resfile = os.path.abspath(res_dir + '/water_table.txt')

    with open(resfile, 'w') as f:
        f.write('Water Fit Results\n')
        f.write('Water@TR1500, ' + str(amp_list[0]) + '\n')
        f.write('Water@TR5000, ' + str(amp_list[1]) + '\n')
        f.write('Water@TRinf, ' + str(m0_est) + '\n')
        f.write('Water T1, ' + str(t1_est) + '\n')
        f.write('\n')