예제 #1
0
    def test_mci_wrapper_book_example_2(self):
        """see if our result matches the one from
        Biomedical Optics
        Principles and Imaging
        page 56 (Table 3.2)"""
        # create a book_p56_mci which shall create a mci file
        book_p56_mci = MciWrapper()
        book_p56_mci.set_mci_filename(self.mci_filename)
        book_p56_mci.set_mco_filename(self.mco_filename)
        book_p56_mci.set_nr_photons(10**6)
        book_p56_mci.add_layer(1.5, 1000, 9000, 0., 1)

        mcml_path, mcml_file = os.path.split(path_to_gpumcml)
        if os.path.isfile(path_to_gpumcml):
            book_p56_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_total_reflectance(os.path.join(mcml_path,
                                                      self.mco_filename))
            self.assertAlmostEqual(refl, 0.26, delta=0.01,
                                   msg="correct reflectance determined " +
                                   "according to book table 3.2")
예제 #2
0
 def test_sim_wrapper(self):
     mcml_path, mcml_file = os.path.split(path_to_gpumcml)
     if os.path.isfile(path_to_gpumcml):
         self.mci_wrapper.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(os.path.join(mcml_path,
                                                     self.mco_filename)),
                     "mco file was created")
예제 #3
0
    def run(self):
        start = time.time()
        # setup simulation wrapper
        sim_wrapper = SimWrapper()
        sim_wrapper.set_mci_filename(MCI_FILENAME)
        sim_wrapper.set_mcml_executable(os.path.join(PATH_TO_MCML, EXEC_MCML))
        # setup model
        tissue_model = self.factory.create_tissue_model()
        tissue_model.set_mci_filename(sim_wrapper.mci_filename)
        tissue_model.set_mco_filename(MCO_FILENAME)
        tissue_model.set_nr_photons(NR_PHOTONS)
        # setup array in which data shall be stored
        batch = self.factory.create_batch_to_simulate()
        batch.create_parameters(self.nr_samples)
        # dataframe created by batch:
        df = batch.df
        # add reflectance column to dataframe
        for w in WAVELENGHTS:
            df["reflectances", w] = np.NAN

        # for each instance of our tissue model
        for i in range(df.shape[0]):
            # set the desired element in the dataframe to be simulated
            tissue_model.set_dataframe_row(df.loc[i, :])
            logging.info("running simulation " + str(i) + " for\n" +
                         str(tissue_model))
            reflectances = create_spectrum(tissue_model, sim_wrapper,
                                           WAVELENGHTS)
            # store in dataframe
            for r, w in zip(reflectances, WAVELENGHTS):
                df["reflectances", w][i] = r

        # clean up temporarily created files
        os.remove(MCI_FILENAME)
        created_mco_file = os.path.join(PATH_TO_MCML, MCO_FILENAME)
        if os.path.isfile(created_mco_file):
            os.remove(created_mco_file)
        # save the created output
        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))
    def run(self):
        start = time.time()
        # setup simulation wrapper
        sim_wrapper = SimWrapper()
        sim_wrapper.set_mci_filename(MCI_FILENAME)
        sim_wrapper.set_mcml_executable(os.path.join(PATH_TO_MCML, EXEC_MCML))
        # setup model
        tissue_model = self.factory.create_tissue_model()
        tissue_model.set_mci_filename(sim_wrapper.mci_filename)
        tissue_model.set_mco_filename(MCO_FILENAME)
        tissue_model.set_nr_photons(NR_PHOTONS)
        # setup array in which data shall be stored
        batch = self.factory.create_batch_to_simulate()
        batch.create_parameters(self.nr_samples)
        # dataframe created by batch:
        df = batch.df
        # add reflectance column to dataframe
        for w in WAVELENGHTS:
            df["reflectances", w] = np.NAN

        # for each instance of our tissue model
        for i in range(df.shape[0]):
            # set the desired element in the dataframe to be simulated
            tissue_model.set_dataframe_row(df.loc[i, :])
            logging.info("running simulation " + str(i) + " for\n" +
                         str(tissue_model))
            reflectances = create_spectrum(tissue_model, sim_wrapper,
                                           WAVELENGHTS)
            # store in dataframe
            for r, w in zip(reflectances, WAVELENGHTS):
                df["reflectances", w][i] = r

        # clean up temporarily created files
        os.remove(MCI_FILENAME)
        created_mco_file = os.path.join(PATH_TO_MCML, MCO_FILENAME)
        if os.path.isfile(created_mco_file):
            os.remove(created_mco_file)
        # save the created output
        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))
    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))