Exemplo n.º 1
0
    def XXXperform_one_simulation(self, key):
        # method uses the old code as used in abc_coverage
        #def get_partiality_response(key,one_index,spectra_simulation,ROI,rand_ori,tophat_spectrum=True):

        spectra = self.allspectra
        from LS49.sim.step5_pad import microcrystal
        crystal = microcrystal(
            Deff_A=4000, length_um=4.,
            beam_diameter_um=1.0)  # assume smaller than 10 um crystals

        iterator = spectra.generate_recast_renormalized_image(image=key,
                                                              energy=7120.,
                                                              total_flux=1e12)

        file_prefix = "key_slow_nonoise_%06d" % key
        Amatrix_rot = self.models4["Amat"]
        from LS49.work2_for_aca_lsq.util_partiality import run_sim2smv
        pixels = run_sim2smv(self.ROI,
                             prefix=file_prefix,
                             crystal=crystal,
                             spectra=iterator,
                             rotation=Amatrix_rot,
                             tophat_spectrum=False,
                             quick=False,
                             rank=0)
        return pixels["roi_pixels"]
Exemplo n.º 2
0
def get_partiality_response(key,
                            one_index,
                            spectra_simulation,
                            ROI,
                            rand_ori,
                            tophat_spectrum=True):

    spectra = spectra_simulation
    crystal = microcrystal(
        Deff_A=4000, length_um=4.,
        beam_diameter_um=1.0)  # assume smaller than 10 um crystals

    iterator = spectra.generate_recast_renormalized_image(image=key,
                                                          energy=7120.,
                                                          total_flux=1e12)

    file_prefix = "key_slow_nonoise_%06d" % key

    pixels = run_sim2smv(ROI,
                         prefix=file_prefix,
                         crystal=crystal,
                         spectra=iterator,
                         rotation=rand_ori,
                         tophat_spectrum=tophat_spectrum,
                         quick=False,
                         rank=0)
    return pixels
Exemplo n.º 3
0
def get_partiality_response(key, one_index, spectra_simulation, ROI):

    N_total = 100000  # number of items to simulate
    spectra = spectra_simulation
    crystal = microcrystal(
        Deff_A=4000, length_um=4.,
        beam_diameter_um=1.0)  # assume smaller than 10 um crystals
    mt = flex.mersenne_twister(seed=0)
    random_orientations = []
    for iteration in range(N_total):
        random_orientations.append(mt.random_double_r3_rotation_matrix())

    iterator = spectra.generate_recast_renormalized_image(image=key,
                                                          energy=7120.,
                                                          total_flux=1e12)

    file_prefix = "key_slow_nonoise_%06d" % key
    rand_ori = sqr(random_orientations[key])

    pixels = run_sim2smv(ROI,
                         prefix=file_prefix,
                         crystal=crystal,
                         spectra=iterator,
                         rotation=rand_ori,
                         quick=False,
                         rank=0)
    return pixels
Exemplo n.º 4
0
def get_partiality_response(key,one_index,spectra_simulation,ROI):

  N_total = 100000 # number of items to simulate
  spectra = spectra_simulation
  crystal = microcrystal(Deff_A = 4000, length_um = 4., beam_diameter_um = 1.0) # assume smaller than 10 um crystals
  from LS49 import legacy_random_orientations
  random_orientations = legacy_random_orientations(N_total)

  iterator = spectra.generate_recast_renormalized_image(image=key,energy=7120.,total_flux=1e12)

  file_prefix = "key_slow_nonoise_%06d"%key
  rand_ori = sqr(random_orientations[key])

  pixels = run_sim2smv(ROI,prefix = file_prefix,crystal = crystal,spectra=iterator,rotation=rand_ori,quick=False,rank=0)
  return pixels
Exemplo n.º 5
0
 workaround_nt = int(os.environ.get("OMP_NUM_THREADS", 1))
 omptbx.omp_set_num_threads(workaround_nt)
 N_total = 100000  # number of items to simulate
 N_stride = size  # total number of worker tasks
 print("hello from rank %d of %d" % (rank, size), "with omp_threads=",
       omp_get_num_procs())
 import datetime
 start_elapse = time()
 if rank == 0:
     print("Rank 0 time", datetime.datetime.now())
     from LS49.spectra.generate_spectra import spectra_simulation
     from LS49.sim.step5_pad import microcrystal
     print("hello2 from rank %d of %d" % (rank, size))
     SS = spectra_simulation()
     C = microcrystal(
         Deff_A=4000, length_um=4.,
         beam_diameter_um=1.0)  # assume smaller than 10 um crystals
     mt = flex.mersenne_twister(seed=0)
     random_orientations = []
     for iteration in range(N_total):
         random_orientations.append(mt.random_double_r3_rotation_matrix())
     transmitted_info = dict(spectra=SS,
                             crystal=C,
                             random_orientations=random_orientations)
 else:
     transmitted_info = None
 transmitted_info = comm.bcast(transmitted_info, root=0)
 comm.barrier()
 parcels = list(range(rank, N_total, N_stride))
 while len(parcels) > 0:
     import random
Exemplo n.º 6
0
    def run(self):

        self.parse_input()

        N_total = 100000  # self.params.N_total # number of items to simulate, nominally 100000
        logical_rank = self.mpi_helper.rank
        logical_size = self.mpi_helper.size
        if self.mpi_helper.rank == 0 and self.mpi_helper.size == 1:  # special case of testing it
            try:
                logical_rank = self.params.tester.rank
                logical_size = self.params.tester.size
            except Exception:
                pass
        N_stride = int(math.ceil(
            N_total / logical_size))  # total number of tasks per rank
        print("hello from rank %d of %d with stride %d" %
              (logical_rank, logical_size, N_stride))

        #from scitbx.lbfgs.tst_mpi_split_evaluator import mpi_split_evaluator_run
        #from scitbx.lbfgs.tst_mpi_split_evaluator import run_mpi as simple_tester
        #simple_tester()

        if self.params.starting_model.algorithm == "to_file":
            if self.mpi_helper.rank == 0:
                HKL_lookup, static_fcalcs = get_static_fcalcs_with_HKL_lookup()
                from LS49.work2_for_aca_lsq.remake_range_intensities_with_complex \
                   import get_intensity_structure
                model_intensities = get_intensity_structure(
                    static_fcalcs,
                    FE1_model=Fe_oxidized_model,
                    FE2_model=Fe_reduced_model)
                with (open(self.params.starting_model.filename, "wb")) as out:
                    pickle.dump(HKL_lookup, out, pickle.HIGHEST_PROTOCOL)
                    pickle.dump(static_fcalcs, out, pickle.HIGHEST_PROTOCOL)
                    pickle.dump(model_intensities, out,
                                pickle.HIGHEST_PROTOCOL)
            return
        else:
            if self.mpi_helper.rank == 0:
                with (open(self.params.starting_model.filename, "rb")) as inp:
                    print("the starting model (used for channel weighting) is",
                          self.params.starting_model.filename)
                    HKL_lookup = pickle.load(inp)
                    static_fcalcs = pickle.load(inp)
                    model_intensities = pickle.load(inp)
                    from LS49.spectra.generate_spectra import spectra_simulation
                    from LS49.sim.step5_pad import microcrystal

                shuffA = list(range(N_total))
                import random
                random.shuffle(shuffA)

                transmitted_info = dict(
                    HKL_lookup=HKL_lookup,
                    static_fcalcs=static_fcalcs,
                    model_intensities=model_intensities,
                    spectra_simulation=spectra_simulation(),
                    crystal=microcrystal(Deff_A=4000,
                                         length_um=4.,
                                         beam_diameter_um=1.0),
                    shuffA=shuffA)
            else:
                transmitted_info = None
        print("before braodcast with ", self.mpi_helper.rank,
              self.mpi_helper.size)
        transmitted_info = self.mpi_helper.comm.bcast(transmitted_info, root=0)
        self.mpi_helper.comm.barrier()
        print("after barrier")

        # -----------------------------------------------------------------------
        if self.mpi_helper.rank == 0:
            print("Finding initial G and abc factors")
        per_rank_items = []
        per_rank_keys = []
        per_rank_G = []
        min_spots = 3
        N_input = 0
        import os, omptbx  # cori workaround, which does not get OMP_NUM_THREADS from environment
        workaround_nt = int(os.environ.get("OMP_NUM_THREADS", 1))
        omptbx.omp_set_num_threads(workaround_nt)
        for item, key in get_items(logical_rank, N_total, N_stride,
                                   transmitted_info["shuffA"],
                                   self.params.cohort):
            N_input += 1
            if len(item) >= min_spots:
                try:
                    FOI = fit_one_image_multispot(
                        key=key,
                        list_of_images=item,
                        HKL_lookup=transmitted_info["HKL_lookup"],
                        model_intensities=transmitted_info[
                            "model_intensities"],
                        spectra=transmitted_info["spectra_simulation"],
                        crystal=transmitted_info["crystal"])
                except RuntimeError as e:
                    # no recovery from LBFGS error, skip event
                    continue
                metric_P1, metric_C2 = FOI.DRM.get_current_angular_offsets(
                    FOI.x[-4:-1])
                print(
                    """LLG Image %06d on %d Bragg spots NLL    channels F = %9.1f angular offsets in P1 and C2 (degrees): %8.5f %8.5f"""
                    %
                    (key, len(item), FOI.compute_functional_and_gradients()[0],
                     metric_P1, metric_C2), FOI.x[-4:-1])

                # reporting out results to new abc_coverage pickles.  Use the old one "item" as a template:
                #    item := [<LS49.work2_for_aca_lsq.abc_background.fit_roi_multichannel>,... one for each spot]
                # each fit_roi has the following attributes and we modify them as follows:
                #        'a', the abcG parameters of the original one-spot fit, unused
                #        'asu_idx_C2_setting', unmodified
                #        'image_no', same as key, unmodified
                #        'n', number of parameters for one-spot fit, unused
                #        'orig_idx_C2_setting', unmodified
                #        'sb_data', original shoebox data [integers as floats], passed along unmodified
                #        'simtbx_P1_miller', unmodified
                #        'simtbx_intensity_7122', unmodified
                #        'x', the abcG parameters of the original one-spot fit, unused
                # modify these:
                #        'bkgrd_a', the spot abc parameters output here, to be passed on to global data fit
                for ispot in range(len(item)):
                    item[ispot].bkgrd_a = FOI.x[3 * ispot:3 * (ispot + 1)]
                    #        'channels', need to save the new data that was set during update_roi_model_pixels_with_current_rotation()
                    #                    but only for the Amat, not the derivatives.
                    item[ispot].channels = FOI.new_calc2_dict_last_round[
                        ispot]["channels"]
                    #        'roi', get new region of interest summation that was set during update_roi_model_pixels_with_current_rotation()
                    item[ispot].roi = FOI.roi_model_pixels[ispot]

                # put the newly refined background model back into the item
                per_rank_keys.append(key)
                per_rank_G.append(FOI.a[-1])

                print(
                    "pickling modified abc_coverage file for key %d in rank %d"
                    % (key, logical_rank), )
                with open(abc_glob_pixel_ref % (key), "wb") as F:
                    pickle.dump(item, F, pickle.HIGHEST_PROTOCOL)

        print("rank %d has %d refined images" %
              (logical_rank, len(per_rank_keys)))

        N_ranks = self.mpi_helper.comm.reduce(1, self.mpi_helper.MPI.SUM, 0)
        N_refined_images = self.mpi_helper.comm.reduce(len(per_rank_keys),
                                                       self.mpi_helper.MPI.SUM,
                                                       0)
        N_input_images = self.mpi_helper.comm.reduce(N_input,
                                                     self.mpi_helper.MPI.SUM,
                                                     0)
        self.mpi_helper.comm.barrier()
        if self.mpi_helper.rank == 0:
            print("final report %d ranks, %d input images, %d refined models" %
                  (N_ranks, N_input_images, N_refined_images))
            print("Finished finding initial G and abc factors")

        self.mpi_helper.comm.barrier()