Exemple #1
0
def rand_clustering(surface, dr, bond_range, n=4, max_trial=10, boundary=None):

    cell = surface.get_cell()
    a = cell[0, 0]
    b = cell[1, 0]
    c = cell[1, 1]
    tol = 1.5

    org_boundary = np.array([[0, 0], [a, 0], [a + b, c], [b, c]])

    if boundary is None:
        boundary = np.array([[-tol, -tol], [a + tol, -tol],
                             [a + b + tol, c + tol], [b - tol, c + tol]])

    boundary_polygon = Polygon(boundary)
    points = random_point_within(boundary_polygon, n=n)

    temp_var = random_point_within(boundary_polygon, n=n)
    points = []
    for elem in zip(temp_var[0], temp_var[1]):
        points.extend([elem])

    points = [list(ele) for ele in points]
    points = np.array(points)

    org_boundary_polygon = Polygon(org_boundary)
    pos = surface.get_positions()
    C_ndx = [atom.index for atom in surface if atom.symbol == 'C']
    O_ndx = [atom.index for atom in surface if atom.symbol == 'O']
    posC = np.zeros((len(C_ndx), 2))
    posO = np.zeros((len(C_ndx), 2))
    i = 0
    for c, o in zip(C_ndx, O_ndx):
        pc = pos[c, 0:2]
        po = pos[o, 0:2]
        posC[i] = pc
        posO[i] = po
        i = i + 1

    # points = [[points[0][0],points[1][0]],[points[0][1],points[1][1]],[points[0][2],points[1][2]],[points[0][3],points[1][3]]]
    diameter = np.linalg.norm(boundary.ptp(axis=0))
    boundary_polygon = Polygon(boundary)
    X = []
    Y = []
    for p in voronoi_polygons(Voronoi(points), diameter):
        try:
            x, y = zip(*p.intersection(boundary_polygon).exterior.coords)
        except (ValueError):
            raise NoReasonableStructureFound(
                "No good structure found at function add_atom_on_surfaces")
        X.append(np.array(x))
        Y.append(np.array(y))

    ro = surface.get_positions()
    rn = ro.copy()
    t = surface.copy()
    count = 0

    for x, y in zip(X, Y):
        disp = np.random.uniform(-1., 1., [
            1, 3
        ])  #difine displacement from particular tesslation region/ cluster
        #find and move atoms in the cluster
        comb = np.vstack((x, y))
        comb = comb.T
        bbPath = mplPath.Path(comb)

        for i, n in enumerate(C_ndx):
            pc = pos[n, 0:2]
            if bbPath.contains_point((pc[0], pc[1])):
                rn[n, :] = ro[n, :] + dr * disp
                rn[O_ndx[i], :] = ro[O_ndx[i], :] + dr * disp
        count = count + 1

    t.set_positions(rn)

    inspector = CheckAtoms(bond_range=bond_range)
    if inspector.is_good(t, quickanswer=True):
        return t
    raise NoReasonableStructureFound(
        "No good structure found at function add_atom_on_surfaces")
def select_groupsw(cont_densew, pw, fwhm, level, limgroupw, characw):

    x = cont_densew  # Comes from the contour plots

    # How many 100% level contours do we have? (most likely this will represent the number of groups you have in that field)
    # some may be fake check at the end

    i = 0

    for i in range(
            len(x.allsegs[1])
    ):  # for all 100% level contours find its closest x% (user defined) contour that corresponds to that 100%

        mass = 0
        volume = 0
        area = 0

        array = []
        ggroup = []

        cont = x.allsegs[1][i]  #ith 100% contour level

        # Find its nearest x% neighbouring contour
        contmin, segmin, indmin, xmin, ymin, dmin = x.find_nearest_contour(
            cont[0][0], cont[0][1], indices=[0], pixel=False)

        bbPath = mplPath.Path(x.allsegs[contmin][segmin])

        # Find the volume of this contour by modeling like an ellipse
        axis, phi, center = fit_ellipse(x.allsegs[contmin][segmin])

        a = axis[0] * 100 * 0.187 * 8.462 / 1000
        b = axis[1] * 100 * 0.187 * 8.462 / 1000
        c = (a + b) / 2

        ad = axis[0] * 100 * 0.187 / 3600  #axis in Deg
        bd = axis[1] * 100 * 0.187 / 3600

        volume = 4 * np.pi * a * b * c / 3
        area = np.pi * a * b
        aread = np.pi * ad * bd  # in deg^2

        for gal in pw:

            field = str(int(gal[43]))
            shiftx, shifty = shift(fieldw, field)

            # shift each position according to the position of their patch, each square degree is 19354 pixels minus the overlap between adjacent patches: 4 arcmin in DEC and 3 in RA
            xa = gal[2] + shiftx * (19354 - 968)
            ya = gal[3] + shifty * (19354 - 1290)

            xag = (xa / scale)  #float or int
            yag = (ya / scale)

            C = bbPath.contains_point(
                (xag,
                 yag))  # search if the  scaled position is within the x% level

            if (C == True):
                m = 10**(-0.348 * gal[17] + 18.284)
                mass = mass + m
                if (len(array) == 0):
                    array = gal
                else:
                    array = np.vstack((array, gal))

        # How many of the galaxies in the group is massive? (at least 3 in group to classify)

        ggroup = array[array[:, 17] < 20.5]

        if (len(ggroup) >=
                3):  # If there are at least 3 massive galaxies in group

            # ID the approx center of the group
            medx = np.mean(array[:, 2])
            medy = np.mean(array[:, 3])
            #fieldg = np.unique(array[:,43])#np.mean(array[:,43]) #or numpy unique

            # Find the number density of this group
            nd = len(array[:, 0]) / volume

            # Find the mass density for this group
            md = np.log10(mass) / volume

            # Find the number area density
            nad = len(array[:, 0]) / area

            print(fieldw,
                  i,
                  medx,
                  medy,
                  np.log10(mass),
                  volume,
                  nd,
                  md,
                  nad,
                  aread,
                  file=characw)

            plt.figure(num=fieldw * 10)

            for element in array:

                fieldarray = str(int(element[43]))
                shiftx, shifty = shift(fieldw, fieldarray)

                # shift each position according to the position of their patch, each square degree is 19354 pixels minus the overlap between adjacent patches: 4 arcmin in DEC and 3 in RA
                xarray = (element[2] + shiftx * (19354 - 968)) / scale
                yarray = (element[3] + shifty * (19354 - 1290)) / scale
Exemple #3
0
def draw_matplotlib(
    g: Union[BaseGraph[VT, ET], Circuit],
    labels: bool = False,
    figsize: Tuple[FloatInt, FloatInt] = (8, 2),
    h_edge_draw: Literal['blue', 'box'] = 'blue',
    show_scalar: bool = False,
    rows: Optional[Tuple[FloatInt, FloatInt]] = None
) -> Any:  # TODO: Returns a matplotlib figure
    if plt is None:
        raise ImportError(
            "This function requires matplotlib to be installed. "
            "If you are running in a Jupyter notebook, you can instead use `zx.draw_d3`."
        )
    if isinstance(g, Circuit):
        g = g.to_graph(zh=True)
    fig1 = plt.figure(figsize=figsize)
    ax = fig1.add_axes([0, 0, 1, 1], frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    vs_on_row: Dict[FloatInt, int] = {}  # count the vertices on each row
    for v in g.vertices():
        vs_on_row[g.row(v)] = vs_on_row.get(g.row(v), 0) + 1

    #Dict[VT,Tuple[FloatInt,FloatInt]]
    layout = {v: (g.row(v), -g.qubit(v)) for v in g.vertices()}

    if rows is not None:
        minrow, maxrow = rows
        vertices: Iterable[VT] = [
            v for v in g.vertices()
            if (minrow <= g.row(v) and g.row(v) <= maxrow)
        ]
        edges: Iterable[ET] = [
            e for e in g.edges()
            if g.edge_s(e) in vertices and g.edge_t(e) in vertices
        ]
    else:
        vertices = g.vertices()
        edges = g.edges()

    for e in edges:
        sp = layout[g.edge_s(e)]
        tp = layout[g.edge_t(e)]
        et = g.edge_type(e)
        n_row = vs_on_row.get(g.row(g.edge_s(e)), 0)

        dx = tp[0] - sp[0]
        dy = tp[1] - sp[1]
        bend_wire = (dx == 0) and h_edge_draw == 'blue' and n_row > 2
        ecol = '#0099ff' if h_edge_draw == 'blue' and et == 2 else 'black'

        if bend_wire:
            bend = 0.25
            mid = (sp[0] + 0.5 * dx + bend * dy, sp[1] + 0.5 * dy - bend * dx)

            pth = path.Path(
                [sp, mid, tp],
                [path.Path.MOVETO, path.Path.CURVE3, path.Path.LINETO])
            patch = patches.PathPatch(pth,
                                      edgecolor=ecol,
                                      linewidth=0.8,
                                      fill=False)
            ax.add_patch(patch)
        else:
            pos = 0.5 if dx == 0 or dy == 0 else 0.4
            mid = (sp[0] + pos * dx, sp[1] + pos * dy)
            ax.add_line(
                lines.Line2D([sp[0], tp[0]], [sp[1], tp[1]],
                             color=ecol,
                             linewidth=0.8,
                             zorder=0))

        if h_edge_draw == 'box' and et == 2:  #hadamard edge
            w = 0.2
            h = 0.15
            diag = math.sqrt(w * w + h * h)
            angle = math.atan2(dy, dx)
            angle2 = math.atan2(h, w)
            centre = (mid[0] - diag / 2 * math.cos(angle + angle2),
                      mid[1] - diag / 2 * math.sin(angle + angle2))
            ax.add_patch(
                patches.Rectangle(centre,
                                  w,
                                  h,
                                  angle=angle / math.pi * 180,
                                  facecolor='yellow',
                                  edgecolor='black'))

        #plt.plot([sp[0],tp[0]],[sp[1],tp[1]], 'k', zorder=0, linewidth=0.8)

    for v in vertices:
        p = layout[v]
        t = g.type(v)
        a = g.phase(v)
        a_offset = 0.5

        if t == VertexType.Z:
            ax.add_patch(
                patches.Circle(p,
                               0.2,
                               facecolor='green',
                               edgecolor='black',
                               zorder=1))
        elif t == VertexType.X:
            ax.add_patch(
                patches.Circle(p,
                               0.2,
                               facecolor='red',
                               edgecolor='black',
                               zorder=1))
        elif t == VertexType.H_BOX:
            ax.add_patch(
                patches.Rectangle((p[0] - 0.1, p[1] - 0.1),
                                  0.2,
                                  0.2,
                                  facecolor='yellow',
                                  edgecolor='black'))
            a_offset = 0.25
        else:
            ax.add_patch(
                patches.Circle(p,
                               0.1,
                               facecolor='black',
                               edgecolor='black',
                               zorder=1))

        if labels:
            plt.text(p[0] + 0.25,
                     p[1] + 0.25,
                     str(v),
                     ha='center',
                     color='gray',
                     fontsize=5)
        if a:
            plt.text(p[0],
                     p[1] - a_offset,
                     phase_to_s(a, t),
                     ha='center',
                     color='blue',
                     fontsize=8)

    if show_scalar:
        x = min((g.row(v) for v in g.vertices()), default=0)
        y = -sum((g.qubit(v) for v in g.vertices())) / (g.num_vertices() + 1)
        ax.text(x - 5, y, g.scalar.to_latex())

    ax.axis('equal')
    plt.close()
    return fig1
Exemple #4
0
def footprints(cam, sensor, base_elev, gui):
    """
    This function calculates the instantaneous field of view (IFOV) for 
    the camera(s) that are passed.\n
    Vars:\n
    \t cam = pandas dataframe (n x ~6, fields: x,y,z,yaw,pitch,roll)\n
    \t sensor = pandas dataframe (1 x 3, fields: focal, sensor_x, sensor_y):
    \t focal length (mm), sensor x dim (mm), sensor y dim (mm)\n
    \t base_elev = average elevation of your site (meters, or in the same
    \t measure as your coordinates)\n
    Creates approx. coordinates for sensor
    corners (north-oriented and zero pitch) at the camera's x,y,z. Rotates
    the sensor coords in 3D space to the camera's pitch and yaw angles (roll
    angles are ignored for now) and projects corner rays through the camera 
    x,y,z to a approx ground plane. The intersection of the rays with the
    ground are the corners of the photo footprint.\n
    *** Photos that have picth angles that cause the horizon to be visable will
    cause the UL and UR path coordniates to wrong. These cameras are 
    disreguarded and the footprint will be set to NaN in the output.***\n 
    RETURNS: footprints = Pandas dataframe (n x 1) of Matplotlib Path objects()
    """

    #qt progress bar
    gui.top_progBar.setValue(0)
    gui.top_progBar.setMaximum(cam.shape[0])

    # Setup DF to house camera footprint polygons
    footprints = pd.DataFrame(np.zeros((cam.shape[0], 1)), columns=['fov'])

    # debug - blank 3d array for inter_points
    #        itp_f = '//thor.ad.uni.edu/users/jdietric/Documents/Python Scripts/py_sfm_depth/WhiteR_2016/itp.npy'
    #        itp = np.zeros((cam.shape[0],4,2))

    # convert sensor dimensions to meters, divide x/y for corner coord calc
    f = sensor.focal[0] * 0.001
    sx = sensor.sensor_x[0] / 2 * 0.001
    sy = sensor.sensor_y[0] / 2 * 0.001

    # calculate the critical pitch (in degrees) where the horizon will be
    #   visible with the horizon viable, the ray projections go backward
    #   and produce erroneous IFOV polygons (90 - 0.5*vert_fov)
    crit_pitch = 90 - np.rad2deg(np.arctan(sy / f))

    # User Feedback
    print("Proccesing Camera IFOVs (%i total)..." % (cam.shape[0]))
    sys.stdout.flush()

    # for each camera...
    for idx, row in cam.iterrows():

        # check is the camera pitch is over the critical value
        if row.pitch < crit_pitch:

            # sensor corners, north-oriented and zero pitch(down look)
            #   [LL,UL,UR,LR,frame center]
            # X & Y flipped for Euler rotation frame (switched back latter)
            corner_p = np.array([[-f, -sx, -sy], [-f, -sx, sy], [-f, sx, sy],
                                 [-f, sx, -sy], [-f, 0, 0]])

            # cam x,y,z for adding to rotation
            cam_pt = np.array([row.x, row.y, row.z])

            # get pitch and yaw from the camera, in degrees
            #   translate to fit euler frame
            pitch_e = 90.0 - row.pitch
            roll_e = -row.roll
            if 0 <= row.yaw <= 180.0:
                yaw_e = row.yaw
            else:
                yaw_e = -1 * (360.0 - row.yaw)

            # create Euler rotation object form y,p,r,
            #   apply to the sensor corner points
            #   add the camera point coords to translate
            r_e = R.from_euler('ZYX', [[yaw_e, pitch_e, roll_e]], degrees=True)
            p_eC = r_e.apply(corner_p) + [cam_pt[1], cam_pt[0], cam_pt[2]]
            reord = [1, 0, 2]
            s_pts = p_eC[:, reord]

            # GEOMETRY
            # Set Sympy 3D point for the camera and a 3D plane for intersection
            cam_sp = spg.Point3D(row.x, row.y, row.z)
            plane = spg.Plane(spg.Point3D(row.x, row.y, base_elev),
                              normal_vector=(0, 0, 1))

            # blank array for footprint intersection coords
            inter_points = np.zeros((corner_p.shape[0] - 1, 2))

            # for each sensor corner point
            idx_b = 0
            for pt in np.asarray(s_pts[0:4]):

                # create a Sympy 3D point and create a Sympy 3D ray from
                #   corner point through camera point
                pt_sp = spg.Point3D(pt[0], pt[1], pt[2])
                ray = spg.Ray3D(pt_sp, cam_sp)

                # calculate the intersection of the ray with the plane
                inter_pt = plane.intersection(ray)

                # Extract out the X,Y coords from the intersection point to
                #   ground intersect points will be in this order (LL,UL,UR,LR)
                inter_points[idx_b, 0] = inter_pt[0].x.evalf()
                inter_points[idx_b, 1] = inter_pt[0].y.evalf()

                idx_b += 1

        # if crit_pitch is exceeded set inter_points to NaN
        else:
            inter_points = np.full((4, 2), np.nan)

        # append inter_points to footprints as a matplotlib path object
        footprints.fov[idx] = mplPath.Path(inter_points)

        #debug - save inter_points
        #            itp[idx,:,:] = inter_points

        # User feedback and progress bar
        if (idx + 1) % 10 == 0:
            print("%i cameras processed..." % (idx + 1))
            gui.top_progBar.setValue(idx)
            gui.topProg_Lbl.setText(
                "Calculating Camera Footprints - %i of %i" %
                (idx + 1, cam.shape[0]))
            QApplication.processEvents()
            #sys.stdout.flush()

    #debug - save inter_points
    #np.save(itp_f,itp)

    return footprints
cmap = plt.get_cmap('RdYlBu')

filein = 'real_vtu/'+args[0]
fileout = 'real_vtu/'+args[0]
contour = 'real_vtu/'+args[1]

#resolution of the regular grid
res = 2000.

#get the contour
xy = np.loadtxt(contour,delimiter=',',skiprows=1)
xy = xy[:,0:2]
lx = xy[:,0]
ly = xy[:,1]
pa = path.Path(xy)

#get the number of steps
files = glob.glob(filein+'*pvtu')
files.sort()

nbfiles = len(files)

for ff in range(nbfiles):

  fich = files[ff]
  print "The pvtu file processed is", fich

  #get the vtu data
  reader = vtk.vtkXMLPUnstructuredGridReader()
  reader.SetFileName(fich)
Exemple #6
0
                           0.1,
                           width=0.1)
    patches.append(arrow)
    label(grid[5], "Arrow")

    # add a path patch
    Path = mpath.Path
    path_data = [(Path.MOVETO, [0.018, -0.11]), (Path.CURVE4, [-0.031,
                                                               -0.051]),
                 (Path.CURVE4, [-0.115, 0.073]), (Path.CURVE4, [-0.03, 0.073]),
                 (Path.LINETO, [-0.011, 0.039]), (Path.CURVE4, [0.043, 0.121]),
                 (Path.CURVE4, [0.075, -0.005]), (Path.CURVE4, [0.035,
                                                                -0.027]),
                 (Path.CLOSEPOLY, [0.018, -0.11])]
    codes, verts = zip(*path_data)
    path = mpath.Path(verts + grid[6], codes)
    patch = mpatches.PathPatch(path)
    patches.append(patch)
    label(grid[6], "PathPatch")

    # add a fancy box
    fancybox = mpatches.FancyBboxPatch(grid[7] - [0.025, 0.05],
                                       0.05,
                                       0.1,
                                       boxstyle=mpatches.BoxStyle("Round",
                                                                  pad=0.02))
    patches.append(fancybox)
    label(grid[7], "FancyBboxPatch")

    # add a line
    x, y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]])
import json
import requests
import numpy as np
import matplotlib.path as mplPath

# Creating Yemen Bounding Box
# Latitude is up down - Longitude is left right
topLeft = [17.18, 41.81]
topRight = [19.52, 51.85]
bottomLeft = [12.25, 43.39]
bottomRight = [15.45, 53.99]
bbPath = mplPath.Path(np.array([topLeft, topRight, bottomRight, bottomLeft]))

# JSON location
jsonFile = 'tweet_locations_stripped.json'


def main():
    with open(jsonFile) as data_file:
        data = json.load(data_file)

    filtered_data = data

    for username in data:
        for text in filtered_data[username]:
            delete = True
            # Prints actual tweet
            # print text['text']

            decoded = entityExtract(text['text']).json()
Exemple #8
0
def plot_3panel(xc, yc, markersize, fignum, suptitle=[], field=[]):

    fig = plt.figure(num=fignum, figsize=[8, 12], clear=True)
    fig.clf()

    geo_axes_top_left = plt.subplot(2,
                                    2,
                                    1,
                                    projection=ccrs.NorthPolarStereo())
    geo_axes_top_right = plt.subplot(2,
                                     2,
                                     2,
                                     projection=ccrs.SouthPolarStereo())
    geo_axes_bottom = plt.subplot(2, 1, 2, projection=ccrs.Robinson())

    axes = []
    axes.append(geo_axes_top_left)
    axes.append(geo_axes_top_right)
    axes.append(geo_axes_bottom)

    circle_boundary = True

    draw_labels = False
    data_projection_code = 4326

    for axis in range(3):
        ax = axes[axis]

        if isinstance(ax.projection, ccrs.NorthPolarStereo):
            ax.set_extent([-180, 180, 45, 90], ccrs.PlateCarree())

        elif isinstance(ax.projection, ccrs.SouthPolarStereo):
            ax.set_extent([-180, 180, -90, -45], ccrs.PlateCarree())

        else:
            circle_boundary = False
            ax.set_extent([-180, 180, -90, 90], ccrs.PlateCarree())

        if circle_boundary:
            theta = np.linspace(0, 2 * np.pi, 100)
            center, radius = [0.5, 0.5], 0.5
            verts = np.vstack([np.sin(theta), np.cos(theta)]).T
            circle = mpath.Path(verts * radius + center)
            ax.set_boundary(circle, transform=ax.transAxes)

        ax.gridlines(crs=ccrs.PlateCarree(),
                     draw_labels=draw_labels,
                     linewidth=1,
                     color='black',
                     alpha=0.5,
                     linestyle='--')

        if data_projection_code == 4326:  # lat lon does nneed to be projected
            data_crs = ccrs.PlateCarree()

        else:
            data_crs = ccrs.epsg(data_projection_code)

        if len(field) == 0:
            ax.plot(xc, yc, 'b.', transform=data_crs, markersize=markersize)

        else:
            cmin = np.min(field)
            cmax = np.max(field)

            print(cmin, cmax)
            im = ax.pcolormesh(xc,yc,field,
                               transform=data_crs, \
                               vmin = cmin-1, vmax=cmax+1)

            if axis == 2:
                fig.colorbar(im, orientation='horizontal')

        ax.add_feature(cfeature.LAND)
        ax.coastlines('110m', linewidth=0.8)

    if len(suptitle) > 0:
        fig.suptitle(suptitle)

    return axes
Exemple #9
0
 def contains(polygon, point):
     return mpltPath.Path(polygon).contains_points([point])[0]
Exemple #10
0
import numpy as np
import csv
import matplotlib.pyplot as plt
from matplotlib import path
import polycrystal
import importlib
import os
importlib.reload(polycrystal)

R_grain = 60
tightness = 1.05
rad = 5

xmax = 2 * R_grain + 200
ymax = 2 * R_grain + 200
buffer = rad * tightness

theta = np.linspace(0, 2 * np.pi, 25)
x = xmax / 2 + (R_grain + buffer) * cos(theta)
y = ymax / 2 + (R_grain + buffer) * sin(theta)

angleNames = ['5', '6p25', '7p5', '8p75', '10', '15', '20', '25']
simNames = ['r60/theta' + aName for aName in angleNames]

for simName in simNames:
    "crystals/" + simName + "/0000.csv"
    windowPath = path.Path(self.windowVertices)
    inWindow = windowPath.contains_points(self.particleCenters)
def mesh_plotter(mesh_pic, image_out_path):
    """plotting and annotating domain and mesh images"""
    plt.rcParams.update({
        # "text.usetex": True,
        'mathtext.fontset': 'stix',
        'font.family': 'STIXGeneral',
        'font.size': 18,
        'figure.figsize': (14, 12),
        'lines.linewidth': 0.5,
        'lines.markersize': 0.1,
        'lines.markerfacecolor': 'white',
        'figure.dpi': 300,
    })

    circle_bg = plt.Circle((0, 0), 20, color='k', linewidth=0.5, fill=False)
    circle_overset = plt.Circle((-5, 0),
                                10,
                                color='k',
                                linewidth=0.5,
                                fill=False)

    mesh_img = mpimg.imread(mesh_pic)

    LE = [-5 + 0.5 * np.cos(45 * np.pi / 180), 0.5 * np.sin(45 * np.pi / 180)]
    TE = [-5 - 0.5 * np.cos(45 * np.pi / 180), -0.5 * np.sin(45 * np.pi / 180)]
    plate_line = [LE, TE]

    nverts = len(plate_line)
    codes = np.ones(nverts, int) * path.Path.LINETO
    codes[0::2] = path.Path.MOVETO
    plinepath = path.Path(plate_line, codes)
    platepatch = patches.PathPatch(plinepath,
                                   edgecolor='k',
                                   linewidth=1,
                                   linestyle='-')

    #-----------domain plot------------
    gs_d = dict(left=0.125,
                right=0.55,
                top=0.9,
                bottom=0.2,
                wspace=0.1,
                hspace=0.0)

    fig, axd = plt.subplots(nrows=1, ncols=1, gridspec_kw=gs_d)

    axd.add_artist(circle_bg)
    axd.add_artist(circle_overset)
    axd.add_patch(platepatch)

    axd.annotate(s='flat plate',
                 xy=(-5.0, -1.1),
                 ha='center',
                 va='center',
                 annotation_clip=False)
    axd.annotate(s='overset domain',
                 xy=(-5.0, 4.5),
                 ha='center',
                 va='center',
                 annotation_clip=False)
    axd.annotate(s='background domain',
                 xy=(0.0, 14.0),
                 ha='center',
                 va='center',
                 annotation_clip=False)
    axd.annotate(s='far-field',
                 xy=(-15.0, 16.7),
                 ha='center',
                 va='center',
                 annotation_clip=False)

    #---------mesh plot-------------------
    gs_m = fig.add_gridspec(nrows=1,
                            ncols=1,
                            left=0.55,
                            right=0.9,
                            top=0.51,
                            bottom=0.31,
                            wspace=0.1,
                            hspace=0.0)

    axm = fig.add_subplot(gs_m[0, 0])
    axm.imshow(mesh_img, origin='upper', interpolation='bicubic')

    axd.set_xlim([-22, 22])
    axd.set_ylim([-22, 22])
    axd.set_aspect('equal')
    axd.set_xlabel('x/c')
    axd.set_ylabel('y/c')
    axm.set_xlim([400, 1400])
    axm.set_xticks([])
    axm.set_yticks([])

    oimage_file = os.path.join(image_out_path, 'domain_mesh.png')
    plt.savefig(oimage_file)
Exemple #12
0
def occ_full_circle(station,
                    year,
                    month_range=None,
                    day_range=None,
                    gate_range=None,
                    beam_range=None,
                    time_units='mlt',
                    plot_type='contour',
                    local_testing=False):
    """

    Produce a full circle stereographic plot in either ut or mlt (12 at the top).
    Can plot a simple echo count, ground scatter count, or average a fitACF parameter over the provided time range.

    Notes:
        - This program was originally written to be run on maxwell.usask.ca.  This decision was made because
            occurrence investigations often require chewing large amounts of data.
        - Does not distinguish frequency
        - Only considers 45 km data.
            (a warning will be printed if other spatial resolution data is stripped from the dataset)
        - This program uses fitACF 3.0 data.  To change this, modify the source code.

    :param station: str:
            The radar station to consider, a 3 character string (e.g. "rkn").
            For a complete listing of available stations, please see https://superdarn.ca/radar-info
    :param year: int:
            The year to consider.
    :param month_range: (<int>, <int>) (optional):
            Inclusive. The months of the year to consider.  If omitted (or None), then all days will be considered.
    :param day_range: (<int>, <int>) (optional):
            Inclusive. The days of the month to consider.  If omitted (or None), then all days will be considered.
    :param gate_range: (<int>, <int>) (optional):
            Inclusive. The gate range to consider.  If omitted (or None), then all the gates will be considered.
            Note that gates start at 0, so gates (0, 3) is 4 gates.
    :param beam_range: (<int>, <int>) (optional):
            Inclusive. The beam range to consider.  If omitted (or None), then all beams will be considered.
            Note that beams start at 0, so beams (0, 3) is 4 beams.
    :param time_units: str: 'ut' for universal time or 'mlt' for magnetic local time:
            The time units to plot on the circle, 12 is always at the top.  Default is 'mlt'
    :param plot_type: str (optional):
            The type of plot, either 'contour' or 'pixel', default is 'contour'
    :param local_testing: bool (optional):
            Set this to true if you are testing on your local machine.  Program will then use local dummy data.
    :return: matplotlib.pyplot.figure: The figure.
            It can then be modified, added to, printed out, or saved in whichever file format is desired.
    """

    time_units = check_time_units(time_units)
    year = check_year(year)

    all_radars_info = SuperDARNRadars()
    this_radars_info = all_radars_info.radars[pydarn.read_hdw_file(
        station).stid]  # Grab radar info
    radar_id = this_radars_info.hardware_info.stid
    hemisphere = this_radars_info.hemisphere
    radar_lon = this_radars_info.hardware_info.geographic.lon
    radar_lat = this_radars_info.hardware_info.geographic.lat

    gate_range = check_gate_range(gate_range, this_radars_info.hardware_info)
    beam_range = check_beam_range(beam_range, this_radars_info.hardware_info)

    print("Retrieving data...")
    df = get_data_handler(station,
                          year_range=(year, year),
                          month_range=month_range,
                          day_range=day_range,
                          gate_range=gate_range,
                          beam_range=beam_range,
                          occ_data=True,
                          local_testing=local_testing)
    df = only_keep_45km_res_data(df)

    # To compute mlt we need longitudes.. use the middle of the month as magnetic field estimate
    date_time_est, _ = build_datetime_epoch(year, 6, 15, 0)

    print("Computing MLTs for " + str(year) + " data...")
    cell_corners_aacgm_lats, cell_corners_aacgm_lons = radar_fov(
        stid=radar_id, coords='aacgm', date=date_time_est)

    df = add_mlt_to_df(cell_corners_aacgm_lons=cell_corners_aacgm_lons,
                       cell_corners_aacgm_lats=cell_corners_aacgm_lats,
                       df=df)

    # Get our raw x-data
    if time_units == "mlt":
        df['xdata'] = df['mlt']

    else:
        print("Computing UTs for " + str(year) + " data...")

        ut_time = []
        for i in range(len(df)):
            ut_time_here = df['datetime'].iat[i].hour + df['datetime'].iat[i].minute / 60 + \
                           df['datetime'].iat[i].second / 3600

            if ut_time_here > 24:
                ut_time_here = ut_time_here - 24
            elif ut_time_here < 0:
                ut_time_here = ut_time_here + 24

            ut_time.append(ut_time_here)

        df['xdata'] = np.asarray(ut_time)

    print("Preparing the plot...")
    fig = plt.figure(figsize=(5, 5), dpi=300)
    lat_extreme = 40 * hemisphere.value  # deg
    if hemisphere.value == 1:
        ax = fig.add_subplot(1, 1, 1, projection=ccrs.NorthPolarStereo())
    elif hemisphere.value == -1:
        ax = fig.add_subplot(1, 1, 1, projection=ccrs.SouthPolarStereo())
    else:
        raise Exception("hemisphere not recognized")

    ax.set_extent([-180, 180, hemisphere.value * 90, lat_extreme],
                  crs=ccrs.PlateCarree())

    # Compute a circle in axis coordinates which can be used as a boundary
    theta = np.linspace(0, 2 * np.pi, 100)
    center, radius = [0.5, 0.5], 0.5
    vertices = np.vstack([np.sin(theta), np.cos(theta)]).T
    circle = mpath.Path(vertices * radius + center)
    ax.set_boundary(circle, transform=ax.transAxes)

    # Add gridlines and mlt labels
    text_offset_multiplier = 1.03
    gl = ax.gridlines(draw_labels=True, linestyle='--', zorder=5)
    gl.xlocator = mticker.FixedLocator([-180, -135, -90, -45, 0, 45, 90, 135])
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER

    # Print clock numbers
    ax.text(0,
            text_offset_multiplier * ax.get_ylim()[1],
            "12",
            ha='center',
            va='bottom')
    ax.text(0,
            text_offset_multiplier * ax.get_ylim()[0],
            "00",
            ha='center',
            va='top')
    ax.text(text_offset_multiplier * ax.get_xlim()[1],
            0,
            "06",
            ha='left',
            va='center')
    ax.text(text_offset_multiplier * ax.get_xlim()[0],
            0,
            "18",
            ha='right',
            va='center')

    # Print time units
    ax.text(text_offset_multiplier * ax.get_xlim()[0],
            text_offset_multiplier * ax.get_ylim()[1],
            time_units.upper(),
            ha='left',
            va='bottom')

    # Convert radar coordinates to aacgm and plot radar track
    radar_lat_aacgm, radar_lon_aacgm, radar_mlt = get_aacgm_coord(
        radar_lat, radar_lon, 0, date_time_est)
    radar_mlts = np.arange(0, 360, 1)
    radar_lats_aacgm = np.asarray([radar_lat_aacgm] * len(radar_mlts))
    ax.plot(radar_mlts,
            radar_lats_aacgm,
            color='k',
            linewidth=0.5,
            linestyle="--",
            transform=ccrs.Geodetic(),
            label="Radar Path")

    # Right now xdata is in the range 0-24, we need to put it in the range 0-360 for circular plotting
    df['xdata'] = 15 * df['xdata']

    print("Computing binned occ rates...")
    # Compute mlt edges
    deg_mlt_per_bin = 2
    n_bins_mlt = int(360 / deg_mlt_per_bin)
    mlt_edges = np.linspace(0, 360, num=(n_bins_mlt + 1))
    delta_mlt = mlt_edges[1] - mlt_edges[0]

    # Compute latitude edges
    n_bins_lat = 90 - abs(lat_extreme)  # One bin per degree of latitude
    lat_edges = np.linspace(lat_extreme, 90, num=(n_bins_lat + 1))
    delta_lat = lat_edges[1] - lat_edges[0]

    contour_data = np.empty(shape=(n_bins_mlt, n_bins_lat))
    contour_data[:] = math.nan

    for mlt_idx, start_mlt in enumerate(mlt_edges):
        if start_mlt == mlt_edges[-1]:
            continue  # The last edge is not a start
        end_mlt = start_mlt + delta_mlt
        df_mlt = df[(df['xdata'] >= start_mlt) & (df['xdata'] <= end_mlt)]

        for lat_idx, start_lat in enumerate(lat_edges):
            if start_lat == lat_edges[-1]:
                continue  # The last edge is not a start

            end_lat = start_lat + delta_lat
            df_mlt_lat = df_mlt[(df_mlt['lat'] >= start_lat)
                                & (df_mlt['lat'] <= end_lat)]

            try:
                contour_data[mlt_idx][lat_idx] = sum(
                    df_mlt_lat['good_echo']) / len(df_mlt_lat)
            except ZeroDivisionError:
                # There are no point in this interval
                contour_data[mlt_idx, lat_idx] = math.nan
            except BaseException as e:
                print("MLT index: " + str(mlt_idx))
                print("LAT index: " + str(lat_idx))
                raise e

    contour_range = [[0, 360], [hemisphere.value * 37, hemisphere.value * 90]]

    # Compute bin centers
    bin_xwidth = (mlt_edges[1] - mlt_edges[0])
    bin_ywidth = (lat_edges[1] - lat_edges[0])
    bin_xcenters = mlt_edges[1:] - bin_xwidth / 2
    bin_ycenters = lat_edges[1:] - bin_ywidth / 2

    levels = 12
    levels = np.linspace(start=0, stop=1, num=(levels + 1))
    if plot_type == "contour":
        plot = ax.contourf(bin_xcenters,
                           bin_ycenters,
                           contour_data.transpose(),
                           cmap='jet',
                           levels=levels,
                           transform=ccrs.PlateCarree())

    elif plot_type == "pixel":
        plot = ax.imshow(np.flip(contour_data.transpose(), axis=0),
                         aspect='auto',
                         cmap="jet",
                         transform=ccrs.PlateCarree())

    else:
        raise Exception("plot_type not recognized")

    cbar = fig.colorbar(plot,
                        ax=ax,
                        shrink=0.75,
                        orientation="horizontal",
                        format='%.2f')
    cbar.ax.tick_params(labelsize=14, labelrotation=30)

    return df, fig
Exemple #13
0
def mesh_plotter(image_out_path):
    """plotting and annotating domain and mesh images"""
    plt.rcParams.update({
        # "text.usetex": True,
        'mathtext.fontset': 'stix',
        'font.family': 'STIXGeneral',
        'font.size': 19,
        'figure.figsize': (10, 10),
        'lines.linewidth': 4,
        'lines.markersize': 0.1,
        'lines.markerfacecolor': 'k',
        'figure.dpi': 300,
    })

    circle_bg = plt.Circle((0, 0), 20, color='k', linewidth=4, fill=False)
    circle_overset = plt.Circle((-3, 0),
                                10,
                                color='k',
                                linewidth=4,
                                fill=False)

    LE = [-3 + 0.5 * np.cos(45 * np.pi / 180), 0.5 * np.sin(45 * np.pi / 180)]
    TE = [-3 - 0.5 * np.cos(45 * np.pi / 180), -0.5 * np.sin(45 * np.pi / 180)]
    plate_line = [LE, TE]

    nverts = len(plate_line)
    codes = np.ones(nverts, int) * path.Path.LINETO
    codes[0::2] = path.Path.MOVETO
    plinepath = path.Path(plate_line, codes)
    platepatch = patches.PathPatch(plinepath,
                                   edgecolor='k',
                                   linewidth=2,
                                   linestyle='-')

    #-----------domain plot------------
    gs_d = dict(left=0.125,
                right=0.9,
                top=0.9,
                bottom=0.2,
                wspace=0.1,
                hspace=0.0)

    fig, axd = plt.subplots(nrows=1, ncols=1, gridspec_kw=gs_d)
    axd.spines['right'].set_visible(False)
    axd.spines['top'].set_visible(False)
    # make arrows
    axd.plot((1), (0),
             ls="",
             marker=">",
             ms=8,
             color="k",
             transform=axd.transAxes,
             clip_on=False)
    axd.plot((0), (1),
             ls="",
             marker="^",
             ms=8,
             color="k",
             transform=axd.transAxes,
             clip_on=False)

    axd.add_artist(circle_bg)
    axd.add_artist(circle_overset)
    axd.add_patch(platepatch)

    axd.annotate(s='Flat plate',
                 xy=(-5.5, -1.5),
                 ha='center',
                 va='center',
                 annotation_clip=False)
    # axd.annotate(s='Domain boundary',
    # xy=(8.2, -8.0),
    # ha='center',
    # va='center',
    # annotation_clip=False)
    axd.annotate(s='Overset domain',
                 xy=(-5.0, 4.5),
                 ha='center',
                 va='center',
                 annotation_clip=False)
    axd.annotate(s='Background domain',
                 xy=(0.0, 14.0),
                 ha='center',
                 va='center',
                 annotation_clip=False)
    axd.annotate(s='Far field',
                 xy=(-16.0, 16.0),
                 ha='center',
                 va='center',
                 annotation_clip=False)

    axd.set_xlim([-22, 22])
    axd.set_ylim([-22, 22])
    axd.set_aspect('equal')
    axd.set_xlabel(r'$x/c$')
    axd.set_ylabel(r'$y/c$')

    oimage_file = os.path.join(image_out_path, 'domain.svg')
    plt.savefig(oimage_file)
Exemple #14
0
def set_map_boundary(ax,
                     lon_range,
                     lat_range,
                     north_pad=0,
                     south_pad=0,
                     east_pad=0,
                     west_pad=0,
                     res=1):
    """Utility function to set the boundary of ax to a path that surrounds a
    given region specified by latitude and longitude coordinates. This boundary
    is drawn in the projection coordinates and therefore follows any curves
    created by the projection. As of now, this only works consistently for the
    Lambert Conformal Projection and North/South Polar Stereographic
    Projections.

    Note: Due to the behavior of cartopy's set_extent() function, the curved
    edges of the boundary may be flattened and cut off. To solve this, use the
    kwargs north_pad, south_pad, east_pad, and west_pad. These will modify the
    coordinates passed to set_extent(). For the Lambert Conformal and Polar
    Stereographic projections, typically only north_pad and south_pad are
    needed. If attempting to use this function for other projections
    (i.e. Othographic) east_pad and west_pad may be needed.

    Args:
        ax (:class:`matplotlib.axes`):
            The axes to which the boundary will be applied.

        lon_range (:class:`tuple` or :class:`list`):
            The two-tuple containing the start and end of the desired range of
            longitudes. The first entry must be smaller than the second entry,
            except when the region crosses the antimeridian. Both entries must
            be between [-180 , 180]. If lon_range is from -180 to 180, then a
            full circle centered on the pole with a radius from the pole to the
            lowest latitude given by lat_range will be set as the boundary.

        lat_range (:class:`tuple` or :class:`list`):
            The two-tuple containing the start and end of the desired range of
            latitudes. The first entry must be smaller than the second entry.
            Both entries must be between [-90 , 90].

        north_pad (:class:`int`):
            A constant to be added to the second entry in lat_range. Use this
            if the northern edge of the plot is cut off. Defaults to 0.

        south_pad (:class:`int`);
            A constant to be subtracted from the first entry in lat_range. Use
            this if the southern edge of the plot is cut off. Defaults to 0.

        east_pad (:class:`int`):
            A constant to be added to the second entry in lon_range. Use this
            if the eastern edge of the plot is cut off. Defaults to 0.

        west_pad (:class:`int`):
            A constant to be subtracted from the first entry in lon_range. Use
            this if the western edge of the plot is cut off. Defaults to 0.

        res (:class:`int`):
            The size of the incrementation for vertices in degrees. Default is
            a vertex every one degree of longitude. A higher number results in
            a lower resolution boundary.
    """
    import cartopy.crs as ccrs
    import matplotlib.path as mpath

    if lon_range[0] >= lon_range[1]:
        if not (lon_range[0] > 0 > lon_range[1]):
            raise ValueError(
                "The first longitude value must be strictly less than the second longitude value unless the region crosses over the antimeridian"
            )

    if lat_range[0] >= lat_range[1]:
        raise ValueError(
            "The first latitude value must be strictly less than the second latitude value"
        )

    if (lon_range[0] > 180 or lon_range[0] < -180 or lon_range[1] > 180
            or lon_range[1] < -180):
        raise ValueError(
            "The longitudes must be within the range [-180, 180] inclusive")

    if (lat_range[0] > 90 or lat_range[0] < -90 or lat_range[1] > 90
            or lat_range[1] < -90):
        raise ValueError(
            "The latitudes must be within the range [-90, 90] inclusive")

    # Make a boundary path in PlateCarree projection beginning in the south
    # west and continuing anticlockwise creating a point every `res` degree
    if lon_range[0] >= 0 >= lon_range[
            1]:  # Case when range crosses antimeridian
        vertices = [(lon, lat_range[0]) for lon in range(lon_range[0], 180 + 1, res)] + \
                   [(lon, lat_range[0]) for lon in range(-180, lon_range[1] + 1, res)] + \
                   [(lon_range[1], lat) for lat in range(lat_range[0], lat_range[1] + 1, res)] + \
                   [(lon, lat_range[1]) for lon in range(lon_range[1], -180 - 1, -res)] + \
                   [(lon, lat_range[1]) for lon in range(180, lon_range[0] - 1, -res)] + \
                   [(lon_range[0], lat) for lat in range(lat_range[1], lat_range[0] - 1, -res)]
        path = mpath.Path(vertices)
    elif ((lon_range[0] == 180 or lon_range[0] == -180)
          and (lon_range[1] == 180 or lon_range[1] == -180)):
        vertices = [(lon, lat_range[0]) for lon in range(0, 360 + 1, res)]
        path = mpath.Path(vertices)
    else:
        vertices = [(lon, lat_range[0]) for lon in range(lon_range[0], lon_range[1] + 1, res)] + \
                   [(lon_range[1], lat) for lat in range(lat_range[0], lat_range[1] + 1, res)] + \
                   [(lon, lat_range[1]) for lon in range(lon_range[1], lon_range[0] - 1, -res)] + \
                   [(lon_range[0], lat) for lat in range(lat_range[1], lat_range[0] - 1, -res)]
        path = mpath.Path(vertices)

    proj_to_data = ccrs.PlateCarree()._as_mpl_transform(ax) - ax.transData
    ax.set_boundary(proj_to_data.transform_path(path))

    ax.set_extent([
        lon_range[0] - west_pad, lon_range[1] + east_pad,
        lat_range[0] - south_pad, lat_range[1] + north_pad
    ],
                  crs=ccrs.PlateCarree())
Exemple #15
0
#         cv2.destroyAllWindows()
#
#
# if __name__ == '__main__':
#     import sys
#     try:
#         video_src = sys.argv[1]
#     except:
#         video_src = 0
#     print(__doc__)
#     App(video_src).run()


from matplotlib import path
import matplotlib.pyplot as plt
import numpy as np

first = -3
size  = (3-first)/100
xv,yv = np.meshgrid(np.linspace(-3,3,100),np.linspace(-3,3,100))
p = path.Path([(0,0), (0, 1), (1, 1), (1, 0)])  # square with legs length 1 and bottom left corner at the origin
flags = p.contains_points(np.hstack((xv.flatten()[:,np.newaxis],yv.flatten()[:,np.newaxis])))
grid = np.zeros((101,101),dtype='bool')
grid[((xv.flatten()-first)/size).astype('int'),((yv.flatten()-first)/size).astype('int')] = flags

xi,yi = np.random.randint(-300,300,100)/100,np.random.randint(-300,300,100)/100
vflag = grid[((xi-first)/size).astype('int'),((yi-first)/size).astype('int')]
plt.imshow(grid.T,origin='lower',interpolation='nearest',cmap='binary')
plt.scatter(((xi-first)/size).astype('int'),((yi-first)/size).astype('int'),c=vflag,cmap='Greens',s=90)
plt.show()
    def _create_histograms(self):

        # Histograms
        self.nbins = 20  # unmber of bins
        xn, xbins = np.histogram(self.data_series[0][2], bins=self.nbins)
        yn, ybins = np.histogram(self.data_series[0][3], bins=self.nbins)

        # get edges of histogram bars
        x_left = np.array(xbins[:-1])
        y_left = np.array(ybins[:-1])
        x_right = np.array(xbins[:-1])
        y_right = np.array(ybins[:-1])
        x_bottom = np.zeros(self.nbins)
        y_bottom = np.zeros(self.nbins)
        x_top = xn
        y_top = yn

        num_verts = self.nbins * (1 + 3 + 1
                                  )  # 1 move to, 3 vertices, 1 close poly
        self.verts = np.zeros(shape=(2, num_verts,
                                     2))  # (axis, value, coordinate)

        # x axis
        self.verts[0, 0::5, 0] = x_left
        self.verts[0, 0::5, 1] = x_bottom
        self.verts[0, 1::5, 0] = x_left
        self.verts[0, 1::5, 1] = x_top
        self.verts[0, 2::5, 0] = x_right
        self.verts[0, 2::5, 1] = x_top
        self.verts[0, 3::5, 0] = x_right
        self.verts[0, 3::5, 1] = x_bottom

        # y axis
        self.verts[1, 0::5, 0] = y_bottom
        self.verts[1, 0::5, 1] = y_left
        self.verts[1, 1::5, 0] = y_top
        self.verts[1, 1::5, 1] = y_left
        self.verts[1, 2::5, 0] = y_top
        self.verts[1, 2::5, 1] = y_right
        self.verts[1, 3::5, 0] = y_bottom
        self.verts[1, 3::5, 1] = y_right

        # Drawing Codes
        codes = np.ones((num_verts), int) * path.Path.LINETO  # Instructions
        codes[0::5] = path.Path.MOVETO
        codes[4::5] = path.Path.CLOSEPOLY

        x_path = path.Path(self.verts[0], codes)
        y_path = path.Path(self.verts[1], codes)

        self.x_patch = patches.PathPatch(x_path,
                                         facecolor='#FA6367',
                                         edgecolor='#78C9EC',
                                         linewidth=15,
                                         alpha=1)
        self.y_patch = patches.PathPatch(y_path,
                                         facecolor='#FA6367',
                                         edgecolor='#78C9EC',
                                         linewidth=15,
                                         alpha=1)

        self.ax2.add_patch(self.x_patch)
        self.ax3.add_patch(self.y_patch)

        self.ax2.set_xlim(xbins[0], xbins[-1])
        self.ax3.set_ylim(ybins[0], ybins[-1])
        self.ax2.set_ylim(x_bottom.min(), x_top.max())
        self.ax3.set_xlim(y_bottom.min(), y_top.max())
Exemple #17
0
fig, ax = plt.subplots()

Path = mpath.Path
path_data = [
    (Path.MOVETO, (1.58, -2.57)),
    (Path.CURVE4, (0.35, -1.1)),
    (Path.CURVE4, (-1.75, 2.0)),
    (Path.CURVE4, (0.375, 2.0)),
    (Path.LINETO, (0.85, 1.15)),
    (Path.CURVE4, (2.2, 3.2)),
    (Path.CURVE4, (3, 0.05)),
    (Path.CURVE4, (2.0, -0.5)),
    (Path.CLOSEPOLY, (1.58, -2.57)),
]
codes, verts = zip(*path_data)
path = mpath.Path(verts, codes)
patch = mpatches.PathPatch(path, facecolor='r', alpha=0.5)
ax.add_patch(patch)

# plot control points and connecting lines
x, y = zip(*path.vertices[:-1])
points = ax.plot(x, y, 'go', ms=10)
line = ax.plot(x, y, '-k')

ax.grid(True, color='gray', alpha=0.5)
ax.axis('equal')
ax.set_title("Drag Points to Change Path", fontsize=18)

plugins.connect(fig, LinkedDragPlugin(points[0], line[0], patch))

mpld3.show()
Exemple #18
0
def animate_results(mu, var, theta_hist, n_bins=20):
    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    import matplotlib.path as path
    import matplotlib.animation as animation

    fig, ax = plt.subplots()

    # histogram our data with numpy
    data = theta_hist[:, 0]
    n, bins = np.histogram(data, bins=n_bins, density=True)

    # get the corners of the rectangles for the histogram
    left = np.array(bins[:-1])
    right = np.array(bins[1:])
    bottom = np.zeros(len(left))
    top = bottom + n
    nrects = len(left)

    # here comes the tricky part -- we have to set up the vertex and path
    # codes arrays using moveto, lineto and closepoly

    # for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
    # CLOSEPOLY; the vert for the closepoly is ignored but we still need
    # it to keep the codes aligned with the vertices
    nverts = nrects * (1 + 3 + 1)
    verts = np.zeros((nverts, 2))
    codes = np.ones(nverts, int) * path.Path.LINETO
    codes[0::5] = path.Path.MOVETO
    codes[4::5] = path.Path.CLOSEPOLY
    verts[0::5, 0] = left
    verts[0::5, 1] = bottom
    verts[1::5, 0] = left
    verts[1::5, 1] = top
    verts[2::5, 0] = right
    verts[2::5, 1] = top
    verts[3::5, 0] = right
    verts[3::5, 1] = bottom

    barpath = path.Path(verts, codes)
    patch = patches.PathPatch(barpath,
                              facecolor='blue',
                              edgecolor='blue',
                              alpha=0.5)
    ax.add_patch(patch)

    ax.set_xlim(mu - 3 * np.sqrt(var), mu + 3 * np.sqrt(var))
    ax.set_ylim(bottom.min(), 0.5)

    x = np.linspace(mu - 3 * np.sqrt(var), mu + 3 * np.sqrt(var), 100)
    plt.plot(x, mlab.normpdf(x, mu, np.sqrt(var)))

    def animate(i):
        # simulate new data coming in
        data = theta_hist[:, int(i * 25)]
        n, bins = np.histogram(data, bins=n_bins, density=True)
        left = np.array(bins[:-1])
        right = np.array(bins[1:])
        bottom = np.zeros(len(left))
        top = bottom + n
        nrects = len(left)

        # here comes the tricky part -- we have to set up the vertex and path
        # codes arrays using moveto, lineto and closepoly

        # for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
        # CLOSEPOLY; the vert for the closepoly is ignored but we still need
        # it to keep the codes aligned with the vertices
        nverts = nrects * (1 + 3 + 1)
        verts = np.zeros((nverts, 2))
        verts[0::5, 0] = left
        verts[0::5, 1] = bottom
        verts[1::5, 0] = left
        verts[1::5, 1] = top
        verts[2::5, 0] = right
        verts[2::5, 1] = top
        verts[3::5, 0] = right
        verts[3::5, 1] = bottom

        [p.remove() for p in reversed(ax.patches)]
        barpath = path.Path(verts, codes)
        patch = patches.PathPatch(barpath,
                                  facecolor='blue',
                                  edgecolor='blue',
                                  alpha=0.5)
        ax.add_patch(patch)

        # ax.set_xlim(left[0], right[-1])
        # ax.set_ylim(bottom.min(), top.max())

        # plt.plot(bins, 1/(np.sqrt(var) * np.sqrt(2 * np.pi)) *
        # np.exp( - (bins - mu)**2 / (2 * np.sqrt(var)**2) ),
        # linewidth=2, color='r')

        return [
            patch,
        ]

    ani = animation.FuncAnimation(fig,
                                  animate,
                                  int(theta_hist.shape[1] / 25),
                                  repeat=False,
                                  blit=False)
    plt.show()
    # Get TGraph and convert to arrays
    infile = ROOT.TFile.Open(inputDir + scenario_files[scenario], "READ")
    curve = infile.Get("obs_contour")
    xvals = []
    yvals = []
    for i in range(curve.GetN()):
        xvals.append(curve.GetPointX(i))
        yvals.append(curve.GetPointY(i))

    # Now make a curve
    vertices = np.column_stack((xvals, yvals))
    # Doesn't seem to be working?
    treatments = [Path.MOVETO] + [Path.LINETO] * (vertices.size - 1)

    string_path = mpath.Path(vertices)  #, treatments)
    patch = mpatches.PathPatch(string_path,
                               edgecolor="red",
                               facecolor=None,
                               fill=False,
                               lw=2)

    text_here = "{0}\ng$_{4}$={1}, g$_{5}$={2}, g$_{6}$={3}".format(
        ("Axial-vector" if this_scenario["model"] == "AV" else "Vector"),
        this_scenario["gq"], this_scenario["gDM"], this_scenario["gl"], "q",
        "\chi", "l")
    make_plot(scenarios[scenario]["xvals"],
              scenarios[scenario]["yvals"],
              scenarios[scenario]["zvals"],
              scenario + "_compare" + plot_tag,
              addText=text_here,
Exemple #20
0
def map(h2: Histogram2D,
        ax: Axes,
        *,
        show_zero: bool = True,
        show_values: bool = False,
        show_colorbar: bool = True,
        x=None,
        y=None,
        **kwargs):
    """Coloured-rectangle plot of 2D histogram.

    Parameters
    ----------
    show_zero : Whether to show coloured box for bins with 0 frequency (otherwise background).
    show_values : Whether to show labels with frequencies/densities in the middle of the bin


    text_color : Optional
        Colour of text descriptions
    text_alpha : Optional[float]
        Alpha for the text labels only
    x : Optional[Callable]
        Transformation of x bin coordinates
    y : Optional[Callable]
        Transformation of y bin coordinates
    zorder : float
        z-order in the axis (higher number above lower)

    See Also
    --------
    image, polar_map, surface_map

    Notes
    -----
    If you transform axes using x or y parameters, the deduction of axis limits
    does not work well automatically. Please, make sure to attend to it yourself.
    The densities in transformed maps are calculated from original bins.
    """
    # Detect transformation
    transformed = False
    if x is not None or y is not None:
        if not x:
            x = lambda x, y: x
        if not y:
            y = lambda x, y: y
        transformed = True

    value_format = kwargs.pop("value_format", lambda x: str(x))
    # TODO: Implement correctly the text_kwargs

    if isinstance(value_format, str):
        format_str = "{0:" + value_format + "}"
        value_format = lambda x: format_str.format(x)

    rect_args = {}
    if "zorder" in kwargs:
        rect_args["zorder"] = kwargs.pop("zorder")

    data = get_data(h2,
                    cumulative=False,
                    flatten=True,
                    density=kwargs.pop("density", False))

    cmap = _get_cmap(kwargs)
    norm, cmap_data = _get_cmap_data(data, kwargs)
    colors = cmap(cmap_data)

    xpos, ypos = (arr.flatten() for arr in h2.get_bin_left_edges())
    dx, dy = (arr.flatten() for arr in h2.get_bin_widths())
    text_x, text_y = (arr.flatten() for arr in h2.get_bin_centers())

    _apply_xy_lims(ax, h2, data=data, kwargs=kwargs)
    _add_labels(ax, h2, kwargs)

    ax.autoscale_view()

    alphas = _get_alpha_data(cmap_data, kwargs)
    if np.isscalar(alphas):
        alphas = np.ones_like(data) * alphas

    for i in range(len(xpos)):
        bin_color = colors[i]
        alpha = alphas[i]

        if data[i] != 0 or show_zero:
            if not transformed:
                rect = plt.Rectangle([xpos[i], ypos[i]],
                                     dx[i],
                                     dy[i],
                                     facecolor=bin_color,
                                     edgecolor=kwargs.get(
                                         "grid_color", cmap(0.5)),
                                     lw=kwargs.get("lw", 0.5),
                                     alpha=alpha,
                                     **rect_args)
                tx, ty = text_x[i], text_y[i]

            else:
                # See http://matplotlib.org/users/path_tutorial.html
                points = ((xpos[i], ypos[i]), (xpos[i] + dx[i], ypos[i]),
                          (xpos[i] + dx[i], ypos[i] + dy[i]),
                          (xpos[i], ypos[i] + dy[i]), (xpos[i], ypos[i]))

                verts = [(x(*p), y(*p)) for p in points]

                codes = [
                    path.Path.MOVETO,
                    path.Path.LINETO,
                    path.Path.LINETO,
                    path.Path.LINETO,
                    path.Path.CLOSEPOLY,
                ]

                rect_path = path.Path(verts, codes)
                rect = patches.PathPatch(rect_path,
                                         facecolor=bin_color,
                                         edgecolor=kwargs.get(
                                             "grid_color", cmap(0.5)),
                                         lw=kwargs.get("lw", 0.5),
                                         alpha=alpha,
                                         **rect_args)

                tx = x(text_x[i], text_y[i])
                ty = y(text_x[i], text_y[i])
            ax.add_patch(rect)

            if show_values:
                text = value_format(data[i])
                yiq_y = np.dot(bin_color[:3], [0.299, 0.587, 0.114])

                text_color = kwargs.get("text_color", None)
                if not text_color:
                    if yiq_y > 0.5:
                        text_color = (0.0, 0.0, 0.0,
                                      kwargs.get("text_alpha", alpha))
                    else:
                        text_color = (1.0, 1.0, 1.0,
                                      kwargs.get("text_alpha", alpha))
                ax.text(tx,
                        ty,
                        text,
                        horizontalalignment='center',
                        verticalalignment='center',
                        color=text_color,
                        clip_on=True,
                        **rect_args)

    if show_colorbar:
        _add_colorbar(ax, cmap, cmap_data, norm)
    # Generate the grid points
    Xgrid = np.linspace(xVals[0], xVals[1],
                        nGridX)  # X-values in evenly spaced grid
    Ygrid = np.linspace(yVals[0], yVals[1],
                        nGridY)  # Y-values in evenly spaced grid
    XX, YY = np.meshgrid(Xgrid,
                         Ygrid)  # Create meshgrid from X and Y grid arrays

    # Initialize velocities
    Vx = np.zeros([nGridX, nGridY])  # Initialize X velocity matrix
    Vy = np.zeros([nGridX, nGridY])  # Initialize Y velocity matrix

    # Path to figure out if grid point is inside polygon or not
    AF = np.vstack((XB.T, YB.T)).T  # Concatenate XB and YB geometry points
    afPath = path.Path(AF)  # Create a path for the geometry

    # Solve for grid point X and Y velocities
    for m in range(nGridX):  # Loop over X grid points
        for n in range(nGridY):  # Loop over Y grid points
            XP = XX[m, n]  # Current iteration's X grid point
            YP = YY[m, n]  # Current iteration's Y grid point
            Mx, My = STREAMLINE_SPM(XP, YP, XB, YB, phi,
                                    S)  # Compute Mx and My geometric integrals

            # Check if grid points are in object
            # - If they are, assign a velocity of zero
            if afPath.contains_points([(XP, YP)]):  # If (XP,YP) is in the body
                Vx[m, n] = 0  # X-velocity is zero
                Vy[m, n] = 0  # Y-velocity is zero
            else:
Exemple #22
0
def backgroundFalseNegErrors( coco_analyze, imgs_info, saveDir ):
    loc_dir = saveDir + '/background_errors/false_negatives'
    if not os.path.exists(loc_dir):
        os.makedirs(loc_dir)
    f = open('%s/std_out.txt'%loc_dir, 'w')
    f.write("Running Analysis: [False Negatives]\n\n")
    tic = time.time()
    paths = {}

    oksThrs     = [.5,.55,.6,.65,.7,.75,.8,.85,.9,.95]
    areaRngs    = [[32**2,1e5**2]]
    areaRngLbls = ['all']

    coco_analyze.params.areaRng    = areaRngs
    coco_analyze.params.areaRngLbl = areaRngLbls
    coco_analyze.params.oksThrs    = oksThrs
    coco_analyze.cocoEval.params.useGtIgnore = 0
    coco_analyze.cocoEval.params.gtIgnoreIds = []
    coco_analyze.analyze(check_kpts=False, check_scores=False, check_bckgd=True)

    badFalseNeg = coco_analyze.false_neg_gts['all',str(.5)]
    for tind, t in enumerate(coco_analyze.params.oksThrs):
        badFalseNeg = badFalseNeg & coco_analyze.false_neg_gts['all',str(t)]
    # bad false negatives are those that are false negatives at all oks thresholds
    fn_gts = [coco_analyze.cocoGt.loadAnns(b)[0] for b in badFalseNeg]

    f.write("Num. annotations: [%d]\n"%len(coco_analyze.cocoGt.loadAnns(coco_analyze.cocoGt.getAnnIds())))
    for oks in oksThrs:
        f.write("OKS thresh: [%f]\n"%oks)
        f.write(" - Matches:      [%d]\n"%len(coco_analyze.bckgd_err_matches[areaRngLbls[0], str(oks), 'gts']))
        f.write(" - Bckgd. FN:    [%d]\n"%len(coco_analyze.false_neg_gts[areaRngLbls[0],str(oks)]))

    sorted_fns = sorted(fn_gts, key=lambda k: -k['num_keypoints'])
    sorted_fns = [fff for fff in sorted_fns if fff['num_keypoints']>0]
    show_fn = sorted_fns[0:4] + sorted_fns[-4:]
    f.write("\nBackground False Negative Errors:\n")
    for tind, t in enumerate(show_fn):
        name    = 'bckd_false_neg_%d'%tind
        paths[name] = "%s/%s.pdf"%(loc_dir,name)
        f.write("Image_id, ground_truth id, num_keypoints: [%d][%d][%d]\n"%(t['image_id'],t['id'],t['num_keypoints']))
        utilities.show_dets([],[t],imgs_info[t['image_id']],paths[name])

    max_height = max([d['bbox'][3] for d in fn_gts])
    min_height = min([d['bbox'][3] for d in fn_gts])
    max_width = max([d['bbox'][2] for d in fn_gts])
    min_width = min([d['bbox'][2] for d in fn_gts])

    f.write("\nBackground False Negatives Bounding Box Dimenstions:\n")
    f.write(" - Min width:  [%d]\n"%min_width)
    f.write(" - Max width:  [%d]\n"%max_width)
    f.write(" - Min height: [%d]\n"%min_height)
    f.write(" - Max height: [%d]\n"%max_height)

    ar_pic = np.zeros((int(max_height)+1,int(max_width)+1))
    ar_pic_2 = np.zeros((30,30))
    ar_bins = range(10)+range(10,100,10)+range(100,1000,100)+[1000]
    ar_pic_3 = np.zeros((10,10))
    ar_bins_3 = [np.power(2,x) for x in xrange(11)]

    num_fn_keypoints = {}

    areaRngs    = [[0, 32 ** 2],[32 ** 2, 64 ** 2],[64 ** 2, 96 ** 2],[96 ** 2, 128 ** 2],[128 ** 2, 1e5 ** 2]]
    areaRngLbls = ['small','medium','large','xlarge','xxlarge']
    small = 0; medium = 0; large = 0; xlarge = 0; xxlarge = 0

    num_people_ranges = [[0,0],[1,1],[2,4],[5,8],[9,100]]
    num_people_labels = ['none','one','small grp.','large grp.', 'crowd']
    no_people = 0; one = 0; small_grp = 0; large_grp = 0; crowd = 0

    segm_heatmap = np.zeros((128,128))
    for i,b in enumerate(fn_gts):
        if b['num_keypoints'] in num_fn_keypoints:
            num_fn_keypoints[b['num_keypoints']] += 1
        else:
            num_fn_keypoints[b['num_keypoints']] = 1

        if b['num_keypoints']==0: continue

        b_width  = int(b['bbox'][2])
        b_height = int(b['bbox'][3])
        ar_pic[0:b_height,0:b_width] += 1
        if b_width < 1024 and b_height < 1024:
            col = [i for i in xrange(len(ar_bins)-1) if ar_bins[i]<b_width<ar_bins[i+1]]
            row = [i for i in xrange(len(ar_bins)-1) if ar_bins[i]<b_height<ar_bins[i+1]]
            ar_pic_2[row,col] += 1

            col = [i for i in xrange(len(ar_bins_3)-1) if ar_bins_3[i]<b_width<ar_bins_3[i+1]]
            row = [i for i in xrange(len(ar_bins_3)-1) if ar_bins_3[i]<b_height<ar_bins_3[i+1]]
            ar_pic_3[row,col] += 1
        else:
            print "False Positive bbox has a side larger than 1024 pixels."
            print "Change lists ar_bins_2 and ar_bins_3 to include larger bins."
            assert(False)

        area = b_width * b_height * .5
        if areaRngs[0][0] <= area < areaRngs[0][1]:
            small += 1
        elif areaRngs[1][0] <= area < areaRngs[1][1]:
            medium += 1
        elif areaRngs[2][0] <= area < areaRngs[2][1]:
            large += 1
        elif areaRngs[3][0] <= area < areaRngs[3][1]:
            xlarge += 1
        elif areaRngs[4][0] <= area < areaRngs[4][1]:
            xxlarge += 1

        anns = coco_analyze.cocoGt.loadAnns(coco_analyze.cocoGt.getAnnIds(b['image_id']))
        iscrowd = [ann['iscrowd'] for ann in anns]
        num_people = len(anns) if sum(iscrowd)==0 else 100
        if num_people_ranges[0][0] <= num_people <= num_people_ranges[0][1]:
            no_people += 1
        elif num_people_ranges[1][0] <= num_people <= num_people_ranges[1][1]:
            one += 1
        elif num_people_ranges[2][0] <= num_people <= num_people_ranges[2][1]:
            small_grp += 1
        elif num_people_ranges[3][0] <= num_people <= num_people_ranges[3][1]:
            large_grp += 1
        elif num_people_ranges[4][0] <= num_people <= num_people_ranges[4][1]:
            crowd += 1

        if b['iscrowd']==1: continue
        nx, ny = imgs_info[b['image_id']]['width'],imgs_info[b['image_id']]['height']
        the_mask = np.zeros((ny,nx))

        # Create vertex coordinates for each grid cell...
        # (<0,0> is at the top left of the grid in this system)
        x, y = np.meshgrid(np.arange(nx), np.arange(ny))
        x, y = x.flatten(), y.flatten()
        points = np.vstack((x,y)).T

        for poly_verts in b['segmentation']:
            path = mplPath.Path(np.array([[x,y] for x,y in zip(poly_verts[0::2],poly_verts[1::2])]))

            grid = path.contains_points(points)
            grid = grid.reshape((ny,nx))

            the_mask += np.array(grid, dtype=int)
        segm_heatmap += imresize(the_mask,(128,128))

    fig = plt.figure(figsize=(10,10))
    ax = plt.subplot(111)
    ax.imshow(segm_heatmap)
    path = "%s/bckd_false_neg_heatmaps.pdf"%(loc_dir)
    paths['false_neg_hm'] = path
    plt.axis('off')
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    f.write("\nNumber of people in images with Background False Negatives:\n")
    f.write(" - No people:         [%d]\n"%no_people)
    f.write(" - One person:        [%d]\n"%one)
    f.write(" - Small group (2-4): [%d]\n"%small_grp)
    f.write(" - Large Group (5-8): [%d]\n"%large_grp)
    f.write(" - Crowd       (>=9): [%d]\n"%crowd)

    f.write("\nArea size (in pixels) of Background False Negatives:\n")
    f.write(" - Small    (%d,%d):            [%d]\n"%(areaRngs[0][0],areaRngs[0][1],small))
    f.write(" - Medium   (%d,%d):         [%d]\n"%(areaRngs[1][0],areaRngs[1][1],medium))
    f.write(" - Large    (%d,%d):         [%d]\n"%(areaRngs[2][0],areaRngs[2][1],large))
    f.write(" - X-Large  (%d,%d):        [%d]\n"%(areaRngs[3][0],areaRngs[3][1],xlarge))
    f.write(" - XX-Large (%d,%d): [%d]\n"%(areaRngs[4][0],areaRngs[4][1],xxlarge))

    f.write("\nNumber of visible keypoints for Background False Negatives:\n")
    for k in num_fn_keypoints.keys():
        if k == 0: continue
        f.write(" - [%d] kpts: [%d] False Neg.\n"%(k,num_fn_keypoints[k]))

    plt.figure(figsize=(10,10))
    plt.imshow(ar_pic,origin='lower')
    plt.colorbar()
    plt.title('BBox Aspect Ratio',fontsize=20)
    plt.xlabel('Width (px)',fontsize=20)
    plt.ylabel('Height (px)',fontsize=20)
    path = "%s/bckd_false_neg_bbox_aspect_ratio.pdf"%(loc_dir)
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    fig, ax = plt.subplots(figsize=(10,10))
    plt.imshow(ar_pic_2,origin='lower')
    plt.xticks(xrange(1,len(ar_bins)+1),["%d"%(x) for x in ar_bins],rotation='vertical')
    plt.yticks(xrange(1,len(ar_bins)+1),["%d"%(x) for x in ar_bins])
    plt.colorbar()
    plt.grid()
    plt.title('BBox Aspect Ratio',fontsize=20)
    plt.xlabel('Width (px)',fontsize=20)
    plt.ylabel('Height (px)',fontsize=20)
    path = "%s/bckd_false_neg_bbox_aspect_ratio_2.pdf"%(loc_dir)
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    fig, ax = plt.subplots(figsize=(10,10))
    plt.imshow(ar_pic_3,origin='lower')
    plt.xticks([-.5 + x for x in range(11)],["%d"%(x) for x in ar_bins_3])
    plt.yticks([-.5 + x for x in range(11)],["%d"%(x) for x in ar_bins_3])
    plt.colorbar()
    plt.grid()
    plt.title('BBox Aspect Ratio',fontsize=20)
    plt.xlabel('Width (px)',fontsize=20)
    plt.ylabel('Height (px)',fontsize=20)
    path = "%s/bckd_false_neg_bbox_aspect_ratio_3.pdf"%(loc_dir)
    paths['false_neg_bbox_ar'] = path
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    fig, ax = plt.subplots(figsize=(10,10))
    ax.set_axis_bgcolor('lightgray')
    plt.bar(xrange(5),[small,medium,large,xlarge,xxlarge],color='g',align='center')
    plt.xticks(xrange(5),areaRngLbls)
    plt.grid()
    plt.title('Histogram of Area Size',fontsize=20)
    path = "%s/bckd_false_neg_bbox_area_hist.pdf"%(loc_dir)
    paths['false_neg_bbox_area_hist'] = path
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    fig, ax = plt.subplots(figsize=(10,10))
    ax.set_axis_bgcolor('lightgray')
    plt.bar(xrange(5),[no_people,one,small_grp,large_grp,crowd],color='g',align='center')
    plt.xticks(xrange(5),num_people_labels)
    plt.grid()
    plt.title('Histogram of Num. of People in Images',fontsize=20)
    path = "%s/bckd_false_neg_num_people_histogram.pdf"%(loc_dir)
    paths['false_neg_num_ppl_hist'] = path
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    plt.figure(figsize=(10,10))
    plt.bar([k for k in num_fn_keypoints.keys() if k!=0],[num_fn_keypoints[k] for k in num_fn_keypoints.keys() if k!= 0],align='center')
    plt.title("Histogram of Number of Keypoints",fontsize=20)
    path = "%s/bckd_false_neg_num_keypoints_histogram.pdf"%(loc_dir)
    paths['false_neg_num_kpts_hist'] = path
    plt.savefig(path, bbox_inches='tight')
    plt.close()

    f.write("\nDone, (t=%.2fs)."%(time.time()-tic))
    f.close()

    return paths
Exemple #23
0
def plot_connectivity_circle(con,
                             node_names,
                             indices=None,
                             n_lines=None,
                             node_angles=None,
                             node_width=None,
                             node_colors=None,
                             facecolor='black',
                             textcolor='white',
                             node_edgecolor='black',
                             linewidth=1.5,
                             colormap='hot',
                             vmin=None,
                             vmax=None,
                             colorbar=True,
                             title=None,
                             colorbar_size=0.2,
                             colorbar_pos=(-0.3, 0.1),
                             fontsize_title=12,
                             fontsize_names=8,
                             fontsize_colorbar=8,
                             padding=6.,
                             fig=None,
                             subplot=111,
                             interactive=True,
                             node_linewidth=2.,
                             show=True):
    """Visualize connectivity as a circular graph.

    Parameters
    ----------
    con : array
        Connectivity scores. Can be a square matrix, or a 1D array. If a 1D
        array is provided, "indices" has to be used to define the connection
        indices.
    node_names : list of str
        Node names. The order corresponds to the order in con.
    indices : tuple of arrays | None
        Two arrays with indices of connections for which the connections
        strengths are defined in con. Only needed if con is a 1D array.
    n_lines : int | None
        If not None, only the n_lines strongest connections (strength=abs(con))
        are drawn.
    node_angles : array, shape=(len(node_names,)) | None
        Array with node positions in degrees. If None, the nodes are equally
        spaced on the circle. See mne.viz.circular_layout.
    node_width : float | None
        Width of each node in degrees. If None, the minimum angle between any
        two nodes is used as the width.
    node_colors : list of tuples | list of str
        List with the color to use for each node. If fewer colors than nodes
        are provided, the colors will be repeated. Any color supported by
        matplotlib can be used, e.g., RGBA tuples, named colors.
    facecolor : str
        Color to use for background. See matplotlib.colors.
    textcolor : str
        Color to use for text. See matplotlib.colors.
    node_edgecolor : str
        Color to use for lines around nodes. See matplotlib.colors.
    linewidth : float
        Line width to use for connections.
    colormap : str
        Colormap to use for coloring the connections.
    vmin : float | None
        Minimum value for colormap. If None, it is determined automatically.
    vmax : float | None
        Maximum value for colormap. If None, it is determined automatically.
    colorbar : bool
        Display a colorbar or not.
    title : str
        The figure title.
    colorbar_size : float
        Size of the colorbar.
    colorbar_pos : 2-tuple
        Position of the colorbar.
    fontsize_title : int
        Font size to use for title.
    fontsize_names : int
        Font size to use for node names.
    fontsize_colorbar : int
        Font size to use for colorbar.
    padding : float
        Space to add around figure to accommodate long labels.
    fig : None | instance of matplotlib.pyplot.Figure
        The figure to use. If None, a new figure with the specified background
        color will be created.
    subplot : int | 3-tuple
        Location of the subplot when creating figures with multiple plots. E.g.
        121 or (1, 2, 1) for 1 row, 2 columns, plot 1. See
        matplotlib.pyplot.subplot.
    interactive : bool
        When enabled, left-click on a node to show only connections to that
        node. Right-click shows all connections.
    node_linewidth : float
        Line with for nodes.
    show : bool
        Show figure if True.

    Returns
    -------
    fig : instance of matplotlib.pyplot.Figure
        The figure handle.
    axes : instance of matplotlib.axes.PolarAxesSubplot
        The subplot handle.

    Notes
    -----
    This code is based on the circle graph example by Nicolas P. Rougier
    http://www.labri.fr/perso/nrougier/coding/.

    By default, :func:`matplotlib.pyplot.savefig` does not take ``facecolor``
    into account when saving, even if set when a figure is generated. This
    can be addressed via, e.g.::

    >>> fig.savefig(fname_fig, facecolor='black') # doctest:+SKIP

    If ``facecolor`` is not set via :func:`matplotlib.pyplot.savefig`, the
    figure labels, title, and legend may be cut off in the output figure.
    """
    import matplotlib.pyplot as plt
    import matplotlib.path as m_path
    import matplotlib.patches as m_patches

    n_nodes = len(node_names)

    if node_angles is not None:
        if len(node_angles) != n_nodes:
            raise ValueError('node_angles has to be the same length '
                             'as node_names')
        # convert it to radians
        node_angles = node_angles * np.pi / 180
    else:
        # uniform layout on unit circle
        node_angles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False)

    if node_width is None:
        # widths correspond to the minimum angle between two nodes
        dist_mat = node_angles[None, :] - node_angles[:, None]
        dist_mat[np.diag_indices(n_nodes)] = 1e9
        node_width = np.min(np.abs(dist_mat))
    else:
        node_width = node_width * np.pi / 180

    if node_colors is not None:
        if len(node_colors) < n_nodes:
            node_colors = cycle(node_colors)
    else:
        # assign colors using colormap
        try:
            spectral = plt.cm.spectral
        except AttributeError:
            spectral = plt.cm.Spectral
        node_colors = [spectral(i / float(n_nodes)) for i in range(n_nodes)]

    # handle 1D and 2D connectivity information
    if con.ndim == 1:
        if indices is None:
            raise ValueError('indices has to be provided if con.ndim == 1')
    elif con.ndim == 2:
        if con.shape[0] != n_nodes or con.shape[1] != n_nodes:
            raise ValueError('con has to be 1D or a square matrix')
        # we use the lower-triangular part
        indices = np.tril_indices(n_nodes, -1)
        con = con[indices]
    else:
        raise ValueError('con has to be 1D or a square matrix')

    # get the colormap
    if isinstance(colormap, string_types):
        colormap = plt.get_cmap(colormap)

    # Make figure background the same colors as axes
    if fig is None:
        fig = plt.figure(figsize=(8, 8), facecolor=facecolor)

    # Use a polar axes
    if not isinstance(subplot, tuple):
        subplot = (subplot, )
    axes = plt.subplot(*subplot, polar=True)
    _set_ax_facecolor(axes, facecolor)

    # No ticks, we'll put our own
    plt.xticks([])
    plt.yticks([])

    # Set y axes limit, add additional space if requested
    plt.ylim(0, 10 + padding)

    # Remove the black axes border which may obscure the labels
    axes.spines['polar'].set_visible(False)

    # Draw lines between connected nodes, only draw the strongest connections
    if n_lines is not None and len(con) > n_lines:
        con_thresh = np.sort(np.abs(con).ravel())[-n_lines]
    else:
        con_thresh = 0.

    # get the connections which we are drawing and sort by connection strength
    # this will allow us to draw the strongest connections first
    con_abs = np.abs(con)
    con_draw_idx = np.where(con_abs >= con_thresh)[0]

    con = con[con_draw_idx]
    con_abs = con_abs[con_draw_idx]
    indices = [ind[con_draw_idx] for ind in indices]

    # now sort them
    sort_idx = np.argsort(con_abs)
    con_abs = con_abs[sort_idx]
    con = con[sort_idx]
    indices = [ind[sort_idx] for ind in indices]

    # Get vmin vmax for color scaling
    if vmin is None:
        vmin = np.min(con[np.abs(con) >= con_thresh])
    if vmax is None:
        vmax = np.max(con)
    vrange = vmax - vmin

    # We want to add some "noise" to the start and end position of the
    # edges: We modulate the noise with the number of connections of the
    # node and the connection strength, such that the strongest connections
    # are closer to the node center
    nodes_n_con = np.zeros((n_nodes), dtype=np.int)
    for i, j in zip(indices[0], indices[1]):
        nodes_n_con[i] += 1
        nodes_n_con[j] += 1

    # initialize random number generator so plot is reproducible
    rng = np.random.mtrand.RandomState(seed=0)

    n_con = len(indices[0])
    noise_max = 0.25 * node_width
    start_noise = rng.uniform(-noise_max, noise_max, n_con)
    end_noise = rng.uniform(-noise_max, noise_max, n_con)

    nodes_n_con_seen = np.zeros_like(nodes_n_con)
    for i, (start, end) in enumerate(zip(indices[0], indices[1])):
        nodes_n_con_seen[start] += 1
        nodes_n_con_seen[end] += 1

        start_noise[i] *= ((nodes_n_con[start] - nodes_n_con_seen[start]) /
                           float(nodes_n_con[start]))
        end_noise[i] *= ((nodes_n_con[end] - nodes_n_con_seen[end]) /
                         float(nodes_n_con[end]))

    # scale connectivity for colormap (vmin<=>0, vmax<=>1)
    con_val_scaled = (con - vmin) / vrange

    # Finally, we draw the connections
    for pos, (i, j) in enumerate(zip(indices[0], indices[1])):
        # Start point
        t0, r0 = node_angles[i], 10

        # End point
        t1, r1 = node_angles[j], 10

        # Some noise in start and end point
        t0 += start_noise[pos]
        t1 += end_noise[pos]

        verts = [(t0, r0), (t0, 5), (t1, 5), (t1, r1)]
        codes = [
            m_path.Path.MOVETO, m_path.Path.CURVE4, m_path.Path.CURVE4,
            m_path.Path.LINETO
        ]
        path = m_path.Path(verts, codes)

        color = colormap(con_val_scaled[pos])

        # Actual line
        patch = m_patches.PathPatch(path,
                                    fill=False,
                                    edgecolor=color,
                                    linewidth=linewidth,
                                    alpha=1.)
        axes.add_patch(patch)

    # Draw ring with colored nodes
    height = np.ones(n_nodes) * 1.0
    bars = axes.bar(node_angles,
                    height,
                    width=node_width,
                    bottom=9,
                    edgecolor=node_edgecolor,
                    lw=node_linewidth,
                    facecolor='.9',
                    align='center')

    for bar, color in zip(bars, node_colors):
        bar.set_facecolor(color)

    # Draw node labels
    angles_deg = 180 * node_angles / np.pi
    for name, angle_rad, angle_deg in zip(node_names, node_angles, angles_deg):
        if angle_deg >= 270:
            ha = 'left'
        else:
            # Flip the label, so text is always upright
            angle_deg += 180
            ha = 'right'

        axes.text(angle_rad,
                  10.4,
                  name,
                  size=fontsize_names,
                  rotation=angle_deg,
                  rotation_mode='anchor',
                  horizontalalignment=ha,
                  verticalalignment='center',
                  color=textcolor)

    if title is not None:
        plt.title(title, color=textcolor, fontsize=fontsize_title, axes=axes)

    if colorbar:
        sm = plt.cm.ScalarMappable(cmap=colormap,
                                   norm=plt.Normalize(vmin, vmax))
        sm.set_array(np.linspace(vmin, vmax))
        cb = plt.colorbar(sm,
                          ax=axes,
                          use_gridspec=False,
                          shrink=colorbar_size,
                          anchor=colorbar_pos)
        cb_yticks = plt.getp(cb.ax.axes, 'yticklabels')
        cb.ax.tick_params(labelsize=fontsize_colorbar)
        plt.setp(cb_yticks, color=textcolor)

    # Add callback for interaction
    if interactive:
        callback = partial(_plot_connectivity_circle_onpick,
                           fig=fig,
                           axes=axes,
                           indices=indices,
                           n_nodes=n_nodes,
                           node_angles=node_angles)

        fig.canvas.mpl_connect('button_press_event', callback)

    plt_show(show)
    return fig, axes
Exemple #24
0
def in_polygon(point, polygon):
    bbPath = mplPath.Path(np.array(polygon))
    return bbPath.contains_point(point)
verts[3::5, 1] = bottom

patch = None


def animate(i):
    # simulate new data coming in
    data = np.random.randn(1000)
    n, bins = np.histogram(data, 100)
    top = bottom + n
    verts[1::5, 1] = top
    verts[2::5, 1] = top
    return [patch, ]

fig, ax = plt.subplots()
barpath = path.Path(verts, codes)
patch = patches.PathPatch(
    barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)

ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max())

ani = animation.FuncAnimation(fig, animate, 100, repeat=False, blit=True)
plt.show()

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Fixing random state for reproducibility
def _points_in_figure(figure: [], points: []):
    path = mpltPath.Path(figure)
    inside = path.contains_points(points)
    return inside
def select_groups_correctedw(cont_densew, pw, fwhm, level, limgroupw, fieldw,
                             scale, sw, gsmall):
    '''
    The difference between this one and the previous function is that
    in this one I will attempt to correct for background numbers
    '''
    areas_wide = {'1': 15.53, '4': 9.56}
    #print('Mag lim of selected objects', max(pw[:,17]))

    ###To correct for background numbers: How many in this field per deg2
    afield = areas_wide[str(fieldw)]
    back = len(
        pw[:, 0]
    ) / afield  # Number of PEGs in this field per deg^2 (up to the completeness of the field)
    #print('Background correction per deg^2', back)

    ### To correct for background in mass estimates: total mass per deg^2 of field
    mfield = np.sum(10**(-0.348 * pw[:, 17] + 18.284))
    backm = mfield / afield

    ## Search for groups inside these contours
    x = cont_densew
    ngroup = 1  #number assigned to complete group
    i = 0

    for i in range(len(x.allsegs[1])):  # For all 100% contour paths

        #print('I am in contour', i)

        massg = 0
        volume = 0
        area = 0

        array = []
        ggroup = []

        cont = x.allsegs[1][i]  #ith 100% contour level

        # Find its nearest contour, but only those that are on x% level (the x is user defined)
        contmin, segmin, indmin, xmin, ymin, dmin = x.find_nearest_contour(
            cont[0][0], cont[0][1], indices=[0], pixel=False)

        bbPath = mplPath.Path(x.allsegs[contmin][segmin])

        # Find the volume of this contour by modeling like an ellipse
        axis, phi, center = fit_ellipse(x.allsegs[contmin][segmin])

        a = axis[0] * scale * 0.187 * 8.462 / 1000  #axis in Mpc
        b = axis[1] * scale * 0.187 * 8.462 / 1000
        c = (a + b) / 2

        ad = axis[0] * scale * 0.187 / 3600  #axis in Deg
        bd = axis[1] * scale * 0.187 / 3600
        cd = (ad + bd) / 2

        aread = np.pi * ad * bd  # in deg^2
        volume = 4 / 3 * np.pi * a * b * c  # volume in cubic megaparsecs

        #print('Area in deg', aread)

        for gal in pw:

            overlapx = 968
            overlapy = 1290

            field = str(int(gal[43]))

            if (fieldw == 4) and ((field == 220930003100) or
                                  (field == 221318002300)):
                overlapy = 1741

            shiftx, shifty = shift(fieldw, field)

            xa = gal[2] + shiftx * (19354 - overlapx)
            ya = gal[3] + shifty * (19354 - overlapy)

            xga = (xa / scale)
            yga = (ya / scale)

            C = bbPath.contains_point(
                (xga,
                 yga))  # search if the  scaled position is within the x% level

            if (C == True):
                m = 10**(-0.348 * gal[17] + 18.284)
                massg = massg + m

                if (len(array) == 0):
                    array = gal
                else:
                    array = np.vstack((array, gal))

        # How many of the galaxies in the group is massive? (at least 3 in 100% equi-level path to classify)

        ggroup = array[array[:, 17] < 20.5]

        if (len(ggroup) >=
                3):  # If there are at least 3 massive galaxies in group

            #print('Contour', i, 'Did make the cut')
            #print(bbPath, file=test_contours2)
            #from catalog_groups import combinedcat
            #combinedcat(bbPath, i, contmin, segmin, indmin, xmin, ymin, dmin, fieldw, scale, mastercat)

            # Find how many SFGs are in the group
            nsf, ncsf = find_sf(
                bbPath, i, aread, afield, ngroup
            )  #it returns the original and corrected number of SFGs in ith group

            allfields = array[:, 43]
            field = allfields[1]

            # ID the approx center of the group
            medx = np.mean(array[:, 2])
            medy = np.mean(array[:, 3])

            # Find corrected number of elements in group
            nggd = len(
                array[:, 0]) / aread  #Number of elements in group per deg^2
            nggdc = nggd - back  # Number of elements per deg^2 corrected in group
            #print('Corrected elements in group per deg^2', nggdc, 'Group', i)
            ngg = nggdc * aread  # Number of corrected elements in group
            #print('Corrected elements in group', ngg, 'Group', i)

            # Find corrected number of elements per Mpc^2
            nareacm = nggdc / (60**2) / (60**2) / (8.462**2) * (
                1000**2)  # in squared megaparsecs

            # Find the CORRECTED number for background number of elements per Mpc^3
            nvcm = nareacm * (3 / (4 * c))

            # Find the mass per squared degree for this group
            md = massg / aread  #Total mass of group/area of group in deg^2

            # Find CORRECTED mass per squared degree for this group
            mdc = md - backm  #mgroup/deg^2 - mfield/deg^2 =  mgroup/deg^2 corrected

            # Find CORRECTED total mass of group
            mmmgc = mdc * aread

            # Find CORRECTED mass per Mpc^2
            mdcm = mdc / (60**2) / (60**2) / (8.462**2) * (
                1000**2)  # in squared megaparsecs

            # Find CORRECTED mass per cubic megaparsecs
            mvcm = mdcm * (3 / (4 * c))
            '''
            # Find the number of objects in this group per deg2
            narea = len(array[:,0])/aread
            
            # Find the CORRECTED for background number of objects in this group per deg2 and Mpc^2
            nareac = narea - back #in degrees squared
            nareacm = nareac/(60**2)/(60**2)/(8.462**2)*(1000**2)  # in squared megaparsecs
            
            
            # Find the CORRECTED number for background per Mpc^3
            nvcm = nareacm/(4/3*c)
        
            # Find the mass per squared degree for this group
            md = np.log10(mass)/aread
            
            # Find CORRECTED mass per squared degree for this group
            mdc = md - backm
            mdcm = mdc/(60**2)/(60**2)/(8.462**2)*(1000**2)  # in squared megaparsecs
            
            # Find corrected mass per cubic megaparsecs
            mvcm = mdcm/(4/3*c)
            '''

            fig = plt.figure(num=fieldw * 20)
            ax = fig.add_subplot(111)
            pat = patchesbi.PathPatch(bbPath,
                                      facecolor='None',
                                      edgecolor='None',
                                      lw=1.3)
            ax.add_patch(pat)

            centpx = np.mean(bbPath.vertices[:, 0])
            centpy = np.mean(bbPath.vertices[:, 1])

            print(centpx, centpy, i, file=clusters)

            cir = patchesbi.Circle((centpx, centpy),
                                   17,
                                   facecolor='None',
                                   edgecolor='white')
            ax.add_patch(cir)

            if (ngroup == 19) and (fieldw == 1):
                ax.text(centpx + 32,
                        centpy - 46,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='#3c0ff0',
                        fontsize=10,
                        fontweight='bold')
                ax.text(centpx + 32,
                        centpy - 46,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='white',
                        fontsize=10,
                        fontweight='bold')

            elif (ngroup == 14) and (fieldw == 1):
                ax.text(centpx + 35,
                        centpy + 26,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='#3c0ff0',
                        fontsize=10,
                        fontweight='bold')
                ax.text(centpx + 35,
                        centpy + 26,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='white',
                        fontsize=10,
                        fontweight='bold')

            elif (fieldw == 4) and (ngroup == 7):

                ax.text(centpx + 26,
                        centpy - 37,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='#3c0ff0',
                        fontsize=10,
                        fontweight='bold')
                ax.text(centpx + 26,
                        centpy - 37,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='white',
                        fontsize=10,
                        fontweight='bold')

            else:
                ax.text(centpx + 26,
                        centpy + 26,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='#3c0ff0',
                        fontsize=10,
                        fontweight='bold')
                ax.text(centpx + 26,
                        centpy + 26,
                        'W' + str(fieldw) + '_' + str(ngroup),
                        color='white',
                        fontsize=10,
                        fontweight='bold')
            '''
            ## If you want to plot individually each group
            '''
            if (ngroup == 14):  #and(ngroup<=15): #for testing
                plot_groups_wide_wsf(fieldw, bbPath, ngroup, pw, scale, sw)
                tempx, tempy, tempng, tpatch, tbbpath, tggroup = color_plots_wide(
                    fieldw, field, ngroup, centpx, centpy, scale, bbPath,
                    ggroup, sw
                )  #, array, xall, yall, msall, xmass, ymass, msmass, xsfs, ysfs, xsfsb, ysfsb, marsf, marsfb)
                #plot_groups_wide(fieldw, bbPath, i, pw, scale, ngroup, gsmall)

            ## If you want to know the position of the brightest source in group
            brightest = array[array[:, 17] == np.min(array[:, 17])]
            #print('For field', fieldw, 'Group', i, 'Brightest gal has Ks', brightest[:,17])
            #print('Group', i, 'And position', brightest[:,4][0], brightest[:,5][0])
            #print('W'+str(fieldw)+'_'+str(i)+' '+str(brightest[:,4][0])+' '+str(brightest[:,5][0]))
            #print(brightest[:,4][0], brightest[:,5][0])

            print(fieldw,
                  field,
                  ngroup,
                  medx,
                  medy,
                  np.log10(mmmgc),
                  volume,
                  nggdc,
                  nareacm,
                  nvcm,
                  np.log10(mdc),
                  np.log10(mdcm),
                  np.log10(mvcm),
                  aread,
                  len(array[:, 0]),
                  ngg,
                  nsf,
                  ncsf,
                  brightest[:, 4][0],
                  brightest[:, 5][0],
                  file=characw)

            for element in array:

                overlapx = 968
                overlapy = 1290

                field = str(int(element[43]))

                if (fieldw == 4) and ((field == 220930003100) or
                                      (field == 221318002300)):
                    overlapy = 1741

                shiftx, shifty = shift(fieldw, field)

                # shift each position according to the position of their patch, each square degree is 19354 pixels minus the overlap between adjacent patches: 4 arcmin in DEC and 3 in RA
                xarray = (element[2] + shiftx * (19354 - overlapx)) / scale
                yarray = (element[3] + shifty * (19354 - overlapy)) / scale
                #plt.figure(num=fieldw*10)
                #plt.scatter(xarray, yarray, marker='d', edgecolor='Blue', facecolor='None', s=10)

            #np.savetxt('/Users/osejo/Desktop/Tesis/massivend/k_catalog/automatize/0215/environment/sfandpe/density_plots/separate_populations/gaussian/dense/140916/fwhm_'+str(int(fwhm*100))+'/updated_positions/test_group_'+str(ngroup)+'_'+str(limgroupw*100)+'%_w'+str(fieldw)+'_fwhm_'+str(int(fwhm*100))+'_l'+str(level)+'.dat', array, fmt='%s')

            ## New group search
            ngroup = ngroup + 1

    return (tempx, tempy, tempng, tpatch, tbbpath, tggroup)
Exemple #28
0
    def clabel(self, *args, **kwargs):
        # nb: contour labelling does not work very well for filled
        # contours - it is recommended to only label line contours.
        # This is especially true when inline=True.

        # This wrapper exist because mpl does not properly transform
        # paths. Instead it simply assumes one path represents one polygon
        # (not necessarily the case), and it assumes that
        # transform(path.verts) is equivalent to transform_path(path).
        # Unfortunately there is no way to easily correct this error,
        # so we are forced to pre-transform the ContourSet's paths from
        # the source coordinate system to the axes' projection.
        # The existing mpl code then has a much simpler job of handling
        # pre-projected paths (which can now effectively be transformed
        # naively).

        for col in self.collections:
            # Snaffle the collection's path list. We will change the
            # list in-place (as the contour label code does in mpl).
            paths = col.get_paths()

            # The ax attribute is deprecated in MPL 3.3 in favor of
            # axes. So, here we test if axes is present and fall back
            # on the old self.ax to support MPL versions less than 3.3
            if hasattr(self, "axes"):
                data_t = self.axes.transData
            else:
                data_t = self.ax.transData

            # Define the transform that will take us from collection
            # coordinates through to axes projection coordinates.
            col_to_data = col.get_transform() - data_t

            # Now that we have the transform, project all of this
            # collection's paths.
            new_paths = [col_to_data.transform_path(path) for path in paths]
            new_paths = [path for path in new_paths if path.vertices.size >= 1]

            # The collection will now be referenced in axes projection
            # coordinates.
            col.set_transform(data_t)

            # Clear the now incorrectly referenced paths.
            del paths[:]

            for path in new_paths:
                if path.vertices.size == 0:
                    # Don't persist empty paths. Let's get rid of them.
                    continue

                # Split the path if it has multiple MOVETO statements.
                codes = np.array(path.codes if path.codes is not None else [0])
                moveto = codes == mpath.Path.MOVETO
                if moveto.sum() <= 1:
                    # This is only one path, so add it to the collection.
                    paths.append(path)
                else:
                    # The first MOVETO doesn't need cutting-out.
                    moveto[0] = False
                    split_locs = np.flatnonzero(moveto)

                    split_verts = np.split(path.vertices, split_locs)
                    split_codes = np.split(path.codes, split_locs)

                    for verts, codes in zip(split_verts, split_codes):
                        # Add this path to the collection's list of paths.
                        paths.append(mpath.Path(verts, codes))

        # Now that we have prepared the collection paths, call on
        # through to the underlying implementation.
        return super().clabel(*args, **kwargs)
Exemple #29
0
def draw(g,
         layout=None,
         labels=False,
         figsize=(8, 2),
         h_edge_draw='blue',
         rows=None):
    if not isinstance(g, BaseGraph):
        g = g.to_graph()
    fig1 = plt.figure(figsize=figsize)
    ax = fig1.add_axes([0, 0, 1, 1], frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    vs_on_row = dict()  # count the vertices on each row
    for v in g.vertices():
        vs_on_row[g.row(v)] = vs_on_row.get(g.row(v), 0) + 1

    if not layout:
        layout = circuit_layout(g)

    if rows:
        minrow, maxrow = rows
        vertices = [
            v for v in g.vertices()
            if (minrow <= g.row(v) and g.row(v) <= maxrow)
        ]
        edges = [
            e for e in g.edges()
            if g.edge_s(e) in vertices and g.edge_t(e) in vertices
        ]
    else:
        vertices = g.vertices()
        edges = g.edges()

    for e in edges:
        sp = layout[g.edge_s(e)]
        tp = layout[g.edge_t(e)]
        et = g.edge_type(e)
        n_row = vs_on_row.get(g.row(g.edge_s(e)), 0)

        dx = tp[0] - sp[0]
        dy = tp[1] - sp[1]
        bend_wire = (dx == 0) and h_edge_draw == 'blue' and n_row > 2
        ecol = '#0099ff' if h_edge_draw == 'blue' and et == 2 else 'black'

        if bend_wire:
            bend = 0.25
            mid = (sp[0] + 0.5 * dx + bend * dy, sp[1] + 0.5 * dy - bend * dx)

            pth = path.Path(
                [sp, mid, tp],
                [path.Path.MOVETO, path.Path.CURVE3, path.Path.LINETO])
            patch = patches.PathPatch(pth,
                                      edgecolor=ecol,
                                      linewidth=0.8,
                                      fill=False)
            ax.add_patch(patch)
        else:
            pos = 0.5 if dx == 0 or dy == 0 else 0.4
            mid = (sp[0] + pos * dx, sp[1] + pos * dy)
            ax.add_line(
                lines.Line2D([sp[0], tp[0]], [sp[1], tp[1]],
                             color=ecol,
                             linewidth=0.8,
                             zorder=0))

        if h_edge_draw == 'box' and et == 2:  #hadamard edge
            w = 0.2
            h = 0.15
            diag = math.sqrt(w * w + h * h)
            angle = math.atan2(dy, dx)
            angle2 = math.atan2(h, w)
            centre = (mid[0] - diag / 2 * math.cos(angle + angle2),
                      mid[1] - diag / 2 * math.sin(angle + angle2))
            ax.add_patch(
                patches.Rectangle(centre,
                                  w,
                                  h,
                                  angle=angle / math.pi * 180,
                                  facecolor='yellow',
                                  edgecolor='black'))

        #plt.plot([sp[0],tp[0]],[sp[1],tp[1]], 'k', zorder=0, linewidth=0.8)

    for v in vertices:
        p = layout[v]
        t = g.type(v)
        a = g.phase(v)

        sz = 0.2
        col = 'black'
        if t == 1: col = 'green'
        elif t == 2: col = 'red'
        else: sz = 0.1

        ax.add_patch(
            patches.Circle(p, sz, facecolor=col, edgecolor='black', zorder=1))
        if labels:
            plt.text(p[0] + 0.25,
                     p[1] + 0.25,
                     str(v),
                     ha='center',
                     color='gray',
                     fontsize=5)
        if a:
            plt.text(p[0],
                     p[1] - 0.5,
                     phase_to_s(a),
                     ha='center',
                     color='blue',
                     fontsize=8)

    ax.axis('equal')
    plt.close()
    return fig1
    parser.add_argument('--gpu', action='store_false',
                        help='use gpu or cpu.')
    parser.add_argument('--dump', type=bool, default=False, help='save result into video or not')
    
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()


    out = open(args.clip,'r')
    clip = json.load(out)['roi']
    bbPath = mplPath.Path(clip)


    if args.gpu:
        ctx = mx.gpu()
    else:
        ctx = mx.cpu()
        
    net = model_zoo.get_model(args.network, pretrained=False)
    
    classes = ['hat', 'person']
    for param in net.collect_params().values():
        if param._data is not None:
            continue
        param.initialize()
    net.reset_class(classes)