コード例 #1
0
ファイル: window_weights.py プロジェクト: wjlei1990/pytomo3d
def get_receiver_weights(component, center, points, max_ratio, plot=False,
                         figname_prefix=None):
    """
    Calculate the receiver weights given receiver(points) distribution.

    :param component:
    :param center:
    :param points:
    :param max_ratio:
    :param plot:
    :param figname_prefix:
    :return:
    """
    # calculate weight; otherwise, leave it as default value(1)
    weightobj = SphereDistRel(points, center=center)

    if plot:
        scan_figname = figname_prefix + ".%s.smart_scan.png" % component
    else:
        scan_figname = None

    ref_distance, cond_number = weightobj.smart_scan(
        max_ratio=max_ratio, start=0.5, gap=0.5,
        drop_ratio=0.95, plot=plot,
        figname=scan_figname)

    if plot:
        map_figname = figname_prefix + ".%s.weight.pdf" % component
        weightobj.plot_global_map(figname=map_figname, lon0=180.0)

    return ref_distance, cond_number
コード例 #2
0
ファイル: source_weights.py プロジェクト: mpbl/pytomo3d
def calculate_source_weights_on_location(
        points, search_ratio, plot_flag, outputdir):
    """
    :param outputdir: output directory for figures
    """
    # set a fake center point
    center = SpherePoint(0, 180.0, tag="Center")
    weightobj = SphereDistRel(points, center=center)

    if plot_flag:
        scan_figname = os.path.join(
            outputdir, "source_weights.smart_scan.png")
    else:
        scan_figname = None

    ref_distance, cond_number = weightobj.smart_scan(
        max_ratio=search_ratio, start=0.1, gap=0.2,
        drop_ratio=0.95, plot=plot_flag,
        figname=scan_figname)

    print("Reference distance and condition number: %f, %f"
          % (ref_distance, cond_number))

    if plot_flag:
        map_figname = os.path.join(
            outputdir, "source_weights.global_map.pdf")
        weightobj.plot_global_map(figname=map_figname, lon0=180.0)

    return ref_distance, cond_number
コード例 #3
0
def calculate_source_weights_on_location(points, search_ratio, plot_flag,
                                         outputdir):
    """
    :param outputdir: output directory for figures
    """
    # set a fake center point
    center = SpherePoint(0, 180.0, tag="Center")
    weightobj = SphereDistRel(points, center=center)

    if plot_flag:
        scan_figname = os.path.join(outputdir, "source_weights.smart_scan.png")
    else:
        scan_figname = None

    ref_distance, cond_number = weightobj.smart_scan(max_ratio=search_ratio,
                                                     start=0.1,
                                                     gap=0.2,
                                                     drop_ratio=0.95,
                                                     plot=plot_flag,
                                                     figname=scan_figname)

    print("Reference distance and condition number: %f, %f" %
          (ref_distance, cond_number))

    if plot_flag:
        map_figname = os.path.join(outputdir, "source_weights.global_map.pdf")
        weightobj.plot_global_map(figname=map_figname, lon0=180.0)

    return ref_distance, cond_number
コード例 #4
0
def test_spheredistrel(points, center):

    outputdir = "test23/"
    weight = SphereDistRel(points)
    #weight.calculate_weight(23.0)
    #weight.scan(start=0.1, end=50.0, gap=1.0, plot=True,
    #            figname="scan.png")
    weight.smart_scan(plot=True)
    weight.plot_exp_matrix(outputdir + "exp_matrx.png")
    weight.plot_global_map(outputdir + "map.png")
    weight.plot_weight_histogram(outputdir + "weight_hist.png")
コード例 #5
0
def test_spheredistrel(points, center):

    outputdir = "test23/"
    weight = SphereDistRel(points)
    #weight.calculate_weight(23.0)
    #weight.scan(start=0.1, end=50.0, gap=1.0, plot=True,
    #            figname="scan.png")
    weight.smart_scan(plot=True)
    weight.plot_exp_matrix(outputdir + "exp_matrx.png")
    weight.plot_global_map(outputdir + "map.png")
    weight.plot_weight_histogram(outputdir + "weight_hist.png")
コード例 #6
0
def get_receiver_weights(component,
                         center,
                         points,
                         max_ratio,
                         plot=False,
                         figname_prefix=None):
    """
    Calculate the receiver weights given receiver(points) distribution.

    :param component:
    :param center:
    :param points:
    :param max_ratio:
    :param plot:
    :param figname_prefix:
    :return:
    """
    # calculate weight; otherwise, leave it as default value(1)
    weightobj = SphereDistRel(points, center=center)

    if plot:
        scan_figname = figname_prefix + ".%s.smart_scan.png" % component
    else:
        scan_figname = None

    ref_distance, cond_number = weightobj.smart_scan(max_ratio=max_ratio,
                                                     start=0.5,
                                                     gap=0.5,
                                                     drop_ratio=0.95,
                                                     plot=plot,
                                                     figname=scan_figname)

    if plot:
        map_figname = figname_prefix + ".%s.weight.pdf" % component
        weightobj.plot_global_map(figname=map_figname, lon0=180.0)

    return ref_distance, cond_number
コード例 #7
0
def determine_source_weighting(src_info, src_wcounts, max_ratio=0.35,
                               flag=True, plot=False,
                               figname_prefix=None):
    """
    Determine the source weighting based on source distribution and
    window counts.
    Attention here, there is still 3 components and each category
    should be weighting independently with the window count information
    in this components.
    """
    logger.info("Number of sources: %s" % len(src_info))
    logger.info("Window counts information: %s" % src_wcounts)

    # determine the weightins based on location
    points = []
    for eventname, event_info in src_info.iteritems():
        point = SpherePoint(event_info["latitude"],
                            event_info["longitude"],
                            tag=eventname,
                            weight=1.0)
        points.append(point)

    if flag:
        weightobj = SphereDistRel(points)
        scan_figname = figname_prefix + ".smart_scan.png"
        _ref_dist, _cond_num = weightobj.smart_scan(
            max_ratio=max_ratio, start=0.5, gap=0.5,
            drop_ratio=0.80, plot=plot,
            figname=scan_figname)
        if plot:
            figname = figname_prefix + ".weight.png"
            weightobj.plot_global_map(figname=figname, lon0=180.0)

    # stats window counts in category level
    cat_wcounts = {}
    for event, event_info in src_wcounts.iteritems():
        for comp, comp_info in event_info.iteritems():
            cat_wcounts.setdefault(comp, 0)
            cat_wcounts[comp] += comp_info

    weights = {}
    ref_dists = {}
    cond_nums = {}
    # nomalization
    for comp in cat_wcounts:
        # ref dist and condition number are the same for different components
        # because the source distribution is exactly the same
        ref_dists[comp] = _ref_dist
        cond_nums[comp] = _cond_num
        wsum = 0
        for point in points:
            nwin = src_wcounts[point.tag][comp]
            wsum += point.weight * nwin
        norm_factor = cat_wcounts[comp] / wsum
        weights[comp] = {}
        for point in points:
            eventname = point.tag
            weights[comp][eventname] = point.weight * norm_factor

    _source_validator(weights, src_wcounts, cat_wcounts)
    return {"src_weights": weights, "cat_wcounts": cat_wcounts,
            "src_ref_dists": ref_dists, "src_cond_nums": cond_nums}
コード例 #8
0
def determine_receiver_weighting(src, stations, windows, max_ratio=0.35,
                                 flag=True, plot=False,
                                 figname_prefix=None):
    """
    Given one station and window information, determine the receiver
    weighting
    In one asdf file, there are still 3 components, for example,
    ["BHR", "BHT", "BHZ"]. These three components should be treated
    indepandently and weights will be calculated independantly.

    :return: dict of weights which contains 3 components. Each components
        contains weights values
    """
    center = SpherePoint(src["latitude"], src["longitude"],
                         tag="source")

    # extract window information
    weights = {}
    rec_wcounts = {}
    src_wcounts = defaultdict(lambda: 0)
    for sta, sta_window in windows.iteritems():
        for chan, chan_win in sta_window.iteritems():
            comp = chan.split(".")[-1]
            _nwin = len(chan_win)
            if _nwin == 0:
                continue
            weights.setdefault(comp, {}).update({chan: 0.0})
            rec_wcounts[chan] = _nwin
            src_wcounts[comp] += _nwin

    # in each components, calculate weight
    ref_dists = {}
    cond_nums = {}
    for comp, comp_info in weights.iteritems():
        points = []
        logger.info("Components:%s" % comp)
        for chan in comp_info:
            _comp = chan[-1]
            if _comp == "Z":
                point = SpherePoint(stations[chan]["latitude"],
                                    stations[chan]["longitude"],
                                    tag=chan, weight=1.0)
            else:
                # for R and T component. In station file, there
                # are only E and N component. So we need transfer
                echan = chan[:-1] + "E"
                chan1 = chan[:-1] + "1"
                zchan = chan[:-1] + "Z"
                if echan in stations:
                    point = SpherePoint(stations[echan]["latitude"],
                                        stations[echan]["longitude"],
                                        tag=chan, weight=1.0)
                elif chan1 in stations:
                    point = SpherePoint(stations[chan1]["latitude"],
                                        stations[chan1]["longitude"],
                                        tag=chan, weight=1.0)
                elif zchan in stations:
                    point = SpherePoint(stations[zchan]["latitude"],
                                        stations[zchan]["longitude"],
                                        tag=chan, weight=1.0)
            points.append(point)

        if flag:
            # calculate weight; otherwise, leave it as default value(1)
            weightobj = SphereDistRel(points, center=center)
            scan_figname = figname_prefix + "%s.smart_scan.png" % comp
            ref_dists[comp], cond_nums[comp] = weightobj.smart_scan(
                max_ratio=max_ratio, start=0.5, gap=0.5,
                drop_ratio=0.95, plot=plot,
                figname=scan_figname)
            if plot:
                figname = figname_prefix + "%s.weight.png" % comp
                weightobj.plot_global_map(figname=figname, lon0=180.0)
        else:
            ref_dists[comp] = None
            cond_nums[comp] = None

        wsum = 0
        for point in points:
            nwin = rec_wcounts[point.tag]
            wsum += point.weight * nwin
        norm_factor = src_wcounts[comp] / wsum

        for point in points:
            weights[comp][point.tag] = point.weight * norm_factor

    _receiver_validator(weights, rec_wcounts, src_wcounts)

    return {"rec_weights": weights, "rec_wcounts": rec_wcounts,
            "src_wcounts": src_wcounts, "rec_ref_dists": ref_dists,
            "rec_cond_nums": cond_nums}
コード例 #9
0
def plot_global_map(points, figname=None):
    weightobj = SphereDistRel(points, SpherePoint(0, 180.0, tag="Center"))
    weightobj.plot_global_map(figname=figname, lon0=180.0, figsize=(20, 8))