Ejemplo n.º 1
0
def calculate_receiver_weights_asdf_one_event(cat, event_info, param):
    results = {}
    nperiods = len(event_info)
    period_idx = 0
    origin = cat[0].preferred_origin()
    src_info = {
        "latitude": origin.latitude,
        "longitude": origin.longitude,
        "depth_in_m": origin.depth
    }
    # determine receiver weightings for each asdf file
    for period, period_info in event_info["period_info"].iteritems():
        period_idx += 1
        logger.info("-" * 15 + "[%d/%d]Period band: %s" %
                    (period_idx, nperiods, period) + "-" * 15)
        _path_info = {
            "station_file": event_info["stationfile"],
            "window_file": period_info["window_file"],
            "output_file": period_info["output_file"]
        }
        # the _results contains three components data
        results[period] = calculate_receiver_weights_interface(
            src_info, _path_info, param)

        outputdir = os.path.dirname(period_info["output_file"])
        receiver_weights_file = os.path.join(outputdir,
                                             "receiver_weights.json")
        print("Receiver weights log file: %s" % receiver_weights_file)
        dump_json(results[period], receiver_weights_file)

    return results
Ejemplo n.º 2
0
def calculate_receiver_weights_asdf_one_event(cat, event_info, param):
    results = {}
    nperiods = len(event_info)
    period_idx = 0
    origin = cat[0].preferred_origin()
    src_info = {"latitude": origin.latitude, "longitude": origin.longitude,
                "depth_in_m": origin.depth}
    # determine receiver weightings for each asdf file
    for period, period_info in event_info["period_info"].iteritems():
        period_idx += 1
        logger.info("-" * 15 + "[%d/%d]Period band: %s"
                    % (period_idx, nperiods, period) + "-" * 15)
        _path_info = {"station_file": event_info["stationfile"],
                      "window_file": period_info["window_file"],
                      "output_file": period_info["output_file"]}
        # the _results contains three components data
        results[period] = calculate_receiver_weights_interface(
            src_info, _path_info, param)

        outputdir = os.path.dirname(period_info["output_file"])
        receiver_weights_file = os.path.join(
            outputdir, "receiver_weights.json")
        print("Receiver weights log file: %s" % receiver_weights_file)
        dump_json(results[period], receiver_weights_file)

    return results
Ejemplo n.º 3
0
def calculate_category_weights(src_weights, path_info, param, logdir):

    cat_wcounts = get_event_category_window_counts(path_info)
    # category weight = 1 / (N_c * \sum_{s} w_{s} N_{sc})
    sumv = {}
    for ev in cat_wcounts:
        srcw = src_weights[ev]
        for pb in cat_wcounts[ev]:
            if pb not in sumv:
                sumv[pb] = {}
            for comp in cat_wcounts[ev][pb]:
                if comp not in sumv[pb]:
                    sumv[pb][comp] = 0
                sumv[pb][comp] += srcw * cat_wcounts[ev][pb][comp]

    print("\sum_{s} source_weight * N_{sc}: %s" % sumv)

    cat_weights, ratios = normalize_category_weights(sumv)

    print("Final value of category weightings:")
    pprint(cat_weights)
    print("Ratio:")
    pprint(ratios)

    log_content = {"weights": cat_weights, "ratio": ratios}
    outputfn = os.path.join(logdir, "category_weights.log.json")
    print("category weighting log file: %s" % outputfn)
    dump_json(log_content, outputfn)

    return cat_weights
Ejemplo n.º 4
0
    def save_log(self):
        def _get_max_and_min(dictv):
            minv = 9999999999
            maxv = -1
            for e, einfo in dictv.iteritems():
                for p, pinfo in einfo.iteritems():
                    for c, cinfo in pinfo.iteritems():
                        if minv > cinfo:
                            minv = cinfo
                        if maxv < cinfo:
                            maxv = cinfo
            return minv, maxv

        logfile = self.path["logfile"]
        cond_min, cond_max = _get_max_and_min(self.rec_cond_nums)
        ref_min, ref_max = _get_max_and_min(self.rec_ref_dists)
        log_content = {
            "receiver_ref_dist": self.rec_ref_dists,
            "recevier_cond_numb": self.rec_cond_nums,
            "summary": {
                "receiver_ref_dist": {
                    "min": ref_min,
                    "max": ref_max
                },
                "receiver_cond_num": {
                    "min": cond_min,
                    "max": cond_max
                }
            }
        }
        print("log file: %s" % logfile)
        dump_json(log_content, logfile)
Ejemplo n.º 5
0
def calculate_category_weights(src_weights, path_info, param, logdir):

    cat_wcounts = get_event_category_window_counts(path_info)
    # category weight = 1 / (N_c * \sum_{s} w_{s} N_{sc})
    sumv = {}
    for ev in cat_wcounts:
        srcw = src_weights[ev]
        for pb in cat_wcounts[ev]:
            if pb not in sumv:
                sumv[pb] = {}
            for comp in cat_wcounts[ev][pb]:
                if comp not in sumv[pb]:
                    sumv[pb][comp] = 0
                sumv[pb][comp] += srcw * cat_wcounts[ev][pb][comp]

    print("\sum_{s} source_weight * N_{sc}: %s" % sumv)

    cat_weights, ratios = normalize_category_weights(sumv)

    print("Final value of category weightings:")
    pprint(cat_weights)
    print("Ratio:")
    pprint(ratios)

    log_content = {"weights": cat_weights, "ratio": ratios}
    outputfn = os.path.join(logdir, "category_weights.log.json")
    print("category weighting log file: %s" % outputfn)
    dump_json(log_content, outputfn)

    return cat_weights
Ejemplo n.º 6
0
def analyze_overall_weights(weights, rec_wcounts, log_prefix):
    nwins_array = []
    weights_array = []
    # validate the sum of all weights is 1
    for _p, _pw in weights.iteritems():
        for _chan, _chanw in _pw.iteritems():
            comp = _chan.split(".")[-1]
            nwins_array.append(rec_wcounts[_p][comp][_chan])
            weights_array.append(_chanw["weight"])

    validate_overall_weights(weights_array, nwins_array)

    figname = log_prefix + ".weights.hist.png"
    plot_histogram(figname, weights_array)
    figname = log_prefix + ".wcounts.hist.png"
    plot_histogram(figname, nwins_array)

    maxw = max(weights_array)
    minw = min(weights_array)
    nreceivers = len(weights_array)
    nwindows = np.sum(nwins_array)
    logger.info("Total number of receivers: %d" % nreceivers)
    logger.info("Total number of windows: %d" % nwindows)
    logger.info("Weight max, min, max/min: %f, %f, %f"
                % (maxw, minw, maxw/minw))

    logfile = log_prefix + ".weights.summary.json"
    content = {"max_weights": maxw, "min_weights": minw,
               "total_nwindows": np.sum(nwins_array),
               "windows": nwindows, "receivers": nreceivers}
    logger.info("Overall log file: %s" % logfile)
    dump_json(content, logfile)
Ejemplo n.º 7
0
 def dump_weights(self):
     """ dump weights to files """
     for ev, ev_info in self.weights.iteritems():
         for period, period_info in ev_info.iteritems():
             _info = self.path['input'][ev]["period_info"]
             outputfn = _info[period]["output_file"]
             # print("Final weights dumped to: %s" % outputfn)
             dump_json(period_info, outputfn)
Ejemplo n.º 8
0
 def dump_weights(self):
     """ dump weights to files """
     for ev, ev_info in self.weights.iteritems():
         for period, period_info in ev_info.iteritems():
             _info = self.path['input'][ev]["period_info"]
             outputfn = _info[period]["output_file"]
             # print("Final weights dumped to: %s" % outputfn)
             dump_json(period_info, outputfn)
Ejemplo n.º 9
0
def analyze_category_weights(cat_weights, logfile):
    log = {"category_weights": cat_weights}
    maxw = 0
    minw = 10**9
    for _p, _pw in cat_weights.iteritems():
        for _comp, _compw in _pw.iteritems():
            if _compw > maxw:
                maxw = _compw
            if _compw < minw:
                minw = _compw
    log["summary"] = {"maxw": maxw, "minw": minw, "cond_num": maxw / minw}

    dump_json(log, logfile)
Ejemplo n.º 10
0
def analyze_category_weights(cat_weights, logfile):
    log = {"category_weights": cat_weights}
    maxw = 0
    minw = 10**9
    for _p, _pw in cat_weights.iteritems():
        for _comp, _compw in _pw.iteritems():
            if _compw > maxw:
                maxw = _compw
            if _compw < minw:
                minw = _compw
    log["summary"] = {"maxw": maxw, "minw": minw,
                      "cond_num": maxw/minw}

    dump_json(log, logfile)
Ejemplo n.º 11
0
    def analyze_receiver_weights(self, logfile):
        log = {}
        for _p, _pw in self.weights.iteritems():
            log[_p] = {}
            maxw = defaultdict(lambda: 0)
            minw = defaultdict(lambda: 10**9)
            for _chan, _chanw in _pw.iteritems():
                comp = _chan.split(".")[-1]
                if _chanw["weight"] > maxw[comp]:
                    maxw[comp] = _chanw["weight"]
                if _chanw["weight"] < minw[comp]:
                    minw[comp] = _chanw["weight"]
            for comp in maxw:
                log[_p][comp] = \
                    {"maxw": maxw[comp], "minw": minw[comp],
                     "nwindows": self.cat_wcounts[_p][comp],
                     "ref_dist": self.rec_ref_dists[_p][comp],
                     "cond_num": self.rec_cond_nums[_p][comp]}

        dump_json(log, logfile)
Ejemplo n.º 12
0
def calculate_source_weights(src_info, param, logdir):
    logger_block("Source Weighting")
    points = assign_source_to_points(src_info)

    ref_distance = -1.0
    cond_numb = -1.0
    if param["flag"]:
        ref_distance, cond_numb = calculate_source_weights_on_location(
            points, param["search_ratio"], param["plot"], logdir)

    src_weights = normalize_source_weights(points)

    # generate log file
    log_content = \
        {"weights": src_weights, "reference_distance": ref_distance,
         "cond_num": cond_numb, "weight_flag": param["flag"],
         "search_ratio": param["search_ratio"]}
    outputfn = os.path.join(logdir, "source_weights.log.json")
    print("Source weights log file: %s" % outputfn)
    dump_json(log_content, outputfn)

    return src_weights
Ejemplo n.º 13
0
def analyze_overall_weights(weights, rec_wcounts, log_prefix):
    nwins_array = []
    weights_array = []
    # validate the sum of all weights is 1
    for _p, _pw in weights.iteritems():
        for _chan, _chanw in _pw.iteritems():
            comp = _chan.split(".")[-1]
            nwins_array.append(rec_wcounts[_p][comp][_chan])
            weights_array.append(_chanw["weight"])

    validate_overall_weights(weights_array, nwins_array)

    figname = log_prefix + ".weights.hist.png"
    plot_histogram(figname, weights_array)
    figname = log_prefix + ".wcounts.hist.png"
    plot_histogram(figname, nwins_array)

    nreceivers = len(weights_array)
    nwindows = np.sum(nwins_array)
    if len(weights_array) > 0:
        maxw = max(weights_array)
        minw = min(weights_array)
        max_over_min = maxw / minw
    else:
        maxw = 0.0
        minw = 0.0
        max_over_min = 0.0

    logger.info("Total number of receivers: %d" % nreceivers)
    logger.info("Total number of windows: %d" % nwindows)
    logger.info("Weight max, min, max/min: %f, %f, %f"
                % (maxw, minw, max_over_min))

    logfile = log_prefix + ".weights.summary.json"
    content = {"max_weights": maxw, "min_weights": minw,
               "total_nwindows": np.sum(nwins_array),
               "windows": nwindows, "receivers": nreceivers}
    logger.info("Overall log file: %s" % logfile)
    dump_json(content, logfile)
Ejemplo n.º 14
0
def calculate_source_weights(src_info, param, logdir):
    logger_block("Source Weighting")
    points = assign_source_to_points(src_info)

    ref_distance = -1.0
    cond_numb = -1.0
    if param["flag"]:
        ref_distance, cond_numb = calculate_source_weights_on_location(
            points, param["search_ratio"], param["plot"], logdir)

    src_weights = normalize_source_weights(points)

    # generate log file
    log_content = \
        {"weights": src_weights, "reference_distance": ref_distance,
         "cond_num": cond_numb, "weight_flag": param["flag"],
         "search_ratio": param["search_ratio"]}
    outputfn = os.path.join(logdir, "source_weights.log.json")
    print("Source weights log file: %s" % outputfn)
    dump_json(log_content, outputfn)

    return src_weights
Ejemplo n.º 15
0
    def analysis_source(self, logfile):
        """
        dump source weights and some statistic information
        """
        log = {"source_weights": self.src_weights}
        summary = {}
        for _p, _pw in self.src_weights.iteritems():
            summary[_p] = {}
            for _comp, _compw in _pw.iteritems():
                maxw = 0
                minw = 10**9
                for _ev, _evw in _compw.iteritems():
                    if _evw > maxw:
                        maxw = _evw
                    if _evw < minw:
                        minw = _evw
                summary[_p][_comp] = \
                    {"maxw": maxw, "minw": minw,
                     "ref_distance": self.src_ref_dists[_p][_comp],
                     "cond_num": self.src_cond_nums[_p][_comp],
                     "nwindows": self.cat_wcounts[_p][_comp]}

        log["summary"] = summary
        dump_json(log, logfile)
Ejemplo n.º 16
0
    def save_log(self):
        def _get_max_and_min(dictv):
            minv = 9999999999
            maxv = -1
            for e, einfo in dictv.iteritems():
                for p, pinfo in einfo.iteritems():
                    for c, cinfo in pinfo.iteritems():
                        if minv > cinfo:
                            minv = cinfo
                        if maxv < cinfo:
                            maxv = cinfo
            return minv, maxv

        logfile = self.path["logfile"]
        cond_min, cond_max = _get_max_and_min(self.rec_cond_nums)
        ref_min, ref_max = _get_max_and_min(self.rec_ref_dists)
        log_content = {
            "receiver_ref_dist": self.rec_ref_dists,
            "recevier_cond_numb": self.rec_cond_nums,
            "summary": {
                "receiver_ref_dist": {"min": ref_min, "max": ref_max},
                "receiver_cond_num": {"min": cond_min, "max": cond_max}}}
        print("log file: %s" % logfile)
        dump_json(log_content, logfile)
Ejemplo n.º 17
0
 def dump_weights(self):
     """ dump weights to files """
     for period, period_info in self.weights.iteritems():
         for event, event_info in period_info.iteritems():
             outputfn = self.path['input'][period][event]["output_file"]
             dump_json(event_info, outputfn)
Ejemplo n.º 18
0
 def dump_weights(self):
     """ dump weights to files """
     for period, period_info in self.weights.iteritems():
         outputfn = self.path['input'][period]["output_file"]
         dump_json(period_info, outputfn)