def set_up_lci_calculations(activity_list, result_dir, worker_id,
                            database_name, samples_batch, project_name):
    """Dispatch LCI calculation for a list of activities

    Parameters
    --------------
    activity_list : list
        List of codes to activities for which LCI arrays should be calculated
    result_dir : str
        Path to directory where results are stored
    worker_id : int
        Identification of the worker if using MultiProcessing, used only for
        error messages.
    database_name : str
        Name of the LCI database
    samples_batch : int
        Integer id for sample batch. Used for campaigns names and for
        generating a seed for the RNG. The maximum value is 14.
    project_name : str
        Name of the brightway2 project where the database is imported

    Returns
    ---------
    None
    """
    projects.set_current(_check_project(project_name))
    g_samples_dir = _get_lci_dir(result_dir,
                                 'probabilistic',
                                 samples_batch,
                                 must_exist=False)
    g_samples_dir_temp = g_samples_dir / "temp"
    g_samples_dir_temp.mkdir(exist_ok=True, parents=True)

    ref_bio_dict = get_ref_bio_dict_from_common_files(
        Path(result_dir) / "common_files")
    campaign = _get_campaign(str(samples_batch), expect_base_presamples=True)
    presample_paths = [p for p in campaign]
    assert presample_paths
    pps = [presamples.PresamplesPackage(p) for p in presample_paths]
    iterations = list(set([pp.ncols for pp in pps]))
    assert len(iterations) == 1
    total_iterations = iterations[0]
    print("Worker ID {}, requested iterations: {}".format(
        worker_id, total_iterations))

    g_dimensions = len(ref_bio_dict)
    times = []
    for act_i, act_code in enumerate(activity_list):
        t0 = time.time()
        try:
            calculate_lci_array(database_name, act_code, presample_paths,
                                g_dimensions, total_iterations, g_samples_dir)
            times.append(time.time() - t0)
        except Exception as err:
            print("************Failure for {} by {}: {}************".format(
                act_code, worker_id, err))
    print("Worker ID: {}\n\tTotal of {} LCIs of {} iterations"
          "\n\tTotal time: {} minutes\n\tAverage time: {} minutes per LCI".
          format(worker_id, len(activity_list), total_iterations,
                 sum(times) / 60, (sum(times) / len(times)) / 60))
예제 #2
0
    def __init__(self, cs_name: str, ps_name: str):
        self.package = ps.PresamplesPackage(get_package_path(ps_name))
        self.resource = ps.PresampleResource.get_or_none(
            name=self.package.name)
        if not self.package:
            raise ValueError(
                "Presamples package with name or id '{}' not found.".format(
                    ps_name))
        super().__init__(cs_name)
        self.total = self.package.ncols
        self._current_index = 0

        # Construct an index dictionary similar to fu_index and method_index
        self.presamples_index = {
            k: i
            for i, k in enumerate(self.scenario_names)
        }

        # Rebuild numpy arrays with presample dimension included.
        self.lca_scores = np.zeros(
            (len(self.func_units), len(self.methods), self.total))
        self.elementary_flow_contributions = np.zeros(
            (len(self.func_units), len(self.methods), self.total,
             self.lca.biosphere_matrix.shape[0]))
        self.process_contributions = np.zeros(
            (len(self.func_units), len(self.methods), self.total,
             self.lca.technosphere_matrix.shape[0]))