Ejemplo n.º 1
0
def load_trace(directory: str, model=None) -> MultiTrace:
    """Loads a multitrace that has been written to file.

    A the model used for the trace must be passed in, or the command
    must be run in a model context.

    Parameters
    ----------
    directory : str
        Path to a pymc3 serialized trace
    model : pm.Model (optional)
        Model used to create the trace.  Can also be inferred from context

    Returns
    -------
    pm.Multitrace that was saved in the directory
    """
    straces = []
    for subdir in glob.glob(os.path.join(directory, '*')):
        if os.path.isdir(subdir):
            straces.append(SerializeNDArray(subdir).load(model))
    if not straces:
        raise TraceDirectoryError("%s is not a PyMC3 saved chain directory." %
                                  directory)
    return base.MultiTrace(straces)
Ejemplo n.º 2
0
def load_trace(directory: str, model=None) -> MultiTrace:
    """Loads a multitrace that has been written to file.

    A the model used for the trace must be passed in, or the command
    must be run in a model context.

    Parameters
    ----------
    directory: str
        Path to a pymc3 serialized trace
    model: pm.Model (optional)
        Model used to create the trace.  Can also be inferred from context

    Returns
    -------
    pm.Multitrace that was saved in the directory
    """
    warnings.warn(
        "The `load_trace` function will soon be removed."
        "Instead, use `arviz.from_netcdf` to load traces.",
        DeprecationWarning,
    )
    straces = []
    for subdir in glob.glob(os.path.join(directory, "*")):
        if os.path.isdir(subdir):
            straces.append(SerializeNDArray(subdir).load(model))
    if not straces:
        raise TraceDirectoryError("%s is not a PyMC3 saved chain directory." %
                                  directory)
    return base.MultiTrace(straces)
Ejemplo n.º 3
0
    def load(self, model: Model) -> "NDArray":
        """Load the saved ndarray from file"""
        if not os.path.exists(self.samples_path) or not os.path.exists(
                self.metadata_path):
            raise TraceDirectoryError("%s is not a trace directory" %
                                      self.directory)

        new_trace = NDArray(model=model)
        with open(self.metadata_path) as buff:
            metadata = json.load(buff)

        metadata["_stats"] = [{k: np.array(v)
                               for k, v in stat.items()}
                              for stat in metadata["_stats"]]

        # it seems like at least some old traces don't have 'sampler_vars'
        try:
            sampler_vars = metadata.pop("sampler_vars")
            new_trace._set_sampler_vars(sampler_vars)
        except KeyError:
            pass

        for key, value in metadata.items():
            setattr(new_trace, key, value)
        new_trace.samples = dict(np.load(self.samples_path))
        return new_trace
Ejemplo n.º 4
0
    def load(self, model: Model) -> 'NDArray':
        """Load the saved ndarray from file"""
        if not os.path.exists(self.samples_path) or not os.path.exists(self.metadata_path):
            raise TraceDirectoryError("%s is not a trace directory" % self.directory)

        new_trace = NDArray(model=model)
        with open(self.metadata_path, 'r') as buff:
            metadata = json.load(buff)

        metadata['_stats'] = [{k: np.array(v) for k, v in stat.items()} for stat in metadata['_stats']]

        for key, value in metadata.items():
            setattr(new_trace, key, value)
        new_trace.samples = dict(np.load(self.samples_path))
        return new_trace