Esempio n. 1
0
def upgrade_config_file(fn, diff=True):
    rules = [
        ('grond.HighScoreOptimizerConfig',
            rename_class('grond.HighScoreOptimiserConfig')),
        ('grond.Config',
            rename_attribute('optimizer_config', 'optimiser_config')),
        ('grond.CMTProblemConfig',
            drop_attribute('apply_balancing_weights')),
        ('grond.DoubleDCProblemConfig',
            drop_attribute('apply_balancing_weights')),
        ('grond.RectangularProblemConfig',
            drop_attribute('apply_balancing_weights')),
        ('grond.Config',
            drop_attribute('analyser_config')),
        ('grond.Config',
            set_attribute(
                'analyser_configs',
                aguts.load(string='''
                    - !grond.TargetBalancingAnalyserConfig
                      niterations: 1000
                    '''))),

    ]

    def apply_rules(path, obj):
        for tagname, func in rules:
            if obj._tagname == tagname:
                func(path, obj)

    t1 = aguts.load(filename=fn)
    t2 = copy.deepcopy(t1)

    aguts.apply_tree(t2, apply_rules)

    s1 = aguts.dump(t1)
    s2 = aguts.dump(t2)

    if diff:
        result = list(difflib.unified_diff(
            s1.splitlines(1), s2.splitlines(1),
            'normalized old', 'normalized new'))

        if sys.stdout.isatty():
            sys.stdout.writelines(color_diff(result))
        else:
            sys.stdout.writelines(result)
    else:
        print(aguts.dump(t2, header=True))
Esempio n. 2
0
def diff_configs(path1, path2):
    import sys
    import difflib
    from pyrocko import guts_agnostic as aguts

    t1 = aguts.load(filename=path1)
    t2 = aguts.load(filename=path2)

    s1 = aguts.dump(t1)
    s2 = aguts.dump(t2)

    result = list(
        difflib.unified_diff(s1.splitlines(1), s2.splitlines(1), 'left',
                             'right'))

    if sys.stdout.isatty():
        sys.stdout.writelines(color_diff(result))
    else:
        sys.stdout.writelines(result)
Esempio n. 3
0
def upgrade_config_file(fn, diff=True, update=[]):
    rules = [
        ('beat.SeismicConfig', drop_attribute('blacklist')),
        ('beat.SeismicConfig', drop_attribute('calc_data_cov')),
        ('beat.SeismicConfig',
         set_attribute(
             'noise_estimator',
             aguts.load(string='''!beat.SeismicNoiseAnalyserConfig
                      structure: variance
                      pre_arrival_time: 5
                    '''), False)),
        ('beat.ProblemConfig',
         drop_attribute('dataset_specific_residual_noise_estimation')),
        ('beat.ProblemConfig', set_attribute('mode', 'ffi', 'ffo')),
        ('beat.WaveformFitConfig', set_attribute('preprocess_data', True,
                                                 True)),
        ('beat.WaveformFitConfig',
         set_attribute(
             'filterer',
             aguts.load(string='''
                    - !beat.heart.Filter
                      lower_corner: 0.01
                      upper_corner: 0.1
                      order: 4
                      '''))),
        ('beat.MetropolisConfig', drop_attribute('n_stages')),
        ('beat.MetropolisConfig', drop_attribute('stage')),
        ('beat.ParallelTemperingConfig', set_attribute('resample', False,
                                                       False)),
        ('beat.FFOConfig', rename_class('beat.FFIConfig')),
        ('beat.GeodeticLinearGFConfig', drop_attribute('extension_widths')),
        ('beat.GeodeticLinearGFConfig', drop_attribute('extension_lengths')),
        ('beat.GeodeticLinearGFConfig', drop_attribute('patch_widths')),
        ('beat.GeodeticLinearGFConfig', drop_attribute('patch_lengths')),
        ('beat.SeismicLinearGFConfig', drop_attribute('extension_widths')),
        ('beat.SeismicLinearGFConfig', drop_attribute('extension_lengths')),
        ('beat.SeismicLinearGFConfig', drop_attribute('patch_widths')),
        ('beat.SeismicLinearGFConfig', drop_attribute('patch_lengths')),
        ('beat.GeodeticConfig', drop_attribute('fit_plane')),
        ('beat.GeodeticConfig', drop_attribute('datadir')),
        ('beat.GeodeticConfig', drop_attribute('names')),
        ('beat.GeodeticConfig', drop_attribute('blacklist')),
        ('beat.GeodeticConfig', drop_attribute('types')),
        ('beat.EulerPoleConfig',
         rename_attribute('blacklist', 'station_blacklist'))
    ]

    def apply_rules(path, obj):
        for tagname, func in rules:
            if obj._tagname == tagname:
                func(path, obj)

    updates_avail = ['hierarchicals', 'hypers', 'structure']

    t1 = aguts.load(filename=fn)
    t2 = copy.deepcopy(t1)

    for upd in update:
        if upd not in updates_avail:
            raise TypeError('Update not available for "%s"' % upd)

    n_upd = len(update)
    if n_upd > 0:
        fn_tmp = fn + 'tmp'
        if 'structure' in update:
            aguts.apply_tree(t2, apply_rules)

        aguts.dump(t2, filename=fn_tmp, header=True)
        t2 = guts.load(filename=fn_tmp)
        if 'hypers' in update:
            t2.update_hypers()

        if 'hierarchicals' in update:
            t2.update_hierarchicals()

        t2.problem_config.validate_priors()
        guts.dump(t2, filename=fn_tmp)
        t2 = aguts.load(filename=fn_tmp)
    else:
        fn_tmp = fn

    s1 = aguts.dump(t1)
    s2 = aguts.dump(t2)

    if diff:
        result = list(
            difflib.unified_diff(s1.splitlines(1), s2.splitlines(1),
                                 'normalized old', 'normalized new'))

        if sys.stdout.isatty():
            sys.stdout.writelines(color_diff(result))
        else:
            sys.stdout.writelines(result)
    else:
        aguts.dump(t2, filename=fn_tmp, header=True)
        upd_config = guts.load(filename=fn_tmp)
        if 'hypers' in update:
            logger.info('Updated hyper parameters! Previous hyper'
                        ' parameter bounds are invalid now!')

        if 'hierarchicals' in update:
            logger.info('Updated hierarchicals.')

        guts.dump(upd_config, filename=fn)

    if n_upd > 0:
        os.remove(fn_tmp)
Esempio n. 4
0
def upgrade_config_file(fn, diff=True, update=[]):
    rules = [
        ('beat.SeismicConfig', drop_attribute('blacklist')),
        ('beat.SeismicConfig', drop_attribute('calc_data_cov')),
        ('beat.SeismicConfig',
         set_attribute(
             'noise_estimator',
             aguts.load(string='''!beat.SeismicNoiseAnalyserConfig
                      structure: identity
                      pre_arrival_time: 5
                    '''))),
        ('beat.WaveformFitConfig', set_attribute('blacklist', [])),
        ('beat.WaveformFitConfig', set_attribute('preprocess_data', True)),
        ('beat.MetropolisConfig', drop_attribute('n_stages')),
        ('beat.MetropolisConfig', drop_attribute('stage')),
        ('beat.ParallelTemperingConfig', set_attribute('resample', False)),
    ]

    def apply_rules(path, obj):
        for tagname, func in rules:
            if obj._tagname == tagname:
                func(path, obj)

    updates_avail = ['hierarchicals', 'hypers', 'structure']

    t1 = aguts.load(filename=fn)
    t2 = copy.deepcopy(t1)

    for upd in update:
        if upd not in updates_avail:
            raise TypeError('Update not available for "%s"' % upd)

    n_upd = len(update)
    if n_upd > 0:
        fn_tmp = fn + 'tmp'
        if 'structure' in update:
            aguts.apply_tree(t2, apply_rules)

        aguts.dump(t2, filename=fn_tmp, header=True)
        t2 = guts.load(filename=fn_tmp)
        if 'hypers' in update:
            t2.update_hypers()

        if 'hierarchicals' in update:
            t2.update_hierarchicals()

        guts.dump(t2, filename=fn_tmp)
        t2 = aguts.load(filename=fn_tmp)
    else:
        fn_tmp = fn

    s1 = aguts.dump(t1)
    s2 = aguts.dump(t2)

    if diff:
        result = list(
            difflib.unified_diff(s1.splitlines(1), s2.splitlines(1),
                                 'normalized old', 'normalized new'))

        if sys.stdout.isatty():
            sys.stdout.writelines(color_diff(result))
        else:
            sys.stdout.writelines(result)
    else:
        aguts.dump(t2, filename=fn_tmp, header=True)
        upd_config = guts.load(filename=fn_tmp)
        if 'hypers' in update:
            logger.info('Updated hyper parameters! Previous hyper'
                        ' parameter bounds are invalid now!')

        if 'hierarchicals' in update:
            logger.info('Updated hierarchicals.')

        guts.dump(upd_config, filename=fn)

    if n_upd > 0:
        os.remove(fn_tmp)