Example #1
0
File: node.py Project: mkhan45/1984
def plot_graph(graph, adj):
    """ Use the package networkx to produce a diagrammatic plot of the graph, with
        the nodes in the graph colored according to their current labels.

        Note that only 20 unique colors are available for the current color map,
        so common colors across nodes may be coincidental.

        Parameters
        ----------
        graph : Tuple[Node, ...]
            The graph to plot
        adj : numpy.ndarray, shape=(N, N)
            The adjacency-matrix for the graph. Nonzero entries indicate
            the presence of edges.

        Returns
        -------
        Tuple[matplotlib.fig.Fig, matplotlib.axis.Axes]
            The figure and axes for the plot."""
    import networkx as nx
    import numpy as np
    import matplotlib.cm as cm
    import matplotlib.pyplot as plt

    g = nx.Graph()
    for n, node in enumerate(graph):
        g.add_node(n)

    # construct a network-x graph from the adjacency matrix: a non-zero entry at adj[i, j]
    # indicates that an egde is present between Node-i and Node-j. Because the edges are
    # undirected, the adjacency matrix must be symmetric, thus we only look ate the triangular
    # upper-half of the entries to avoid adding redundant nodes/edges
    g.add_edges_from(zip(*np.where(np.triu(adj) > 0)))

    # we want to visualize our graph of nodes and edges; to give the graph a spatial representation,
    # we treat each node as a point in 2D space, and edges like compressed springs. We simulate
    # all of these springs decompressing (relaxing) to naturally space out the nodes of the graph
    # this will hopefully give us a sensible (x, y) for each node, so that our graph is given
    # a reasonable visual depiction
    pos = nx.spring_layout(g)

    # make a mapping that maps: node-lab -> color, for each unique label in the graph
    color = list(
        iter(cm.tab20b(np.linspace(0, 1, len(set(i.label for i in graph))))))
    color_map = dict(zip(sorted(set(i.label for i in graph)), color))
    colors = [
        color_map[i.label] for i in graph
    ]  # the color for each node in the graph, according to the node's label

    # render the visualization of the graph, with the nodes colored based on their labels!
    fig, ax = plt.subplots()
    nx.draw_networkx_nodes(g,
                           pos=pos,
                           ax=ax,
                           nodelist=range(len(graph)),
                           node_color=colors)
    nx.draw_networkx_edges(g, pos, ax=ax, edgelist=g.edges())
    return fig, ax
def plot_representation(fig_name='representation_tsne', file_name=None):
    file_name = fig_name if file_name is None else file_name
    ax = plt(fig_name, figsize=(8, 10)).gca()
    path = os.path.join(last_experiment_path(experiment_name),
                        file_name + '.dump')
    with open(path, 'rb') as f:
        artists = pickle.load(f)
    colors = cm.tab20b(np.linspace(0, 1, len(artists)))

    for row in artists:
        ax.scatter(row[0][:, 0],
                   row[0][:, 1],
                   c=colors[row[1]],
                   label=clean_labels(row[2]))

    ax.legend(bbox_to_anchor=(0, 0, 1, 1),
              bbox_transform=plt(fig_name).gcf().transFigure,
              prop={'size': 6},
              ncol=4,
              loc=3)
    ax.set_title('Painting representation')
Example #3
0
def stack_plot(group_labels, categories, labels, title):
    font = {'family': 'normal', 'weight': 'normal', 'size': 15}

    plt.rc('font', **font)
    colors = cm.tab20b(np.linspace(0, 1, len(categories) + 1))

    plt.ylabel('#Applications')
    width = 0.35
    y_pos = np.arange(len(categories[0]))

    plots = []
    for i in range(len(categories)):
        bott = 0
        for j in range(0, i):
            bott += np.array(categories[j])
        print colors[i]
        p = plt.bar(y_pos, categories[i], color=colors[i], bottom=bott)
        plots.append(p)

    plt.xticks(y_pos, group_labels, rotation=40, ha='right')
    plt.legend([p[0] for p in plots], labels)
    plt.show()
Example #4
0
import nxviz as nxv

#####
from matplotlib import cm
words = ['Biological Process', 'Molecular Function', 'Cellular Component']
pie_colors = {'Set3':cm.Set3(np.arange(12)/12.),
              'Set2':cm.Set2(np.arange(8)/8.),
              'Set1':cm.Set1(np.arange(9)/9.),
              'Pastel2':cm.Pastel2(np.arange(8)/8.),
              'Pastel1':cm.Pastel1(np.arange(9)/9.),
              'Dark2':cm.Dark2(np.arange(8)/8.),
              'Paired':cm.Paired(np.arange(12)/12.),
              'Accent':cm.Accent(np.arange(8)/8.),
              'Spectral':cm.Spectral(np.arange(11)/11.),
              'tab20':cm.tab20(np.arange(20)/20.),
              'tab20b':cm.tab20b(np.arange(20)/20.),
              'tab20c':cm.tab20c(np.arange(20)/20.)}
colors = {'#8DD3C7':pie_colors['Set3'][0:1], '#FFFFB3':pie_colors['Set3'][1:2],
         '#BEBADA':pie_colors['Set3'][2:3], '#FB8072':pie_colors['Set3'][3:4],
         '#80B1D3':pie_colors['Set3'][4:5], '#FDB462':pie_colors['Set3'][5:6],
         '#B3DE69':pie_colors['Set3'][6:7], '#FCCDE5':pie_colors['Set3'][7:8],
         '#D9D9D9':pie_colors['Set3'][8:9], '#BC80BD':pie_colors['Set3'][9:10],
         '#CCEBC5':pie_colors['Set3'][10:11], '#FFED6F':pie_colors['Set3'][11:12]}
sizes = ['3x3','4x4','5x5','6x6','7x7','8x8','9x9','10x10']
escala_uno = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]


#from __future__ import division
#import matplotlib.pyplot as plt
#from matplotlib import colors as mcolors
#colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
Example #5
0
def plot_decision_boundaries(model, num_node, num_poly, err, method='contour', depth=True):
    """
     Plot the decision boundaries of the model 
    """
    # create grid to evaluate model
    x_min, x_max = -5,5 
    y_min, y_max = -5,5 
    XX, YY = np.meshgrid(np.arange(x_min, x_max, (x_max - x_min) / 50),
                         np.arange(y_min, y_max, (y_max - y_min) / 50))

    XY = np.vstack([XX.ravel(), YY.ravel()]).T
    
    poly_m, activations = get_polytopes(model, torch.FloatTensor(XY))
    
    with torch.no_grad():
        pred = model(torch.FloatTensor(XY).cuda())
        pred = torch.sigmoid(pred).detach().cpu().numpy()

    gini_list = gini_impurity_list(poly_m[0], np.round(pred))

    Z = poly_m[0].reshape(XX.shape)
    bins = np.arange(0,len(poly_m[0]))
    act_bin = np.digitize(poly_m[0], bins)
    
    if method == 'all':
        fig, ax = plt.subplots(1, 3, figsize=(21, 5))
        for a in ax:
            a.axes.xaxis.set_visible(False)
            a.axes.yaxis.set_visible(False)
    else:
        fig, ax = plt.subplots(1, 1, figsize=(7, 5))
        ax.axes.xaxis.set_visible(False)
        ax.axes.yaxis.set_visible(False)
    if method == 'surface' or method=='all':
        m = poly_m[0]
        m = minmax_scale(m, feature_range=(0, 1), axis=0, copy=True)
        my_col = cm.tab20b(m.reshape(XX.shape))

        if method == 'surface':
            fig = plt.figure(figsize=(7, 5))
            ax = fig.add_subplot(111, projection='3d')
        else:
            ax = fig.add_subplot(132, projection='3d')
        
        ax.view_init(elev=45., azim=15)
        ax.plot_surface(X=XX, Y=YY, Z=pred.reshape(XX.shape), facecolors=my_col, linewidth=0, antialiased=False, rstride=1, cstride=1)        
        # Get rid of the panes
        ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
        ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
        ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))

        # Get rid of the spines
        ax.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
        ax.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
        ax.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))

        # Get rid of the ticks
        ax.set_xticks([]) 
        ax.set_yticks([]) 
        ax.set_zticks([])
        ax.set_title("Generalization error: %.4f" % err)

    if method == 'colormesh' or method=='all':
        if method == 'all':
            ax = fig.add_subplot(131)
        plt.pcolormesh(XX, YY, Z, cmap="PRGn")
        ax.set_xticks([]) 
        ax.set_yticks([]) 

        ax.set_title("Nodes: " + str(num_node) + "; # of activated regions: " + str(num_poly))

    # if method == 'contour' or method=='all':
    #     if method == 'all':
    #         ax = fig.add_subplot(142)
        
    #     plt.contourf(XX, YY, Z, cmap="tab20b",  vmin = np.min(Z), vmax = np.max(Z))
    #     ax.set_title("Nodes: " + str(num_node) + "; # of activated regions: " + str(num_poly))
    #     ax.set_xticks([]) 
    #     ax.set_yticks([]) 
    
    if method == 'gini' or method=='all':
        if method == 'all':
            ax = fig.add_subplot(133)
        # gini_Z = minmax_scale(gini_list, feature_range=(0, 1), axis=0, copy=True)
        gini_Z = gini_list.reshape(XX.shape)
        plt.pcolormesh(XX, YY, gini_Z, cmap="Reds", vmin = 0, vmax = 0.5)
        cbar = plt.colorbar(ticks=np.linspace(0, 0.5, 2))
        cbar.ax.set_title('gini index', fontsize=12, pad=12)
        cbar.set_ticklabels(['0','0.5'])
        ax.set_title("Mean: %.4f" % np.mean(gini_list) )
        ax.set_xticks([]) 
        ax.set_yticks([]) 

    exp = "depth" if depth else "width"
    os.makedirs('../polytopes/', exist_ok=True)
    plt.savefig('../polytopes/xor_%s_%s_%04d.png'%(exp,method,num_node))
Example #6
0
def setupVisualization(dataset, args, selected_collection_key):
    """
    Creates the necessary variables in a dictionary "dataset_graphics", which will be passed onto the visualization
    function
    """

    # Create a python dictionary that will contain all the visualization related information
    graphics = {
        'collections': {},
        'sensors': {},
        'pattern': {},
        'ros': {},
        'args': args
    }

    # Initialize ROS stuff
    rospy.init_node("calibrate")
    graphics['ros']['tf_broadcaster'] = tf.TransformBroadcaster()
    graphics['ros']['publisher_models'] = rospy.Publisher('~robot_meshes',
                                                          MarkerArray,
                                                          queue_size=0,
                                                          latch=True)
    now = rospy.Time.now()

    # Parse xacro description file
    description_file, _, _ = uriReader(
        dataset['calibration_config']['description_file'])
    rospy.loginfo('Reading description file ' + description_file + '...')
    xml_robot = readXacroFile(description_file)

    pattern = dataset['calibration_config']['calibration_pattern']

    # Create colormaps to be used for coloring the elements. Each collection contains a color, each sensor likewise.
    graphics['pattern']['colormap'] = cm.gist_rainbow(
        np.linspace(0, 1,
                    pattern['dimension']['x'] * pattern['dimension']['y']))

    graphics['collections']['colormap'] = cm.tab20b(
        np.linspace(0, 1, len(dataset['collections'].keys())))
    for idx, collection_key in enumerate(sorted(
            dataset['collections'].keys())):
        graphics['collections'][str(collection_key)] = {
            'color': graphics['collections']['colormap'][idx, :]
        }

    # color_map_sensors = cm.gist_rainbow(np.linspace(0, 1, len(dataset['sensors'].keys())))
    # for idx, sensor_key in enumerate(sorted(dataset['sensors'].keys())):
    #     dataset['sensors'][str(sensor_key)]['color'] = color_map_sensors[idx, :]

    # Create image publishers ----------------------------------------------------------
    # We need to republish a new image at every visualization
    for collection_key, collection in dataset['collections'].items():
        for sensor_key, sensor in dataset['sensors'].items():
            if not collection['labels'][str(sensor_key)][
                    'detected']:  # not detected by sensor in collection
                continue

            if sensor['msg_type'] == 'Image':
                msg_type = sensor_msgs.msg.Image
                topic = dataset['calibration_config']['sensors'][sensor_key][
                    'topic_name']
                topic_name = '~c' + str(collection_key) + topic + '/labeled'
                graphics['collections'][collection_key][str(sensor_key)] = {
                    'publisher':
                    rospy.Publisher(topic_name,
                                    msg_type,
                                    queue_size=0,
                                    latch=True)
                }

                msg_type = sensor_msgs.msg.CameraInfo
                topic_name = '~c' + str(collection_key) + '/' + str(
                    sensor_key) + '/camera_info'
                graphics['collections'][collection_key][str(sensor_key)]['publisher_camera_info'] = \
                    rospy.Publisher(topic_name, msg_type, queue_size=0, latch=True)

    # Create Labeled Data publishers ----------------------------------------------------------
    markers = MarkerArray()
    for collection_key, collection in dataset['collections'].items():
        for sensor_key, sensor in dataset['sensors'].items():
            if not collection['labels'][str(sensor_key)][
                    'detected']:  # not detected by sensor in collection
                continue

            if sensor[
                    'msg_type'] == 'LaserScan':  # -------- Publish the laser scan data ------------------------------
                frame_id = genCollectionPrefix(
                    collection_key,
                    collection['data'][sensor_key]['header']['frame_id'])
                marker = Marker(
                    header=Header(frame_id=frame_id, stamp=now),
                    ns=str(collection_key) + '-' + str(sensor_key),
                    id=0,
                    frame_locked=True,
                    type=Marker.POINTS,
                    action=Marker.ADD,
                    lifetime=rospy.Duration(0),
                    pose=Pose(position=Point(x=0, y=0, z=0),
                              orientation=Quaternion(x=0, y=0, z=0, w=1)),
                    scale=Vector3(x=0.03, y=0.03, z=0),
                    color=ColorRGBA(
                        r=graphics['collections'][collection_key]['color'][0],
                        g=graphics['collections'][collection_key]['color'][1],
                        b=graphics['collections'][collection_key]['color'][2],
                        a=1.0))

                # Get laser points that belong to the chessboard (labelled)
                idxs = collection['labels'][sensor_key]['idxs']
                rhos = [
                    collection['data'][sensor_key]['ranges'][idx]
                    for idx in idxs
                ]
                thetas = [
                    collection['data'][sensor_key]['angle_min'] +
                    collection['data'][sensor_key]['angle_increment'] * idx
                    for idx in idxs
                ]

                for idx, (rho, theta) in enumerate(zip(rhos, thetas)):
                    marker.points.append(
                        Point(x=rho * math.cos(theta),
                              y=rho * math.sin(theta),
                              z=0))

                markers.markers.append(copy.deepcopy(marker))

                # Draw extrema points
                marker.ns = str(collection_key) + '-' + str(sensor_key)
                marker.type = Marker.SPHERE_LIST
                marker.id = 1
                marker.scale.x = 0.1
                marker.scale.y = 0.1
                marker.scale.z = 0.1
                marker.color.a = 0.5
                marker.points = [marker.points[0], marker.points[-1]]

                markers.markers.append(copy.deepcopy(marker))

                # Draw detected edges
                marker.ns = str(collection_key) + '-' + str(sensor_key)
                marker.type = Marker.CUBE_LIST
                marker.id = 2
                marker.scale.x = 0.05
                marker.scale.y = 0.05
                marker.scale.z = 0.05
                marker.color.a = 0.5

                marker.points = []  # Reset the list of marker points
                for edge_idx in collection['labels'][sensor_key][
                        'edge_idxs']:  # add edge points
                    p = Point()
                    p.x = rhos[edge_idx] * math.cos(thetas[edge_idx])
                    p.y = rhos[edge_idx] * math.sin(thetas[edge_idx])
                    p.z = 0
                    marker.points.append(p)
                markers.markers.append(copy.deepcopy(marker))

            if sensor[
                    'msg_type'] == 'PointCloud2':  # -------- Publish the velodyne data ------------------------------

                # Convert velodyne data on .json dictionary to ROS message type
                cloud_msg = message_converter.convert_dictionary_to_ros_message(
                    "sensor_msgs/PointCloud2", collection['data'][sensor_key])

                # Get LiDAR points that belong to the pattern
                idxs = collection['labels'][sensor_key]['idxs']
                pc = ros_numpy.numpify(cloud_msg)
                points = np.zeros((pc.shape[0], 3))
                points[:, 0] = pc['x']
                points[:, 1] = pc['y']
                points[:, 2] = pc['z']

                frame_id = genCollectionPrefix(
                    collection_key,
                    collection['data'][sensor_key]['header']['frame_id'])
                marker = Marker(
                    header=Header(frame_id=frame_id, stamp=now),
                    ns=str(collection_key) + '-' + str(sensor_key),
                    id=0,
                    frame_locked=True,
                    type=Marker.SPHERE_LIST,
                    action=Marker.ADD,
                    lifetime=rospy.Duration(0),
                    pose=Pose(position=Point(x=0, y=0, z=0),
                              orientation=Quaternion(x=0, y=0, z=0, w=1)),
                    scale=Vector3(x=0.02, y=0.02, z=0.02),
                    color=ColorRGBA(
                        r=graphics['collections'][collection_key]['color'][0],
                        g=graphics['collections'][collection_key]['color'][1],
                        b=graphics['collections'][collection_key]['color'][2],
                        a=0.4))

                for idx in idxs:
                    marker.points.append(
                        Point(x=points[idx, 0],
                              y=points[idx, 1],
                              z=points[idx, 2]))

                markers.markers.append(copy.deepcopy(marker))

                # Visualize LiDAR corner points
                marker = Marker(
                    header=Header(frame_id=frame_id, stamp=now),
                    ns=str(collection_key) + '-' + str(sensor_key) +
                    '-limit_points',
                    id=0,
                    frame_locked=True,
                    type=Marker.SPHERE_LIST,
                    action=Marker.ADD,
                    lifetime=rospy.Duration(0),
                    pose=Pose(position=Point(x=0, y=0, z=0),
                              orientation=Quaternion(x=0, y=0, z=0, w=1)),
                    scale=Vector3(x=0.07, y=0.07, z=0.07),
                    color=ColorRGBA(
                        r=graphics['collections'][collection_key]['color'][0],
                        g=graphics['collections'][collection_key]['color'][1],
                        b=graphics['collections'][collection_key]['color'][2],
                        a=0.8))

                for pt in collection['labels'][sensor_key]['limit_points']:
                    marker.points.append(Point(x=pt['x'], y=pt['y'],
                                               z=pt['z']))

                markers.markers.append(copy.deepcopy(marker))

    graphics['ros']['MarkersLabeled'] = markers
    graphics['ros']['PubLabeled'] = rospy.Publisher('~labeled_data',
                                                    MarkerArray,
                                                    queue_size=0,
                                                    latch=True)

    # -----------------------------------------------------------------------------------------------------
    # -------- Robot meshes
    # -----------------------------------------------------------------------------------------------------
    # Check whether the robot is static, in the sense that all of its joints are fixed. If so, for efficiency purposes,
    # only one robot mesh (from the selected collection) is published.
    all_joints_fixed = True
    for joint in xml_robot.joints:
        if not joint.type == 'fixed':
            print('Robot has at least joint ' + joint.name +
                  ' non fixed. Will render all collections')
            all_joints_fixed = False
            break

    markers = MarkerArray()
    if all_joints_fixed:  # render a single robot mesh
        print('Robot has all joints fixed. Will render only collection ' +
              selected_collection_key)
        rgba = [.5, .5, .5, 1]  # best color we could find
        m = urdfToMarkerArray(xml_robot,
                              frame_id_prefix=genCollectionPrefix(
                                  selected_collection_key, ''),
                              namespace=selected_collection_key,
                              rgba=rgba)
        markers.markers.extend(m.markers)
    else:  # render robot meshes for all collections
        for collection_key, collection in dataset['collections'].items():
            # rgba = graphics['collections'][collection_key]['color']
            # rgba[3] = 0.4  # change the alpha
            rgba = [.5, .5, .5, 0.7]  # best color we could find
            m = urdfToMarkerArray(xml_robot,
                                  frame_id_prefix=genCollectionPrefix(
                                      collection_key, ''),
                                  namespace=collection_key,
                                  rgba=rgba)
            markers.markers.extend(m.markers)

    graphics['ros']['robot_mesh_markers'] = markers

    # -----------------------------------------------------------------------------------------------------
    # -------- Publish the pattern data
    # -----------------------------------------------------------------------------------------------------
    if dataset['calibration_config']['calibration_pattern'][
            'fixed']:  # Draw single pattern for selected collection key
        frame_id = generateName(
            dataset['calibration_config']['calibration_pattern']['link'],
            prefix='c' + selected_collection_key)
        ns = str(selected_collection_key)
        markers = createPatternMarkers(frame_id, ns, selected_collection_key,
                                       now, dataset, graphics)
    else:  # Draw a pattern per collection
        markers = MarkerArray()
        for idx, (collection_key,
                  collection) in enumerate(dataset['collections'].items()):
            frame_id = generateName(
                dataset['calibration_config']['calibration_pattern']['link'],
                prefix='c' + collection_key)
            ns = str(collection_key)
            collection_markers = createPatternMarkers(frame_id, ns,
                                                      collection_key, now,
                                                      dataset, graphics)
            markers.markers.extend(collection_markers.markers)

    graphics['ros']['MarkersPattern'] = markers
    graphics['ros']['PubPattern'] = rospy.Publisher('~patterns',
                                                    MarkerArray,
                                                    queue_size=0,
                                                    latch=True)

    # Create LaserBeams Publisher -----------------------------------------------------------
    # This one is recomputed every time in the objective function, so just create the generic properties.
    markers = MarkerArray()

    for collection_key, collection in dataset['collections'].items():
        for sensor_key, sensor in dataset['sensors'].items():
            if not collection['labels'][sensor_key][
                    'detected']:  # chess not detected by sensor in collection
                continue
            if sensor['msg_type'] == 'LaserScan' or sensor[
                    'msg_type'] == 'PointCloud2':
                frame_id = genCollectionPrefix(
                    collection_key,
                    collection['data'][sensor_key]['header']['frame_id'])
                marker = Marker(
                    header=Header(frame_id=frame_id, stamp=rospy.Time.now()),
                    ns=str(collection_key) + '-' + str(sensor_key),
                    id=0,
                    frame_locked=True,
                    type=Marker.LINE_LIST,
                    action=Marker.ADD,
                    lifetime=rospy.Duration(0),
                    pose=Pose(position=Point(x=0, y=0, z=0),
                              orientation=Quaternion(x=0, y=0, z=0, w=1)),
                    scale=Vector3(x=0.001, y=0, z=0),
                    color=ColorRGBA(
                        r=graphics['collections'][collection_key]['color'][0],
                        g=graphics['collections'][collection_key]['color'][1],
                        b=graphics['collections'][collection_key]['color'][2],
                        a=1.0))
                markers.markers.append(marker)

    graphics['ros']['MarkersLaserBeams'] = markers
    graphics['ros']['PubLaserBeams'] = rospy.Publisher('~LaserBeams',
                                                       MarkerArray,
                                                       queue_size=0,
                                                       latch=True)

    # Create Miscellaneous MarkerArray -----------------------------------------------------------
    markers = MarkerArray()

    # Text signaling the anchored sensor
    for _sensor_key, sensor in dataset['sensors'].items():
        if _sensor_key == dataset['calibration_config']['anchored_sensor']:
            marker = Marker(header=Header(frame_id=str(_sensor_key),
                                          stamp=now),
                            ns=str(_sensor_key),
                            id=0,
                            frame_locked=True,
                            type=Marker.TEXT_VIEW_FACING,
                            action=Marker.ADD,
                            lifetime=rospy.Duration(0),
                            pose=Pose(position=Point(x=0, y=0, z=0.2),
                                      orientation=Quaternion(x=0,
                                                             y=0,
                                                             z=0,
                                                             w=1)),
                            scale=Vector3(x=0.0, y=0.0, z=0.1),
                            color=ColorRGBA(r=0.6, g=0.6, b=0.6, a=1.0),
                            text='Anchored')

            markers.markers.append(marker)

    graphics['ros']['MarkersMiscellaneous'] = markers
    graphics['ros']['PubMiscellaneous'] = rospy.Publisher('~Miscellaneous',
                                                          MarkerArray,
                                                          queue_size=0,
                                                          latch=True)
    # Publish only once in latched mode
    graphics['ros']['PubMiscellaneous'].publish(
        graphics['ros']['MarkersMiscellaneous'])

    return graphics
import os
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm

plt.style.use('seaborn')

os.chdir('..')
os.chdir('data')
df = pd.read_csv('Casos_Diarios_Estado_Nacional_Confirmados_20200812.csv')

population = df['poblacion'].iloc[:-1]
state = df['nombre'].iloc[:-1]

for i in range(0,len(state)):
    if i % 2 == 0:
        plt.barh(state[i],population[i],color=cm.tab20b(1.*i/len(state)))
    else:
        plt.barh(state[i],population[i],color=cm.tab20(1.*i/len(state)))

plt.title('Population of México')
plt.xlabel('Population')
plt.tight_layout()
plt.show()
Example #8
0
import psycopg2
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

# TALK TO DA DB
conn = psycopg2.connect('dbname=budget')
cur = conn.cursor()

cur.execute('SELECT SUM(amount) FROM expenses')
expenses = cur.fetchone()[0]
cur.execute('SELECT SUM(amount) FROM income')
income = cur.fetchone()[0]

conn.close()

# SHOW ME THE MONEY (lol u so funny)
slices = 2
colors = cm.tab20b(np.linspace(0, 1, slices))
plt.pie([expenses, income], labels=['expenses', 'income'], colors=colors)
plt.axis('equal')
plt.show()
Example #9
0
def calc_projection_errors(axis, dataset, from_sensor, to_sensor):

    error = []
    colors = cm.tab20b(np.linspace(0, 1, len(dataset['collections'].items())))
    axis.grid(True)

    sorted_collection = OrderedDict(
        sorted(dataset['collections'].items(), key=lambda x: int(x[0])))

    for collection_key, collection in sorted_collection.items():
        if not collection['labels'][from_sensor]['detected']:
            continue

        if not collection['labels'][to_sensor]['detected']:
            continue

        tree = collection['tf_tree']
        labels = collection['labels'][from_sensor]

        # 1. Calculate pattern to sensor transformation.
        K = np.ndarray(
            (3, 3),
            dtype=np.float,
            buffer=np.array(
                dataset['sensors'][from_sensor]['camera_info']['K']))
        D = np.ndarray(
            (5, 1),
            dtype=np.float,
            buffer=np.array(
                dataset['sensors'][from_sensor]['camera_info']['D']))

        corners = np.zeros((3, len(labels['idxs'])), dtype=np.float)
        ids = [0] * len(labels['idxs'])
        for idx, point in enumerate(labels['idxs']):
            corners[0, idx] = point['x']
            corners[1, idx] = point['y']
            corners[2, idx] = 1
            ids[idx] = point['id']

        _, rvecs, tvecs = cv2.solvePnP(np.array(dataset['grid'][:3, :].T[ids]),
                                       corners[:2, :].T.reshape(-1, 1,
                                                                2), K, D)
        sTc = utilities.traslationRodriguesToTransform(tvecs, rvecs)

        # 2. Get the transformation between sensors
        tTf = tree.lookup_transform(
            dataset['sensors'][from_sensor]['camera_info']['header']
            ['frame_id'], dataset['sensors'][to_sensor]['camera_info']
            ['header']['frame_id']).matrix

        if from_sensor == to_sensor:
            x1 = tree.lookup_transform(
                dataset['calibration_config']['calibration_pattern']['link'],
                dataset['calibration_config']['world_link']).matrix

            x2 = tree.lookup_transform(
                dataset['calibration_config']['world_link'], dataset['sensors']
                [to_sensor]['camera_info']['header']['frame_id']).matrix

            sTc = np.dot(x2, x1)

        # 3. Transform the corners to the destination sensor
        K2 = np.ndarray(
            (3, 3),
            dtype=np.float,
            buffer=np.array(dataset['sensors'][to_sensor]['camera_info']['K']))
        D2 = np.ndarray(
            (5, 1),
            dtype=np.float,
            buffer=np.array(dataset['sensors'][to_sensor]['camera_info']['D']))

        labels = collection['labels'][to_sensor]
        corners2 = np.zeros((2, len(labels['idxs'])), dtype=np.float)
        ids2 = [0] * len(labels['idxs'])
        for idx, point in enumerate(labels['idxs']):
            corners2[0, idx] = point['x']
            corners2[1, idx] = point['y']
            ids2[idx] = point['id']

        corners = np.dot(tTf, np.dot(sTc, dataset['grid'].T[ids2].T))
        projected, _, _ = utilities.projectToCamera(K2, D2, 0, 0, corners)

        # 4. Calculate difference
        diff = projected - corners2
        axis.plot(diff[0, :],
                  diff[1, :],
                  'o',
                  label=collection_key,
                  alpha=0.7,
                  color=colors[int(collection_key)])

        error.extend(np.sum(diff * diff, 0).tolist())

    if from_sensor == to_sensor:
        axis.patch.set_facecolor('blue')
        axis.patch.set_alpha(0.05)

    rmse = (np.sqrt(np.mean(np.array(error))))
    axis.set_title('$e_{\mathrm{rmse}} ' + ' = {:.4}$'.format(rmse))

    lim = 4
    axis.set_ylim(-lim, lim)
    axis.set_xlim(-lim, lim)

    print("From '{}' to '{}'".format(from_sensor, to_sensor))
    print(np.sqrt(np.mean(np.array(error))))
Example #10
0
import os

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm


#====== COLOUR SCHEMES =========================================================
tab10 = [cm.tab10(i) for i in np.linspace(0, 1, 10)]
tab20b = np.array([cm.tab20b(i) for i in np.linspace(0,1,20)]).reshape((5,4,4))

colours = [cm.Set2(i) for i in np.linspace(0,1,8)]
colours2 = [cm.Dark2(i) for i in np.linspace(0,1,8)]
#===============================================================================

#====== LOAD FILES =============================================================
PI_17_files = ['PI_Kc-17_limit-20.csv',
               'PI_Kc-17_limit-15.csv',
               'PI_Kc-17_limit-10.csv']

PI_10_files = ['PI_Kc-10_limit-20.csv',
               'PI_Kc-10_limit-15.csv',
               'PI_Kc-10_limit-10.csv']

PI_5_files = ['PI_Kc-5_limit-20.csv']

thresh_files = ['lab_to_-10.csv',
                'lab_to_-15.csv',
                'lab_to_-20.csv'][::-1]

PI_17 = [np.genfromtxt(f, delimiter=',')[1:] for f in PI_17_files]
        ws.append([aaI] + rowValues)
        allValues.append(rowValues)

    #wb.save("/mnt/c/Users/mjopp/Desktop/"+subMatrix2Org[mdx]+".xlsx")

    organism = "Helicobacter pylori" if mdx == 0 else "Campylobacter jejuni"

    allP = []
    ind = [x for x in range(len(sortedAA))]

    accumVal = [0] * len(sortedAA)

    print(accumVal, len(accumVal))
    print(ind, len(ind))

    colors = iter(cm.tab20b(np.linspace(0, 1, len(sortedAA))))

    ax = lax[mdx]

    for idx, row in enumerate(allValues):

        p = ax.bar(ind,
                   row,
                   0.35,
                   bottom=accumVal,
                   label=translateLegendName.get(sortedAA[idx], sortedAA[idx]),
                   color=next(colors))

        allP.append(p)

        for idx, x in enumerate(row):
Example #12
0
'''
# Print the sheet names
print(xl.sheet_names)
'''
# Load a sheet into a DataFrame by name
data_gripper = xl.parse('gripper')
data_robot = xl.parse('robot')
data_robot_typ = xl.parse('robot_type')

print('Anzahl gripper: ' + str(len(data_gripper)))
print('Anzahl robot: ' + str(len(data_robot)))

data_gripper = data_gripper.sort_values(by=['id'])

len_groups = len(data_gripper.groupby(['version']))
color = cm.tab20b(np.linspace(.05, 1, len_groups))

fig, ax = plt.subplots()
keys = []
locator = [0]
i = 0
for key, grp in data_gripper.groupby(['version']):
    ax = grp.plot(ax=ax,
                  kind='scatter',
                  x='max_workpiece_weight',
                  y='stroke_per_jaw',
                  label=key,
                  c=color[i])  #, y='number_fingers', c=key, label=key)
    print(color[i])
    keys.append(key)
    i += 1
           ['D','Y','C'],
           [argmins_deaths,argmins_yll,argmins_cases])
for jj,(total_best,total,ylabel,letter,argmins) in enumerate(zipp):
    f,ax=plt.subplots(figsize=(3,1.5))
    ax.plot(hesitancies,total_best,color='k')
    ax.plot(hesitancies,total,color=cm.tab10(3))
    ax.set_xlabel('hesitancy')
    ax.set_ylabel(ylabel)
    [y1,y2]=ax.get_ylim()
    for ii in range(max(argmins)+1):
        xs = hesitancies[np.where(argmins==ii)[0][0]:np.where(argmins==ii)[0][-1]+1]
        try:
            print([letter,min(xs),max(xs),ii])
        except:
            pass
        ax.fill_between(xs,[0]*len(xs),1e8*xs,color=cm.tab20b(colorstarter[jj]+ii))
        ax.text(np.mean(xs),y1+0.9*(y2-y1),letter+str(ii+1),ha='center',va='center')
    ax.set_ylim([y1,y2])
    ax.set_xlim([min(hesitancies),max(hesitancies)])
    ax.set_xticks([0,0.1,0.2,0.3])
    ax.set_xticklabels(['%i%%' % int(el*100) for el in ax.get_xticks()])
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    
    if SHOW_DIFFERENCES:
        ax2=ax.twinx()
        ax2.plot(hesitancies,[el1-el2 if ABSOLUTE_DIFFERENCES else (el1/el2-1)*100 for el1,el2 in zip(total,total_best)],'k:')
        ax2.set_ylabel('difference' if ABSOLUTE_DIFFERENCES else '% difference')
        if not ABSOLUTE_DIFFERENCES:
            ax2.set_yticklabels(['%s%%' % str(round(el,3)) for el in ax2.get_yticks()])
        ax2.spines['top'].set_visible(False)
def create_plots(dict_counter, name, jaro, text_on=False, which_text=None):
    distances = {}
    group = {}

    labels = []
    for k1 in dict_counter.keys():
        for k2 in dict_counter[k1].keys():
            name1 = f"{k2}_{k1}"
            distances[name1] = Levenshtein.jaro_winkler(
                k1, k2) + 0.0000001 * np.random.random()
            group[name1] = k1
            labels.append(k1)
    testa = list(set(group.values()))
    best_word = find_first_word(testa)
    testa.remove(best_word)
    testb = sort_jaro_worst(best_word, testa[:])
    colors = []
    if len(testb) <= 20:
        cm_subsection1 = linspace(0.0, 1.0, len(testb))
    elif len(testb) > 20 and len(testb) <= 40:
        cm_subsection1 = linspace(0.0, 1.0, 20)
        cm_subsection2 = linspace(0.0, 1.0, len(testb) - 20)
    else:
        cm_subsection1 = linspace(0.0, 1.0, 20)
        cm_subsection2 = linspace(0.0, 1.0, 20)
        cm_subsection3 = linspace(0.0, 1.0, len(testb) - 40)
    for i in range(len(testb)):
        if i < 20:
            colors.append(cm.tab20(cm_subsection1[i]))
        elif i >= 20 and i < 39:
            colors.append(cm.tab20b(cm_subsection2[i - 20]))
        else:
            colors.append(cm.tab20c(cm_subsection3[i - 40]))
    colorsdict = dict(zip(testb, colors))
    df = pd.Series(distances)

    c = [colorsdict.get(group[label], 'k') for label in df.index]
    fig, aa = plt.subplots(figsize=(11, 9))
    aa.axes.get_xaxis().set_visible(False)
    aa.set_xlim(-11, 0.1)
    aa.set_ylim(jaro-.005, 1.005)
    scatter_items = []
    legend_items = []
    for t, d, ca, l in zip([0 for _ in df], df, c, df.index):
        scatter = aa.scatter(t, d, c=ca, alpha=0.5,
                             edgecolors='none')

    for l, colo in zip(colorsdict.keys(), colorsdict.values()):

        scatter_items += aa.plot((-100,), (-100,),
                                 ls='none', marker='.', c=colo, label=l)
    aa.legend(handles=scatter_items, title="Terms", fontsize=7,
              loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol=10)
    aa.spines['left'].set_visible(False)
    aa.spines['top'].set_visible(False)
    aa.spines['bottom'].set_visible(False)
    aa.yaxis.set_label_position('right')
    aa.yaxis.set_ticks_position('right')
    plt.tight_layout()

    # We add a rectangle to make sure the labels don't move to the right
    patch = patches.Rectangle((-0.1, 0), 0.2, 100, fill=False, alpha=0)
    aa.add_patch(patch)
    texts = []
    np.random.seed(0)
    for label, y in zip(df.index, df):
        texts += [aa.text(-.1+np.random.random()/1000, y, label.split("_")[0],
                          color=colorsdict.get(group[label], 'k'), fontsize=9)]

    adjust_text(texts, [0 for _ in df], df.values,  ha='right', va='center', add_objects=[patch],
                expand_text=(1.1, 1.25),
                force_text=(0.75, 0), force_objects=(1, 0),
                autoalign=False, only_move={'points': 'x', 'text': 'x', 'objects': 'x'})
    plt.savefig(f'{name}_scatter.png')
    print(len(list(set(group.values()))))
    print(len(testb))
    print(testb)
Example #15
0
def df_input(dfUniprot=DataFrame([])):

    num_phylum = len(dfUniprot.PHYLUM.drop_duplicates())
    from matplotlib import cm
    set3 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Set3(np.arange(12) / 12.)
    ]
    set2 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Set2(np.arange(8) / 8.)
    ]
    set1 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Set1(np.arange(9) / 9.)
    ]
    pastel2 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Pastel2(np.arange(8) / 8.)
    ]
    pastel1 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Pastel1(np.arange(9) / 9.)
    ]
    dark2 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Dark2(np.arange(8) / 8.)
    ]
    paired = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Paired(np.arange(12) / 12.)
    ]
    accent = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Accent(np.arange(8) / 8.)
    ]
    spectral = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.Spectral(np.arange(11) / 11.)
    ]
    tab20 = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.tab20(np.arange(20) / 20.)
    ]
    tab20b = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.tab20b(np.arange(20) / 20.)
    ]
    tab20c = [
        matplotlib.colors.rgb2hex(tuple(i))
        for i in cm.tab20c(np.arange(20) / 20.)
    ]

    Colors1 = set2 + set1 + dark2 + paired + accent + spectral + tab20 + tab20b + tab20c
    Colors2 = accent + spectral + tab20 + tab20b + tab20c + set1 + set2 + dark2 + paired
    Colors3 = dark2 + paired + accent + spectral + tab20 + tab20b + tab20c + set1 + set2
    Colors4 = tab20b + tab20c + set1 + set2 + dark2 + paired + accent + spectral + tab20
    Colors5 = spectral + tab20 + tab20b + tab20c + set1 + set2 + dark2 + paired + accent

    pie_colors = {
        'Set3': cm.Set3(np.arange(12) / 12.),
        'Set2': cm.Set2(np.arange(8) / 8.),
        'Set1': cm.Set1(np.arange(9) / 9.),
        'Pastel2': cm.Pastel2(np.arange(8) / 8.),
        'Pastel1': cm.Pastel1(np.arange(9) / 9.),
        'Dark2': cm.Dark2(np.arange(8) / 8.),
        'Paired': cm.Paired(np.arange(12) / 12.),
        'Accent': cm.Accent(np.arange(8) / 8.),
        'Spectral': cm.Spectral(np.arange(11) / 11.),
        'tab20': cm.tab20(np.arange(20) / 20.),
        'tab20b': cm.tab20b(np.arange(20) / 20.),
        'tab20c': cm.tab20c(np.arange(20) / 20.)
    }
    circle_colors = {
        'Colors1': Colors1[0:num_phylum],
        'Colors2': Colors2[0:num_phylum],
        'Colors3': Colors3[0:num_phylum],
        'Colors4': Colors4[0:num_phylum],
        'Colors5': Colors5[0:num_phylum]
    }

    def tax_colors(color_list=circle_colors['Colors1'], taxx=dfUniprot):
        tax_cols = [
            'Entry', 'Tax_ID', 'KINGDOM', 'PHYLUM', 'CLASS', 'ORDER', 'FAMILY',
            'GENUS', 'SPECIES', 'Organism'
        ]
        new2 = taxx[tax_cols].drop_duplicates()
        #>>>>>>>>>>>>>>>>>>>>>>>>>>>
        phylum0 = new2.groupby(['PHYLUM'
                                ]).Entry.count().reset_index().sort_values(
                                    by='Entry',
                                    ascending=False).reset_index(drop=True)
        asign_color = {}
        for i, j in zip(phylum0.PHYLUM, color_list):
            if i == 'NA':
                asign_color[i] = 'black'
            else:
                asign_color[i] = j
        phylum0['phy_col'] = list(asign_color.values())
        # distribución de Class
        phylum1 = new2.groupby(['PHYLUM', 'CLASS']).Entry.count().reset_index()
        class0 = []
        class0_colors = []
        for i in phylum0.PHYLUM:
            for j in phylum1.PHYLUM:
                if i == j:
                    class0_colors.append(asign_color[j])
                    class0.append(phylum1[phylum1.PHYLUM == i].sort_values(
                        by='Entry', ascending=False).reset_index(drop=True))
                else:
                    pass
        class1 = pd.concat(class0).drop_duplicates()
        class1['class_col'] = class0_colors
        class0_colors_corregido = []
        for index, row in class1.iterrows():
            if row.PHYLUM == 'NA':
                if row.CLASS == 'NA':
                    class0_colors_corregido.append(row.class_col)
                else:
                    class0_colors_corregido.append('grey')
            else:
                if row.CLASS == 'NA':
                    class0_colors_corregido.append('black')
                else:
                    class0_colors_corregido.append(row.class_col)
        class1['class_col'] = class0_colors_corregido
        class11 = class1.groupby(['CLASS'
                                  ]).Entry.sum().reset_index().sort_values(
                                      by='Entry',
                                      ascending=False).reset_index(drop=True)
        class11 = class11.merge(class1[['CLASS',
                                        'class_col']].drop_duplicates(),
                                on='CLASS',
                                how='left')
        # distribución de Order
        phylum2 = new2.groupby(['PHYLUM', 'CLASS',
                                'ORDER']).Entry.count().reset_index()
        order0 = []
        order0_colors = []
        for i in phylum0.PHYLUM:
            for j in phylum2.PHYLUM:
                if i == j:
                    order0_colors.append(asign_color[j])
                    order0.append(phylum2[phylum2.PHYLUM == i].sort_values(
                        by='Entry', ascending=False).reset_index(drop=True))
                else:
                    pass
        order1 = pd.concat(order0).drop_duplicates()
        order1['order_col'] = order0_colors
        order0_colors_corregido = []
        for index, row in order1.iterrows():
            if row.PHYLUM == 'NA':
                if row.ORDER == 'NA':
                    order0_colors_corregido.append(row.order_col)
                else:
                    order0_colors_corregido.append('grey')
            else:
                if row.ORDER == 'NA':
                    order0_colors_corregido.append('black')
                else:
                    order0_colors_corregido.append(row.order_col)
        order1['order_col'] = order0_colors_corregido
        order11 = order1.groupby(['ORDER'
                                  ]).Entry.sum().reset_index().sort_values(
                                      by='Entry',
                                      ascending=False).reset_index(drop=True)
        order11 = order11.merge(order1[['ORDER',
                                        'order_col']].drop_duplicates(),
                                on='ORDER',
                                how='left')
        # distribución de Genus
        phylum3 = new2.groupby(['PHYLUM', 'CLASS', 'ORDER',
                                'GENUS']).Entry.count().reset_index()
        genus0 = []
        genus0_colors = []
        for i in phylum0.PHYLUM:
            for j in phylum3.PHYLUM:
                if i == j:
                    genus0_colors.append(asign_color[j])
                    genus0.append(phylum3[phylum3.PHYLUM == i].sort_values(
                        by='Entry', ascending=False).reset_index(drop=True))
                else:
                    pass
        genus1 = pd.concat(genus0).drop_duplicates()
        genus1['genus_col'] = genus0_colors
        genus0_colors_corregido = []
        for index, row in genus1.iterrows():
            if row.PHYLUM == 'NA':
                if row.GENUS == 'NA':
                    genus0_colors_corregido.append(row.genus_col)
                else:
                    genus0_colors_corregido.append('grey')
            else:
                if row.GENUS == 'NA':
                    genus0_colors_corregido.append('black')
                else:
                    genus0_colors_corregido.append(row.genus_col)
        genus1['genus_col'] = genus0_colors_corregido
        genus11 = genus1.groupby(['GENUS'
                                  ]).Entry.sum().reset_index().sort_values(
                                      by='Entry',
                                      ascending=False).reset_index(drop=True)
        genus11 = genus11.merge(genus1[['GENUS',
                                        'genus_col']].drop_duplicates(),
                                on='GENUS',
                                how='left')
        # distribución de Organism
        phylum4 = new2.groupby(
            ['PHYLUM', 'CLASS', 'ORDER', 'GENUS',
             'Organism']).Entry.count().reset_index()
        org0 = []
        org0_colors = []
        for i in phylum0.PHYLUM:
            for j in phylum4.PHYLUM:
                if i == j:
                    org0_colors.append(asign_color[j])
                    org0.append(phylum4[phylum4.PHYLUM == i].sort_values(
                        by='Entry', ascending=False).reset_index(drop=True))
                else:
                    pass
        org1 = pd.concat(org0).drop_duplicates()
        org1['org_col'] = org0_colors
        org0_colors_corregido = []
        for index, row in org1.iterrows():
            if row.PHYLUM == 'NA':
                if row.Organism == 'NA':
                    org0_colors_corregido.append(row.org_col)
                else:
                    org0_colors_corregido.append('grey')
            else:
                if row.Organism == 'NA':
                    org0_colors_corregido.append('black')
                else:
                    org0_colors_corregido.append(row.org_col)
        org1['org_col'] = org0_colors_corregido
        org11 = org1.groupby(['Organism'
                              ]).Entry.sum().reset_index().sort_values(
                                  by='Entry',
                                  ascending=False).reset_index(drop=True)
        org11 = org11.merge(org1[['Organism', 'org_col']].drop_duplicates(),
                            on='Organism',
                            how='left')
        os.makedirs('tax', exist_ok=True)
        return phylum0.to_csv('tax/phylum0.tsv', sep = '\t', index = None),\
            class1.to_csv('tax/class1.tsv', sep = '\t', index = None),\
            class11.to_csv('tax/class11.tsv', sep = '\t', index = None),\
            order1.to_csv('tax/order1.tsv', sep = '\t', index = None),\
            order11.to_csv('tax/order11.tsv', sep = '\t', index = None),\
            genus1.to_csv('tax/genus1.tsv', sep = '\t', index = None),\
            genus11.to_csv('tax/genus11.tsv', sep = '\t', index = None),\
            org1.to_csv('tax/org1.tsv', sep = '\t', index = None),\
            org11.to_csv('tax/org11.tsv', sep = '\t', index = None)

    alfas = {
        'Lineage*': [1, 1, 1, 1, 1],
        'Phylum': [1, 0.3, 0.3, 0.3, 0.3],
        'Class': [0.3, 1, 0.3, 0.3, 0.3],
        'Order': [0.3, 0.3, 1, 0.3, 0.3],
        'Genus': [0.3, 0.3, 0.3, 1, 0.3],
        'Species': [0.3, 0.3, 0.3, 0.3, 1],
        'Gradient1*': [1, 0.85, 0.7, 0.55, 0.4],
        'Gradient2*': [0.4, 0.55, 0.7, 0.85, 1],
        'Attenuate*': [0.3, 0.3, 0.3, 0.3, 0.3],
        'None*': [0, 0, 0, 0, 0]
    }

    def circle_lineage(alphas=alfas['Phylum']):
        #fig, ax = plt.subplots(111, facecolor= 'white')
        #fig, ax = plt.subplot(111)
        phylum0 = pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA')
        class1 = pd.read_csv('tax/class1.tsv', sep='\t').fillna('NA')
        order1 = pd.read_csv('tax/order1.tsv', sep='\t').fillna('NA')
        genus1 = pd.read_csv('tax/genus1.tsv', sep='\t').fillna('NA')
        org1 = pd.read_csv('tax/org1.tsv', sep='\t').fillna('NA')

        radio = 0.5

        linaje = [phylum0, class1, order1, genus1, org1]
        #colores = [list(asign_color.values()), class0_colors, order0_colors, genus0_colors, org0_colors]
        colores = ['phy_col', 'class_col', 'order_col', 'genus_col', 'org_col']
        pat = []
        size = -.205
        for i, j, k in zip(linaje, colores, alphas):
            size += .205
            patches, texts = plt.pie(
                i.Entry,
                radius=radio + size,
                labels=None,
                labeldistance=0.8,
                rotatelabels=True,
                colors=
                i[j],  # new_colors(valor = len(i.Entry), col = 'nipy_spectral'),
                wedgeprops=dict(width=0.2, edgecolor='white', alpha=k),
                textprops=dict(size=10))
            pat.append(patches)

        #plt.legend(pat[0], df_phylum.PHYLUM, loc=2,fontsize=13,labelspacing = 0.4,
        #          bbox_to_anchor=(1.05, 1),frameon=False)

        plt.gca().set(aspect='equal')
        plt.title('Root', fontsize=10, x=0.5, y=0.465)
        plt.text(-1.8,
                 1.35,
                 'Lineage',
                 fontsize=15,
                 ha='left',
                 va='center',
                 color='black')
        #plt.title('Lineage',fontsize=20, fontweight='bold', x = -0.17, y = 1)
        #plt.text(1.1, 1.35, linaje_seleccionado, fontsize = 15, ha='left', va='center',
        #                    color='black')
        #>>>>>>>>>>>>>>>>>>>>>>>
        #### insetplot
        #ax2 = plt.axes([0.1, 0.66, 0.13, 0.14])
        ax2 = plt.axes([-0.07, 1.71, 0.17, 0.18])
        logo = [20, 20, 20, 20, 20, 20, 20, 20]
        logo_col = [
            'white', 'white', 'black', 'white', 'white', 'white', 'white',
            'white'
        ]
        logo_col1 = [
            'white', 'white', 'black', 'black', 'black', 'black', 'black',
            'black'
        ]
        radio = 0.5
        linaje = [logo, logo, logo, logo, logo]
        colores = [logo_col1, logo_col, logo_col, logo_col, logo_col]
        name_linaje = ['Phylum', 'Class', 'Order', 'Genus', 'Species']

        pat = []
        size = -.44
        pos = -.18
        for i, j, k, l in zip(linaje, colores, name_linaje, alphas):
            pos += .47
            size += .44
            ax2.pie(i,
                    radius=radio + size,
                    labels=None,
                    colors=j,
                    wedgeprops=dict(width=0.35, edgecolor='white', alpha=l),
                    textprops=dict(size=10))
            ax2.text(0.1,
                     pos,
                     k,
                     fontsize=9,
                     ha='left',
                     va='center',
                     fontweight='bold',
                     alpha=l)  #color='black'

    def barras_tax(df=DataFrame([]),
                   column=0,
                   dim=111,
                   title='',
                   row_num=10,
                   color=['#ff7f0e'],
                   size_x=8,
                   size_y=10,
                   ylabel_text='',
                   xlabel=10,
                   ylabel=10,
                   size_title=15,
                   size_bartxt=10,
                   sep=1.2):
        if len(df) == 0:
            print('Data frame sin datos')
        else:
            #plt.subplot(dim, facecolor= 'white')
            barWidth = 0.9
            if row_num == len(df):
                ejey = list(df.iloc[0:len(df), 1])
                val = max(ejey)
                ejex = list(df.iloc[0:len(df), column])
                colores = list(df.iloc[0:len(df), 2])
                borde = list(
                    np.repeat('white', len(df.iloc[0:row_num, column])))
                linea = list(np.repeat(0, len(df.iloc[0:row_num, column])))
            if row_num < len(df):
                ejey = list(df.iloc[0:row_num,
                                    1]) + [df.iloc[row_num:len(df), 1].sum()]
                val = max(ejey)
                ejex = list(df.iloc[0:row_num, column]) + ['Others']
                borde = list(
                    np.repeat('white', len(df.iloc[0:row_num,
                                                   column]))) + ['black']
                colores = list(df.iloc[0:row_num, 2]) + ['linen']
                linea = list(np.repeat(0, len(df.iloc[0:row_num,
                                                      column]))) + [1]
            if row_num > len(df):
                ejey = list(df.iloc[0:len(df), 1])
                val = max(ejey)
                ejex = list(df.iloc[0:len(df), column])
                borde = list(
                    np.repeat('white', len(df.iloc[0:row_num, column])))
                colores = list(df.iloc[0:len(df), 2])
                linea = list(np.repeat(0, len(df.iloc[0:row_num, column])))

            for i, j, k, l, m in zip(ejex, ejey, borde, colores, linea):
                plt.barh(i,
                         j,
                         color=l,
                         align='center',
                         height=0.7,
                         linewidth=m,
                         alpha=1,
                         edgecolor=k)
            plt.gca().spines['right'].set_visible(False)
            plt.gca().spines['top'].set_visible(False)
            plt.gca().spines['bottom'].set_position(('data', -0.6))
            plt.gca().spines['left'].set_visible(False)
            plt.title(title, size=size_title, loc='left')
            plt.tick_params(axis="y", color="gray")
            plt.yticks(size=size_y)

            v1 = -50
            v2 = 0
            v3 = 0
            for i in range(10000):
                v1 += 50
                v2 += 50
                v3 += 10
                if v1 <= max(list(ejey)) < v2:
                    #print(v3, v1, val, v2)
                    escala = v3

            plt.xticks(range(0, val, escala), size=size_x)  #fontweight='bold'
            plt.ylabel(ylabel_text, size=ylabel)
            plt.xlabel("Number of Proteins", size=xlabel)
            #plt.tick_params(top = 'on', bottom = 'on', right = 'on', left = 'on')
            #plt.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)

            for j, k in zip(ejey, range(0, len(ejey))):
                plt.text(j + sep,
                         k - 0.2,
                         j,
                         size=size_bartxt,
                         ha='left',
                         color='black')

    import ipywidgets as widgets
    from ipywidgets import interact, interactive, fixed, interact_manual, Button, HBox, VBox, IntSlider, Label, IntRangeSlider
    from ipywidgets import Checkbox, RadioButtons
    from ipywidgets import Button, Layout
    alfas = {
        'Lineage*': [1, 1, 1, 1, 1],
        'Phylum': [1, 0.3, 0.3, 0.3, 0.3],
        'Class': [0.3, 1, 0.3, 0.3, 0.3],
        'Order': [0.3, 0.3, 1, 0.3, 0.3],
        'Genus': [0.3, 0.3, 0.3, 1, 0.3],
        'Species': [0.3, 0.3, 0.3, 0.3, 1],
        'Gradient1*': [1, 0.85, 0.7, 0.55, 0.4],
        'Gradient2*': [0.4, 0.55, 0.7, 0.85, 1],
        'Attenuate*': [0.3, 0.3, 0.3, 0.3, 0.3],
        'None*': [0, 0, 0, 0, 0]
    }
    plotss = ['Phylum', 'Class', 'Order', 'Genus', 'Species']
    posicion_subplots = []
    n = 0.9
    while n < 2:
        n += 0.1
        posicion_subplots.append(np.around(n, 1))

    color_a6 = widgets.Dropdown(options=list(circle_colors.keys()),
                                value='Colors1',
                                description='Colors:',
                                disabled=False,
                                button_style='',
                                layout=Layout(width='20%', height='25px'))
    a6 = widgets.Dropdown(options=list(alfas.keys()),
                          description='Chart 1:',
                          value='Phylum',
                          disabled=False,
                          button_style='',
                          layout=Layout(width='20%', height='25px'))
    a61 = widgets.Dropdown(options=plotss,
                           description='Chart 2:',
                           disabled=False,
                           button_style='',
                           layout=Layout(width='20%', height='25px'))
    pos_sub1 = widgets.Dropdown(options=posicion_subplots,
                                value=1.3,
                                description='xloc1:',
                                disabled=False,
                                layout=Layout(width='15%', height='25px'))
    pos_sub2 = widgets.Dropdown(options=posicion_subplots,
                                value=1.3,
                                description='xloc2:',
                                disabled=False,
                                layout=Layout(width='15%', height='25px'))
    b6 = widgets.Dropdown(options=list(range(0, 101)),
                          value=10,
                          description='rows1:',
                          disabled=False,
                          layout=Layout(width='15%', height='25px'))
    c6 = widgets.Dropdown(options=list(range(0, 101)),
                          value=10,
                          description='rows2:',
                          disabled=False,
                          layout=Layout(width='15%', height='25px'))
    z6 = widgets.ToggleButton(value=False,
                              description='Save Chart',
                              disabled=False,
                              button_style='',
                              tooltip='Description')
    o6 = widgets.Dropdown(options=[0, 0.25, 0.5, 0.75] + list(range(0, 201)),
                          value=3,
                          description='sep1:',
                          disabled=False,
                          layout=Layout(width='15%', height='25px'))
    o61 = widgets.Dropdown(options=[0, 0.25, 0.5, 0.75] + list(range(0, 201)),
                           value=3,
                           description='sep2:',
                           disabled=False,
                           layout=Layout(width='15%', height='25px'))

    d6 = widgets.Dropdown(options=list(range(0, 51)),
                          value=8,
                          description='size_y1:',
                          disabled=False,
                          layout=Layout(width='15%', height='25px'))
    d61 = widgets.Dropdown(options=list(range(0, 51)),
                           value=8,
                           description='size_y2:',
                           disabled=False,
                           layout=Layout(width='15%', height='25px'))
    g6 = widgets.Dropdown(options=list(range(0, 51)),
                          value=8,
                          description='bartxt1:',
                          disabled=False,
                          layout=Layout(width='15%', height='25px'))
    g61 = widgets.Dropdown(options=list(range(0, 51)),
                           value=8,
                           description='bartxt2:',
                           disabled=False,
                           layout=Layout(width='15%', height='25px'))

    xxx = Button(layout=Layout(width='5%', height='25px'), disabled=True)
    xxx.style.button_color = 'white'
    yyy = Button(layout=Layout(width='94%', height='5px'), disabled=True)
    yyy.style.button_color = 'red'

    ww = widgets.HBox([color_a6, xxx, z6])
    w6 = widgets.HBox([
        a6,
        b6,
        o6,
        d6,
        g6,
        pos_sub1,
    ])
    w7 = widgets.HBox([
        a61,
        c6,
        o61,
        d61,
        g61,
        pos_sub2,
    ])
    w8 = widgets.VBox([w6, w7, yyy])

    ######

    def col(color_a6):
        tax_colors(color_list=circle_colors[color_a6], taxx=dfUniprot)

    out7 = widgets.interactive_output(col, {'color_a6': color_a6})

    def box1(a6, a61, pos_sub1, pos_sub2, b6, c6, z6, o6, o61, d6, d61, g6,
             g61):
        yetiquetas_plot1 = {
            'Lineage*': 'Phylum',
            'Phylum': 'Phylum',
            'Class': 'Class',
            'Order': 'Order',
            'Genus': 'Genus',
            'Species': 'Species',
            'Gradient1*': 'Phylum',
            'Gradient2*': 'Phylum',
            'Attenuate*': 'Phylum',
            'None*': 'Phylum'
        }
        plots1 = {
            'Lineage*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'),
            'Phylum': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'),
            'Class': pd.read_csv('tax/class11.tsv', sep='\t').fillna('NA'),
            'Order': pd.read_csv('tax/order11.tsv', sep='\t').fillna('NA'),
            'Genus': pd.read_csv('tax/genus11.tsv', sep='\t').fillna('NA'),
            'Species': pd.read_csv('tax/org11.tsv', sep='\t').fillna('NA'),
            'Gradient1*': pd.read_csv('tax/phylum0.tsv',
                                      sep='\t').fillna('NA'),
            'Gradient2*': pd.read_csv('tax/phylum0.tsv',
                                      sep='\t').fillna('NA'),
            'Attenuate*': pd.read_csv('tax/phylum0.tsv',
                                      sep='\t').fillna('NA'),
            'None*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA')
        }
        plots2 = {
            'Phylum': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'),
            'Class': pd.read_csv('tax/class11.tsv', sep='\t').fillna('NA'),
            'Order': pd.read_csv('tax/order11.tsv', sep='\t').fillna('NA'),
            'Genus': pd.read_csv('tax/genus11.tsv', sep='\t').fillna('NA'),
            'Species': pd.read_csv('tax/org11.tsv', sep='\t').fillna('NA')
        }
        ax3 = plt.axes([pos_sub2, .97, .3, 0.55])
        ##>>>>>>>>>>> grafico circular
        ax = plt.axes([0, 1, 0.9, 1])
        circle_lineage(alphas=alfas[a6])
        ##>>>>>>>>>>> grafico 1
        #ax2 = plt.axes([pos_sub1, 1.51, .3, 0.55])
        ax2 = plt.axes([pos_sub1, 1.63, .3, 0.4])  #>>>>>>>>>>

        barras_tax(
            plots1[a6],
            #barras_tax(tax_colors(color_list = circle_colors['Spectral'])[0],
            row_num=b6,
            color=plots1[a6].iloc[0:b6, 2],
            sep=o6,
            size_y=d6,
            size_bartxt=g6,
            ylabel_text=yetiquetas_plot1[a6],
            ylabel=10)

        ##>>>>>>>>>>> grafico 2
        ax3 = plt.axes([pos_sub2, .97, .3, 0.55])

        barras_tax(
            plots2[a61],
            #barras_tax(tax_colors(color_list = circle_colors['Spectral'])[0],
            row_num=c6,
            color=plots2[a61].iloc[0:b6, 2],
            sep=o61,
            size_y=d61,
            size_bartxt=g61,
            ylabel_text=yetiquetas_plot1[a61],
            ylabel=10)

        ##>>>>>>>>>>>> save
        if z6 == True:
            import datetime
            plt.savefig('img/Lineage' +
                        datetime.datetime.now().strftime('%d.%B.%Y_%I-%M%p') +
                        '.png',
                        dpi=900,
                        bbox_inches='tight')
        else:
            pass

    out6 = widgets.interactive_output(
        box1, {
            'a6': a6,
            'a61': a61,
            'pos_sub1': pos_sub1,
            'pos_sub2': pos_sub2,
            'b6': b6,
            'c6': c6,
            'z6': z6,
            'o6': o6,
            'o61': o61,
            'd6': d6,
            'd61': d61,
            'g6': g6,
            'g61': g61
        })
    import warnings
    warnings.filterwarnings("ignore")
    return display(VBox([yyy, ww, w8, out6]))
Example #16
0
K_005.temps.sort(reverse = True)
K_005.temps = K_005.temps[:-1]
K_005.frac = K_005.frozen_frac(K_005.temps)
K_005.j = K_005.J()
K_005.n = K_005.N_s()
K_005.frac_error()
K_005.jErr = K_005.JError()
K_005.nErr = K_005.NError()

G_1 = c.nucleation_curve("peak_data_glassy_1%.csv", 1800, 0.22, 1, label="Glassy K-Feldspar 1%wt")
G_01 = c.nucleation_curve("peak_data_glassy_0.1%.csv", 1800, 0.22, 0.1, label="Glassy K-Feldspar 0.1%wt")

cryst = (K_01, K_005, K_0025, K_00125)
glass = (G_1, G_01)

cmapCryst = cm.tab20b(np.linspace(0,0.19,4))#4 shades of blue
cmapGlass = cm.tab20b(np.linspace(0.2,0.4,3))#3 shades of red

symCryst = ["v", "^", ">", "<"]
symGlass = ["+", "x"]

fig, (ax1, ax2) = plt.subplots(2, sharex=True)
fig.subplots_adjust(hspace=0)

for i,val in enumerate(cryst):
    ax1.plot(val.frac[:,0], val.frac[:,1], c=cmapCryst[i],label=val.label)
    ax2.plot(val.frac[:,0], val.frac[:,1], c=cmapCryst[i])
    ax1.plot(val.sigFit[:,0], val.sigFit[:,1], c=cmapCryst[i],alpha=0.5,
             linestyle='--')
    x,y = val.frac[:,0],val.frac[:,1]
    x = x[::-1]#x must be monotonic increasing to spline
    rerr = []

    # Deleting collections where the pattern is not found by all sensors:
    for collection_key, collection in test_dataset['collections'].items():
        for sensor_key, sensor in test_dataset['sensors'].items():
            if not collection['labels'][sensor_key]['detected'] and (
                    sensor_key == source_sensor
                    or sensor_key == target_sensor):
                print(Fore.RED + "Removing collection " + collection_key +
                      ' -> pattern was not found in sensor ' + sensor_key +
                      ' (must be found in all sensors).' + Style.RESET_ALL)
                del test_dataset['collections'][collection_key]
                break

    # Reprojection error graphics definitions
    colors = cm.tab20b(
        np.linspace(0, 1, len(test_dataset['collections'].items())))

    od = OrderedDict(
        sorted(test_dataset['collections'].items(), key=lambda t: int(t[0])))
    for collection_key, collection in od.items():
        # Get pattern number of corners
        nx = test_dataset['calibration_config']['calibration_pattern'][
            'dimension']['x']
        ny = test_dataset['calibration_config']['calibration_pattern'][
            'dimension']['y']
        square = test_dataset['calibration_config']['calibration_pattern'][
            'size']

        # Get corners on both images
        corners_s = np.zeros(
            (len(collection['labels'][source_sensor]['idxs']), 2),
Example #18
0
def show_me_the_money(categories, amounts):
    colors = cm.tab20b(np.linspace(0, 1, len(categories)))
    plt.pie(amounts, labels=categories, colors=colors)
    plt.axis('equal')
    plt.show()
Example #19
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("error",
                        help="Error file",
                        metavar='error_file',
                        type=str)
    parser.add_argument("-t",
                        "--title",
                        help="Plot title",
                        default='Hand-Eye',
                        type=str)
    parser.add_argument("-s",
                        "--save",
                        help="Save plots to file",
                        dest='save',
                        action='store_true')
    args = vars(parser.parse_args())
    name = args['title'].replace(' ', '_').lower()

    data = load_data(args['error'])

    # snames = data.values()[0].keys()

    all, per_collection = get_projection_errors(data, 'errors')

    print("Errors for {} collections!".format(len(data)))

    calculate_errors(data)

    sensor_names = data.values()[0].keys()

    all, per_collection = get_projection_errors(data, 'errors')

    # print(np.mean(np.sqrt(all)))
    rmse = np.sqrt(np.mean(all))

    fig, axes = plt.subplots(2, 1, sharex=True, figsize=(10, 5))

    y = np.array(
        [[np.sqrt(np.mean(xx['error'])) for xx in x['sensors'].values()]
         for x in per_collection.values()])

    ret = axes[0].plot(y, '-o')
    axes[0].legend(ret, sensor_names, loc="upper right")
    axes[0].grid(True)
    axes[0].set_ylabel('RMSE')
    axes[0].set_title('RMSE per collection per sensor')

    y = [np.sqrt(np.mean(x['error'])) for x in per_collection.values()]
    axes[1].plot(y, '-o', label='combined')
    axes[1].legend(loc='upper right')
    axes[1].grid(True)
    axes[1].set_ylabel('RMSE')
    axes[1].set_xlabel('# Collection')

    axes[1].set_title('RMSE per collection')

    plt.xticks(range(len(y)), per_collection.keys(), rotation=45)

    fig.tight_layout()

    st = fig.suptitle('{} ($RMSE = {}$)'.format(args['title'], rmse),
                      fontsize=16)
    st.set_y(0.98)
    fig.subplots_adjust(top=0.85)

    if args['save']:
        fig.savefig(name + '_rmse.png')

    plt.show()

    #=========================================

    colors = cm.tab20b(np.linspace(0, 1, len(per_collection)))
    fig, axes = plt.subplots(1, 2, figsize=(10, 5))

    y = np.array([[xx['yerr'] for xx in x['sensors'].values()]
                  for x in per_collection.values()])
    x = np.array([[xx['xerr'] for xx in x['sensors'].values()]
                  for x in per_collection.values()])

    if x.shape[1] > 1:
        xmean = np.mean(np.concatenate(x.ravel()))
        xstd = np.std(np.concatenate(x.ravel()))

        ymean = np.mean(np.concatenate(y.ravel()))
        ystd = np.std(np.concatenate(y.ravel()))
    else:
        xmean = np.mean(x.ravel())
        xstd = np.std(x.ravel())

        ymean = np.mean(y.ravel())
        ystd = np.std(y.ravel())

    dev = 4
    axes[1].set_title("Final")
    axes[1].grid(True)
    axes[1].set_xlim(xmean - dev * xstd, xmean + dev * xstd)
    axes[1].set_ylim(ymean - dev * ystd, ymean + dev * ystd)
    # axes[1].set_xscale('log')
    # axes[1].set_yscale('log')

    keys = per_collection.keys()
    for i in range(x.shape[0]):
        axes[1].plot(np.concatenate(x[i]),
                     np.concatenate(y[i]),
                     'o',
                     label=keys[i],
                     alpha=0.7,
                     color=colors[i])

    axes[1].set_xlabel('$x$ error (pixel)')
    axes[1].set_ylabel('$y$ error (pixel)')

    # axes.set_aspect('equal', 'box')

    axes[1].legend(ncol=2, fontsize='xx-small', title='Collections')

    all, per_collection = get_projection_errors(data, 'init_errors')

    y = np.array([[xx['yerr'] for xx in x['sensors'].values()]
                  for x in per_collection.values()])
    x = np.array([[xx['xerr'] for xx in x['sensors'].values()]
                  for x in per_collection.values()])

    if x.shape[1] > 1:
        xmean = np.mean(np.concatenate(x.ravel()))
        xstd = np.std(np.concatenate(x.ravel()))

        ymean = np.mean(np.concatenate(y.ravel()))
        ystd = np.std(np.concatenate(y.ravel()))
    else:
        xmean = np.mean(x.ravel())
        xstd = np.std(x.ravel())

        ymean = np.mean(y.ravel())
        ystd = np.std(y.ravel())

    axes[0].set_title("Initial")
    axes[0].grid(True)

    for i in range(x.shape[0]):
        axes[0].plot(np.concatenate(x[i]),
                     np.concatenate(y[i]),
                     'o',
                     label=keys[i],
                     alpha=0.7,
                     color=colors[i])

    axes[0].set_xlabel('$x$ error (pixel)')
    axes[0].set_ylabel('$y$ error (pixel)')

    axes[0].set_xlim(xmean - dev * xstd, xmean + dev * xstd)
    axes[0].set_ylim(ymean - 2 * dev * ystd, ymean + 2 * dev * ystd)

    # axes.set_aspect('equal', 'box')

    axes[0].legend(ncol=2, fontsize='xx-small', title='Collections')

    fig.tight_layout()
    st = fig.suptitle('{} - Reprojection errors'.format(args['title']),
                      fontsize=16)
    st.set_y(0.98)
    fig.subplots_adjust(top=0.85)

    if args['save']:
        fig.savefig(name + '_proj.png')

    plt.show()