core.py 35.6 KB
Newer Older
Sebastian Heimann's avatar
Sebastian Heimann committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
                '%-15s %15i %-15s %15i %15i' % (
                    'iteration', iiter+1, '(%s)' % phase,
                    ntries_sample, ntries_preconstrain))

        if 'matrix' in status:
            matrix = (chains_i[:, :30] % 94 + 32).T
            for row in matrix[::-1]:
                lines.append(''.join(chr(xxx) for xxx in row))

        if status:
            lines[0:0] = ['\033[2J']
            lines.append('')
            print '\n'.join(lines)

        iiter += 1


Sebastian Heimann's avatar
Sebastian Heimann committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
def bootstrap_outliers(problem, misfits, std_factor=1.0):
    '''
    Identify bootstrap configurations performing bad in global configuration
    '''

    gms = problem.global_misfits(misfits)

    ibests = []
    for ibootstrap in xrange(problem.nbootstrap):
        bms = problem.bootstrap_misfits(misfits, ibootstrap)
        ibests.append(num.argmin(bms))

    m = num.median(gms[ibests])
    s = num.std(gms[ibests])

    return num.where(gms > m+s)[0]


Sebastian Heimann's avatar
Sebastian Heimann committed
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
def forward(rundir):

    # config = guts.load(filename=op.join('.', 'grond_td.conf'))
    # config.set_basepath('.')

    config = guts.load(filename=op.join(rundir, 'config.yaml'))
    config.set_basepath(rundir)
    ds = config.get_dataset()

    problem, xs, misfits = load_problem_info_and_data(rundir, subset='harvest')
    for target in problem.targets:
        target.set_dataset(ds)

    gms = problem.global_misfits(misfits)
    isort = num.argsort(gms)
    gms = gms[isort]
    xs = xs[isort, :]

    all_trs = []
    print gms[0]
    for xbest in xs[:1, :]:
        ms, ns, results = problem.evaluate(xbest, return_traces=True)
        print problem.global_misfit(ms, ns)

        for result in results:
            if result:
                result.filtered_obs.set_codes(location='ob')
                result.filtered_syn.set_codes(location='sy')
                all_trs.append(result.filtered_obs)
                all_trs.append(result.filtered_syn)

    trace.snuffle(all_trs)


Sebastian Heimann's avatar
Sebastian Heimann committed
1070
def harvest(rundir, problem=None, nbest=10, force=False, weed=0):
Sebastian Heimann's avatar
Sebastian Heimann committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085

    if problem is None:
        problem, xs, misfits = load_problem_info_and_data(rundir)
    else:
        xs, misfits = load_problem_data(rundir, problem)

    dumpdir = op.join(rundir, 'harvest')
    if op.exists(dumpdir):
        if force:
            shutil.rmtree(dumpdir)
        else:
            raise DirectoryAlreadyExists(dumpdir)

    util.ensuredir(dumpdir)

Sebastian Heimann's avatar
Sebastian Heimann committed
1086
1087
    ibests_list = []
    ibests = []
Sebastian Heimann's avatar
Sebastian Heimann committed
1088
1089
1090
    for ibootstrap in xrange(problem.nbootstrap):
        bms = problem.bootstrap_misfits(misfits, ibootstrap)
        isort = num.argsort(bms)
Sebastian Heimann's avatar
Sebastian Heimann committed
1091
1092
        ibests_list.append(isort[:nbest])
        ibests.append(isort[0])
Sebastian Heimann's avatar
Sebastian Heimann committed
1093
1094
1095
1096

    gms = problem.global_misfits(misfits)
    isort = num.argsort(gms)

Sebastian Heimann's avatar
Sebastian Heimann committed
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
    ibests_list.append(isort[:nbest])

    if weed:
        mean_gm_best = num.median(gms[ibests])
        std_gm_best = num.std(gms[ibests])
        ibad = set()

        for ibootstrap, ibest in enumerate(ibests):
            if gms[ibest] > mean_gm_best + std_gm_best:
                ibad.add(ibootstrap)
Sebastian Heimann's avatar
Sebastian Heimann committed
1107

Sebastian Heimann's avatar
Sebastian Heimann committed
1108
1109
1110
1111
        print ibad
        ibests_list = [
            ibests_ for (ibootstrap, ibests_) in enumerate(ibests_list)
            if ibootstrap not in ibad]
Sebastian Heimann's avatar
Sebastian Heimann committed
1112

Sebastian Heimann's avatar
Sebastian Heimann committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
    ibests = num.vstack(ibests_list)

    if weed == 2:
        ibests = ibests[gms[ibests] < mean_gm_best]

    for i in ibests:
        x = xs[i]
        ms = misfits[i, :, 0]
        ns = misfits[i, :, 1]
Sebastian Heimann's avatar
Sebastian Heimann committed
1122
1123
1124
        problem.dump_problem_data(dumpdir, x, ms, ns)


Sebastian Heimann's avatar
Sebastian Heimann committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
def check(problem):
    if len(problem.targets) == 0:
        raise GrondError('no targets available')


g_state = {}


def go(config, force=False, nparallel=1, status=('state',)):
Sebastian Heimann's avatar
Sebastian Heimann committed
1134
1135
1136
1137
1138
1139
1140

    status = tuple(status)

    ds = config.get_dataset()
    events = ds.get_events()
    nevents = len(events)

Sebastian Heimann's avatar
Sebastian Heimann committed
1141
    g_data = (config, force, status, nparallel)
Sebastian Heimann's avatar
Sebastian Heimann committed
1142

Sebastian Heimann's avatar
Sebastian Heimann committed
1143
    g_state[id(g_data)] = g_data
Sebastian Heimann's avatar
Sebastian Heimann committed
1144

Sebastian Heimann's avatar
Sebastian Heimann committed
1145
1146
1147
1148
1149
    for x in parimap.parimap(
            process_event, 
            xrange(nevents),
            [id(g_data)] * nevents,
            nprocs=nparallel):
Sebastian Heimann's avatar
Sebastian Heimann committed
1150

Sebastian Heimann's avatar
Sebastian Heimann committed
1151
        pass
Sebastian Heimann's avatar
Sebastian Heimann committed
1152
1153


Sebastian Heimann's avatar
Sebastian Heimann committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
def process_event(ievent, g_data_id):

    config, force, status, nparallel = g_state[g_data_id]

    if nparallel > 1:
        status = ()

    ds = config.get_dataset()

    events = ds.get_events()
    nevents = len(events)

    event = events[ievent]

    ds.empty_cache()

    tstart = time.time()

    problem = config.get_problem(event)

    check(problem)

    rundir = config.rundir_template % dict(
        problem_name=problem.name)

    if op.exists(rundir):
        if force:
            shutil.rmtree(rundir)
        else:
            logger.warn('skipping problem %s: rundir already exists: %s' %
                        (problem.name, rundir))
            return

    util.ensuredir(rundir)

    logger.info(
        'start %i / %i' % (ievent+1, nevents))

    analyse(
        problem,
        niter=config.analyser_config.niter,
        show_progress=nparallel==1)
Sebastian Heimann's avatar
Sebastian Heimann committed
1196

Sebastian Heimann's avatar
Sebastian Heimann committed
1197
1198
1199
1200
    basepath = config.get_basepath()
    config.change_basepath(rundir)
    guts.dump(config, filename=op.join(rundir, 'config.yaml'))
    config.change_basepath(basepath)
Sebastian Heimann's avatar
Sebastian Heimann committed
1201

Sebastian Heimann's avatar
Sebastian Heimann committed
1202
    problem.dump_problem_info(rundir)
Sebastian Heimann's avatar
Sebastian Heimann committed
1203

Sebastian Heimann's avatar
Sebastian Heimann committed
1204
1205
1206
1207
    xs_inject = None
    synt = ds.synthetic_test
    if synt and synt.inject_solution:
        xs_inject = synt.get_x()[num.newaxis, :]
Sebastian Heimann's avatar
Sebastian Heimann committed
1208

Sebastian Heimann's avatar
Sebastian Heimann committed
1209
1210
1211
1212
1213
    solve(problem,
          rundir=rundir,
          status=status,
          xs_inject=xs_inject,
          **config.solver_config.get_solver_kwargs())
Sebastian Heimann's avatar
Sebastian Heimann committed
1214

Sebastian Heimann's avatar
Sebastian Heimann committed
1215
    harvest(rundir, problem)
Sebastian Heimann's avatar
Sebastian Heimann committed
1216

Sebastian Heimann's avatar
Sebastian Heimann committed
1217
1218
1219
    tstop = time.time()
    logger.info(
        'stop %i / %i (%g min)' % (ievent, nevents, (tstop - tstart)/60.))
Sebastian Heimann's avatar
Sebastian Heimann committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241


def export(rundir, output_filename=None):
    problem, xs, misfits = load_problem_info_and_data(rundir, subset='harvest')

    if output_filename is None:
        out = sys.stdout

    else:
        out = open(output_filename, 'w')

    gms = problem.global_misfits(misfits)
    isort = num.argsort(gms)
    for i in isort:
        x = xs[i]
        gm = gms[i]
        source = problem.unpack(x)

        values = [
            source.lat,
            source.lon,
            source.depth,
Sebastian Heimann's avatar
Sebastian Heimann committed
1242
            ] + list(source.m6_astuple) + [source.stf.duration]
Sebastian Heimann's avatar
Sebastian Heimann committed
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275

        print >>out, '%12.5g' % gm, util.time_to_str(source.time), ' '.join(
            '%12.5g' % x for x in values)

    if out is not sys.stdout:
        out.close()


__all__ = '''
    GrondError
    Parameter
    ADict
    Path
    Problem
    ProblemConfig
    MisfitTarget
    MisfitResult
    Forbidden
    InnerMisfitConfig
    DatasetConfig
    TargetConfig
    SamplerDistributionChoice
    SolverConfig
    Config
    HasPaths
    TargetAnalysisResult
    load_problem_info_and_data
    read_config
    forward
    harvest
    go
    export
'''.split()