예제 #1
0
def print_diff(diff, show_plot=False):
    mean_prefix = '+' if diff.r1['mean'] < diff.r2['mean'] else '-'
    median_prefix = '+' if diff.r1['percentile']['50'] < diff.r2['percentile'][
        '50'] else '-'
    print(
        f'| Version |         Mean ±    Stdev |        Min |     Median |         Q3 |        Max |'
    )
    print(
        f"|   V1    |   {diff.r1['mean']:10.3f} ± {diff.r1['stdev']:8.3f} | {diff.r1['min']:10.3f} | {diff.r1['percentile']['50']:10.3f} | {diff.r1['percentile']['75']:10.3f} | {diff.r1['max']:10.3f} |"
    )
    print(
        f"|   V2    |   {diff.r2['mean']:10.3f} ± {diff.r2['stdev']:8.3f} | {diff.r2['min']:10.3f} | {diff.r2['percentile']['50']:10.3f} | {diff.r2['percentile']['75']:10.3f} | {diff.r2['max']:10.3f} |"
    )
    print(
        f'├---------┴-------------------------┴------------┴------------┴------------┴------------┘'
    )
    print(
        f"|               {mean_prefix}{diff.mean_diff:7.2f}%                           {median_prefix}{diff.median_diff:7.2f}%   "
    )
    print(f'{diff.ptext}')
    print(f'{diff.significance}')
    print('')
    if show_plot:
        plt.subplots(2, 1)
        plt.subplot(1, 1)
        plt.scatter(diff.r1.get('samples', diff.r1['mean']), label='v1')
        plt.subplot(2, 1)
        plt.scatter(diff.r2.get('samples', diff.r2['mean']), label='v2')
        plt.show()
예제 #2
0
def make_figure_uhs_cluster(extractors, what):
    """
    $ oq plot "uhs_cluster?k=12"
    """
    plt = import_plt()
    import matplotlib.cm as cm
    kstr = what.split('?')[1]
    k = int(kstr.split('=')[1])
    fig, ax = plt.subplots()
    [ex] = extractors
    hmaps = ex.get('hmaps?kind=rlzs')
    rlzs = ex.get('realizations').array
    labels = []
    for p, poe in enumerate(ex.oqparam.poes):
        for imt in ex.oqparam.imtls:
            labels.append('%s' % imt)
    xs = numpy.arange(len(labels))
    ax.set_xticks(xs)
    ax.set_xticklabels(labels)
    ax.set_ylabel('IML')
    obs = [getattr(hmaps, 'rlz-%03d' % rlz)[0] for rlz in range(len(rlzs))]
    arr, cluster = clusterize(numpy.array(obs), rlzs, k)
    colors = cm.rainbow(numpy.linspace(0, 1, len(arr)))
    paths = [p.decode('ascii') for p in arr['branch_paths']]
    for rlz in range(len(rlzs)):
        # ush-rlz has shape NMP
        ys = getattr(hmaps, 'rlz-%03d' % rlz)[0].T.flatten()
        ax.plot(xs, ys, '-', color=colors[cluster[rlz]])
    for c, curve in enumerate(arr['centroid']):
        lbl = '%s:%s' % (c + 1, paths[c])
        print(lbl)
        ax.plot(xs, curve, '--', color=colors[c], label=lbl)
    ax.grid(True)
    ax.legend()
    return plt
예제 #3
0
def make_figure_effect_by_mag(extractors, what):
    """
    $ oq plot "effect_by_mag?"
    """
    # NB: matplotlib is imported inside since it is a costly import
    plt = import_plt()
    [ex] = extractors
    gsims_by_trt = ex.get('gsims_by_trt', asdict=True)
    mags = ex.get('source_mags').array
    try:
        effect = ex.get('effect')
    except KeyError:
        onesite = ex.get('sitecol').one()
        maximum_distance = IntegrationDistance(ex.oqparam.maximum_distance)
        imtls = ex.oqparam.imtls
        ebm = get_effect_by_mag(mags, onesite, gsims_by_trt, maximum_distance,
                                imtls)
        effect = numpy.array(list(ebm.values()))
    fig, ax = plt.subplots()
    trti = 0
    for trt in gsims_by_trt:
        ax.plot(mags, effect[:, -1, trti], label=trt)
        ax.set_xlabel('Mag')
        ax.set_ylabel('Intensity')
        ax.set_title('Effect at maximum distance')
        trti += 1
    ax.legend()
    return plt
예제 #4
0
def make_figure_dist_by_mag(extractors, what):
    """
    $ oq plot "dist_by_mag?"
    """
    # NB: matplotlib is imported inside since it is a costly import
    plt = import_plt()
    [ex] = extractors
    effect = ex.get('effect')
    mags = ['%.2f' % mag for mag in effect.mags]
    fig, ax = plt.subplots()
    trti = 0
    for trt, dists in effect.dist_bins.items():
        dic = dict(zip(mags, effect[:, :, trti]))
        if ex.oqparam.pointsource_distance:
            pdist = getdefault(ex.oqparam.pointsource_distance, trt)
        else:
            pdist = None
        eff = Effect(dic, dists, pdist)
        dist_by_mag = eff.dist_by_mag()
        ax.plot(effect.mags,
                list(dist_by_mag.values()),
                label=trt,
                color='red')
        if pdist:
            dist_by_mag = eff.dist_by_mag(eff.collapse_value)
            ax.plot(effect.mags,
                    list(dist_by_mag.values()),
                    label=trt,
                    color='green')
        ax.set_xlabel('Mag')
        ax.set_ylabel('Dist')
        ax.set_title('Integration Distance at intensity=%s' % eff.zero_value)
        trti += 1
    ax.legend()
    return plt
예제 #5
0
def make_figure_rupture_info(extractors, what):
    """
    $ oq plot "rupture_info?min_mag=6"
    """
    # NB: matplotlib is imported inside since it is a costly import
    plt = import_plt()
    [ex] = extractors
    info = ex.get(what)
    fig, ax = plt.subplots()
    ax.grid(True)
    n = 0
    tot = 0
    pp = PolygonPlotter(ax)
    geoms = gzip.decompress(info['boundaries']).decode('utf8').split('\n')
    for rec, wkt in zip(info, geoms):
        poly = shapely.wkt.loads(wkt)
        if poly.is_valid:
            pp.add(poly)
            n += 1
        else:
            print('Invalid %s' % wkt)
        tot += 1
    pp.set_lim()
    ax.set_title('%d/%d valid ruptures' % (n, tot))
    if tot == 1:
        # print the full geometry
        print(ex.get('rupture/%d' % rec['rupid']).toml())
    return plt
예제 #6
0
def make_figure_gridded_sources(extractors, what):
    """
    $ oq plot "gridded_sources?task_no=0"
    """
    # NB: matplotlib is imported inside since it is a costly import
    plt = import_plt()
    [ex] = extractors
    dic = json.loads(ex.get(what).json)  # id -> lonlats
    fig, ax = plt.subplots()
    ax.grid(True)
    sitecol = ex.get('sitecol')
    tot = 0
    for lonlats in dic.values():
        if len(lonlats) == 2:  # not collapsed
            tot += 1
        else:  # collapsed
            tot += len(lonlats) / 2 - 1
        ax.plot([lonlats[0]], [lonlats[1]], '*')
        lons = lonlats[2::2]
        lats = lonlats[3::2]
        ax.scatter(lons, lats)
    ax.plot(sitecol['lon'], sitecol['lat'], '.')
    ax.set_title('Reduced %d->%d sources' % (tot, len(dic)))
    # TODO: fix plot around the IDL
    return plt
예제 #7
0
def make_figure_source_data(extractors, what):
    """
    $ oq plot "source_data?taskno=XX"
    """
    plt = import_plt()
    fig, ax = plt.subplots()
    [ex] = extractors
    aw = ex.get(what)
    x, y = aw.ctimes, aw.weight
    reg = linregress(x, y)
    ax.plot(x, reg.intercept + reg.slope * x)
    ax.plot(x, y)
    ax.set_xlabel("duration")
    ax.set_ylabel("weight")
    return plt
예제 #8
0
def make_figure_sources(extractors, what):
    """
    $ oq plot "sources?limit=100"
    $ oq plot "sources?source_id=1&source_id=2"
    $ oq plot "sources?code=A&code=N"
    """
    # NB: matplotlib is imported inside since it is a costly import
    plt = import_plt()
    [ex] = extractors
    info = ex.get(what)
    wkts = gzip.decompress(info.wkt_gz).decode('utf8').split(';')
    srcs = gzip.decompress(info.src_gz).decode('utf8').split(';')
    fig, ax = plt.subplots()
    ax.grid(True)
    sitecol = ex.get('sitecol')
    pp = PolygonPlotter(ax)
    n = 0
    tot = 0
    psources = []
    for rec, srcid, wkt in zip(info, srcs, wkts):
        if not wkt:
            logging.warning('No geometries for source id %s', srcid)
            continue
        if rec['eff_ruptures']:  # not filtered out
            color = 'green'
            alpha = .3
            n += 1
        else:
            color = 'yellow'
            alpha = .1
        if wkt.startswith('POINT'):
            psources.append(shapely.wkt.loads(wkt))
        else:
            pp.add(shapely.wkt.loads(wkt), alpha=alpha, color=color)
        tot += 1
    lons = [p.x for p in psources]
    lats = [p.y for p in psources]
    ss_lons = lons + list(sitecol['lon'])  # sites + sources longitudes
    ss_lats = lats + list(sitecol['lat'])  # sites + sources latitudes
    if len(ss_lons) > 1 and cross_idl(*ss_lons):
        ss_lons = [lon % 360 for lon in ss_lons]
        lons = [lon % 360 for lon in lons]
        sitecol['lon'] = sitecol['lon'] % 360
    ax.plot(sitecol['lon'], sitecol['lat'], '.')
    ax.plot(lons, lats, 'o')
    pp.set_lim(ss_lons, ss_lats)
    ax.set_title('%d/%d sources' % (n, tot))
    return plt
예제 #9
0
def make_figure_memory(extractors, what):
    """
    $ oq plot "memory?"
    """
    # NB: matplotlib is imported inside since it is a costly import
    plt = import_plt()

    [ex] = extractors
    task_info = ex.get('task_info').to_dict()
    fig, ax = plt.subplots()
    ax.grid(True)
    ax.set_xlabel('tasks')
    ax.set_ylabel('GB')
    start = 0
    for task_name in task_info:
        mem = task_info[task_name]['mem_gb']
        ax.plot(range(start, start + len(mem)), mem, label=task_name)
        start += len(mem)
    ax.legend()
    return plt
예제 #10
0
파일: log.py 프로젝트: Popeyef5/Cassandra
    def animate(self):
        """
    Animates the rocket's movement according to the data in the logs.
    """
        # Ideally we'd use something other than matplotlib. It really wasn't designed for
        # this sort of application.
        import matplotlib.pyplot as plt
        import matplotlib.patches as patches
        from matplotlib.animation import FuncAnimation
        import numpy as np
        import quaternion as q

        cm_positions = self.logs['cm_position'].get_data()
        cm_orientations = self.logs['cm_orientation'].get_data()

        frame_rel_positions = self.logs['frame_position'].get_data()
        frame_rel_orientations = self.logs['frame_orientation'].get_data()

        mount_rel_positions = self.logs['mount_position'].get_data()
        mount_rel_orientations = self.logs['mount_orientation'].get_data()

        thrust = self.logs['thrust'].get_data()

        fig, ax = plt.subplots(2, 2)
        ax[0, 0].set_xlim(-21, 21)
        ax[0, 0].set_ylim(-2, 20)
        ax[0, 1].set_xlim(-21, 21)
        ax[0, 1].set_ylim(-2, 20)
        ax[1, 0].set_xlim(-21, 21)
        ax[1, 0].set_ylim(-21, 21)

        # Rockets definition
        rocket_xz = patches.Rectangle((0, 0.5), 0.2, 1)
        rocket_yz = patches.Rectangle((0, 0.5), 0.2, 1)
        rocket_xy = patches.Rectangle((0, 0), 0.2, 0.2)

        # Thrust definition
        thrust_xz = patches.Rectangle((0, 0), 0.1, -1, facecolor='r')
        thrust_yz = patches.Rectangle((0, 0), 0.1, -1, facecolor='r')

        # Floor
        floor_xz = patches.Rectangle((-50, 0), 100, -2, facecolor='g')
        floor_yz = patches.Rectangle((-50, 0), 100, -2, facecolor='g')

        def init():
            ax[0, 0].add_patch(rocket_xz)
            ax[0, 0].add_patch(thrust_xz)
            ax[0, 0].add_patch(floor_xz)
            ax[0, 1].add_patch(rocket_yz)
            ax[0, 1].add_patch(thrust_yz)
            ax[0, 1].add_patch(floor_yz)
            ax[1, 0].add_patch(rocket_xy)
            return rocket_xz, thrust_xz, floor_xz, rocket_yz, thrust_yz, floor_yz, rocket_xy

        def update(frame_count):
            cm_position = cm_positions[frame_count]
            cm_orientation = cm_orientations[frame_count]

            frame_position = cm_position + q.rotate_vectors(
                cm_orientation, frame_rel_positions[frame_count] +
                np.array([-0.1, -0.1, -0.5]))
            frame_direction = q.rotate_vectors(
                frame_rel_orientations[frame_count] * cm_orientation,
                np.array([0, 0, 1]))

            mount_position = cm_position + q.rotate_vectors(
                cm_orientation, mount_rel_positions[frame_count])
            mount_direction = q.rotate_vectors(
                mount_rel_orientations[frame_count] * cm_orientation,
                np.array([0, 0, 1]))

            frame_angle_xz = np.rad2deg(
                math.atan2(frame_direction[0], frame_direction[2]))
            frame_angle_yz = np.rad2deg(
                math.atan2(frame_direction[1], frame_direction[2]))

            # Rockets update
            rocket_xz.set_xy(frame_position[[0, 2]])
            rocket_xz.angle = frame_angle_xz
            rocket_yz.set_xy(frame_position[[1, 2]])
            rocket_yz.angle = frame_angle_yz
            rocket_xy.set_xy(frame_position[[0, 1]])

            thrust_angle_xz = np.rad2deg(
                math.atan2(mount_direction[0], mount_direction[2]))
            thrust_angle_yz = np.rad2deg(
                math.atan2(mount_direction[1], mount_direction[2]))

            thrust_xz.set_xy(mount_position[[0, 2]])
            thrust_xz.angle = thrust_angle_xz
            thrust_yz.set_xy(mount_position[[1, 2]])
            thrust_yz.angle = thrust_angle_yz
            return rocket_xz, thrust_xz, floor_xz, rocket_yz, thrust_yz, floor_yz, rocket_xy

        anim = FuncAnimation(fig,
                             update,
                             init_func=init,
                             frames=len(cm_positions),
                             interval=20,
                             blit=True)

        plt.show()