Пример #1
0
    def create_presamples(self, name=None, id_=None, overwrite=False, dirpath=None,
                            seed='sequential'):
        """Create a presamples package from generated samples

        Parameters
        -----------
           name: str, optional
               A human-readable name for these samples.
           \id_: str, optional
               Unique id for this collection of presamples. Optional, generated automatically if not set.
           overwrite: bool, default=False
               If True, replace an existing presamples package with the same ``\id_`` if it exists.
           dirpath: str, optional
               An optional directory path where presamples can be created. If None, a subdirectory in the ``project`` folder.
           seed: {None, int, "sequential"}, optional, default="sequential"
               Seed used by indexer to return array columns in random order. Can be an integer, "sequential" or None.
        """
        if not all([self.matrix_samples is not None, self.matrix_indices]):
            warnings.warn("No presamples created because there were no matrix data. "
                      "Make sure to run `add_samples_for_all_acts` or "
                      "`add_samples_for_act` for a set of acts first.")
            return

        id_, dirpath = create_presamples_package(
            matrix_data=split_inventory_presamples(self.matrix_samples, self.matrix_indices),
            name=name, id_=id_, overwrite=overwrite, dirpath=dirpath, seed=seed)
        print("Presamples with id_ {} written at {}".format(id_, dirpath))
        return id_, dirpath
Пример #2
0
 def presamples_from_scenarios(self, name: str, scenarios: Iterable[Tuple[str, Iterable]]) -> (str, str):
     """ When given a iterable of multiple parameter scenarios, construct
     a presamples package with all of the recalculated exchange amounts.
     """
     samples, indices = self.arrays_from_scenarios(scenarios)
     arrays = ps.split_inventory_presamples(samples, indices)
     ps_id, ps_path = ps.create_presamples_package(
         matrix_data=arrays, name=name, seed="sequential"
     )
     return ps_id, ps_path
Пример #3
0
    def presamples_from_scenarios(
            self, name: str,
            scenarios: Iterable[Tuple[str, Iterable]]) -> (str, str):
        """ When given a iterable of multiple parameter scenarios, construct
        a presamples package with all of the recalculated exchange amounts.
        """
        sample_data, indice_data = zip(*(self.recalculate_scenario(values)
                                         for _, values in scenarios))
        samples = np.concatenate(sample_data, axis=1)
        indices = next(iter(indice_data))

        arrays = ps.split_inventory_presamples(samples, indices)
        ps_id, ps_path = ps.create_presamples_package(matrix_data=arrays,
                                                      name=name,
                                                      seed="sequential")
        return ps_id, ps_path
Пример #4
0
def process_arrays_to_package(indices: np.ndarray, values: np.ndarray) -> list:
    """ Follow along `presamples` path of splitting the inventories and
    formatting the exchange indexes into numpy arrays.

    Instead of writing the entire thing to a file, return the list of formatted
    data.
    """
    package = []
    arrays = ps.split_inventory_presamples(values, indices)
    for obj in arrays:
        samples, indices, kind, *other = obj
        samples = to_2d(to_array(samples))
        # Convert the key indexes to the actual db ids
        indices, metadata = format_matrix_data(indices, kind, *other)
        metadata["type"] = kind
        package.append((samples, indices, metadata))
    return package