Ejemplo n.º 1
0
def Create_Ideal_DCE(input_folder, output_filepath = '', input_aif=''):

    input_DCEs = []
    input_niis = glob.glob(os.path.join(input_folder, '*nrrd'))

    for nii in input_niis:
        if 'ktrans' in nii or 've' in nii:
            continue
        else:
            input_DCEs += [nii]

    for input_4d_nifti in input_DCEs:

        print 'Regenerating... ', input_4d_nifti

        # if output_filepath == '':
        output_filepath = str.split(input_4d_nifti, '.')[0]

        input_ktrans = output_filepath + '_ktrans.nii.gz'
        input_ve = output_filepath + '_ve.nii.gz'

        input_4d_nifti = Convert_NRRD_to_Nifti(input_4d_nifti, input_ktrans)

        input_numpy_4d = convert_input_2_numpy(input_4d_nifti)
        output_numpy_4d = np.zeros_like(input_numpy_4d)
        input_numpy_ktrans = convert_input_2_numpy(input_ktrans)
        input_numpy_ve = convert_input_2_numpy(input_ve)

        baseline_numpy = np.mean(input_numpy_4d[..., 0:7], axis=3)

        scan_time_seconds = 307.2
        time_interval_seconds = float((scan_time_seconds) / input_numpy_4d.shape[-1])
        time_interval_minutes = time_interval_seconds/60
        time_series = np.arange(0, input_numpy_4d.shape[-1]) / (60 / time_interval_seconds)
        injection_start_time_seconds=38.4

        T1_tissue=1000
        T1_blood=1440
        TR=3.8
        flip_angle_degrees=25
        relaxivity=.0045
        hematocrit=.45

        if input_aif == '':
            population_AIF = parker_model_AIF(scan_time_seconds, injection_start_time_seconds, time_interval_seconds, input_numpy_4d)
            concentration_AIF = population_AIF
        else:
            print 'extracting AIF...'
            AIF_label_numpy = convert_input_2_numpy(input_aif)
            AIF = generate_AIF(scan_time_seconds, injection_start_time_seconds, time_interval_seconds, input_numpy_4d, AIF_label_numpy)
            concentration_AIF = convert_intensity_to_concentration(AIF, T1_tissue, TR, flip_angle_degrees, injection_start_time_seconds, relaxivity, time_interval_seconds, hematocrit, T1_blood=T1_blood)

        for index in np.ndindex(input_numpy_ktrans.shape):

            output_numpy_4d[index] = np.array(estimate_concentration([input_numpy_ktrans[index],input_numpy_ve[index]], concentration_AIF, time_interval_minutes))

        # Or load presaved..
        # output_numpy_4d = nifti_2_numpy('DCE_MRI_Phantom_Regenerated_Concentrations.nii.gz')

        save_numpy_2_nifti(output_numpy_4d, input_4d_nifti, output_filepath + '_Regenerated_Concentrations.nii.gz')

        output_numpy_4d = revert_concentration_to_intensity(data_numpy=output_numpy_4d, reference_data_numpy=input_numpy_4d, T1_tissue=T1_tissue, TR=TR, flip_angle_degrees=flip_angle_degrees, injection_start_time_seconds=injection_start_time_seconds, relaxivity=relaxivity, time_interval_seconds=time_interval_seconds, hematocrit=hematocrit, T1_blood=0, T1_map = [])

        save_numpy_2_nifti(output_numpy_4d, input_4d_nifti, output_filepath + '_Regenerated_Signal.nii.gz')

    return
Ejemplo n.º 2
0
    def __init__(self,
                 n_samples=1000,
                 max_seq_len=60,
                 min_seq_len=25,
                 ktrans_range=[.001, 2],
                 ve_range=[0.01, .99],
                 gaussian_noise=[0, 0],
                 T1_range=[1000, 1000],
                 TR_range=[5, 5],
                 flip_angle_degrees_range=[30, 30],
                 relaxivity_range=[.0045, .0045],
                 hematocrit_range=[.45, .45],
                 sequence_length_range=[50, 50],
                 time_interval_seconds_range=[2, 2],
                 injection_start_time_seconds_range=[10, 10],
                 T1_blood_range=[1440, 1440],
                 baseline_intensity=[100, 100]):

        ktrans_low_range = [.001, .3]

        self.data = []
        self.labels = []
        self.seqlen = []

        for i in range(n_samples):

            # Random sequence length
            seq_len = np.random.random_integers(*sequence_length_range)

            # Monitor sequence length for TensorFlow dynamic calculation
            self.seqlen.append(seq_len)

            # Add a random or linear int sequence (50% prob)
            if random.random() < .5 or True:

                injection_start_time_seconds = np.random.uniform(
                    *injection_start_time_seconds_range)
                time_interval_seconds = np.random.uniform(
                    *time_interval_seconds_range)
                time_interval_minutes = time_interval_seconds / 60
                scan_time_seconds = seq_len * time_interval_seconds

                # Adjust for Unrealistically late injection time. Do this in a one liner later.
                while injection_start_time_seconds > .8 * scan_time_seconds:
                    injection_start_time_seconds = np.random.uniform(
                        *injection_start_time_seconds_range)

                AIF = parker_model_AIF(scan_time_seconds,
                                       injection_start_time_seconds,
                                       time_interval_seconds,
                                       timepoints=seq_len)

                ktrans = np.random.uniform(*ktrans_range)
                ve = np.random.uniform(*ve_range)

                Concentration = np.array(
                    estimate_concentration([ktrans, ve], AIF,
                                           time_interval_minutes))

                Intensity = revert_concentration_to_intensity(
                    data_numpy=Concentration,
                    reference_data_numpy=[],
                    T1_tissue=np.random.uniform(*T1_range),
                    TR=np.random.uniform(*TR_range),
                    flip_angle_degrees=np.random.uniform(
                        *flip_angle_degrees_range),
                    injection_start_time_seconds=injection_start_time_seconds,
                    relaxivity=np.random.uniform(*relaxivity_range),
                    time_interval_seconds=time_interval_seconds,
                    hematocrit=np.random.uniform(*hematocrit_range),
                    T1_blood=0,
                    T1_map=None,
                    static_baseline=np.random.uniform(
                        *baseline_intensity)).tolist()

                s = Intensity

                # Normalize
                # Intensity = (Intensity - np.mean(Intensity)) / np.std(Intensity)

                s += [0. for i in range(max_seq_len - seq_len)]

                s = [[i] for i in s]

                self.data.append(s)
                self.labels.append([ktrans, ve])

        self.batch_id = 0
Ejemplo n.º 3
0
    def __init__(self,
                 n_samples=1000,
                 max_seq_len=65,
                 min_seq_len=56,
                 patch_x=5,
                 patch_y=5,
                 ktrans_range=[.001, 2],
                 ve_range=[0.01, .99],
                 gaussian_noise=[0, 0],
                 T1_range=[900, 1500],
                 TR_range=[3, 6],
                 flip_angle_degrees_range=[15, 35],
                 relaxivity_range=[.0045, .0045],
                 hematocrit_range=[.45, .45],
                 sequence_length_range=[65, 65],
                 time_interval_seconds_range=[1.5, 6],
                 injection_start_time_seconds_range=[8, 20],
                 T1_blood_range=[1440, 1440],
                 baseline_intensity=[20, 300]):

        self.data = []
        self.labels = []
        self.seqlen = []

        for i in range(n_samples):

            # Random sequence length
            seq_len = np.random.random_integers(*sequence_length_range)

            # Monitor sequence length for TensorFlow dynamic calculation
            self.seqlen.append(seq_len)

            # Add a random or linear int sequence (50% prob)
            if random.random() < .5 or True:

                injection_start_time_seconds = np.random.uniform(
                    *injection_start_time_seconds_range)
                time_interval_seconds = np.random.uniform(
                    *time_interval_seconds_range)
                time_interval_minutes = time_interval_seconds / 60
                scan_time_seconds = seq_len * time_interval_seconds

                # Adjust for Unrealistically late injection time. Do this in a one liner later.
                while injection_start_time_seconds > .8 * scan_time_seconds:
                    injection_start_time_seconds = np.random.uniform(
                        *injection_start_time_seconds_range)

                AIF = parker_model_AIF(scan_time_seconds,
                                       injection_start_time_seconds,
                                       time_interval_seconds,
                                       timepoints=seq_len)

                true_ktrans = np.random.uniform(*ktrans_range)
                true_ve = np.random.uniform(*ve_range)

                ktrans = true_ktrans + np.zeros((patch_x, patch_y))
                ve = true_ve + np.zeros((patch_x, patch_y))

                Concentration = np.array(
                    estimate_concentration_general([ktrans, ve], AIF,
                                                   time_interval_minutes))

                Concentration = Concentration * np.abs(
                    np.random.normal(1, .25, Concentration.shape))

                Intensity = revert_concentration_to_intensity(
                    data_numpy=Concentration,
                    reference_data_numpy=[],
                    T1_tissue=np.random.uniform(*T1_range),
                    TR=np.random.uniform(*TR_range),
                    flip_angle_degrees=np.random.uniform(
                        *flip_angle_degrees_range),
                    injection_start_time_seconds=injection_start_time_seconds,
                    relaxivity=np.random.uniform(*relaxivity_range),
                    time_interval_seconds=time_interval_seconds,
                    hematocrit=np.random.uniform(*hematocrit_range),
                    T1_blood=0,
                    T1_map=None,
                    static_baseline=np.random.uniform(
                        *baseline_intensity)).tolist()

                s = np.expand_dims(Intensity, -1)

                self.data.append(s)
                self.labels.append([true_ktrans, true_ve])

        self.batch_id = 0
    def __init__(self,
                 n_samples=1000,
                 max_seq_len=50,
                 min_seq_len=3,
                 max_value=1000,
                 ktrans_range=[.001, 2],
                 ve_range=[0.001, .95],
                 gaussian_noise=[0, 0],
                 T1_range=[1000, 1000],
                 TR_range=[5, 5],
                 flip_angle_degrees_range=[30, 30],
                 relaxivity_range=[.0045, .0045],
                 hematocrit_range=[.45, .45],
                 sequence_length_range=[50, 50],
                 time_interval_seconds_range=[2, 2],
                 injection_start_time_seconds_range=[10, 10],
                 T1_blood_range=[1440, 1440],
                 baseline_intensity=[100, 100]):

        ktrans_low_range = [.001, .3]

        self.data = []
        self.labels = []
        self.seqlen = []

        for i in range(n_samples):

            # Random sequence length
            seq_len = np.random.random_integers(*sequence_length_range)

            # Monitor sequence length for TensorFlow dynamic calculation
            self.seqlen.append(seq_len)

            # Add a random or linear int sequence (50% prob)
            if random.random() < .5 or True:

                injection_start_time_seconds = np.random.uniform(
                    *injection_start_time_seconds_range)
                time_interval_seconds = np.random.uniform(
                    *time_interval_seconds_range)
                time_interval_minutes = time_interval_seconds / 60
                scan_time_seconds = seq_len * time_interval_seconds

                while injection_start_time_seconds > .8 * scan_time_seconds:
                    injection_start_time_seconds = np.random.uniform(
                        *injection_start_time_seconds_range)

                AIF = parker_model_AIF(scan_time_seconds,
                                       injection_start_time_seconds,
                                       time_interval_seconds,
                                       timepoints=seq_len)

                ktrans = np.random.uniform(*ktrans_range)
                ve = np.random.uniform(*ve_range)

                Concentration = np.array(
                    estimate_concentration([ktrans, ve], AIF,
                                           time_interval_minutes))

                Intensity = revert_concentration_to_intensity(
                    data_numpy=Concentration,
                    reference_data_numpy=[],
                    T1_tissue=np.random.uniform(*T1_range),
                    TR=np.random.uniform(*TR_range),
                    flip_angle_degrees=np.random.uniform(
                        *flip_angle_degrees_range),
                    injection_start_time_seconds=injection_start_time_seconds,
                    relaxivity=np.random.uniform(*relaxivity_range),
                    time_interval_seconds=time_interval_seconds,
                    hematocrit=np.random.uniform(*hematocrit_range),
                    T1_blood=0,
                    T1_map=[],
                    static_baseline=np.random.uniform(
                        *baseline_intensity)).tolist()

                s = Intensity

                # Intensity = (Intensity - np.mean(Intensity)) / np.std(Intensity)

                # s = []
                # s += [[value] for value in Intensity]
                s += [0. for i in range(max_seq_len - seq_len)]

                self.data.append(s)
                self.labels.append([ktrans])

            # else:
            #     # Generate a random sequence
            #     s = [[np.random.uniform(baseline_intensity[0]*2, baseline_intensity[0]*3)]
            #          for i in range(seq_len)]
            #     # Pad sequence for dimension consistency
            #     s += [[0.] for i in range(max_seq_len - seq_len)]

            #     self.data.append(s)
            #     self.labels.append([0., 1.])

            # else:

            #     injection_start_time_seconds = np.random.uniform(*injection_start_time_seconds_range)
            #     time_interval_seconds = np.random.uniform(*time_interval_seconds_range)
            #     time_interval_minutes = time_interval_seconds/60
            #     scan_time_seconds = seq_len * time_interval_seconds

            #     while injection_start_time_seconds > .8*scan_time_seconds:
            #         injection_start_time_seconds = np.random.uniform(*injection_start_time_seconds_range)

            #     AIF = parker_model_AIF(scan_time_seconds, injection_start_time_seconds, time_interval_seconds, timepoints=seq_len)

            #     Concentration = np.array(estimate_concentration([np.random.uniform(*ktrans_low_range),np.random.uniform(*ve_range)], AIF, time_interval_minutes))

            #     Intensity = revert_concentration_to_intensity(data_numpy=Concentration, reference_data_numpy=[], T1_tissue=np.random.uniform(*T1_range), TR=np.random.uniform(*TR_range), flip_angle_degrees=np.random.uniform(*flip_angle_degrees_range), injection_start_time_seconds=injection_start_time_seconds, relaxivity=np.random.uniform(*relaxivity_range), time_interval_seconds=time_interval_seconds, hematocrit=np.random.uniform(*hematocrit_range), T1_blood=0, T1_map = [], static_baseline=np.random.uniform(*baseline_intensity)).tolist()

            #     s = []

            #     Intensity = Intensity - np.mean(Intensity) / np.std(Intensity)

            #     s += [[value] for value in Intensity]
            #     s += [[0.] for i in range(max_seq_len - seq_len)]

            #     self.data.append(s)
            #     self.labels.append([0., 1.])

        self.batch_id = 0