|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +DWI_functions.py |
| 4 | +==================================== |
| 5 | +This module can be used to calculate ADC/IVIM maps. |
| 6 | +IVIM fitting is done in a segmented fashion: https://doi.org/10.3389/fonc.2021.705964 |
| 7 | +
|
| 8 | +@authors: s.zijlema, p.vhoudt, k.baas, e.kooreman |
| 9 | +Group: Uulke van der Heide - NKI |
| 10 | +""" |
| 11 | + |
| 12 | +import numpy as np |
| 13 | + |
| 14 | +def generate_ADC_standalone(DWIdata, bvalues, bmin: int = 150, bmax: int = 1000, specificBvals: list = []): |
| 15 | + """ |
| 16 | + Function to create an ADC map from DWI data. |
| 17 | + Required input is a numpy array with the data and the corresponding b-values |
| 18 | +
|
| 19 | + Example use: |
| 20 | + generate_ADC(data, bvalues) |
| 21 | +
|
| 22 | + Parameters |
| 23 | + --------- |
| 24 | + DWIdata: numpy array |
| 25 | + DWI data |
| 26 | + bvalues: list |
| 27 | + bvalues corresponding to the data |
| 28 | + bmin/bmax (optional) |
| 29 | + Used to select range of b-values that you want to include to calculate the ADC. |
| 30 | + Not used when specificBvals is defined. |
| 31 | + specificBvals (optional) |
| 32 | + List of b-values that should be used for ADC map calculation. |
| 33 | + If set, bmin/bmax options are not used. |
| 34 | +
|
| 35 | + Returns |
| 36 | + ------- |
| 37 | + ADClog |
| 38 | + ADC map |
| 39 | + b0_intercept |
| 40 | + Map with y-intercepts (b=0) of ADC fit |
| 41 | + bvalues |
| 42 | + b-values that matched the criteria |
| 43 | + """ |
| 44 | + |
| 45 | + #Obtain indices of matching b-values |
| 46 | + if specificBvals == []: |
| 47 | + #Select only b-values >=bmin and <=bmax |
| 48 | + bvalueindex = [i for i,v in enumerate(bvalues) if v >= bmin and v <= bmax] |
| 49 | + else: #manual b-value selection |
| 50 | + #Check if all specified b-values were acquired |
| 51 | + if not all(elem in bvalues for elem in specificBvals): |
| 52 | + raise Exception('Not all specified b-values were found. Available b-values: '+str(bvalues)+'. You requested: '+str(specificBvals)) |
| 53 | + #Get b-value indices |
| 54 | + bvalueindex = [i for i,v in enumerate(bvalues) if v in specificBvals] |
| 55 | + |
| 56 | + use_bvalues = [bvalues[index] for index in bvalueindex] |
| 57 | + if len(use_bvalues) < 2: |
| 58 | + raise Exception('Less than 2 b values were found that fulfill your min and max criteria.') |
| 59 | + |
| 60 | + # Calculate ADC map using linear regression |
| 61 | + ADClog, b0_intercept = fit_ADCmap_loglinear_standalone(DWIdata, bvalues, use_bvalues) |
| 62 | + ADClog[ADClog<0] = 0 #set values below zero to zero |
| 63 | + |
| 64 | + return ADClog, b0_intercept, use_bvalues |
| 65 | + |
| 66 | +def generate_IVIMmaps_standalone(DWIdata, bvalues, bminADC=150, bmaxADC=1000, bmaxDstar=50): |
| 67 | + """ |
| 68 | + Function to calculate IVIM maps (perfusion fraction and D*). |
| 69 | + NOTE: D* is calculated using only the lowest two b-values. |
| 70 | +
|
| 71 | + Example use: |
| 72 | + generate_IVIMmaps(data, bvalues) |
| 73 | +
|
| 74 | + Parameters |
| 75 | + --------- |
| 76 | + DWIdata: numpy array |
| 77 | + DWI data |
| 78 | + bvalues: list |
| 79 | + bvalues corresponding to the data |
| 80 | + bminADC/bmaxADC (optional) |
| 81 | + Used to select the range of b-values for ADC calculation. |
| 82 | + bmaxDstar (optional) |
| 83 | + Used to select the b-values for D* calculation. |
| 84 | + outputOptions |
| 85 | + ['ADC','f','D*'] (default) will output the ADC, f, and D*. |
| 86 | +
|
| 87 | +
|
| 88 | + """ |
| 89 | + |
| 90 | + # Calculate ADC map |
| 91 | + ADClog, b0_intercept, used_bvalues = generate_ADC_standalone(DWIdata, bvalues, bminADC, bmaxADC) |
| 92 | + |
| 93 | + # Check if b=0 is acquired, otherwise f and D* cannot be calculated |
| 94 | + if not 0 in bvalues: # if b=0 is not acquired |
| 95 | + print('B=0 is not available. Hence, only the ADC is calculated.') |
| 96 | + return # stop execution |
| 97 | + |
| 98 | + # Get b0 data |
| 99 | + b0index = bvalues.index(0) # gets first index of b=0 |
| 100 | + DWIb0 = DWIdata[:, :, :, b0index] # b=0 data |
| 101 | + |
| 102 | + #### Calculate perfusion fraction |
| 103 | + with np.errstate(divide='ignore', invalid='ignore'): # hide division by 0 warnings |
| 104 | + perfFrac = (DWIb0 - np.exp(b0_intercept)) / DWIb0 # perfusion fraction |
| 105 | + perfFrac[np.isnan(perfFrac)] = 0 |
| 106 | + perfFrac[np.isinf(perfFrac)] = 0 |
| 107 | + perfFrac[perfFrac < 0] = 0 # f <0 is not possible |
| 108 | + perfFrac[perfFrac > 1] = 1 # f >1 is also not possible |
| 109 | + |
| 110 | + # Get matching b-values |
| 111 | + try: |
| 112 | + _, bvalues = select_b_values_standalone(bvalues, bmin=0, bmax=bmaxDstar) |
| 113 | + |
| 114 | + # Check if b=0 was acquired |
| 115 | + if 0 not in bvalues: |
| 116 | + raise Exception('b=0 was not acquired') |
| 117 | + |
| 118 | + if len(bvalues) > 2: |
| 119 | + print('More than two b-values were detected for D* calculation. ' + |
| 120 | + 'Note that only the first two will be used for D*.') |
| 121 | + |
| 122 | + except Exception as e: |
| 123 | + print("Could not calculate D* due to an error: " + str(e)) |
| 124 | + return |
| 125 | + |
| 126 | + # Use b=0 and the lowest b-value to calculate D* |
| 127 | + with np.errstate(divide='ignore', invalid='ignore'): # hide division by 0 warnings |
| 128 | + Dstar = -np.log( |
| 129 | + (DWIdata[:,:,:,1] / DWIdata[:,:,:,0] - (1 - perfFrac) * np.exp(-bvalues[1] * 0.001 * ADClog)) / perfFrac) / \ |
| 130 | + bvalues[1] # Calculate D* using f, d, and the lowest 2 b-values |
| 131 | + Dstar[np.isnan(Dstar)] = 0 |
| 132 | + Dstar[np.isinf(Dstar)] = 0 |
| 133 | + Dstar = Dstar * 1000 # scale to same as ADC |
| 134 | + |
| 135 | + return ADClog, perfFrac, Dstar |
| 136 | + |
| 137 | + |
| 138 | +def select_b_values_standalone(all_bvalues, bmin=0, bmax=float("inf"), specificBvals: list = []): |
| 139 | + """ |
| 140 | + Find b-values and their indices between bmin and bmax of a scan (bmin <= b <= b max) |
| 141 | + or of b-values that are defined in specificBvals. |
| 142 | + If specificBvals is defined, only b-values are returned that are acquired |
| 143 | + in the scan AND are present in specificBvals. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + all_bvalues : list of int |
| 148 | + All b values that are in the data in the order that they are stored |
| 149 | + bmin : int |
| 150 | + Minimal b-value. |
| 151 | + bmax : int |
| 152 | + Maximal b-value. |
| 153 | + specificBvals : list |
| 154 | + List of b-values. |
| 155 | +
|
| 156 | +
|
| 157 | + Returns |
| 158 | + ------- |
| 159 | + bvalueindex : list of int |
| 160 | + Indices of b-values between bmin and bmax. |
| 161 | + bvalues : list of int |
| 162 | + B-values between bmin and bmax. |
| 163 | +
|
| 164 | + """ |
| 165 | + |
| 166 | + # Obtain indices of matching b-values |
| 167 | + if specificBvals == []: |
| 168 | + # Select only b-values >=bmin and <=bmax |
| 169 | + bvalueindex = [i for i, v in enumerate(all_bvalues) if v >= bmin and v <= bmax] |
| 170 | + else: # manual b-value selection |
| 171 | + # Check if all specified b-values were acquired |
| 172 | + if not all(elem in all_bvalues for elem in specificBvals): |
| 173 | + raise Exception('Not all specified b-values were found. Available b-values: ' + str( |
| 174 | + all_bvalues) + '. You requested: ' + str(specificBvals)) |
| 175 | + |
| 176 | + # Get b-value indices |
| 177 | + bvalueindex = [i for i, v in enumerate(all_bvalues) if v in specificBvals] |
| 178 | + |
| 179 | + # Check if enough b-values were found |
| 180 | + if len(bvalueindex) < 2: |
| 181 | + raise Exception("No(t enough) b-values matched the selected range between " + str(bmin) + " and " + str( |
| 182 | + bmax) + " (found: " + str(len(bvalueindex)) + ")") |
| 183 | + |
| 184 | + # Select matching b-values |
| 185 | + bvalues = [all_bvalues[i] for i in bvalueindex] |
| 186 | + |
| 187 | + # Return index and b-values |
| 188 | + return bvalueindex, bvalues |
| 189 | + |
| 190 | + |
| 191 | +def fit_ADCmap_loglinear_standalone(DWIdata, all_bvalues, use_bvalues, size=[]): |
| 192 | + """ |
| 193 | + Function to calculate ADC based on linear regression. |
| 194 | + Taken from ADC_functions.py but adjusted to also output intercept and |
| 195 | + removed size input requirement (could be extracted from si_array). |
| 196 | + The ADC values have been validated with the Philips ADC maps (2022). |
| 197 | +
|
| 198 | + Parameters |
| 199 | + --------- |
| 200 | + DWIdata |
| 201 | + DWI data |
| 202 | + all_bvalues |
| 203 | + list or array of b-values that are stored in the data |
| 204 | + use_bvalues |
| 205 | + list or array of selected b-values selected for calculation. |
| 206 | +
|
| 207 | + Returns |
| 208 | + ------- |
| 209 | + ADC calculated ADC and the intercept with the y-axis at b=0 in the same |
| 210 | + x,y,z coordinates as the original DWI image (i.e. size) |
| 211 | +
|
| 212 | + """ |
| 213 | + |
| 214 | + nr_bvalues = len(use_bvalues) |
| 215 | + |
| 216 | + b_mat = np.ones([nr_bvalues, 2]) |
| 217 | + for curBvalNr in range(nr_bvalues): |
| 218 | + b_mat[curBvalNr, 0] = use_bvalues[curBvalNr] |
| 219 | + |
| 220 | + if size == []: |
| 221 | + size = DWIdata.shape[:-1] # b-values are stored in the last dimension |
| 222 | + |
| 223 | + # Extract the b values that are used for calculation |
| 224 | + indices = [all_bvalues.index(value) for value in use_bvalues] |
| 225 | + DWIdata = DWIdata[:,:,:,indices] |
| 226 | + DWIdata = DWIdata.transpose(3, 0, 1, 2) # code below expects different format |
| 227 | + |
| 228 | + with np.errstate(divide='ignore', invalid='ignore'): # hide division by 0 warnings |
| 229 | + log_si = -np.log(DWIdata.reshape(nr_bvalues, -1)) # calculate log of data and reshape to one row per b-value |
| 230 | + #log_si = -np.log(DWIdata.reshape(-1, nr_bvalues)) |
| 231 | + log_si[np.isinf(log_si)] = 0 # to fix 0 values in si_array |
| 232 | + |
| 233 | + ADC, intercept = np.linalg.lstsq(b_mat, log_si, rcond=None)[0] # calculate ADC and b=0 intercept |
| 234 | + ADC = ADC.reshape(*size) * 1000 # Convert ADC to 10^-3 mm^2/s |
| 235 | + b0_intercept = -intercept.reshape(*size) # can be used to calculate perfusion fraction |
| 236 | + |
| 237 | + return ADC, b0_intercept |
| 238 | + |
| 239 | + |
0 commit comments