def randomUniform(generatedFilename): infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths() infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.perfect() reflectances = np.zeros((nrSimulations, len(wavelengths))) parameters = np.zeros((nrSimulations, 4)) print('start simulations...') #%% start program logic start = time.time() for i in range(nrSimulations): print('starting simulation ' + str(i) + ' of ' + str(nrSimulations)) BVF = random.uniform(min(BVFs), max(BVFs)) Vs = random.uniform(min(Vss), max(Vss)) d = random.uniform(min(ds), max(ds)) r = random.uniform(min(rs), max(rs)) SaO2= random.uniform(min(SaO2s), max(SaO2s)) parameters[i,:] = np.array([BVF, Vs, d, SaO2]) for j, wavelength in enumerate(wavelengths): reflectanceValue = mch.runOneSimulation( wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, r, SaO2, Fwhm = FWHM, nrPhotons=photons) print((BVF, Vs, d, SaO2, r, wavelength)) # here, summarize result from wavelength in reflectance spectrum reflectances[i, j] = reflectanceValue infile.close() # save the reflectance results! now = datetime.datetime.now().strftime("%Y%B%d%I:%M%p") np.save(outfolderRS + now + generatedFilename + "reflectances" + str(photons) + "photons", reflectances) np.save(outfolderRS + now + generatedFilename + str(nrSimulations) + "parameters", parameters) end = time.time() print "total time to generate random uniform data: " + str((end - start))
def perfectGrid(generatedFilename): infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths() infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.perfect() reflectances = np.zeros((nrSamples, len(wavelengths))) print ("start simulations...") #%% start program logic start = time.time() currentSimulation = 0 params = itertools.product(BVFs, Vss, ds, SaO2s, rs) paramsList = list(itertools.product(BVFs, Vss, ds, SaO2s, rs)) for BVF, Vs, d, SaO2, r in params: print ("starting simulation " + str(currentSimulation) + " of " + str(nrSamples)) for idx, wavelength in enumerate(wavelengths): # here the magic happens: run monte carlo simulation for tissue parameters # and specific wavelength reflectanceValue = mch.runOneSimulation( wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, r, SaO2, Fwhm=FWHM, nrPhotons=photons, ) print ((BVF, Vs, d, SaO2, r, wavelength)) # here, summarize result from wavelength in reflectance spectrum reflectances[currentSimulation, idx] = reflectanceValue currentSimulation += 1 infile.close() # save the reflectance results! now = datetime.datetime.now().strftime("%Y%B%d%I:%M%p") np.save(outfolderRS + now + generatedFilename + "reflectances" + str(photons) + "photons", reflectances) np.save(outfolderRS + now + generatedFilename + str(nrSamples) + "parameters", paramsList) end = time.time() print "total time for generating perfect data on grid: " + str((end - start))
def noisyRandom(generatedFilename): infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths() infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.noisy() reflectances = np.zeros((nrSimulations, len(wavelengths))) parameters = np.zeros((nrSimulations, 7)) print('start simulations...') #%% start program logic start = time.time() for i in range(nrSimulations): print('starting simulation ' + str(i) + ' of ' + str(nrSimulations)) BVF = random.uniform(min(BVFs), max(BVFs)) Vs = random.uniform(min(Vss), max(Vss)) d = random.uniform(min(ds), max(ds)) SaO2= random.uniform(min(SaO2s), max(SaO2s)) r = random.uniform(min(rs), max(rs)) min_sm_BVF = max(min(BVFs), 0.03) sm_BVF = random.uniform(min_sm_BVF, max(BVFs)) sm_Vs = random.uniform(min(Vss), max(Vss)) parameters[i,:] = np.array([BVF, Vs, d, SaO2, r, sm_BVF, sm_Vs]) for j, wavelength in enumerate(wavelengths): reflectanceValue = mch.runOneSimulation( wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, r, SaO2, submucosa_BVF=sm_BVF, submucosa_Vs=sm_Vs, submucosa_SaO2=SaO2, Fwhm = FWHM, nrPhotons=photons) print((BVF, Vs, d, SaO2, r, wavelength, sm_BVF, sm_Vs)) # here, summarize result from wavelength in reflectance spectrum reflectances[i, j] = reflectanceValue infile.close() # save the reflectance results! now = datetime.datetime.now().strftime("%Y%B%d%I:%M%p") np.save(outfolderRS + now + generatedFilename + "reflectances" + str(photons) + "photons", reflectances) np.save(outfolderRS + now + generatedFilename + str(nrSimulations) + "parameters", parameters) end = time.time() print "total time to generate noisy random data: " + str((end - start))
def estimateParametersRealImage(trainingParameters, trainingReflectances, shape, image, trainsegmentation, testsegmentation, activateDA): sourceReflectancesDA = image[np.nonzero(trainsegmentation)[0], :] # choose m reflectances for training DA m = trainingReflectances.shape[0] sourceReflectancesDA = np.matrix(random.sample(sourceReflectancesDA, m)) #%% 2. determine domain adaptation weights trainingWeights = np.ones(trainingReflectances.shape[0]) if (activateDA): trainingWeights = calculateWeights(trainingReflectances, sourceReflectancesDA) #%% 3. train forest rf = randomForest(trainingParameters, trainingReflectances, trainingWeights) #%% 4. estimate the parameters for the image print "starting to estimate the tissue parameters" start = time.time() estimatedParameters = rf.predict(image) # set to zero if not in segmentation mask #estimatedParameters[np.where(0 == testsegmentation), :] = 0 end = time.time() print "time necessary to estimate parameters for image [s]: " + str( (end - start)) #%% save the parametric images TODO delete after everything works # import Image # # for i in np.arange(0,estimatedParameters.shape[1]): # parameterImage_i = np.reshape(estimatedParameters[:,i], shape) # im = Image.fromarray(parameterImage_i) # im.save("data/output/" + "parameterImage_" + str(i) + ".tiff") #%% 6. evaluate data # for this, create monte carlo simulation for each # parameter estimate. The resulted reflectance estimate can then be compared to # the measured reflectance. from setup import systemPaths from setup import simulation import helper.monteCarloHelper as mch infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths( ) infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.noisy( ) # the estimated parameters within the segmentation estimatedParametersOnlySegmented = estimatedParameters[ np.nonzero(testsegmentation)[0], :] # the image reflectances from which these parameters where estimated inputReflectancesOnlySegmented = image[np.nonzero(testsegmentation)[0], :] # index vector for selecting n samples from this data indices = np.arange(0, estimatedParametersOnlySegmented.shape[0], 1) # choose n n = 20 nSamples = random.sample(indices, n) estimatedParametersOnlySegmented = estimatedParametersOnlySegmented[ nSamples] inputReflectancesOnlySegmented = inputReflectancesOnlySegmented[nSamples] # placeholder for the reflectance computed from MC with the estimated parameters reflectancesFromEstimatedParameters = np.zeros( (inputReflectancesOnlySegmented.shape[0], inputReflectancesOnlySegmented.shape[1] + 1)) #wavelengths = np.delete(wavelengths, [2, 7]) for i, (BVF, Vs, d) in enumerate(estimatedParametersOnlySegmented): print('starting simulation ' + str(i) + ' of ' + str(estimatedParametersOnlySegmented.shape[0])) for j, wavelength in enumerate(wavelengths): reflectanceValue = mch.runOneSimulation( wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, # np.mean(rs), SaO2, # submucosa_BVF=sm_BVF, submucosa_Vs=sm_Vs, submucosa_SaO2=SaO2, Fwhm=FWHM, nrPhotons=photons) #print((BVF, Vs, d, wavelength)) reflectancesFromEstimatedParameters[i, j] = reflectanceValue # correct these reflectances by image quotient reflectancesFromEstimatedParameters = mch.normalizeImageQuotient( reflectancesFromEstimatedParameters) wavelengths = mch.removeIqWavelength(wavelengths) #%% plot data for nicer inspection from sklearn.metrics import r2_score r2Score = r2_score(reflectancesFromEstimatedParameters.T, inputReflectancesOnlySegmented.T) print("r2Score for random forest estimatation of:", str(r2Score)) #%% sort by wavelength: for plot_i in range(n): sortedIndices = sorted(range(len(wavelengths)), key=lambda k: wavelengths[k]) plt.figure() plt.plot(wavelengths[sortedIndices], reflectancesFromEstimatedParameters[plot_i, sortedIndices], 'g-o') plt.plot(wavelengths[sortedIndices], inputReflectancesOnlySegmented[plot_i, sortedIndices], 'b-o') print( str( r2_score(reflectancesFromEstimatedParameters[plot_i, :], inputReflectancesOnlySegmented[plot_i, :]))) plt.legend(["estimated", "measurement"]) plt.xlabel("wavelength [m]") plt.ylabel("normalized reflectance") plt.savefig("data/output/example_fit_" + str(plot_i) + '.png') return estimatedParameters, r2Score, reflectancesFromEstimatedParameters, inputReflectancesOnlySegmented
def estimateParametersRealImage(trainingParameters, trainingReflectances, shape, image, trainsegmentation, testsegmentation, activateDA): sourceReflectancesDA = image[np.nonzero(trainsegmentation)[0], :] # choose m reflectances for training DA m = trainingReflectances.shape[0] sourceReflectancesDA = np.matrix(random.sample(sourceReflectancesDA, m)) #%% 2. determine domain adaptation weights trainingWeights = np.ones(trainingReflectances.shape[0]) if (activateDA): trainingWeights = calculateWeights(trainingReflectances, sourceReflectancesDA) #%% 3. train forest rf = randomForest(trainingParameters, trainingReflectances, trainingWeights) #%% 4. estimate the parameters for the image print "starting to estimate the tissue parameters" start = time.time() estimatedParameters = rf.predict(image) # set to zero if not in segmentation mask #estimatedParameters[np.where(0 == testsegmentation), :] = 0 end = time.time() print "time necessary to estimate parameters for image [s]: " + str((end - start)) #%% save the parametric images TODO delete after everything works # import Image # # for i in np.arange(0,estimatedParameters.shape[1]): # parameterImage_i = np.reshape(estimatedParameters[:,i], shape) # im = Image.fromarray(parameterImage_i) # im.save("data/output/" + "parameterImage_" + str(i) + ".tiff") #%% 6. evaluate data # for this, create monte carlo simulation for each # parameter estimate. The resulted reflectance estimate can then be compared to # the measured reflectance. from setup import systemPaths from setup import simulation import helper.monteCarloHelper as mch infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths() infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.noisy() # the estimated parameters within the segmentation estimatedParametersOnlySegmented = estimatedParameters[np.nonzero(testsegmentation)[0], :] # the image reflectances from which these parameters where estimated inputReflectancesOnlySegmented = image[np.nonzero(testsegmentation)[0], :] # index vector for selecting n samples from this data indices = np.arange(0, estimatedParametersOnlySegmented.shape[0], 1) # choose n n = 20 nSamples = random.sample(indices, n) estimatedParametersOnlySegmented = estimatedParametersOnlySegmented[nSamples] inputReflectancesOnlySegmented = inputReflectancesOnlySegmented[nSamples] # placeholder for the reflectance computed from MC with the estimated parameters reflectancesFromEstimatedParameters = np.zeros((inputReflectancesOnlySegmented.shape[0], inputReflectancesOnlySegmented.shape[1]+1)) #wavelengths = np.delete(wavelengths, [2, 7]) for i, (BVF, Vs, d) in enumerate(estimatedParametersOnlySegmented): print('starting simulation ' + str(i) + ' of ' + str(estimatedParametersOnlySegmented.shape[0])) for j, wavelength in enumerate(wavelengths): reflectanceValue = mch.runOneSimulation( wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, # np.mean(rs), SaO2, # submucosa_BVF=sm_BVF, submucosa_Vs=sm_Vs, submucosa_SaO2=SaO2, Fwhm = FWHM, nrPhotons=photons) #print((BVF, Vs, d, wavelength)) reflectancesFromEstimatedParameters[i, j] = reflectanceValue # correct these reflectances by image quotient reflectancesFromEstimatedParameters = mch.normalizeImageQuotient(reflectancesFromEstimatedParameters) wavelengths = mch.removeIqWavelength(wavelengths) #%% plot data for nicer inspection from sklearn.metrics import r2_score r2Score = r2_score(reflectancesFromEstimatedParameters.T, inputReflectancesOnlySegmented.T) print("r2Score for random forest estimatation of:", str(r2Score)) #%% sort by wavelength: for plot_i in range(n): sortedIndices = sorted(range(len(wavelengths)), key=lambda k: wavelengths[k]) plt.figure() plt.plot(wavelengths[sortedIndices], reflectancesFromEstimatedParameters[plot_i,sortedIndices], 'g-o') plt.plot(wavelengths[sortedIndices], inputReflectancesOnlySegmented[plot_i,sortedIndices], 'b-o') print(str(r2_score(reflectancesFromEstimatedParameters[plot_i, :], inputReflectancesOnlySegmented[plot_i, :]))) plt.legend(["estimated", "measurement"]) plt.xlabel("wavelength [m]") plt.ylabel("normalized reflectance") plt.savefig("data/output/example_fit_" + str(plot_i) + '.png') return estimatedParameters, r2Score, reflectancesFromEstimatedParameters, inputReflectancesOnlySegmented
def perfectGrid(generatedFilename): infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths( ) infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.perfect( ) reflectances = np.zeros((nrSamples, len(wavelengths))) print('start simulations...') #%% start program logic start = time.time() currentSimulation = 0 params = itertools.product(BVFs, Vss, ds, SaO2s, rs) paramsList = list(itertools.product(BVFs, Vss, ds, SaO2s, rs)) for BVF, Vs, d, SaO2, r in params: print('starting simulation ' + str(currentSimulation) + ' of ' + str(nrSamples)) for idx, wavelength in enumerate(wavelengths): # here the magic happens: run monte carlo simulation for tissue parameters # and specific wavelength reflectanceValue = mch.runOneSimulation(wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, r, SaO2, Fwhm=FWHM, nrPhotons=photons) print((BVF, Vs, d, SaO2, r, wavelength)) # here, summarize result from wavelength in reflectance spectrum reflectances[currentSimulation, idx] = reflectanceValue currentSimulation += 1 infile.close() # save the reflectance results! now = datetime.datetime.now().strftime("%Y%B%d%I:%M%p") np.save( outfolderRS + now + generatedFilename + "reflectances" + str(photons) + "photons", reflectances) np.save( outfolderRS + now + generatedFilename + str(nrSamples) + "parameters", paramsList) end = time.time() print "total time for generating perfect data on grid: " + str( (end - start))
def randomNonUniform(generatedFilename): infileString, outfolderMC, outfolderRS, gpumcmlDirectory, gpumcmlExecutable = systemPaths.initPaths() infile = open(infileString) BVFs, Vss, ds, SaO2s, rs, nrSamples, photons, wavelengths, FWHM, eHbO2, eHb, nrSimulations = simulation.noisy() reflectances = np.zeros((nrSimulations, len(wavelengths))) parameters = np.zeros((nrSimulations, 8)) print ("start simulations...") #%% start program logic start = time.time() for i in range(nrSimulations): print ("starting simulation " + str(i) + " of " + str(nrSimulations)) BVF = drawFromNormal(BVFs) Vs = drawFromNormal(Vss) d = drawFromNormal(ds) SaO2 = drawFromNormal(SaO2s) r = drawFromNormal(rs) min_sm_BVF = max(min(BVFs), 0.03) sm_BVF = drawFromNormal([min_sm_BVF, max(BVFs)]) sm_Vs = drawFromNormal(Vss) sm_SaO2 = drawFromNormal(SaO2s) parameters[i, :] = np.array([BVF, Vs, d, SaO2, r, sm_BVF, sm_Vs, sm_SaO2]) for j, wavelength in enumerate(wavelengths): reflectanceValue = mch.runOneSimulation( wavelength, eHbO2, eHb, infile, outfolderMC, gpumcmlDirectory, gpumcmlExecutable, BVF, Vs, d, r, SaO2, Fwhm=FWHM, nrPhotons=photons, ) print ((BVF, Vs, d, SaO2, r, wavelength)) # here, summarize result from wavelength in reflectance spectrum reflectances[i, j] = reflectanceValue infile.close() # save the reflectance results! now = datetime.datetime.now().strftime("%Y%B%d%I:%M%p") np.save(outfolderRS + now + generatedFilename + "reflectances" + str(photons) + "photons", reflectances) np.save(outfolderRS + now + generatedFilename + str(nrSimulations) + "parameters", parameters) end = time.time() print "total time for generating random non uniform data: " + str((end - start))