示例#1
0
def logisticRegressionArtificialData(dataFolder):
    uniformReflectances = np.load(dataFolder + "2015February1704:00PMreflectancesRandom1000000photons.npy")
    uniformReflectances = mch.normalizeImageQuotient(uniformReflectances, iqBand=1)

    gaussReflectances = np.load(dataFolder + "2015February1703:56PMreflectancesRandomNonUniform1000000photons.npy")
    gaussReflectances = mch.normalizeImageQuotient(gaussReflectances, iqBand=1)

    return uniformReflectances, gaussReflectances
示例#2
0
def perfect(dataFolder):
    trainingParameters   = np.load(dataFolder + "2015February1612:04AMparamtersPerfectArtificial.npy")
    trainingParameters   = trainingParameters[:,0:2] # only BVF and Vs for perfect data simulations
    trainingReflectances = np.load(dataFolder + "2015February1612:04AMreflectancesPerfectArtificial1000000photons.npy")
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances, iqBand=1)

    testParameters   = np.load(dataFolder + "2015February1702:02AMparamtersRandom.npy")
    testParameters   = testParameters[:,0:2] # only BVF and Vs for perfect data simulations
    testReflectances = np.load(dataFolder + "2015February1702:02AMreflectancesRandom.npy")
    testReflectances = mch.normalizeImageQuotient(testReflectances, iqBand=1)

    return trainingParameters, trainingReflectances, testParameters, testReflectances
示例#3
0
文件: data.py 项目: dkuegler/MITK
def noisy(dataFolder):
    trainingParameters = np.load(dataFolder + "2015February2208:16PMNoisyRandomTraining10000parameters.npy")
    trainingParameters = trainingParameters[:, 0:2]
    trainingReflectances = np.load(
        dataFolder + "2015February2208:16PMNoisyRandomTrainingreflectances1000000photons.npy"
    )
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances)

    testParameters = np.load(dataFolder + "2015February2301:23AMNoisyRandomTesting10000parameters.npy")
    testParameters = testParameters[:, 0:2]
    testReflectances = np.load(dataFolder + "2015February2301:23AMNoisyRandomTestingreflectances1000000photons.npy")
    testReflectances = mch.normalizeImageQuotient(testReflectances)

    return trainingParameters, trainingReflectances, testParameters, testReflectances
示例#4
0
文件: data.py 项目: dkuegler/MITK
def perfect(dataFolder):
    trainingParameters = np.load(dataFolder + "2015February2401:43AMPerfectGridTraining10000parameters.npy")
    trainingParameters = trainingParameters[:, 0:2]  # only BVF and Vs for perfect data simulations
    trainingReflectances = np.load(
        dataFolder + "2015February2401:43AMPerfectGridTrainingreflectances1000000photons.npy"
    )
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances)

    testParameters = np.load(dataFolder + "2015February2306:52PMUniformRandom10000parameters.npy")
    testParameters = testParameters[:, 0:2]  # only BVF and Vs for perfect data simulations
    testReflectances = np.load(dataFolder + "2015February2306:52PMUniformRandomreflectances1000000photons.npy")
    testReflectances = mch.normalizeImageQuotient(testReflectances)

    return trainingParameters, trainingReflectances, testParameters, testReflectances
示例#5
0
文件: data.py 项目: dkuegler/MITK
def realImage(dataFolder, imageToLoad):
    trainingParameters = np.load(dataFolder + "2015February2807:13PMNoisyRandomTraining10000parameters.npy")
    # estimate: BVF, Vs, d, SaO2:
    trainingParameters = trainingParameters[:, 0:3]
    trainingReflectances = np.load(
        dataFolder + "2015February2807:13PMNoisyRandomTrainingreflectances1000000photons.npy"
    )

    # trainingReflectances = np.delete(trainingReflectances, [2, 7], axis=1)
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances)

    shape, image, trainsegmentation, testsegmentation = csvR.csvMultiSpectralImageReader2(dataFolder + imageToLoad)

    # image = np.delete(image, [2, 7], axis=1)
    image = mch.normalizeImageQuotient(image)

    return trainingParameters, trainingReflectances, shape, image, trainsegmentation, testsegmentation
示例#6
0
def realImage(dataFolder, imageToLoad):
    trainingParameters = np.load(
        dataFolder +
        "2015February2807:13PMNoisyRandomTraining10000parameters.npy")
    # estimate: BVF, Vs, d, SaO2:
    trainingParameters = trainingParameters[:, 0:3]
    trainingReflectances = np.load(
        dataFolder +
        "2015February2807:13PMNoisyRandomTrainingreflectances1000000photons.npy"
    )

    #trainingReflectances = np.delete(trainingReflectances, [2, 7], axis=1)
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances)

    shape, image, trainsegmentation, testsegmentation = csvR.csvMultiSpectralImageReader2(
        dataFolder + imageToLoad)

    #image = np.delete(image, [2, 7], axis=1)
    image = mch.normalizeImageQuotient(image)

    return trainingParameters, trainingReflectances, shape, image, trainsegmentation, testsegmentation
示例#7
0
def noisy(dataFolder):
    trainingParameters = np.load(
        dataFolder +
        "2015February2208:16PMNoisyRandomTraining10000parameters.npy")
    trainingParameters = trainingParameters[:, 0:2]
    trainingReflectances = np.load(
        dataFolder +
        "2015February2208:16PMNoisyRandomTrainingreflectances1000000photons.npy"
    )
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances)

    testParameters = np.load(
        dataFolder +
        "2015February2301:23AMNoisyRandomTesting10000parameters.npy")
    testParameters = testParameters[:, 0:2]
    testReflectances = np.load(
        dataFolder +
        "2015February2301:23AMNoisyRandomTestingreflectances1000000photons.npy"
    )
    testReflectances = mch.normalizeImageQuotient(testReflectances)

    return trainingParameters, trainingReflectances, testParameters, testReflectances
示例#8
0
def perfect(dataFolder):
    trainingParameters = np.load(
        dataFolder +
        "2015February2401:43AMPerfectGridTraining10000parameters.npy")
    trainingParameters = trainingParameters[:, 0:
                                            2]  # only BVF and Vs for perfect data simulations
    trainingReflectances = np.load(
        dataFolder +
        "2015February2401:43AMPerfectGridTrainingreflectances1000000photons.npy"
    )
    trainingReflectances = mch.normalizeImageQuotient(trainingReflectances)

    testParameters = np.load(
        dataFolder + "2015February2306:52PMUniformRandom10000parameters.npy")
    testParameters = testParameters[:, 0:
                                    2]  # only BVF and Vs for perfect data simulations
    testReflectances = np.load(
        dataFolder +
        "2015February2306:52PMUniformRandomreflectances1000000photons.npy")
    testReflectances = mch.normalizeImageQuotient(testReflectances)

    return trainingParameters, trainingReflectances, testParameters, testReflectances
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
示例#10
0
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