예제 #1
0
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):
        """
        Get seismic likelihood formula for the model built. Has to be called
        within a with model context.

        Parameters
        ----------
        input_rvs : list
            of :class:`pymc3.distribution.Distribution`
        fixed_rvs : dict
            of :class:`numpy.array`
        hyperparams : dict
            of :class:`pymc3.distribution.Distribution`
        problem_config : :class:`config.ProblemConfig`

        Returns
        -------
        posterior_llk : :class:`theano.tensor.Tensor`
        """
        hp_specific = problem_config.dataset_specific_residual_noise_estimation

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs

        logger.info('Seismic optimization on: \n '
                    ' %s' % ', '.join(self.input_rvs.keys()))

        t2 = time()
        wlogpts = []

        self.init_hierarchicals(problem_config)
        if self.config.station_corrections:
            logger.info('Initialized %i hierarchical parameters for '
                        'station corrections.' %
                        len(self.get_unique_stations()))

        for wmap in self.wavemaps:
            synths, tmins = self.synthesizers[wmap.name](self.input_rvs)

            if len(self.hierarchicals) > 0:
                tmins += self.hierarchicals[self.correction_name][
                    wmap.station_correction_idxs]

            data_trcs = self.choppers[wmap.name](tmins)
            residuals = data_trcs - synths

            logpts = multivariate_normal_chol(wmap.datasets,
                                              wmap.weights,
                                              hyperparams,
                                              residuals,
                                              hp_specific=hp_specific)

            wlogpts.append(logpts)

        t3 = time()
        logger.debug('Teleseismic forward model on test model takes: %f' %
                     (t3 - t2))

        llk = Deterministic(self._like_name, tt.concatenate((wlogpts)))
        return llk.sum()
예제 #2
0
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):
        """
        Get geodetic likelihood formula for the model built. Has to be called
        within a with model context.
        Part of the pymc3 model.

        Parameters
        ----------
        input_rvs : dict
            of :class:`pymc3.distribution.Distribution`
        fixed_rvs : dict
            of :class:`numpy.array`
        hyperparams : dict
            of :class:`pymc3.distribution.Distribution`
        problem_config : :class:`config.ProblemConfig`

        Returns
        -------
        posterior_llk : :class:`theano.tensor.Tensor`
        """
        hp_specific = self.config.dataset_specific_residual_noise_estimation

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs

        logger.info('Geodetic optimization on: \n '
                    '%s' % ', '.join(self.input_rvs.keys()))

        self.input_rvs.update(fixed_rvs)

        t0 = time()
        disp = self.get_synths(self.input_rvs)
        t1 = time()
        logger.debug('Geodetic forward model on test model takes: %f' %
                     (t1 - t0))

        los_disp = (disp * self.slos_vectors).sum(axis=1)

        residuals = self.Bij.srmap(
            tt.cast((self.sdata - los_disp) * self.sodws, tconfig.floatX))

        self.init_hierarchicals(problem_config)
        if self.config.corrections_config.has_enabled_corrections:
            logger.info('Applying corrections! ...')
            residuals = self.apply_corrections(residuals, operation='-')

        logpts = multivariate_normal_chol(self.datasets,
                                          self.weights,
                                          hyperparams,
                                          residuals,
                                          hp_specific=hp_specific)

        llk = Deterministic(self._like_name, logpts)
        return llk.sum()
예제 #3
0
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):
        """
        Formulation of the distribution problem for the model built. Has to be
        called within a with-model-context.

        Parameters
        ----------
        input_rvs : list
            of :class:`pymc3.distribution.Distribution`
        hyperparams : dict
            of :class:`pymc3.distribution.Distribution`

        Returns
        -------
        llk : :class:`theano.tensor.Tensor`
            log-likelihood for the distributed slip
        """
        logger.info("Loading %s Green's Functions" % self.name)
        self.load_gfs(crust_inds=[self.config.gf_config.reference_model_idx],
                      make_shared=True)

        hp_specific = self.config.dataset_specific_residual_noise_estimation

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs
        ref_idx = self.config.gf_config.reference_model_idx

        mu = tt.zeros((self.Bij.ordering.size), tconfig.floatX)
        for var in self.slip_varnames:
            key = self.get_gflibrary_key(crust_ind=ref_idx,
                                         wavename='static',
                                         component=var)
            mu += self.gfs[key].stack_all(slips=input_rvs[var])

        residuals = self.Bij.srmap(
            tt.cast((self.sdata - mu) * self.sodws, tconfig.floatX))

        self.init_hierarchicals(problem_config)
        if len(self.hierarchicals) > 0:
            residuals = self.remove_ramps(residuals)

        logpts = multivariate_normal_chol(self.datasets,
                                          self.weights,
                                          hyperparams,
                                          residuals,
                                          hp_specific=hp_specific)

        llk = Deterministic(self._like_name, logpts)

        return llk.sum()
예제 #4
0
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):
        """
        Get seismic likelihood formula for the model built. Has to be called
        within a with model context.

        Parameters
        ----------
        input_rvs : list
            of :class:`pymc3.distribution.Distribution` of source parameters
        fixed_rvs : dict
            of :class:`numpy.array`
        hyperparams : dict
            of :class:`pymc3.distribution.Distribution`
        problem_config : :class:`config.ProblemConfig`

        Returns
        -------
        posterior_llk : :class:`theano.tensor.Tensor`
        """
        chop_bounds = ['b', 'c']  # we want llk calculation only between b c

        hp_specific = self.config.dataset_specific_residual_noise_estimation
        tpoint = problem_config.get_test_point()

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs

        logger.info('Seismic optimization on: \n '
                    ' %s' % ', '.join(self.input_rvs.keys()))

        self.input_rvs.update(fixed_rvs)

        t2 = time()
        wlogpts = []

        self.init_hierarchicals(problem_config)
        self.analyse_noise(tpoint, chop_bounds=chop_bounds)
        self.init_weights()
        if self.config.station_corrections:
            logger.info('Initialized %i hierarchical parameters for '
                        'station corrections.' %
                        len(self.get_all_station_names()))

        for wmap in self.wavemaps:
            if len(self.hierarchicals) > 0:
                time_shifts = self.hierarchicals[wmap.time_shifts_id][
                    wmap.station_correction_idxs]
                self.input_rvs[self.correction_name] = time_shifts

            wc = wmap.config

            logger.info('Preparing data of "%s" for optimization' %
                        wmap._mapid)
            wmap.prepare_data(source=self.events[wc.event_idx],
                              engine=self.engine,
                              outmode='array',
                              chop_bounds=chop_bounds)

            logger.info('Initializing synthesizer for "%s"' % wmap._mapid)

            if self.nevents == 1:
                logger.info('Using all sources for wavemap %s !' % wmap._mapid)
                sources = self.sources
            else:
                logger.info('Using source based on event %i for wavemap %s!' %
                            (wc.event_idx, wmap._mapid))
                sources = [self.sources[wc.event_idx]]

            self.synthesizers[wmap._mapid] = theanof.SeisSynthesizer(
                engine=self.engine,
                sources=sources,
                targets=wmap.targets,
                event=self.events[wc.event_idx],
                arrival_taper=wc.arrival_taper,
                arrival_times=wmap._arrival_times,
                wavename=wmap.name,
                filterer=wc.filterer,
                pre_stack_cut=self.config.pre_stack_cut,
                station_corrections=self.config.station_corrections)

            synths, _ = self.synthesizers[wmap._mapid](self.input_rvs)

            residuals = wmap.shared_data_array - synths

            logpts = multivariate_normal_chol(wmap.datasets,
                                              wmap.weights,
                                              hyperparams,
                                              residuals,
                                              hp_specific=hp_specific)

            wlogpts.append(logpts)

        t3 = time()
        logger.debug('Teleseismic forward model on test model takes: %f' %
                     (t3 - t2))

        llk = Deterministic(self._like_name, tt.concatenate((wlogpts)))
        return llk.sum()
예제 #5
0
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):

        # no a, d taper bounds as GF library saved between b c
        chop_bounds = ['b', 'c']

        logger.info("Loading %s Green's Functions" % self.name)
        self.load_gfs(crust_inds=[self.config.gf_config.reference_model_idx],
                      make_shared=False)

        hp_specific = self.config.dataset_specific_residual_noise_estimation
        tpoint = problem_config.get_test_point()

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs

        logger.info('Seismic optimization on: \n '
                    ' %s' % ', '.join(self.input_rvs.keys()))

        t2 = time()
        wlogpts = []

        self.analyse_noise(tpoint, chop_bounds=chop_bounds)
        for gfs in self.gfs.values():
            gfs.init_optimization()

        self.init_weights()
        self.init_hierarchicals(problem_config)
        if self.config.station_corrections:
            logger.info('Initialized %i hierarchical parameters for '
                        'station corrections.' %
                        len(self.get_all_station_names()))

        self.input_rvs.update(fixed_rvs)

        ref_idx = self.config.gf_config.reference_model_idx

        nuc_strike = input_rvs['nucleation_strike']
        nuc_dip = input_rvs['nucleation_dip']

        t2 = time()
        # convert velocities to rupture onset
        logger.debug('Fast sweeping ...')
        starttimes0 = tt.zeros((self.fault.npatches), dtype=tconfig.floatX)
        for index in range(self.fault.nsubfaults):
            nuc_dip_idx, nuc_strike_idx = self.fault.fault_locations2idxs(
                index=index,
                positions_dip=nuc_dip[index],
                positions_strike=nuc_strike[index],
                backend='theano')

            sf_patch_indexs = self.fault.cum_subfault_npatches[index:index + 2]
            starttimes_tmp = self.sweepers[index](
                (1. /
                 self.fault.vector2subfault(index, input_rvs['velocities'])),
                nuc_dip_idx, nuc_strike_idx)

            starttimes_tmp += input_rvs['time'][index]
            starttimes0 = tt.set_subtensor(
                starttimes0[sf_patch_indexs[0]:sf_patch_indexs[1]],
                starttimes_tmp)

        wlogpts = []
        for wmap in self.wavemaps:
            wc = wmap.config
            # station corrections
            if len(self.hierarchicals) > 0:
                logger.info('Applying station corrections ...')
                starttimes = (tt.tile(starttimes0, wmap.n_t) - tt.repeat(
                    self.hierarchicals[wmap.time_shifts_id][
                        wmap.station_correction_idxs],
                    self.fault.npatches)).reshape(
                        (wmap.n_t, self.fault.npatches))
            else:
                logger.info('No station corrections ...')
                starttimes = tt.tile(starttimes0, wmap.n_t).reshape(
                    (wmap.n_t, self.fault.npatches))

            targetidxs = shared(num.atleast_2d(num.arange(wmap.n_t)).T,
                                borrow=True)
            logger.debug('Stacking %s phase ...' % wc.name)
            synthetics = tt.zeros(
                (wmap.n_t,
                 wc.arrival_taper.nsamples(self.config.gf_config.sample_rate)),
                dtype=tconfig.floatX)

            # make sure data is init as array, if non-toeplitz above-traces!
            wmap.prepare_data(source=self.events[wc.event_idx],
                              engine=self.engine,
                              outmode='array',
                              chop_bounds=chop_bounds)

            for var in self.slip_varnames:
                logger.debug('Stacking %s variable' % var)
                key = self.get_gflibrary_key(crust_ind=ref_idx,
                                             wavename=wmap.name,
                                             component=var)
                synthetics += self.gfs[key].stack_all(
                    targetidxs=targetidxs,
                    starttimes=starttimes,
                    durations=input_rvs['durations'],
                    slips=input_rvs[var],
                    interpolation=wc.interpolation)

            residuals = wmap.shared_data_array - synthetics

            logger.debug('Calculating likelihoods ...')
            logpts = multivariate_normal_chol(wmap.datasets,
                                              wmap.weights,
                                              hyperparams,
                                              residuals,
                                              hp_specific=hp_specific)

            wlogpts.append(logpts)

        t3 = time()
        logger.debug('Seismic formula on test model takes: %f' % (t3 - t2))

        llk = Deterministic(self._like_name, tt.concatenate((wlogpts)))
        return llk.sum()
예제 #6
0
파일: seismic.py 프로젝트: braunfuss/beat
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):

        hp_specific = problem_config.dataset_specific_residual_noise_estimation
        tpoint = problem_config.get_test_point()

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs

        logger.info(
            'Seismic optimization on: \n '
            ' %s' % ', '.join(self.input_rvs.keys()))

        t2 = time()
        wlogpts = []

        self.analyse_noise(tpoint)
        self.init_weights()
        self.init_hierarchicals(problem_config)
        if self.config.station_corrections:
            logger.info(
                'Initialized %i hierarchical parameters for '
                'station corrections.' % len(self.get_unique_stations()))

        self.input_rvs.update(fixed_rvs)

        ref_idx = self.config.gf_config.reference_model_idx

        nuc_strike = input_rvs['nucleation_strike']
        nuc_dip = input_rvs['nucleation_dip']

        t2 = time()
        # convert velocities to rupture onset
        logger.debug('Fast sweeping ...')

        nuc_dip_idx, nuc_strike_idx = self.fault.fault_locations2idxs(
            positions_dip=nuc_dip,
            positions_strike=nuc_strike,
            backend='theano')

        starttimes0 = self.sweeper(
            (1. / input_rvs['velocities']), nuc_dip_idx, nuc_strike_idx)

        starttimes0 += input_rvs['nucleation_time']
        wlogpts = []
        for wmap in self.wavemaps:
            # station corrections
            if len(self.hierarchicals) > 0:
                raise NotImplementedError(
                    'Station corrections not fully implemented! for ffi!')
                starttimes = (
                    tt.tile(starttimes0, wmap.n_t) +
                    tt.repeat(self.hierarchicals[self.correction_name][
                        wmap.station_correction_idxs],
                        self.fault.npatches)).reshape(
                            wmap.n_t, self.fault.npatches)

                targetidxs = shared(
                    num.atleast_2d(num.arange(wmap.n_t)).T, borrow=True)
            else:
                starttimes = starttimes0
                targetidxs = shared(num.lib.index_tricks.s_[:], borrow=True)

            logger.debug('Stacking %s phase ...' % wmap.config.name)
            synthetics = tt.zeros(
                (wmap.n_t, wmap.config.arrival_taper.nsamples(
                    self.config.gf_config.sample_rate)),
                dtype=tconfig.floatX)

            for var in self.slip_varnames:
                logger.debug('Stacking %s variable' % var)
                key = self.get_gflibrary_key(
                    crust_ind=ref_idx, wavename=wmap.name, component=var)
                synthetics += self.gfs[key].stack_all(
                    targetidxs=targetidxs,
                    starttimes=starttimes,
                    durations=input_rvs['durations'],
                    slips=input_rvs[var],
                    interpolation=wmap.config.interpolation)

            residuals = wmap.shared_data_array - synthetics

            logger.debug('Calculating likelihoods ...')
            logpts = multivariate_normal_chol(
                wmap.datasets, wmap.weights, hyperparams, residuals,
                hp_specific=hp_specific)

            wlogpts.append(logpts)

        t3 = time()
        logger.debug(
            'Seismic formula on test model takes: %f' % (t3 - t2))

        llk = Deterministic(self._like_name, tt.concatenate((wlogpts)))
        return llk.sum()
예제 #7
0
    def get_formula(self, input_rvs, fixed_rvs, hyperparams, problem_config):

        hp_specific = problem_config.dataset_specific_residual_noise_estimation

        self.input_rvs = input_rvs
        self.fixed_rvs = fixed_rvs

        logger.info('Seismic optimization on: \n '
                    ' %s' % ', '.join(self.input_rvs.keys()))

        t2 = time()
        wlogpts = []

        self.init_hierarchicals(problem_config)
        if self.config.station_corrections:
            logger.info('Initialized %i hierarchical parameters for '
                        'station corrections.' %
                        len(self.get_unique_stations()))

        self.input_rvs.update(fixed_rvs)

        ref_idx = self.config.gf_config.reference_model_idx

        nuc_strike = input_rvs['nucleation_strike']
        nuc_dip = input_rvs['nucleation_dip']

        t2 = time()
        # convert velocities to rupture onset
        logger.debug('Fast sweeping ...')

        nuc_dip_idx, nuc_strike_idx = self.fault.fault_locations2idxs(
            positions_dip=nuc_dip,
            positions_strike=nuc_strike,
            backend='theano')

        starttimes = self.sweeper((1. / input_rvs['velocities']), nuc_dip_idx,
                                  nuc_strike_idx)

        wlogpts = []
        for wmap in self.wavemaps:
            logger.debug('Stacking %s phase ...' % wmap.config.name)
            synthetics = tt.zeros((wmap.n_t,
                                   wmap.config.arrival_taper.nsamples(
                                       self.config.gf_config.sample_rate)),
                                  dtype=tconfig.floatX)

            for var in self.slip_varnames:
                logger.debug('Stacking %s variable' % var)
                key = self.get_gflibrary_key(crust_ind=ref_idx,
                                             wavename=wmap.name,
                                             component=var)
                synthetics += self.gfs[key].stack_all(
                    starttimes=starttimes,
                    durations=input_rvs['durations'],
                    slips=input_rvs[var],
                    interpolation=wmap.config.interpolation)

            logger.debug('Get hypocenter location ...')
            patchidx = self.fault.spatchmap(0,
                                            dipidx=nuc_dip_idx,
                                            strikeidx=nuc_strike_idx)

            # cut data according to wavemaps
            logger.debug('Cut data accordingly')

            tmins = self.gfs[key].get_all_tmins(
                patchidx).ravel() + input_rvs['nucleation_time']

            # add station corrections
            if len(self.hierarchicals) > 0:
                tmins += self.hierarchicals[self.correction_name][
                    wmap.station_correction_idxs]

            data_traces = self.choppers[wmap.name](tmins)

            residuals = data_traces - synthetics

            logger.debug('Calculating likelihoods ...')
            logpts = multivariate_normal_chol(wmap.datasets,
                                              wmap.weights,
                                              hyperparams,
                                              residuals,
                                              hp_specific=hp_specific)

            wlogpts.append(logpts)

        t3 = time()
        logger.debug('Seismic formula on test model takes: %f' % (t3 - t2))

        llk = Deterministic(self._like_name, tt.concatenate((wlogpts)))
        return llk.sum()