Exemplo n.º 1
0
    def test_mci_wrapper_book_example(self):
        """see if our result matches the one from
        Biomedical Optics
        Principles and Imaging
        page 55 (Table 3.1)"""
        # create a book_p55_mci which shall create a mci file
        book_p55_mci = MciWrapper()
        book_p55_mci.set_mci_filename(self.mci_filename)
        book_p55_mci.set_mco_filename(self.mco_filename)
        book_p55_mci.set_nr_photons(10**6)
        book_p55_mci.add_layer(1, 1000, 9000, 0.75, 0.0002)

        mcml_path, mcml_file = os.path.split(path_to_gpumcml)
        if os.path.isfile(path_to_gpumcml):
            book_p55_mci.create_mci_file()
            sim_wrapper = SimWrapper()
            sim_wrapper.set_mci_filename(self.mci_filename)
            sim_wrapper.set_mcml_executable(path_to_gpumcml)
            sim_wrapper.run_simulation()
            self.assertTrue(os.path.isfile(mcml_path + "/" + self.mco_filename),
                        "mco file was created")
            refl = get_diffuse_reflectance(os.path.join(mcml_path,
                                                        self.mco_filename))
            self.assertAlmostEqual(refl, 0.09734, 3,
                                   "correct reflectance determined " +
                                   "according to book table 3.1")
Exemplo n.º 2
0
 def wavelength_to_reflectance(wavelength):
     # helper function to determine the reflectance for a given
     # wavelength using the current model and simulation
     tissue_model.set_wavelength(wavelength)
     tissue_model.create_mci_file()
     sim_wrapper.run_simulation()
     simulation_path = os.path.split(sim_wrapper.mcml_executable)[0]
     return get_diffuse_reflectance(
         os.path.join(simulation_path, tissue_model.get_mco_filename()))
Exemplo n.º 3
0
 def wavelength_to_reflectance(wavelength):
     # helper function to determine the reflectance for a given
     # wavelength using the current model and simulation
     tissue_model.set_wavelength(wavelength)
     tissue_model.create_mci_file()
     sim_wrapper.run_simulation()
     simulation_path = os.path.split(sim_wrapper.mcml_executable)[0]
     return get_diffuse_reflectance(os.path.join(simulation_path,
                                                 tissue_model.
                                                 get_mco_filename()))
    def run(self):
        start = time.time()

        # the specific subfolder where the generated spectra shall be saved
        simulated_data_folder=self._get_folder_name()

        # just to be a bit shorter in the code
        ex = self.experiment_dict

        # the wavelengths to be simulated
        wavelengths = np.arange(float(ex["wavelengths_start"]),
                                float(ex["wavelengths_end"]),
                                float(ex["wavelengths_step"])) * 10**-9

        # create folder for mci files if not exists
        mci_folder = os.path.join(sc.get_full_dir("MC_DATA_FOLDER"), simulated_data_folder,
                                  "mci")
        if not os.path.exists(mci_folder):
            os.makedirs(mci_folder)

        # Setup simulation wrapper
        sim_wrapper = SimWrapper()
        sim_wrapper.set_mcml_executable(os.path.join(ex["path_to_mcml"],
                                                     ex["mcml_executable"]))
        sim_wrapper.set_mci_filename(os.path.join(mci_folder, "Bat_" + str(self.batch_nr) + ".mci"))

        # Setup tissue model
        tissue_model = self.factory.create_tissue_model()
        tissue_model.set_mci_filename(sim_wrapper.mci_filename)
        tissue_model.set_nr_photons(int(ex["nr_photons"]))
        tissue_model._mci_wrapper.set_nr_runs(
            int(ex["nr_elements_in_batch"]) * wavelengths.shape[0])
        tissue_model.create_mci_file()

        tissue_instance = tissueparser.read_tissue_config(self.tissue_file)

        # setup array in which data shall be stored
        batch = self.factory.create_batch_to_simulate()
        batch.set_tissue_instance(tissue_instance)

        # create the tissue samples and return them in dataframe df
        df = batch.create_tissue_samples(int(ex["nr_elements_in_batch"]))
        # add reflectance column to dataframe
        for w in wavelengths:
            df["reflectances", w] = np.NAN

        # Generate MCI file which contains list of all simulations in a Batch
        for i in range(df.shape[0]):
            # set the desired element in the dataframe to be simulated
            base_mco_filename = _create_mco_filename_for(simulated_data_folder,
                                                         self.batch_nr,
                                                         i)
            tissue_model.set_base_mco_filename(base_mco_filename)
            tissue_model.set_tissue_instance(df.loc[i, :])
            tissue_model.update_mci_file(wavelengths)

        # Run simulations for computing reflectance from parameters
        sim_wrapper.run_simulation()

        # get information from created mco files
        for i in range(df.shape[0]):
            for wavelength in wavelengths:
                # for simulation get which mco file was created
                simulation_path = os.path.split(sim_wrapper.mcml_executable)[0]
                base_mco_filename = _create_mco_filename_for(simulated_data_folder,
                                                             self.batch_nr,
                                                             i)
                mco_filename = base_mco_filename + str(wavelength) + '.mco'
                # get diffuse reflectance from simulation
                df["reflectances", wavelength][i] = \
                    get_diffuse_reflectance(os.path.join(simulation_path, mco_filename))
                # delete created mco file
                os.remove(os.path.join(simulation_path, mco_filename))

        f = open(self.output().path, 'w')
        df.to_csv(f)

        end = time.time()
        logging.info("time for creating batch of mc data: %.f s" %
                     (end - start))