Exemplo n.º 1
0
    def sample(self, model, data, nsamples=None, walker_initial_pos=None):
        if nsamples is not None:
            # deprecated as of 3.3
            from holopy.fitting import fit_warning
            fit_warning('EmceeStrategy(nsamples=X)',
                        'passing nsamples to EmceeStrategy.sample')
            self.nsamples = nsamples
        if walker_initial_pos is not None:
            # deprecated as of 3.3
            from holopy.fitting import fit_warning
            fit_warning('EmceeStrategy(walker_initial_pos=X)',
                        'passing walker_initial_pos to EmceeStrategy.sample')
            self.walker_initial_pos = walker_initial_pos
        time_start = time.time()
        if self.npixels is not None:
            data = make_subset_data(data, pixels=self.npixels, seed=self.seed)
        if self.walker_initial_pos is None:
            self.walker_initial_pos = model.generate_guess(self.nwalkers,
                                                           seed=self.seed)
        sampler = sample_emcee(model=model, data=data, nwalkers=self.nwalkers,
                               walker_initial_pos=self.walker_initial_pos,
                               nsamples=self.nsamples, parallel=self.parallel,
                               seed=self.seed)

        samples = emcee_samples_DataArray(sampler, model._parameters)
        lnprobs = emcee_lnprobs_DataArray(sampler)

        d_time = time.time() - time_start
        kwargs = {'lnprobs': lnprobs, 'samples': samples}
        return SamplingResult(data, model, self, d_time, kwargs)
Exemplo n.º 2
0
 def from_yaml(cls, loader, node):
     node = _purge_ties(node)
     fields = loader.construct_mapping(node, deep=True)
     try:
         parameters = fields['_parameters']
         maps = fields['_maps']
     except KeyError:
         from holopy.fitting import fit_warning
         fit_warning('newly saved model', 'the old one')
         kwargs = fields
         return cls(**kwargs)
     else:
         dummy_scatterer = fields['scatterer']
         scatterer_parameters = read_map(maps['scatterer'], parameters)
         scatterer = dummy_scatterer.from_parameters(scatterer_parameters)
         kwargs = {'scatterer': scatterer, 'theory': fields['theory']}
         kwargs.update(read_map(maps['optics'], parameters))
         if 'model' in maps:
             kwargs.update(read_map(maps['model'], parameters))
         if 'theory' in maps:
             kwargs.update(read_map(maps['theory'], parameters))
     model = cls(**kwargs)
     if model._parameters == parameters:
         model._parameter_names = fields['_parameter_names']
     else:
         msg = ("Detected inconsistencies when reloading Model. "
                "It may differ from previously saved object")
         warnings.warn(msg, UserWarning)
     return model
Exemplo n.º 3
0
def _purge_ties(node):
    '''
    Temporary function to purge ties when reloading Scatterers saved pre-3.4
    '''
    if isinstance(node, yaml.MappingNode):
        value = [(scalar, _purge_ties(mapping))
                 for scalar, mapping in node.value if scalar.value != 'ties']
        if value != node.value:
            from holopy.fitting import fit_warning
            tie_msg = ('. Ignoring previously defined ties. '
                       'Use Model.add_tie() to reassign them')
            fit_warning('newly saved model', 'the old one' + tie_msg)
        node.value = value
    elif isinstance(node, yaml.SequenceNode):
        node.value = [_purge_ties(val) for val in node.value]
    return node
Exemplo n.º 4
0
 def values(self):
     from holopy.fitting import fit_warning
     fit_warning('SamplingResult.intervals', 'SamplingResult.values')
     return self.intervals
Exemplo n.º 5
0
 def MAP(self):
     from holopy.fitting import fit_warning
     fit_warning('SamplingResult.parameters', 'SamplingResult.MAP')
     return self._parameters
Exemplo n.º 6
0
 def output_scatterer(self):
     from holopy.fitting import fit_warning
     fit_warning('FitResult.scatterer', 'SamplingResult.output_scatterer()')
     return self.scatterer
Exemplo n.º 7
0
 def best_fit(self):
     # this method is published in the HoloPy paper
     from holopy.fitting import fit_warning
     fit_warning('FitResult.hologram', 'SamplingResult.best_fit()')
     return self.hologram
Exemplo n.º 8
0
 def sample(self, data, strategy=None):
     from holopy.fitting import fit_warning
     from holopy.inference.interface import validate_strategy
     fit_warning('holopy.sample()', 'model.sample()')
     strategy = validate_strategy(strategy, 'sample')
     return strategy.sample(self, data)