Esempio n. 1
0
 def _init_draw(self):
     if self.display:
         lines = [self.line_spike]
         for x in range(self.n_joints):
             lines.append(self.analog_lines[x])
         for l in lines:
             l.set_data([], [])
Esempio n. 2
0
    def plot_quantity_vs_index(
        self,
        ax: plt.Axes,
        a: u.Quantity,
        t: typ.Optional[astropy.time.Time] = None,
        a_name: str = '',
        # drawstyle: str = 'steps-mid',
    ) -> typ.Tuple[plt.Axes, typ.List[plt.Line2D]]:
        ax = plot.datetime_prep(ax)

        if t is None:
            t = self.time

        t_left = t - self.exposure_half_length
        t_right = t + self.exposure_half_length

        with astropy.visualization.quantity_support():
            lines = []
            for c in range(self.num_channels):
                time = astropy.time.Time(
                    np.stack([t_left[:, c], t_right[:, c]],
                             axis=~0).reshape(-1))
                q = np.stack([a[:, c], a[:, c]], axis=~0).reshape(-1)
                line, = ax.plot(
                    time.to_datetime(),
                    q,
                    label=a_name + ', ' + self.channel_labels[c],
                    # drawstyle=drawstyle,
                )
                lines.append(line)

        return ax, lines
Esempio n. 3
0
    def plot_wavelength(
        self,
        ax: matplotlib.axes.Axes,
        num_emission_lines: int = 10,
        digits_after_decimal: int = 3,
        colors: typ.Optional[typ.Sequence[str]] = None,
    ) -> typ.List[matplotlib.lines.Line2D]:
        if colors is None:
            colors = num_emission_lines * ['black']
        with astropy.visualization.quantity_support():
            wavelength = self.wavelength[:num_emission_lines]
            fullname = self.fullname(digits_after_decimal=digits_after_decimal,
                                     use_latex=True)[:num_emission_lines]
            # ax.set_ylabel('{0:latex_inline}'.format(intensity.unit))
            lines = []

            for i in range(wavelength.shape[0]):
                lines.append(
                    ax.axvline(
                        x=wavelength[i],
                        label=fullname[i],
                        linestyle='dashed',
                        color=colors[i],
                    ))

        return lines
Esempio n. 4
0
def make_lines(obstacle_vertices,connect=True):
    lines = []
    for i,vertex in enumerate(obstacle_vertices):
        if i < len(obstacle_vertices)-1:
            lines.append([obstacle_vertices[i],obstacle_vertices[i+1]])
        else:
            if connect:
                lines.append([obstacle_vertices[i],obstacle_vertices[0]])
    return lines
def marker_graph_add(ax: np.ndarray, marker_data: np.ndarray, x_data: np.ndarray, **kwargs) \
        -> List[matplotlib.lines.Line2D]:
    """Plot each column of marker_data ((n,3) numpy array) onto the axes provided in ax.

    **kwargs passed to matplotlib plot().
    """
    lines = []
    for n in range(3):
        current_line, = ax[n].plot(x_data, marker_data[:, n], **kwargs)
        lines.append(current_line)
    return lines
def kine_graph_add(ax: matplotlib.axes.Axes, marker_data: np.ndarray, x_data: np.ndarray, plot_args=None) \
        -> List[matplotlib.lines.Line2D]:
    """Plot each column of marker_data ((n,3) numpy array) onto the axes provided in ax.

    Additionally, visually format each Axes and include a y_label on 2nd Axes. **kwargs passed to matplotlib plot().
    """
    if plot_args is None:
        plot_args = [{}] * marker_data.shape[1]

    lines = []
    for dim in range(marker_data.shape[1]):
        current_line, = ax.plot(x_data, marker_data[:, dim], **plot_args[dim])
        lines.append(current_line)

    return lines
def create_lines(polygons):
    """
    creates all existing lines in each polygon. Lines within polygon are also created to distinguish
    possible paths that lie on the edge of the polygon vs illegal paths through the polygon
    :param polygons:
    :return:
    """
    lines = []
    for polygon in polygons:
        curr_lines = []
        for idx in range(0, len(polygon)):
            for idx_ in range(idx, len(polygon)):
                curr_line = Line(polygon[idx], polygon[idx_])
                curr_lines.append(curr_line)
        lines.append(curr_lines)
    return lines
def cov_trend_graph_add(ax: np.ndarray, variance_data: Any, x_data: np.ndarray, process_func: Callable, **kwargs) \
        -> List[List[matplotlib.lines.Line2D]]:
    """Plot each kinematic variable/dimension combination contained in variance_data (tuple of 3 kinematic variables,
    each comprised of a (n, 3) numpy array) onto the 3 rows (kinematic variable) and 3 columns (dimension) contained in
    ax.

    Apply process_func to each kinematic variable/dimension before plotting. **kwargs passed to matplotlib plot().
    """

    lines = []
    # iterate over kinematic variable
    for i in range(3):
        # iterate over dimension
        dim_lines = []
        for j in range(3):
            line, = ax[i, j].plot(x_data, process_func(variance_data[i][:, j]),
                                  **kwargs)
            dim_lines.append(line)
        lines.append(dim_lines)
    return lines
Esempio n. 9
0
def lorentz(field):
    '''Calculate Lorentz Force from E and B field'''
    print("Calculating LORENTZ-condition...")
    q = 1
    c = 299792458
    #         0                1         2         3                                                      7             8             9             10              11
    lines = [[
        "id", r"s / m", r"x / m", r"y / m", r"z / m", r"$x'$ / rad",
        r"$y'$ / rad", r"$z'$ / rad", r"$B_x$ / T", r"$B_y$ / T", r"$B_z$ / T",
        r"$E_x$ / V/m", r"$E_y$ / V/m", r"$E_z$ / V/m", r"$F_{Bx}$ / eV/m",
        r"$F_{By}$ / eV/m", r"$F_{Bz}$ / eV/m", r"$F_{Ex}$ / eV/m",
        r"$F_{Ey}$ / eV/m", r"$F_{Ez}$ / eV/m", r"$F_x$ / eV/m",
        r"$F_y$ / eV/m", r"$F_z$ / eV/m", r"|B| / T", r"|E| / V/m",
        r"$\sqrt{B_y^2+B_z^2}$ / T", r"$\sqrt{E_x^2+E_z^2}$ / V/m"
    ]]
    for f in tqdm(field):
        s = f[13] * c * math.sqrt(f[4]**2 + f[5]**2 + f[6]**2)
        xprime = f[4] / math.sqrt(f[4]**2 + f[5]**2 + f[6]**2)
        yprime = f[5] / math.sqrt(f[4]**2 + f[5]**2 + f[6]**2)
        zprime = f[6] / math.sqrt(f[4]**2 + f[5]**2 + f[6]**2)
        FBx = q * c * (f[5] * f[9] - f[6] * f[8])
        FBy = q * c * (f[6] * f[7] - f[4] * f[9])
        FBz = q * c * (f[4] * f[8] - f[5] * f[7])
        FEx = q * f[10]
        FEy = q * f[11]
        FEz = q * f[12]
        Fx = FEx + FBx
        Fy = FEy + FBy
        Fz = FEz + FBz
        absB = math.sqrt(f[7]**2 + f[8]**2 + f[9]**2)
        absE = math.sqrt(f[10]**2 + f[11]**2 + f[12]**2)
        unwantedB = math.sqrt(f[8]**2 + f[9]**2)
        unwantedE = math.sqrt(f[10]**2 + f[12]**2)
        line = [f[0], s] + f[1:4] + [xprime, yprime, zprime] + f[7:13] + [
            FBx, FBy, FBz, FEx, FEy, FEz, Fx, Fy, Fz, absB, absE, unwantedB,
            unwantedE
        ]
        lines.append(line)
    return lines
def marker_graph_init(ax: np.ndarray, marker_data: np.ndarray, y_label: str, x_data: np.ndarray, **kwargs) \
        -> List[matplotlib.lines.Line2D]:
    """Plot each column of marker_data ((n,3) numpy array) onto the axes provided in ax.

    Additionally, visually format each Axes and include a y_label on 2nd Axes. **kwargs passed to matplotlib plot().
    """

    lines = []
    for n in range(3):
        current_line, = ax[n].plot(x_data, marker_data[:, n], **kwargs)
        lines.append(current_line)
        plot_utils.update_spines(ax[n])
        plot_utils.update_xticks(ax[n], font_size=8)
        plot_utils.update_yticks(ax[n], fontsize=8)
        ax[n].margins(x=0, y=0.05)
        ax[n].yaxis.set_major_locator(ticker.MaxNLocator(nbins=4,
                                                         integer=True))

        if n == 2:
            plot_utils.update_xlabel(ax[n], 'Frame Number', font_size=10)
        elif n == 1:
            plot_utils.update_ylabel(ax[n], y_label, font_size=10)
    return lines
def kine_graph_init(ax: matplotlib.axes.Axes, marker_data: np.ndarray, y_label: str, x_data: np.ndarray,
                    plot_args=None) -> List[matplotlib.lines.Line2D]:
    """Plot each column of marker_data ((n,3) numpy array) onto the axes provided in ax.

    Additionally, visually format each Axes and include a y_label on 2nd Axes. **kwargs passed to matplotlib plot().
    """
    if plot_args is None:
        plot_args = [{}] * marker_data.shape[1]

    lines = []
    for dim in range(marker_data.shape[1]):
        current_line, = ax.plot(x_data, marker_data[:, dim], **plot_args[dim])
        lines.append(current_line)

    plot_utils.update_spines(ax)
    plot_utils.update_xticks(ax, font_size=8)
    plot_utils.update_yticks(ax, fontsize=8)
    ax.margins(x=0, y=0.05)
    ax.yaxis.set_major_locator(ticker.MaxNLocator(nbins=4, integer=True))
    plot_utils.update_xlabel(ax, 'Frame Number', font_size=10)
    plot_utils.update_ylabel(ax, y_label, font_size=10)

    return lines
def cov_trend_graph_init(ax: np.ndarray, variance_data: Any,
                         x_data: np.ndarray, y_labels: Sequence[str],
                         process_func: Callable,
                         **kwargs) -> List[List[matplotlib.lines.Line2D]]:
    """Plot each kinematic variable/dimension combination contained in variance_data (tuple of 3 kinematic variables,
    each comprised of a (n, 3) numpy array) onto the 3 rows (kinematic variable) and 3 columns (dimension) contained in
    ax.

    Apply process_func to each kinematic variable/dimension before plotting. Additionally, visually format each axes
    and include a y_label for the first column of each row. **kwargs passed to matplotlib plot().
    """

    lines = []
    # iterate over kinematic variable
    for i in range(3):
        # iterate over dimension
        dim_lines = []
        for j in range(3):
            line, = ax[i, j].plot(x_data, process_func(variance_data[i][:, j]),
                                  **kwargs)
            dim_lines.append(line)
            plot_utils.update_spines(ax[i, j])
            plot_utils.update_xticks(ax[i, j], font_size=8)
            plot_utils.update_yticks(ax[i, j], fontsize=8)
            ax[i, j].margins(x=0, y=0.05)
            ax[i, j].yaxis.set_major_locator(
                ticker.MaxNLocator(nbins=4, integer=True))

            if i == 2 and j == 1:
                plot_utils.update_xlabel(ax[i, j],
                                         'Frame Number',
                                         font_size=10)

            if j == 0:
                plot_utils.update_ylabel(ax[i, j], y_labels[i], font_size=10)
        lines.append(dim_lines)
    return lines
Esempio n. 13
0
def readlines(file):
    '''Read in lines as list of strings'''
    print("Reading from %s ..." % file)
    f = open(file, 'r')
    lines = []
    caption = True  #False
    for line in f:
        if not line.split():  # Catch empty lines
            continue
        if line.split()[0] == "ID":  # Capture particle IDs
            id = int(float(line.split()[1]))
            continue
        if id in [10000]:  # last particle sometimes simulated wrong
            continue
        try:
            floats = [float(f) for f in line.split()]
            lines.append([id] + floats)
        except ValueError:  # Catch lines not containing floats (e.g. Captions)
            if caption != True:
                lines = [["id"] + line.split()] + lines
                caption = True
            continue
    f.close()
    return lines
Esempio n. 14
0
def autoconfig_processfile(name, pool_used, f_index, args):
    try:
        with open(
                name
        ) as result_file:  # No need to specify 'r': this is the default.as

            gates = args.config
            if (gates is not None):
                num_gates = len(gates)
            else:
                num_gates = 0

            nameParts = name.split("/")

            if len(nameParts) > 1:
                originalName = nameParts[len(nameParts) - 2]
            else:
                if args.name is not None:
                    originalName = args.name + str(f_index)
                else:
                    originalName = "NoNameSample" + str(f_index)
            print("Processing ", (originalName))

            colorlist = args.colorlist

            events = sum(1 for line in
                         result_file) - 1  #quickly determine number of events
            result_file.seek(0)  #rewind to the beginning of file
            header = result_file.readline()
            header = header.strip()
            headers = header.split(
                "\t")  #parse the headers from the first line of input
            num_markers = len(headers) - 2

            # create a numpy array for faster data access
            if args.debug: print("Assigning data to numpy matrix")
            fcm = loadNp(result_file, len(headers), events)

            # find the start of pop info on fcs_results_all
            if args.flocklegacy is False:
                pop_offset = 0
                for i, header in enumerate(headers):
                    if header == "pop1":
                        pop_offset = i - 1
                if args.debug: print("Pop offset: ", pop_offset)

            # parsing gates from configuration data
            if args.debug: print("Configuring gates from file")
            sub_results = []
            if pool_used == 0: inner_pool = Pool(processes=cores)
            for gate, config in gates.items():
                xmarker = str(headers[config[1] - 1])
                ymarker = str(headers[config[2] - 1])
                startx = int((float(config[3]) / 200) * 4096)
                starty = int((float(config[5]) / 200) * 4096)
                endx = int((float(config[4]) / 200) * 4096)
                endy = int((float(config[6]) / 200) * 4096)
                parent_gate = int(config[7])

                lines = []
                cluster_type = int(config[8])
                if cluster_type == 2:
                    print("slanted")
                else:
                    lines.append([(startx, endx), (starty, starty),
                                  colorlist[1]])
                    lines.append([(startx, startx), (starty, endy),
                                  colorlist[1]])
                    lines.append([(startx, endx), (endy, endy), colorlist[1]])
                    lines.append([(endx, endx), (starty, endy), colorlist[1]])

                key = xmarker + "_vs_" + ymarker

                dim1 = xmarker
                dim2 = ymarker
                print(dim1, dim2)
                dim1_idx = 0
                dim2_idx = 0

                for i, marker in enumerate(headers):
                    if marker == dim1:
                        dim1_idx = i
                        print(("Feature 1: ", marker, i + 1))
                    if marker == dim2:
                        dim2_idx = i
                        print(("Feature 2: ", marker, i + 1))

                header_names = key
                print(header_names)

                if args.flocklegacy is False:
                    poploc = int(config[0]) + pop_offset
                    parent_poploc = parent_gate + pop_offset
                    print("gate location: ", poploc)
                    print("parent gate location: ", parent_poploc)
                    if args.debug:
                        print(
                            "iterate through events to find population members"
                        )
                    fcm[:, -1] = 0
                    if args.showparent and (config[0] > 1):
                        fcm[:, -1] = (1 - fcm[:, poploc]) + (
                            1 - fcm[:, parent_poploc])
                    else:
                        fcm[:, -1] = (2 - 2 * fcm[:, poploc])

                if args.debug: print("sorting numpy array")
                if args.sort:
                    sfcm = fcm[np.argsort(
                        fcm[:, -1]
                    )]  #sort the data set based on population number
                else:
                    sfcm = fcm

                if args.reversesort:
                    sfcm = sfcm[::-1]

                #print sfcm[0, :]
                if args.debug: print("creating color array")
                cdata = []
                for a in sfcm[:, -1]:
                    cdata.append(colorlist[a])

                xdata = sfcm[:, dim1_idx]
                ydata = sfcm[:, dim2_idx]
                sample_name = originalName

                poplist = []
                if args.flocklegacy is False:
                    pop_name = gate
                    parent_pop_name = "pop" + str(parent_gate)
                    if args.showparent: poplist.append(parent_pop_name)
                    poplist.append(pop_name)
                else:
                    pop_name = "FLOCK"

                png_file = sample_name + "_" + header_names + "_" + pop_name + ".png"

                if pool_used == 0:
                    inner_pool.apply_async(plotfig,
                                           args=[
                                               sample_name, xdata, ydata,
                                               cdata, xmarker, ymarker,
                                               poplist, lines, args
                                           ])
                else:
                    plotfig(sample_name, xdata, ydata, cdata, xmarker, ymarker,
                            poplist, lines, args)
                if args.flocklegacy is False:
                    sub_results.append([pop_name, [sample_name, png_file]])
                else:
                    sub_results.append(
                        [pop_name + str(gate), [sample_name, png_file]])
            if pool_used == 0:
                inner_pool.close()
                inner_pool.join()
            return sub_results
    except IOError as exc:
        if exc.errno != errno.EISDIR:  # Do not fail if a directory is found, just ignore it.
            raise  # Propagate other kinds of IOError.args
    except:
        #print >> sys.stderr, "Exception: %s" % str(e)
        raise Exception("".join(traceback.format_exception(*sys.exc_info())))
Esempio n. 15
0
def recall(model, class_names):
    class_dict = {}
    label_dict = ['background']
    if args.label:
        label_file = open(args.label)
        label_lines = label_file.readlines()
        label_id = 1
        for label_line in label_lines:
            label_line = label_line.replace('\n', '')
            class_dict[label_line] = label_id
            label_dict.append(label_line)
            label_id = label_id + 1

    # Validation dataset
    dataset_val = MyDataset()
    dataset_val.load_my(args.dataset, "val", class_dict)
    dataset_val.prepare()

    pre_correct_dict = {}
    pre_total_dict = {}
    pre_iou_dict = {}
    pre_scores_dict = {}
    gt_total_dict = {}
    for i in range(1, len(class_dict) + 1):
        pre_correct_dict[i] = 0
        pre_total_dict[i] = 0
        pre_iou_dict[i] = 0.0
        pre_scores_dict[i] = 0.0
        gt_total_dict[i] = 0

    backbone_shapes = modellib.compute_backbone_shapes(config, [768, 1280, 3])
    anchor_boxes = utils.generate_pyramid_anchors(config.RPN_ANCHOR_SCALES,
                                                  config.RPN_ANCHOR_RATIOS,
                                                  backbone_shapes,
                                                  config.BACKBONE_STRIDES,
                                                  config.RPN_ANCHOR_STRIDE)
    #utils.generate_anchors(300, config.RPN_ANCHOR_RATIOS, [40,40], 32, config.RPN_ANCHOR_STRIDE)
    #print(anchor_boxes)

    rois = []
    obj_groups = []
    # {image_file, [gt_class_id], [gt_box, (y1,x1,y2,x2)], [gt_bbox_area], [gt_wh_ratio], [gt_mask_area], [gt_mask_ratio], [gt_size], }
    for image_id in dataset_val.image_ids:
        image, image_meta, gt_class_id, gt_box, gt_mask = modellib.load_image_gt(
            dataset_val, config, image_id, use_mini_mask=False)
        #print(image.shape)
        gt_detects = {}
        gt_detects['image'] = dataset_val.image_reference(image_id)
        gt_detects['gt_class_id'] = gt_class_id
        gt_detects['gt_bbox'] = gt_box
        gt_detects['gt_bbox_area'] = []
        gt_detects['gt_wh_ratio'] = []
        gt_detects['gt_mask_area'] = []
        gt_detects['gt_mask_ratio'] = []
        gt_detects['gt_size'] = []
        for i in range(0, len(gt_class_id)):
            gt_total_dict[gt_class_id[i]] = gt_total_dict[gt_class_id[i]] + 1

            wh_ratio, box_size, box_area, square_box = toSquareBox(gt_box[i])
            mask_area = np.sum(gt_mask[:, :, i] == True)
            mask_ratio = mask_area / box_area
            gt_detects['gt_bbox_area'].append(box_area)
            gt_detects['gt_wh_ratio'].append(wh_ratio)
            gt_detects['gt_mask_area'].append(mask_area)
            gt_detects['gt_mask_ratio'].append(mask_ratio)
            gt_detects['gt_size'].append(box_size)

        molded_image = modellib.mold_image(image, config)
        #print(molded_image.shape)
        # Anchors
        """
        anchors = model.get_anchors(molded_image.shape)
        # Duplicate across the batch dimension because Keras requires it
        # TODO: can this be optimized to avoid duplicating the anchors?
        anchors = np.broadcast_to(anchors, (config.BATCH_SIZE,) + anchors.shape)
        print(anchors)
        # Run object detection
        detections, mrcnn_class, mrcnn_bbox, mrcnn_mask, rpn_rois, rpn_class, rpn_bbox =\
            model.keras_model.predict([np.expand_dims(molded_image, 0), np.expand_dims(image_meta, 0), anchors], verbose=0)
        print(detections[0])
        print(mrcnn_class[0])
        print(rpn_class[0])
        """
        #skimage.io.imsave("test.jpg", image)
        start_time = time.time()
        results = model.detect_molded(np.expand_dims(molded_image, 0),
                                      np.expand_dims(image_meta, 0),
                                      verbose=0)
        end_time = time.time()
        #print("Time: %s" % str(end_time - start_time))
        #print(results)
        r = results[0]
        pre_class_ids = r['class_ids']
        for i in range(0, len(pre_class_ids)):
            pre_total_dict[
                pre_class_ids[i]] = pre_total_dict[pre_class_ids[i]] + 1
        pre_scores = r['scores']
        #print(r['rois'])
        for roi in r['rois']:
            whr, bsize, _, _ = toSquareBox(roi)
            rois.append([bsize, whr])
            #print(gt_detects['gt_size'])
            #overlaps = utils.compute_iou(roi, gt_detects['gt_bbox'], roi_area, gt_detects['gt_bbox_area'])
            #print(overlaps)
        gt_match, pred_match, overlap = display_differences(
            image,
            gt_box,
            gt_class_id,
            gt_mask,
            r['rois'],
            pre_class_ids,
            pre_scores,
            r['masks'],
            class_names,
            title="",
            ax=None,
            show_mask=True,
            show_box=True,
            iou_threshold=0.1,
            score_threshold=0.1)
        gt_detects['rois'] = r['rois']
        gt_detects['gt_match'] = gt_match
        gt_detects['pred_match'] = pred_match
        #print(gt_match)
        """
        visualize.display_differences(image,
                        gt_box, gt_class_id, gt_mask,
                        r['rois'], pre_class_ids, pre_scores, r['masks'],
                        class_names, title="", ax=None,
                        show_mask=True, show_box=True,
                        iou_threshold=0.1, score_threshold=0.1)
        """
        for i in range(0, len(pred_match)):
            if pred_match[i] > -1.0:
                #print(r['rois'][i])
                pre_correct_dict[
                    pre_class_ids[i]] = pre_correct_dict[pre_class_ids[i]] + 1
                pre_iou_dict[pre_class_ids[i]] = pre_iou_dict[
                    pre_class_ids[i]] + overlap[i, int(pred_match[i])]
                pre_scores_dict[pre_class_ids[i]] = pre_scores_dict[
                    pre_class_ids[i]] + pre_scores[i]
        obj_groups.append(gt_detects)
    #print(rois)

    print("图片,类别,标注框,标注宽高比,标注尺寸,检测框,检测宽高比,检测尺寸,最大IOU")
    for det in obj_groups:
        for i in range(0, len(det['gt_class_id'])):
            overlaped = utils.compute_overlaps(
                anchor_boxes, np.reshape(det['gt_bbox'][i], (1, 4)))
            omax = max(overlaped)
            #if det['gt_size'][i] > 150 and det['gt_size'][i] < 367:
            if omax[0] > 0.0:
                print(det['image'], end='')
                print(",",
                      label_dict[det['gt_class_id'][i]],
                      ",",
                      det['gt_bbox'][i],
                      ",",
                      det['gt_wh_ratio'][i],
                      ",",
                      det['gt_size'][i],
                      end="")
                if det['gt_match'][i] > -1.0:
                    idx = int(det['gt_match'][i])
                    #print(idx, det['rois'])
                    whr, bsize, _, _ = toSquareBox(det['rois'][idx])
                    print(",", det['rois'][idx], ",", whr, ",", bsize, ",",
                          omax[0])
                else:
                    print(",", 0, ",", 0, ",", 0, ",", omax[0])

    tol_pre_correct_dict = 0
    tol_pre_total_dict = 0
    tol_pre_iou_dict = 0
    tol_pre_scores_dict = 0
    tol_gt_total_dict = 0

    lines = []
    tile_line = 'Type,Number,Correct,Proposals,Total,Rps/img,Avg IOU,Avg score,Recall,Precision\n'
    lines.append(tile_line)
    for key in class_dict:
        tol_pre_correct_dict = tol_pre_correct_dict + pre_correct_dict[
            class_dict[key]]
        tol_pre_total_dict = pre_total_dict[
            class_dict[key]] + tol_pre_total_dict
        tol_pre_iou_dict = pre_iou_dict[class_dict[key]] + tol_pre_iou_dict
        tol_pre_scores_dict = pre_scores_dict[
            class_dict[key]] + tol_pre_scores_dict
        tol_gt_total_dict = gt_total_dict[class_dict[key]] + tol_gt_total_dict

        type_rps_img = pre_total_dict[class_dict[key]] / len(
            dataset_val.image_ids)
        if pre_correct_dict[class_dict[key]] > 0:
            type_avg_iou = pre_iou_dict[class_dict[key]] / pre_correct_dict[
                class_dict[key]]
            type_avg_score = pre_scores_dict[
                class_dict[key]] / pre_correct_dict[class_dict[key]]
        else:
            type_avg_iou = 0
            type_avg_score = 0

        if gt_total_dict[class_dict[key]] > 0:
            type_recall = pre_total_dict[class_dict[key]] / gt_total_dict[
                class_dict[key]]
        else:
            type_recall = 0

        if pre_total_dict[class_dict[key]] > 0:
            type_precision = pre_correct_dict[
                class_dict[key]] / pre_total_dict[class_dict[key]]
        else:
            type_precision = 0
        line = '{:s},{:d},{:d},{:d},{:d},{:.2f},{:.2f}%,{:.2f},{:.2f}%,{:.2f}%\n'.format(
            key, len(dataset_val.image_ids), pre_correct_dict[class_dict[key]],
            pre_total_dict[class_dict[key]], gt_total_dict[class_dict[key]],
            type_rps_img, type_avg_iou * 100, type_avg_score,
            type_recall * 100, type_precision * 100)
        lines.append(line)
        print(line)

    tol_rps_img = tol_pre_total_dict / len(dataset_val.image_ids)
    if tol_pre_correct_dict > 0:
        tol_avg_iou = tol_pre_iou_dict / tol_pre_correct_dict
        tol_avg_score = tol_pre_scores_dict / tol_pre_correct_dict
    else:
        tol_avg_iou = 0
        tol_avg_score = 0

    if tol_gt_total_dict > 0:
        tol_recall = tol_pre_total_dict / tol_gt_total_dict
    else:
        tol_recall = 0

    if tol_pre_total_dict > 0:
        tol_precision = tol_pre_correct_dict / tol_pre_total_dict
    else:
        tol_precision = 0

    totle_line = '{:s},{:d},{:d},{:d},{:d},{:.2f},{:.2f}%,{:.2f},{:.2f}%,{:.2f}%\n'.format(
        'Total', len(dataset_val.image_ids), tol_pre_correct_dict,
        tol_pre_total_dict, tol_gt_total_dict, type_rps_img, tol_avg_iou * 100,
        tol_avg_score, tol_recall * 100, tol_precision * 100)
    print(totle_line)
    lines.append(totle_line)

    result_file_name = "result_{:%Y%m%dT%H%M%S}.csv".format(datetime.now())
    result_file = open(result_file_name, 'w+')
    result_file.writelines(lines)
    result_file.close()
              bbox_transform=plt.gcf().transFigure)

fig.suptitle('CTRNN trace comparison between evolved and target networks',
             fontsize=20)
fig.tight_layout()
plt.subplots_adjust(top=0.85, bottom=0.1)
plt.show()

# And here are the network's responses to a $sin(t/25)$ wave:

# In[114]:

fig, ax = plt.subplots(figsize=(9, 6))

lines = []
lines.append(
    ax.plot(fixedStd['0:200'][0]['neur1'], linewidth=4, label='target')[0])

for key in sortedKeys:
    lines.append(
        ax.plot(ctrnnStd[key][0]['neur1'],
                linewidth=3,
                linestyle='--',
                label=key)[0])

plt.ylabel('Output')
plt.xlabel('Time')

inputAx = ax.twinx()
line5 = inputAx.plot(fixedStd['0:200'][0]['input0'],
                     color='gray',
                     linestyle=':',
Esempio n. 17
0
def plot_angles(angs,
                hist,
                mapargs,
                color_map='cool',
                area_mult=1.0,
                alpha=0.9,
                hide_zero_marker=False,
                use_scale=False,
                label_view=[],
                **extra):
    ''' Plot the angular histogram using a map projection from basemap
    
    .. note::
         
        Basemap uses longitude latitude conventions, but the given angles are in
        colatitude, longitude convention.
    
    :Parameters:
        
        angs : array
               Array of view angles
        cnt : array
              Histogram for each view angle 
        mapargs : dict
                  Arguments specific to a map projection in basemap
        color_map : str
                    Name of color map
        area_mult : float
                    Scaling factor for size display
        alpha : float
                Transparency factor
        hide_zero_marker : bool
                           If true, hide the zero projection count marker
        use_scale : bool
                    If true, then display scale for size and color
        label_view : list
                     Label each view with text
        extra : dict
                Unused keyword arguments 
    '''

    cmap = getattr(cm, color_map)
    m = basemap.Basemap(**mapargs)

    # Y -> latitude
    # Z -> longitude

    #longitude, latitude = 90-colatitude
    x, y = m(angs[:, 1], 90.0 - angs[:, 0])
    sel = hist < 1
    hist = hist.astype(numpy.float)
    s = numpy.sqrt(hist) * area_mult
    nhist = hist.copy()
    nhist -= nhist.min()
    nhist /= nhist.max()
    m.drawparallels(numpy.arange(-90., 120., 30.))
    m.drawmeridians(numpy.arange(0., 420., 60.))
    im = m.scatter(x,
                   y,
                   s=s,
                   marker="o",
                   c=cmap(nhist),
                   alpha=alpha,
                   edgecolors='none')

    font_tiny = matplotlib.font_manager.FontProperties()
    font_tiny.set_size('xx-small')

    if len(label_view) > 0:
        for i in label_view:
            if i > len(angs):
                _logger.warn(
                    "Cannot label view: %d when there are only %d views (skipping)"
                    % (i, len(angs)))
                continue
            ytext = -15 if i % 2 == 0 else 15
            pylab.annotate('%d: %.1f,%.1f' % (i + 1, angs[i, 0], angs[i, 1]),
                           xy=(x[i], y[i]),
                           xycoords='data',
                           xytext=(-15, ytext),
                           textcoords='offset points',
                           arrowprops=dict(arrowstyle="->"),
                           fontproperties=font_tiny)

    if numpy.sum(sel) > 0 and not hide_zero_marker:
        im = m.scatter(x[sel],
                       y[sel],
                       numpy.max(s),
                       marker="x",
                       c=cm.gray(0.5))  #@UndefinedVariable

    if not use_scale:
        im = matplotlib.cm.ScalarMappable(cmap=cmap,
                                          norm=matplotlib.colors.Normalize())
        im.set_array(hist)
        m.colorbar(im, "right", size="3%", pad='1%')
    else:
        fontP = matplotlib.font_manager.FontProperties()
        fontP.set_size('small')
        inc = len(hist) / 10
        lines = []
        labels = []
        idx = numpy.argsort(hist)[::-1]
        for j in xrange(0, len(hist), inc):
            i = idx[j]
            labels.append("%s" % hist[i])
            lines.append(
                matplotlib.lines.Line2D(range(1),
                                        range(1),
                                        color=cmap(nhist[i]),
                                        marker='o',
                                        markersize=s[i] / 2,
                                        linestyle='none',
                                        markeredgecolor='white'))
        if numpy.sum(sel) > 0 and not hide_zero_marker:
            i = idx[len(idx) - 1]
            labels.append("%s" % hist[i])
            lines.append(
                matplotlib.lines.Line2D(range(1),
                                        range(1),
                                        color=cm.gray(0.5),
                                        marker='x',
                                        markersize=numpy.max(s) / 5,
                                        linestyle='none'))  #@UndefinedVariable
        pylab.legend(tuple(lines),
                     tuple(labels),
                     numpoints=1,
                     frameon=False,
                     loc='center left',
                     bbox_to_anchor=(1, 0.5),
                     prop=fontP)
Esempio n. 18
0
def main(label):
    num_shells_range = {
        6: [3.5, 14.5],
        12: [4.5, 16.5],
        20: [6.5, 16.5],
        30: [9.5, 16.5],
        42: [11.5, 20.5],
        56: [14.5, 20.5],
    }
    num_particles_ticks = [6, 30, 56]
    LEGEND_FACET_IDX = 2

    if label == "ground":
        freq_ticks = [0.28, 1.0]
        methods = ["mp2", "imsrg", "ccsd", "fci"]
        fn = "../Manuscript/fig-gs2.pdf"
    else:
        freq_ticks = [0.28, 1.0]        # 0.28 needed to show DMC results
        fn = "../Manuscript/fig-{}2.pdf".format(label)
        methods = ["imsrg+qdpt3", "imsrg+eom", "ccsd+eom"]

    d = utils.load_all()
    d_dmc = utils.load_all_dmc()
    d = utils.filter_preferred_ml(d)
    del d["ml"]
    d = d[d["interaction"] == "normal"]
    del d["interaction"]
    d = d[d["label"] == label]
    d_dmc = d_dmc[d_dmc["label"] == label]
    del d["label"]
    del d_dmc["label"]
    del d["num_filled"]
    d_dmc = d_dmc.set_index(["num_particles", "freq"])

    facet_x = {
        "col": "num_particles",
        "symbol": "N",
        "ticks": num_particles_ticks,
    }
    facet_y = {
        "col": "freq",
        "symbol": "\omega",
        "ticks": freq_ticks,
    }
    facet_to_num_particles_freq = lambda x, y: (x, y)

    has_dmc = False
    width = 6.5
    height = 3
    base_markersize = 5.0
    fig = plt.figure(figsize=(width, height))
    gs = matplotlib.gridspec.GridSpec(len(facet_y["ticks"]),
                                      len(facet_x["ticks"]))
    for (fct_x, fct_y), d in d.groupby([facet_x["col"], facet_y["col"]]):
        num_particles, freq = facet_to_num_particles_freq(fct_x, fct_y)
        try:
            gi = (facet_x["ticks"].index(fct_x) +
                  facet_y["ticks"].index(fct_y) * len(facet_x["ticks"]))
        except ValueError:
            continue
        if gi == LEGEND_FACET_IDX:
            continue
        ax = fig.add_subplot(gs[gi])

        try:
            sel_dmc = d_dmc.loc[(num_particles, freq)]
        except KeyError:
            pass
        else:
            has_dmc = True
            ax.axhline(sel_dmc["energy"],
                       color=utils.METHOD_COLOR["dmc"],
                       linestyle=utils.DMC_LINESTYLE)

        for method, d in d.groupby("method"):
            if method not in methods:
                continue

            d = d[(d["num_shells"] >= num_shells_range[num_particles][0]) &
                  (d["num_shells"] <= num_shells_range[num_particles][1])]
            d = d.sort_values(["num_shells"])
            marker = utils.METHOD_MARKER[method]
            ax.plot(d["num_shells"], d["energy"],
                    marker=marker,
                    markerfacecolor="none",
                    markersize=(utils.MARKERSIZE_CORRECTION.get(marker, 1.0) *
                                base_markersize),
                    color=utils.METHOD_COLOR[method])

        title = ("${} = {}$\n${} = {}$"
                 .format(facet_x["symbol"], fct_x,
                         facet_y["symbol"], fct_y))
        ax.text(1.0 - 2.65 / width, 0.6, title,
                color="#505050",
                horizontalalignment="left",
                transform=ax.transAxes,
                fontsize=12)
        ax.set_xlim(num_shells_range[num_particles])
    fig.text(0.5, 0.05 / height, "$K$ (number of shells)",
             horizontalalignment="center",
             verticalalignment="bottom",
             transform=ax.transAxes)
    fig.text(0.05 / width, 0.5, "${}$".format(utils.ENERGY_SYMBOL[label]),
             horizontalalignment="left",
             verticalalignment="center",
             transform=ax.transAxes,
             rotation="vertical")

    # phantom lines to configure the legend
    markersize = (utils.MARKERSIZE_CORRECTION.get(marker, 1.0) *
                  base_markersize)
    lines = [matplotlib.lines.Line2D([], [],
                                     marker=utils.METHOD_MARKER[method],
                                     markersize=markersize,
                                     color=utils.METHOD_COLOR[method],
                                     label=utils.METHOD_LABEL[method])
             for method in methods]
    if has_dmc:
        lines.append(matplotlib.lines.Line2D([], [],
                                             linestyle=utils.DMC_LINESTYLE,
                                             color=utils.METHOD_COLOR["dmc"],
                                             label=utils.METHOD_LABEL["dmc"]))
    ax = fig.add_subplot(gs[LEGEND_FACET_IDX])
    ax.axis("off")
    ax.legend(handles=lines, loc="center", frameon=False,
              bbox_to_anchor=(0.5, 0.5 - 0.1 / height))

    left_margin = 0.1
    right_margin = 0.0
    top_margin = 0.0
    bottom_margin = 0.1
    gs.tight_layout(fig, rect=[left_margin / width,
                               bottom_margin / height,
                               1.0 - right_margin / width,
                               1.0 - top_margin / height])
    utils.savefig(fig, fn=fn)
Esempio n. 19
0
def overlayFan(myData,myMap,myFig,param,coords='geo',gsct=0,site=None,\
                                fov=None,gs_flg=[],fill=True,velscl=1000.,dist=1000.,
                                cmap=None,norm=None,alpha=1):

    """A function of overlay radar scan data on a map

    **Args**:
        * **myData (:class:`pydarn.sdio.radDataTypes.scanData` or :class:`pydarn.sdio.radDataTypes.beamData` or list of :class:`pydarn.sdio.radDataTypes.beamData` objects)**: a radar beam object, a radar scanData object, or simply a list of radar beams
        * **myMap**: the map we are plotting on
        * **[param]**: the parameter we are plotting
        * **[coords]**: the coordinates we are plotting in
        * **[param]**: the parameter to be plotted, valid inputs are 'velocity', 'power', 'width', 'elevation', 'phi0'.  default = 'velocity
        * **[gsct]**: a flag indicating whether we are distinguishing ground scatter.  default = 0
        * **[intensities]**: a list of intensities (used for colorbar)
        * **[fov]**: a radar fov object
        * **[gs_flg]**: a list of gs flags, 1 per range gate
        * **[fill]**: a flag indicating whether to plot filled or point RB cells.  default = True
        * **[velscl]**: the velocity to use as baseline for velocity vector length, only applicable if fill = 0.  default = 1000
        * **[lines]**: an array to have the endpoints of velocity vectors.  only applicable if fill = 0.  default = []
        * **[dist]**: the length in map projection coords of a velscl length velocity vector.  default = 1000. km
    **OUTPUTS**:
        NONE

    **EXAMPLE**:
        ::
            
            overlayFan(aBeam,myMap,param,coords,gsct=gsct,site=sites[i],fov=fovs[i],\
                                                        verts=verts,intensities=intensities,gs_flg=gs_flg)

    Written by AJ 20121004
    """
    
    if(site == None):
        site = pydarn.radar.site(radId=myData[0].stid, dt=myData[0].time)
    if(fov == None):
        fov = pydarn.radar.radFov.fov(site=site,rsep=myData[0].prm.rsep,\
        ngates=myData[0].prm.nrang+1,nbeams= site.maxbeam,coords=coords) 
    
    if(isinstance(myData,pydarn.sdio.beamData)): myData = [myData]
    
    gs_flg,lines = [],[]
    if fill: verts,intensities = [],[]
    else: verts,intensities = [[],[]],[[],[]]
    
    #loop through gates with scatter
    for myBeam in myData:
        for k in range(0,len(myBeam.fit.slist)):
            if myBeam.fit.slist[k] not in fov.gates: continue
            r = myBeam.fit.slist[k]

            if fill:
                x1,y1 = myMap(fov.lonFull[myBeam.bmnum,r],fov.latFull[myBeam.bmnum,r])
                x2,y2 = myMap(fov.lonFull[myBeam.bmnum,r+1],fov.latFull[myBeam.bmnum,r+1])
                x3,y3 = myMap(fov.lonFull[myBeam.bmnum+1,r+1],fov.latFull[myBeam.bmnum+1,r+1])
                x4,y4 = myMap(fov.lonFull[myBeam.bmnum+1,r],fov.latFull[myBeam.bmnum+1,r])

                #save the polygon vertices
                verts.append(((x1,y1),(x2,y2),(x3,y3),(x4,y4),(x1,y1)))
                
                #save the param to use as a color scale
                if(param == 'velocity'): intensities.append(myBeam.fit.v[k])
                elif(param == 'power'): intensities.append(myBeam.fit.p_l[k])
                elif(param == 'width'): intensities.append(myBeam.fit.w_l[k])
                elif(param == 'elevation' and myBeam.prm.xcf): intensities.append(myBeam.fit.elv[k])
                elif(param == 'phi0' and myBeam.prm.xcf): intensities.append(myBeam.fit.phi0[k])
                
            else:
                x1,y1 = myMap(fov.lonCenter[myBeam.bmnum,r],fov.latCenter[myBeam.bmnum,r])
                verts[0].append(x1)
                verts[1].append(y1)
                
                x2,y2 = myMap(fov.lonCenter[myBeam.bmnum,r+1],fov.latCenter[myBeam.bmnum,r+1])
                
                theta = math.atan2(y2-y1,x2-x1)
                
                x2,y2 = x1+myBeam.fit.v[k]/velscl*(-1.0)*math.cos(theta)*dist,y1+myBeam.fit.v[k]/velscl*(-1.0)*math.sin(theta)*dist
                
                lines.append(((x1,y1),(x2,y2)))
                #save the param to use as a color scale
                if(param == 'velocity'): intensities[0].append(myBeam.fit.v[k])
                elif(param == 'power'): intensities[0].append(myBeam.fit.p_l[k])
                elif(param == 'width'): intensities[0].append(myBeam.fit.w_l[k])
                elif(param == 'elevation' and myBeam.prm.xcf): intensities[0].append(myBeam.fit.elv[k])
                elif(param == 'phi0' and myBeam.prm.xcf): intensities[0].append(myBeam.fit.phi0[k])
                
                if(myBeam.fit.p_l[k] > 0): intensities[1].append(myBeam.fit.p_l[k])
                else: intensities[1].append(0.)
            if(gsct): gs_flg.append(myBeam.fit.gflg[k])
            

    #do the actual overlay
    if(fill):
        #if we have data
        if(verts != []):
            if(gsct == 0):
                inx = numpy.arange(len(verts))
            else:
                inx = numpy.where(numpy.array(gs_flg)==0)
                x = PolyCollection(numpy.array(verts)[numpy.where(numpy.array(gs_flg)==1)],
                    facecolors='.3',linewidths=0,zorder=5,alpha=alpha)
                myFig.gca().add_collection(x, autolim=True)
                
            pcoll = PolyCollection(numpy.array(verts)[inx],
                edgecolors='face',linewidths=0,closed=False,zorder=4,
                alpha=alpha,cmap=cmap,norm=norm)
            #set color array to intensities
            pcoll.set_array(numpy.array(intensities)[inx])
            myFig.gca().add_collection(pcoll, autolim=True)
            return intensities,pcoll
    else:
        #if we have data
        if(verts != [[],[]]):
            if(gsct == 0):
                inx = numpy.arange(len(verts[0]))
            else:
                inx = numpy.where(numpy.array(gs_flg)==0)
                #plot the ground scatter as open circles
                x = myFig.scatter(numpy.array(verts[0])[numpy.where(numpy.array(gs_flg)==1)],\
                        numpy.array(verts[1])[numpy.where(numpy.array(gs_flg)==1)],\
                        s=.1*numpy.array(intensities[1])[numpy.where(numpy.array(gs_flg)==1)],\
                        zorder=5,marker='o',linewidths=.5,facecolors='w',edgecolors='k')
                myFig.gca().add_collection(x, autolim=True)
                
            #plot the i-s as filled circles
            ccoll = myFig.gca().scatter(numpy.array(verts[0])[inx],numpy.array(verts[1])[inx], \
                            s=.1*numpy.array(intensities[1])[inx],zorder=10,marker='o',linewidths=.5, \
                            edgecolors='face',cmap=cmap,norm=norm)
            
            #set color array to intensities
            ccoll.set_array(numpy.array(intensities[0])[inx])
            myFig.gca().add_collection(ccoll)
            #plot the velocity vectors
            lcoll = LineCollection(numpy.array(lines)[inx],linewidths=.5,zorder=12,cmap=cmap,norm=norm)
            lcoll.set_array(numpy.array(intensities[0])[inx])
            myFig.gca().add_collection(lcoll)

            return intensities,lcoll
for j in range(0,num_singlets):
    time[j] = frame_number * time_interval

    if not x.loc[(x['C1_T Value (frame)'] == (frame_number + 1)) & (x['C1_Image'] == singlets[j]), 'Corrected XY_Distance (um)'].size == 0:
        dist[j] = x.loc[(x['C1_T Value (frame)'] == (frame_number + 1)) & (x['C1_Image'] == singlets[j]), 'Corrected XY_Distance (um)']
    else:
        dist[j] = np.nan;
    
scat = ax.scatter([time], [dist], marker='o', c='#F4E85A');
x_line = np.full((num_singlets, num_frames), np.nan)
y_line = np.full((num_singlets, num_frames), np.nan)
lines = []
for j in range(0,num_singlets):
    lobj = ax.plot([], [], c='#F4EA9D', linewidth=0.75)[0];
    lines.append(lobj);


def update (frame_number):
    for j in range(0,num_singlets):
        time[j] = frame_number * time_interval
        if not x.loc[(x['C1_T Value (frame)'] == (frame_number + 1)) & (x['C1_Image'] == singlets[j]), 'Corrected XY_Distance (um)'].size == 0:
            dist[j] = x.loc[(x['C1_T Value (frame)'] == (frame_number + 1)) & (x['C1_Image'] == singlets[j]), 'Corrected XY_Distance (um)'];
        else:
            dist[j] = np.nan;


    if not dist.size == 0:
        data = np.stack(([time], [dist]), axis=-1)
        scat.set_offsets(data);
        x_line[:, frame_number] = time;
Esempio n. 21
0
def overlayFan(myData, myMap, myFig, param, coords='geo', gsct=0, site=None,
               fov=None, gs_flg=[], fill=True, velscl=1000., dist=1000.,
               cmap=None, norm=None, alpha=1):

    """A function of overlay radar scan data on a map

    Parameters
    ----------
    myData : pydarn.sdio.radDataTypes.scanData or
             pydarn.sdio.radDataTypes.beamData or
             list of pydarn.sdio.radDataTypes.beamData objects
        A radar beam object, a radar scanData object, or simply a list of
        radar beams
    myMap :
        The map we are plotting on
    myFig :
        Figure object that we are plotting to
    coords : Optional[str]
        The coordinates we are plotting in.  Default: geo
    param : Optional[str]
        The parameter to be plotted, valid inputs are 'velocity', 'power',
        'width', 'elevation', 'phi0'.  default = 'velocity
    gsct : Optional[boolean]
        A flag indicating whether we are distinguishing ground scatter.
        default = 0
    intensities : Optional[  ]
        A list of intensities (used for colorbar)
    fov : Optional[pydarn.radar.radFov.fov]
        A radar fov object
    gs_flg : Optional[  ]
        A list of gs flags, 1 per range gate
    fill : Optional[boolean]
        A flag indicating whether to plot filled or point RB cells.
        default = True
    velscl : Optional[float]
        The velocity to use as baseline for velocity vector length, only
        applicable if fill = 0.  default = 1000
    lines : Optional[  ]
        An array to have the endpoints of velocity vectors.  only applicable if
        fill = 0.  default = []
    dist : Optional [float]
        The length in map projection coords of a velscl length velocity vector.
        default = 1000. km

    Returns
    -------
    intensities

    pcoll

    lcoll


    Example
    -------
        overlayFan(aBeam,myMap,param,coords,gsct=gsct,site=sites[i],fov=fovs[i],
                   verts=verts,intensities=intensities,gs_flg=gs_flg)

    """
    from davitpy import pydarn

    if(isinstance(myData, pydarn.sdio.beamData)): myData = [myData]

    if(site is None):
        site = pydarn.radar.site(radId=myData[0].stid, dt=myData[0].time)
    if(fov is None):
        fov = pydarn.radar.radFov.fov(site=site, rsep=myData[0].prm.rsep,
                                      ngates=myData[0].prm.nrang + 1,
                                      nbeams=site.maxbeam, coords=coords,
                                      date_time=myData[0].time)

    gs_flg, lines = [], []
    if fill: verts, intensities = [], []
    else: verts, intensities = [[], []], [[], []]

    # loop through gates with scatter
    for myBeam in myData:
        for k in range(0, len(myBeam.fit.slist)):
            if myBeam.fit.slist[k] not in fov.gates: continue
            r = myBeam.fit.slist[k]

            if fill:
                x1, y1 = myMap(fov.lonFull[myBeam.bmnum, r],
                               fov.latFull[myBeam.bmnum, r])
                x2, y2 = myMap(fov.lonFull[myBeam.bmnum, r + 1],
                               fov.latFull[myBeam.bmnum, r + 1])
                x3, y3 = myMap(fov.lonFull[myBeam.bmnum + 1, r + 1],
                               fov.latFull[myBeam.bmnum + 1, r + 1])
                x4, y4 = myMap(fov.lonFull[myBeam.bmnum + 1, r],
                               fov.latFull[myBeam.bmnum + 1, r])

                # save the polygon vertices
                verts.append(((x1, y1), (x2, y2), (x3, y3), (x4, y4),
                              (x1, y1)))

                # save the param to use as a color scale
                if(param == 'velocity'):
                    intensities.append(myBeam.fit.v[k])
                elif(param == 'power'):
                    intensities.append(myBeam.fit.p_l[k])
                elif(param == 'width'):
                    intensities.append(myBeam.fit.w_l[k])
                elif(param == 'elevation' and myBeam.prm.xcf):
                    intensities.append(myBeam.fit.elv[k])
                elif(param == 'phi0' and myBeam.prm.xcf):
                    intensities.append(myBeam.fit.phi0[k])

            else:
                x1, y1 = myMap(fov.lonCenter[myBeam.bmnum, r],
                               fov.latCenter[myBeam.bmnum, r])
                verts[0].append(x1)
                verts[1].append(y1)

                x2, y2 = myMap(fov.lonCenter[myBeam.bmnum, r + 1],
                               fov.latCenter[myBeam.bmnum, r + 1])

                theta = math.atan2(y2 - y1, x2 - x1)

                x2, y2 = x1 + myBeam.fit.v[k] / velscl * (-1.0) * \
                    math.cos(theta) * dist, y1 + myBeam.fit.v[k] / velscl * \
                    (-1.0) * math.sin(theta) * dist

                lines.append(((x1, y1), (x2, y2)))
                # save the param to use as a color scale
                if(param == 'velocity'):
                    intensities[0].append(myBeam.fit.v[k])
                elif(param == 'power'):
                    intensities[0].append(myBeam.fit.p_l[k])
                elif(param == 'width'):
                    intensities[0].append(myBeam.fit.w_l[k])
                elif(param == 'elevation' and myBeam.prm.xcf):
                    intensities[0].append(myBeam.fit.elv[k])
                elif(param == 'phi0' and myBeam.prm.xcf):
                    intensities[0].append(myBeam.fit.phi0[k])

                if(myBeam.fit.p_l[k] > 0):
                    intensities[1].append(myBeam.fit.p_l[k])
                else:
                    intensities[1].append(0.)
            if(gsct):
                gs_flg.append(myBeam.fit.gflg[k])

    # do the actual overlay
    if(fill):
        # if we have data
        if(verts != []):
            if(gsct == 0):
                inx = numpy.arange(len(verts))
            else:
                inx = numpy.where(numpy.array(gs_flg) == 0)
                x = PolyCollection(numpy.array(verts)[numpy.where(
                                   numpy.array(gs_flg) == 1)], facecolors='.3',
                                   linewidths=0, zorder=5, alpha=alpha)
                myFig.gca().add_collection(x, autolim=True)

            pcoll = PolyCollection(numpy.array(verts)[inx],
                                   edgecolors='face', linewidths=0,
                                   closed=False, zorder=4, alpha=alpha,
                                   cmap=cmap, norm=norm)
            # set color array to intensities
            pcoll.set_array(numpy.array(intensities)[inx])
            myFig.gca().add_collection(pcoll, autolim=True)
            return intensities, pcoll
    else:
        # if we have data
        if(verts != [[], []]):
            if(gsct == 0):
                inx = numpy.arange(len(verts[0]))
            else:
                inx = numpy.where(numpy.array(gs_flg) == 0)
                # plot the ground scatter as open circles
                x = myFig.scatter(numpy.array(verts[0])[numpy.where(
                                  numpy.array(gs_flg) == 1)],
                                  numpy.array(verts[1])[numpy.where(
                                      numpy.array(gs_flg) == 1)],
                                  s=.1 * numpy.array(intensities[1])[
                                  numpy.where(numpy.array(gs_flg) == 1)],
                                  zorder=5, marker='o', linewidths=.5,
                                  facecolors='w', edgecolors='k')
                myFig.gca().add_collection(x, autolim=True)

            # plot the i-s as filled circles
            ccoll = myFig.gca().scatter(numpy.array(verts[0])[inx],
                                        numpy.array(verts[1])[inx],
                                        s=.1 * numpy.array(
                                        intensities[1])[inx], zorder=10,
                                        marker='o', linewidths=.5,
                                        edgecolors='face', cmap=cmap,
                                        norm=norm)

            # set color array to intensities
            ccoll.set_array(numpy.array(intensities[0])[inx])
            myFig.gca().add_collection(ccoll)
            # plot the velocity vectors
            lcoll = LineCollection(numpy.array(lines)[inx], linewidths=.5,
                                   zorder=12, cmap=cmap, norm=norm)
            lcoll.set_array(numpy.array(intensities[0])[inx])
            myFig.gca().add_collection(lcoll)

            return intensities, lcoll
Esempio n. 22
0
 def createLinesFor3D(self):
     lines = []
     depth_half = self.depthSpin.value() / 2
     x_base = self.dragLine.get_xdata()
     y_base = np.array([0, 0])
     z_base = self.dragLine.get_ydata()
     lines.append([x_base, y_base, z_base])
     lines.append([x_base, y_base - depth_half, z_base])
     lines.append([x_base, y_base + depth_half, z_base])
     lines.append([[x_base[0], x_base[0]],
                   y_base + np.array([-depth_half, depth_half]),
                   [z_base[0], z_base[0]]])
     lines.append([[x_base[1], x_base[1]],
                   y_base + np.array([-depth_half, depth_half]),
                   [z_base[1], z_base[1]]])
     lines.append([[x_base[1], x_base[1]],
                   y_base + np.array([-depth_half, depth_half]),
                   [z_base[1], z_base[1]]])
     return lines
Esempio n. 23
0
    async def graph(self, ctx):
        """
        Creates and sends line graph of data values
        @param ctx: Discord context
        @return: None
        note: note: matplotlib creates x axis if not enough/too little distance from start/end
        """
        dates = []  # dates
        url = []  # emoji URL
        lines = []  # graph lines
        img = []
        temp_db = {}
        legend_counter = 0
        MAX_LEGEND_COUNT = 10
        MAX_DATES = 5

        # last 3 dates
        for date in self.model.db[ctx.guild.id]:
            dates.append(date)
        dates = dates[-MAX_DATES:]

        # temp_db holds list of date & instance count keyed by emoji ID
        for date in dates:
            print(date)
            for emoji_id in self.model.db[ctx.guild.id][date]:
                # if emoji not yet processed
                if emoji_id not in temp_db.keys():
                    temp_db[emoji_id] = {'date': [], 'count': []}
                print(
                    emoji_id, ' - ',
                    self.model.db[ctx.guild.id][date][emoji_id].instance_count)

                temp_db[emoji_id]['count'].append(
                    self.model.db[ctx.guild.id][date][emoji_id].instance_count)
                temp_db[emoji_id]['date'].append(date)
                url.append(
                    str(self.model.db[ctx.guild.id][date]
                        [emoji_id].emoji_obj.url))
            print()
        print(temp_db)

        for emoji, db_content in temp_db.items():
            if legend_counter < MAX_LEGEND_COUNT:
                line, = plt.plot(db_content['date'],
                                 db_content['count'],
                                 marker='.',
                                 label=' - ' + ctx.bot.get_emoji(emoji).name)
            else:
                line, = plt.plot(db_content['date'],
                                 db_content['count'],
                                 marker='.')
            lines.append(line)
            legend_counter += 1

        # adding images in the legend
        for i in range(len(url)):
            async with aiohttp.ClientSession() as session:
                url_link = url[i]
                async with session.get(url_link) as resp:
                    if resp.status == 200:
                        f = await aiofiles.open('resources/emoji.png',
                                                mode='wb')
                        await f.write(await resp.read())
                        await f.close()
                        img.append(HandlerLineImage('resources/emoji.png'))
        legend_obj = dict(zip(lines, img))

        plt.legend(handler_map=legend_obj)  # legend

        plt.grid(True)  # grid lines
        plt.title("Time series of emote use in " + ctx.guild.name)  # title
        plt.xticks(ticks=dates)  # display only given dates

        fig = plt.gcf()
        fig.autofmt_xdate()  # might delete
        plt.show()
        plt.draw()
        fig.savefig('resources/graph.png', bbox_inches='tight')
        await ctx.send(file=discord.File('resources/graph.png'))
Esempio n. 24
0
def plot_angles(angs, hist, mapargs, color_map='cool', area_mult=1.0, alpha=0.9, hide_zero_marker=False, use_scale=False, label_view=[], **extra):
    ''' Plot the angular histogram using a map projection from basemap
    
    .. note::
         
        Basemap uses longitude latitude conventions, but the given angles are in
        colatitude, longitude convention.
    
    :Parameters:
        
        angs : array
               Array of view angles
        cnt : array
              Histogram for each view angle 
        mapargs : dict
                  Arguments specific to a map projection in basemap
        color_map : str
                    Name of color map
        area_mult : float
                    Scaling factor for size display
        alpha : float
                Transparency factor
        hide_zero_marker : bool
                           If true, hide the zero projection count marker
        use_scale : bool
                    If true, then display scale for size and color
        label_view : list
                     Label each view with text
        extra : dict
                Unused keyword arguments 
    '''
    
    cmap = getattr(cm, color_map)
    m = basemap.Basemap(**mapargs)
    
    # Y -> latitude
    # Z -> longitude
    
    #longitude, latitude = 90-colatitude
    x, y = m(angs[:, 1], 90.0-angs[:, 0])
    sel = hist < 1
    hist = hist.astype(numpy.float)
    s = numpy.sqrt(hist)*area_mult
    nhist = hist.copy()
    nhist-=nhist.min()
    nhist/=nhist.max()
    m.drawparallels(numpy.arange(-90.,120.,30.))
    m.drawmeridians(numpy.arange(0.,420.,60.))
    im = m.scatter(x, y, s=s, marker="o", c=cmap(nhist), alpha=alpha, edgecolors='none')
    
    font_tiny=matplotlib.font_manager.FontProperties()
    font_tiny.set_size('xx-small')
    
    if len(label_view) > 0:
        for i in label_view:
            if i > len(angs): 
                _logger.warn("Cannot label view: %d when there are only %d views (skipping)"%(i, len(angs)))
                continue
            ytext = -15 if i%2==0 else 15
            pylab.annotate('%d: %.1f,%.1f'%(i+1, angs[i, 0], angs[i, 1]), xy=(x[i],y[i]),  xycoords='data',
                                    xytext=(-15, ytext), textcoords='offset points',
                                    arrowprops=dict(arrowstyle="->"), fontproperties =font_tiny
                                    )
    
    if numpy.sum(sel) > 0 and not hide_zero_marker:
        im = m.scatter(x[sel], y[sel], numpy.max(s), marker="x", c=cm.gray(0.5))#@UndefinedVariable
    
    if not use_scale:
        im = matplotlib.cm.ScalarMappable(cmap=cmap, norm=matplotlib.colors.Normalize())
        im.set_array(hist)
        m.colorbar(im, "right", size="3%", pad='1%')
    else:
        fontP=matplotlib.font_manager.FontProperties()
        fontP.set_size('small')
        inc = len(hist)/10
        lines = []
        labels = []
        idx = numpy.argsort(hist)[::-1]
        for j in xrange(0, len(hist), inc):
            i=idx[j]
            labels.append("%s"%hist[i])
            lines.append(matplotlib.lines.Line2D(range(1), range(1), color=cmap(nhist[i]), marker='o', markersize=s[i]/2, linestyle='none', markeredgecolor='white'))
        if numpy.sum(sel) > 0 and not hide_zero_marker:
            i=idx[len(idx)-1]
            labels.append("%s"%hist[i])
            lines.append(matplotlib.lines.Line2D(range(1), range(1), color=cm.gray(0.5), marker='x', markersize=numpy.max(s)/5, linestyle='none')) #@UndefinedVariable
        pylab.legend(tuple(lines),tuple(labels), numpoints=1, frameon=False, loc='center left', bbox_to_anchor=(1, 0.5), prop = fontP)
Esempio n. 25
0
def autoconfig_processfile2(name, pool_used, f_index, args):
    try:
        with open(
                name
        ) as result_file:  # No need to specify 'r': this is the default.as

            gates = args.config
            num_gates = len(gates)
            nameParts = name.split("/")

            if len(nameParts) > 1:
                originalName = nameParts[len(nameParts) - 2]
            else:
                if args.name is not None:
                    originalName = args.name + str(f_index)
                else:
                    originalName = "NoNameSample" + str(f_index)
            print("Processing ", (originalName))

            colorlist = args.colorlist

            events = sum(1 for line in
                         result_file) - 1  #quickly determine number of events
            result_file.seek(0)  #rewind to the beginning of file
            header = result_file.readline()
            header = header.strip()
            headers = header.split(
                "\t")  #parse the headers from the first line of input
            headers = [_f for _f in headers if _f]

            num_markers = len(headers) - 2

            # create a numpy array for faster data access
            if args.debug:
                print("Assigning data to numpy matrix")
                print("header length: ", len(headers))
                print(headers)
                print("events count: ", events)
            fcm = loadNp(result_file, len(headers), events)

            # find the start of pop info on fcs_results_all
            if args.flocklegacy is False:
                pop_offset = 0
                for i, header in enumerate(headers):
                    if header == "pop1":
                        pop_offset = i - 1
                if args.debug: print("Pop offset: ", pop_offset)

            axis_popIndexDict = defaultdict(list)

            print("Configuring axises from gate configuration file")
            axises = []
            composite_axis = 0
            last_xmarker = ""
            last_ymarker = ""
            last_parent = 0
            for pop, config in list(gates.items()):
                #pop="pop"+str(i+1)
                #config=gates.get(pop)

                xmarker = str(headers[config[1] - 1])
                ymarker = str(headers[config[2] - 1])
                startx = int((float(config[3]) / 200) * 4096)
                starty = int((float(config[5]) / 200) * 4096)
                endx = int((float(config[4]) / 200) * 4096)
                endy = int((float(config[6]) / 200) * 4096)
                parent = int(config[7])
                key = "axis" + str(composite_axis)
                if (xmarker != last_xmarker) or (ymarker != last_ymarker) or (
                        parent != last_parent):
                    composite_axis = composite_axis + 1
                    key = "axis" + str(composite_axis)
                    axises.append([xmarker, ymarker, key])
                axis_popIndexDict[key].append(pop)
                last_xmarker = xmarker
                last_ymarker = ymarker
                last_parent = parent

            num_axises = len(axises)
            #print "axis_popIndexDict: ", axis_popIndexDict
            #print axises

            cols = int(min(num_axises, 4))
            rows = int(max(int(math.ceil(num_axises / float(cols))), 1))

            print("Iterating through feature pairs")
            sub_results = []
            composite_list = []
            if pool_used == 0: inner_pool = Pool(processes=cores)
            for w, mpair in enumerate(axises):
                lines = []
                dim1 = mpair[0]
                dim2 = mpair[1]
                #print dim1, dim2
                dim1_idx = 0
                dim2_idx = 0
                for i, marker in enumerate(headers):
                    if marker == dim1:
                        dim1_idx = i
                        print(("Feature 1: ", marker, i + 1))
                    if marker == dim2:
                        dim2_idx = i
                        print(("Feature 2: ", marker, i + 1))

                header_names = dim1 + "_vs_" + dim2
                axis_name = mpair[2]
                print(header_names, axis_name)

                if args.flocklegacy is False:
                    fcm[:, -1] = 0  #reset color mapping in np matrix
                    pops = axis_popIndexDict.get(axis_name)
                    if args.debug:
                        print(
                            "iterate through events to find population members"
                        )
                    poplist_colors = []
                    for i, pop in enumerate(pops):
                        config = gates.get(pop)
                        found_pop = config[0]
                        xmarker = str(headers[config[1] - 1])
                        ymarker = str(headers[config[2] - 1])
                        startx = int((float(config[3]) / 200) * 4096)
                        starty = int((float(config[5]) / 200) * 4096)
                        endx = int((float(config[4]) / 200) * 4096)
                        endy = int((float(config[6]) / 200) * 4096)
                        if i == 0: parent_gate = int(config[7])
                        cluster_type = int(config[8])
                        pop_loc = found_pop + pop_offset
                        parent_poploc = parent_gate + pop_offset

                        if (xmarker == dim1) and (ymarker == dim2):
                            if cluster_type == 2:
                                print("slanted")
                            else:
                                lines.append([(startx, endx), (starty, starty),
                                              colorlist[i + 2]])
                                lines.append([(startx, startx), (starty, endy),
                                              colorlist[i + 2]])
                                lines.append([(startx, endx), (endy, endy),
                                              colorlist[i + 2]])
                                lines.append([(endx, endx), (starty, endy),
                                              colorlist[i + 2]])
                        elif (xmarker == dim2) and (ymarker == dim1):
                            if cluster_type == 2:
                                print("slanted")
                            else:
                                lines.append([(starty, endy), (startx, startx),
                                              colorlist[i + 2]])
                                lines.append([(starty, starty), (startx, endx),
                                              colorlist[i + 2]])
                                lines.append([(starty, endy), (endx, endx),
                                              colorlist[i + 2]])
                                lines.append([(endy, endy), (startx, endx),
                                              colorlist[i + 2]])

                        if args.showparent and (config[0] > 1) and i == 0:
                            fcm[:, -1] = (1 - fcm[:, pop_loc]) + (
                                1 - fcm[:, parent_poploc])
                        else:
                            fcm[:, -1] = np.maximum(
                                fcm[:, -1], (1 - fcm[:, pop_loc]) * (i + 2))

                if args.debug: print("sorting numpy array")
                if args.sort:
                    sfcm = fcm[np.argsort(
                        fcm[:, -1]
                    )]  #sort the data set based on population number
                else:
                    sfcm = fcm

                if args.reversesort:
                    sfcm = sfcm[::-1]

                print("creating color array")
                cdata = []

                for a in sfcm[:, -1]:
                    try:
                        cdata.append(colorlist[a])
                    except Exception as err:
                        print("ERROR in index: ", a)
                        sys.stderr.write('Error: %sn' % str(err))
                        return 1

                xdata = sfcm[:, dim1_idx]
                ydata = sfcm[:, dim2_idx]
                sample_name = originalName

                poplist = []
                if args.flocklegacy is False:
                    parent_pop_name = "pop" + str(parent_gate)
                    if args.showparent:
                        poplist.append(parent_pop_name)
                    else:
                        poplist.append("")
                    poplist = poplist + pops

                    pop_name = ""
                    if args.showparent:
                        for i, pop in enumerate(poplist):
                            if i > 0: pop_name = pop_name + pop
                    else:
                        for i, pop in enumerate(poplist):
                            pop_name = pop_name + pop
                else:
                    pop_name = "FLOCK"

                png_file = sample_name + "_" + header_names + "_" + pop_name + ".png"
                if pool_used == 0:
                    inner_pool.apply_async(plotfig,
                                           args=[
                                               sample_name, xdata, ydata,
                                               cdata, dim1, dim2, poplist,
                                               lines, args
                                           ])
                else:
                    png_file = plotfig(sample_name, xdata, ydata, cdata, dim1,
                                       dim2, poplist, lines, args)
                sub_results.append([axis_name, [sample_name, png_file]])
                composite_list.append([axis_name, png_file])
            if pool_used == 0:
                inner_pool.close()
                inner_pool.join()
            if args.gatescomposite:
                print(
                    "Number of rows and columns for the composite plots (gates): ",
                    rows, cols)
                compose_from_fig(composite_list, rows, cols, num_axises,
                                 sample_name, " ", " ", 0, args)

            return sub_results
    except IOError as exc:
        if exc.errno != errno.EISDIR:  # Do not fail if a directory is found, just ignore it.
            raise  # Propagate other kinds of IOError.args
    except Exception as e:
        print("Exception: %s" % str(e), file=sys.stderr)
        raise Exception("".join(traceback.format_exception(*sys.exc_info())))
Esempio n. 26
0
def processfile(name, pool_used, f_index, args):
    try:
        with open(
                name
        ) as result_file:  # No need to specify 'r': this is the default.as
            gates = args.config
            if (gates is not None): num_gates = len(gates)
            nameParts = name.split("/")
            if args.name is not None:
                originalName = args.name + str(f_index)
            elif (args.flocklegacy is True):
                originalName = nameParts[len(nameParts) - 1]
            else:
                if len(nameParts) > 1:
                    originalName = nameParts[len(nameParts) - 2]
                else:
                    originalName = "NoNameSample" + str(f_index)
            print("Processing ", (originalName))

            # retrieve command line options from args
            colorlist = args.colorlist
            mpairs = args.markers

            events = sum(1 for line in
                         result_file) - 1  #quickly determine number of events

            result_file.seek(0)  #rewind file to beginning
            header = result_file.readline()
            header = header.strip()
            headers = header.split("\t")
            markers = headers
            num_markers = len(headers) - 2

            if ((args.markers is None) and (args.flocklegacy is True)):
                mpairs = list(itertools.permutations(markers[:-2], 2))

            num_pairs = len(mpairs)

            #
            # create a numpy array
            #
            fcm = loadNp(result_file, len(headers), events)

            #
            # Check legacy FLOCK or new DAFI results
            #
            pops = args.listofpop
            if args.flocklegacy is False:
                pop2DList = [[i, j] for i, j in enumerate(pops)]
                # locate position of the specified populations in the input file columns
                for i, marker in enumerate(markers):
                    for j, k in enumerate(pops):
                        if marker == k:
                            pop2DList[j][0] = i
                # create a dictionary for quick population order lookup
                plist = [int(x[0]) for x in pop2DList]
                pop2DIndex = set(plist)
                pdict = {e: (plist.index(e) + 1) for e in plist}
                pdict.update({0: 0})

                # find the start of pop info on fcs_results_all
                pop_offset = 0
                for i, header in enumerate(headers):
                    if header == "pop1":
                        pop_offset = i - 1
                if args.debug: print("Pop offset: ", pop_offset)

                if args.debug: print(("Events: ", events))
                if args.debug: print(("Number of Markers: ", num_markers))

                fcm[:, -1] = 0
                for i, popIndex in enumerate(pop2DIndex):
                    print(i, popIndex)
                    fcm[:,
                        -1] = np.maximum(fcm[:, -1],
                                         (1 - fcm[:, popIndex]) * (popIndex))

            # Sort the matrix data based on the population column (last column)
            if args.sort:
                sfcm = fcm[np.argsort(
                    fcm[:, -1])]  #sort the data set based on population number
            else:
                sfcm = fcm

            if args.reversesort:
                sfcm = sfcm[::-1]

            #generate color array from sorted numpy matrix
            colors = []
            for a in sfcm[:, -1]:
                if args.flocklegacy:
                    colors.append(colorlist[a])
                else:
                    colors.append(colorlist[pdict[a]])

            #calculate cols and rows for composite plot
            cols = int(min(num_pairs, 4))
            rows = int(max(int(math.ceil(num_pairs / float(cols))), 1))

            sub_results = []
            composite_list = []
            #iterate through all 2D dot plot pairs specified
            for w, mpair in enumerate(mpairs):

                dim1 = mpair[0]
                dim2 = mpair[1]
                if args.debug: print(dim1, dim2)
                dim1_idx = 0
                dim2_idx = 0

                lines = []
                if ((gates is not None) and (pops is not None)):
                    for i, pop in enumerate(pops):
                        config = gates.get(pop)
                        xmarker = str(headers[config[1] - 1])
                        ymarker = str(headers[config[2] - 1])
                        startx = int((float(config[3]) / 200) * 4096)
                        starty = int((float(config[5]) / 200) * 4096)
                        endx = int((float(config[4]) / 200) * 4096)
                        endy = int((float(config[6]) / 200) * 4096)
                        parent_gate = int(config[7])
                        cluster_type = int(config[8])
                        if (xmarker == dim1) and (ymarker == dim2):
                            if cluster_type == 2:
                                print("slanted")
                            else:
                                lines.append([
                                    (startx, endx), (starty, starty),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                                lines.append([
                                    (startx, startx), (starty, endy),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                                lines.append([
                                    (startx, endx), (endy, endy),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                                lines.append([
                                    (endx, endx), (starty, endy),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                        elif (xmarker == dim2) and (ymarker == dim1):
                            if cluster_type == 2:
                                print("slanted")
                            else:
                                lines.append([
                                    (starty, endy), (startx, startx),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                                lines.append([
                                    (starty, starty), (startx, endx),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                                lines.append([
                                    (starty, endy), (endx, endx),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])
                                lines.append([
                                    (endy, endy), (startx, endx),
                                    colorlist[pdict[config[0] + pop_offset]]
                                ])

                for i, marker in enumerate(markers):
                    if marker == dim1:
                        dim1_idx = i
                        print(("Feature 1: ", marker, i))
                    if marker == dim2:
                        dim2_idx = i
                        print(("Feature 2: ", marker, i))

                xdata = sfcm[:, dim1_idx]
                ydata = sfcm[:, dim2_idx]
                cdata = colors
                header_names = dim1 + "_vs_" + dim2
                sample_name = originalName

                if args.flocklegacy is False:
                    pop_name = ""
                    if args.showparent:
                        for i, pop in enumerate(pops):
                            if i > 0: pop_name = pop_name + pop
                    else:
                        for i, pop in enumerate(pops):
                            pop_name = pop_name + pop

                png_file = plotfig(sample_name, xdata, ydata, cdata, dim1,
                                   dim2, pops, lines, args)
                print(png_file)
                sub_results.append([header_names, [sample_name, png_file]])
                composite_list.append([header_names, png_file])

            if args.pairscomposite:
                print(
                    "Number of rows and columns for the composite plots (markers): ",
                    rows, cols)
                print(composite_list)
                compose_from_fig(composite_list, rows, cols, num_pairs,
                                 sample_name, " ", " ", 0, args)
            return sub_results
    except IOError as exc:
        if exc.errno != errno.EISDIR:  # Do not fail if a directory is found, just ignore it.
            raise  # Propagate other kinds of IOError.args
    except:
        raise Exception("".join(traceback.format_exception(*sys.exc_info())))
Esempio n. 27
0
def do_plot(plotfile1, plotfile2, plotfile3, component, outFile, 
            log, minval, maxval, ncontours, eps, dpi, 
            xmin, xmax, ymin, ymax,
            label1, label2, label3):


    #--------------------------------------------------------------------------
    # construct the output file name
    #--------------------------------------------------------------------------
    if (outFile == ""):
        outFile = "compare_" + component

        if (not eps):
            outFile += ".png"

        else:
            outFile += ".eps"

    else:
        # make sure the proper extension is used
        if (not eps):
            if (not string.rfind(outFile, ".png") > 0):
                outFile = outFile + ".png"

        else:
            if (not string.rfind(outFile, ".eps") > 0):
                outFile = outFile + ".eps"


    #--------------------------------------------------------------------------
    # read in the data from plotfile1
    #--------------------------------------------------------------------------
    (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile1)

    time = fsnapshot.fplotfile_get_time(plotfile1)

    (xmin1, xmax1, ymin1, ymax1, zmin1, zmax1) = \
        fsnapshot.fplotfile_get_limits(plotfile1)

    x1 = xmin1 + numpy.arange( (nx), dtype=numpy.float64 )*(xmax1 - xmin1)/nx
    y1 = ymin1 + numpy.arange( (ny), dtype=numpy.float64 )*(ymax1 - ymin1)/ny


    if (not nz == -1):
        print "ERROR: 2-d support only"
        sys.exit(2)


    # read in the main component
    data1 = numpy.zeros( (nx, ny), dtype=numpy.float64)

    (data1, err) = fsnapshot.fplotfile_get_data_2d(plotfile1, component, data1)
    if (not err == 0):
        sys.exit(2)

    data1 = numpy.transpose(data1)


    if log:
        data1 = numpy.log10(data1)


    extent1 = [xmin1, xmax1, ymin1, ymax1]


    #--------------------------------------------------------------------------
    # read in the data from plotfile2
    #--------------------------------------------------------------------------
    (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile2)

    time = fsnapshot.fplotfile_get_time(plotfile2)

    (xmin2, xmax2, ymin2, ymax2, zmin2, zmax2) = \
        fsnapshot.fplotfile_get_limits(plotfile2)

    x2 = xmin2 + numpy.arange( (nx), dtype=numpy.float64 )*(xmax2 - xmin2)/nx
    y2 = ymin2 + numpy.arange( (ny), dtype=numpy.float64 )*(ymax2 - ymin2)/ny


    if (not nz == -1):
        print "ERROR: 2-d support only"
        sys.exit(2)


    # read in the main component
    data2 = numpy.zeros( (nx, ny), dtype=numpy.float64)

    (data2, err) = fsnapshot.fplotfile_get_data_2d(plotfile2, component, data2)
    if (not err == 0):
        sys.exit(2)

    data2 = numpy.transpose(data2)


    if log:
        data2 = numpy.log10(data2)


    extent2 = [xmin2, xmax2, ymin2, ymax2]


    #--------------------------------------------------------------------------
    # read in the data from plotfile3 -- if present
    #--------------------------------------------------------------------------
    if (not plotfile3 == ""):
        (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile3)

        time = fsnapshot.fplotfile_get_time(plotfile3)

        (xmin3, xmax3, ymin3, ymax3, zmin3, zmax3) = \
            fsnapshot.fplotfile_get_limits(plotfile3)

        x3 = xmin3 + numpy.arange( (nx), dtype=numpy.float64 )*(xmax3 - xmin3)/nx
        y3 = ymin3 + numpy.arange( (ny), dtype=numpy.float64 )*(ymax3 - ymin3)/ny


        if (not nz == -1):
            print "ERROR: 2-d support only"
            sys.exit(2)


        # read in the main component
        data3 = numpy.zeros( (nx, ny), dtype=numpy.float64)

        (data3, err) = fsnapshot.fplotfile_get_data_2d(plotfile3, component, data3)
        if (not err == 0):
            sys.exit(2)

        data3 = numpy.transpose(data3)


        if log:
            data3 = numpy.log10(data3)


        extent3 = [xmin3, xmax3, ymin3, ymax3]



    #--------------------------------------------------------------------------
    # find data limits, etc.
    #--------------------------------------------------------------------------
    if (not extent1 == extent2):
        print "ERROR: extent of domains do not agree"
        sys.exit(2)

    if (not plotfile3 == ""):
        if (not extent1 == extent3):
            print "ERROR: extent of domains do not agree"
            sys.exit(2)


    extent = extent1

    if (not xmin == None):
        extent[0] = xmin

    if (not xmax == None):
        extent[1] = xmax

    if (not ymin == None):
        extent[2] = ymin

    if (not ymax == None):
        extent[3] = ymax


    if (minval == None):
        minval = min(numpy.min(data1), numpy.min(data2))
        if (not plotfile3 == ""):
            minval = min(minval, numpy.min(data3))

    if (maxval == None):
        maxval = max(numpy.max(data1), numpy.max(data2))
        if (not plotfile3 == ""):
            maxval = max(maxval, numpy.max(data3))


    levels = numpy.linspace(minval, maxval, ncontours, endpoint=True)


    #--------------------------------------------------------------------------
    # make the figure
    #--------------------------------------------------------------------------
    cs1 = pylab.contour(x1, y1, data1, ncontours, colors='k', levels=levels)
    cs2 = pylab.contour(x2, y2, data2, ncontours, colors='r', levels=levels)
    if (not plotfile3 == ""):
        cs3 = pylab.contour(x3, y3, data3, ncontours, colors='g', levels=levels)


    # make the labels -- see http://www.scipy.org/Cookbook/Matplotlib/Legend
    # for this technique
    lines = []
    labels = []

    if (not label1 == None):
        line1 = matplotlib.lines.Line2D(range(10), range(10), linestyle='-', color='k')
        lines.append(line1)
        labels.append(label1)

    if (not label2 == None):
        line2 = matplotlib.lines.Line2D(range(10), range(10), linestyle='-', color='r')
        lines.append(line2)
        labels.append(label2)


    if (not label3 == None):
        line3 = matplotlib.lines.Line2D(range(10), range(10), linestyle='-', color='g')
        lines.append(line3)
        labels.append(label3)

        
    pylab.legend(lines, labels, fontsize="small", frameon=False)


    formatter = matplotlib.ticker.ScalarFormatter(useMathText=True)
    #pylab.clabel(cs, fontsize=9, inline=1)#, fmt=formatter)

    pylab.axis(extent)

    ax = pylab.gca()
    ax.set_aspect("equal")

    fig1 = ax.get_figure()

    ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True))
    ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True))

    pylab.xlabel("x")
    pylab.ylabel("y")


    if (not eps):
        pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.5)
    else:
        pylab.savefig(outFile, bbox_inches='tight', pad_inches=0.5)
    def plotWaterfall(self):

        while True:
            # from matplotlib gallery
            # Fixing random state for reproducibility
            np.random.seed(19680801)

            # Create new Figure with black background
            fig = plt.figure(figsize=(8, 8), facecolor='black')

            # Add a subplot with no frame
            ax = plt.subplot(111, frameon=False)

            # Generate random data
            data = np.random.uniform(0, 1, (5, 400))
            X = np.linspace(-1, 1, data.shape[-1])

            # Generate line plots
            lines = []
            colors = ['w', 'g', 'r', 'b', 'm']
            labels = [
                'chan 174', 'chan 172', 'chan 168', 'chan 246', 'chan 247'
            ]
            for i in range(len(data)):
                # Small reduction of the X extents to get a cheap perspective effect
                xscale = 1 - i / 400.
                # Same for linewidth (thicker strokes on bottom)
                lw = 1.5 - i / 100.0
                line, = ax.plot(xscale * X,
                                i + data[i],
                                color=colors[i],
                                label=labels[i],
                                lw=lw)
                ax.legend()
                lines.append(line)

            # Set y limit (or first line is cropped because of thickness)
            ax.set_ylim(-1, 400)

            # No ticks
            ax.set_xticks([])
            ax.set_yticks([])

            # 2 part titles to get different font weights
            #ax.text(0.5, 1.0, "KIDPY ", transform=ax.transAxes,
            #	ha="right", va="bottom", color="w",
            #	family="sans-serif", fontweight="light", fontsize=16)
            #ax.text(0.5, 1.0, "JOYDIVISION MODE", transform=ax.transAxes,
            #	ha="left", va="bottom", color="w",
            #	family="sans-serif", fontweight="bold", fontsize=16)
            def new_data(self, data):
                data_row = np.random.uniform(0, 1, len(data))
                self.fpga.write_int(
                    self.regs[np.where(
                        self.regs == 'fft_snap_ctrl_reg')[0][0]][1], 0)
                self.fpga.write_int(
                    self.regs[np.where(
                        self.regs == 'fft_snap_ctrl_reg')[0][0]][1], 1)
                fft_snap = (np.fromstring(self.fpga.read(
                    self.regs[np.where(
                        self.regs == 'fft_snap_bram_reg')[0][0]][1],
                    (2**9) * 8),
                                          dtype='>i2')).astype('float')
                I0 = fft_snap[0::4]
                Q0 = fft_snap[1::4]
                I1 = fft_snap[2::4]
                Q1 = fft_snap[3::4]
                mag0 = np.sqrt(I0**2 + Q0**2)
                #mag0 = 20*np.log10(mag0)
                mag1 = np.sqrt(I1**2 + Q1**2)
                mags = np.hstack(zip(mag0, mag1))
                #mag1 = 20*np.log10(mag1)
                fft_mags = np.hstack(zip(mag0, mag1))
                channels = fft_mags[174]  # definitely chan 174, 245-6
                channels = np.append(channels, fft_mags[172])
                channels = np.append(channels, fft_mags[168])
                channels = np.append(channels, fft_mags[246])
                channels = np.append(channels, fft_mags[247])
                return channels

            def update(*args):
                # Shift all data to the right
                data[:, 1:] = data[:, :-1]

                # Fill-in new values
                #data[:, 0] = np.random.uniform(0, 1, len(data))
                data[:, 0] = new_data(self, data)

                # Update data
                for i in range(len(data)):
                    lines[i].set_ydata(i + data[i])
                # Return modified artists
                return lines

            # Construct the animation, using the update function as the animation
            # director.
            anim = animation.FuncAnimation(fig, update, interval=10)
            plt.show()
            #n = n+1
        return
Esempio n. 29
0
def overlayFan(myData, myMap, myFig, param, coords='geo', gsct=0, site=None,
               fov=None, gs_flg=[], fill=True, velscl=1000., dist=1000.,
               cmap=None, norm=None, alpha=1):

    """A function of overlay radar scan data on a map

    Parameters
    ----------
    myData : pydarn.sdio.radDataTypes.scanData or
             pydarn.sdio.radDataTypes.beamData or
             list of pydarn.sdio.radDataTypes.beamData objects
        A radar beam object, a radar scanData object, or simply a list of
        radar beams
    myMap :
        The map we are plotting on
    myFig :
        Figure object that we are plotting to
    coords : Optional[str]
        The coordinates we are plotting in.  Default: geo
    param : Optional[str]
        The parameter to be plotted, valid inputs are 'velocity', 'power',
        'width', 'elevation', 'phi0'.  default = 'velocity
    gsct : Optional[boolean]
        A flag indicating whether we are distinguishing ground scatter.
        default = 0
    intensities : Optional[  ]
        A list of intensities (used for colorbar)
    fov : Optional[pydarn.radar.radFov.fov]
        A radar fov object
    gs_flg : Optional[  ]
        A list of gs flags, 1 per range gate
    fill : Optional[boolean]
        A flag indicating whether to plot filled or point RB cells.
        default = True
    velscl : Optional[float]
        The velocity to use as baseline for velocity vector length, only
        applicable if fill = 0.  default = 1000
    lines : Optional[  ]
        An array to have the endpoints of velocity vectors.  only applicable if
        fill = 0.  default = []
    dist : Optional [float]
        The length in map projection coords of a velscl length velocity vector.
        default = 1000. km

    Returns
    -------
    intensities

    pcoll

    lcoll


    Example
    -------
        overlayFan(aBeam,myMap,param,coords,gsct=gsct,site=sites[i],fov=fovs[i],
                   verts=verts,intensities=intensities,gs_flg=gs_flg)

    """
    from davitpy import pydarn

    if(isinstance(myData, pydarn.sdio.beamData)): myData = [myData]

    if(site is None):
        site = pydarn.radar.site(radId=myData[0].stid, dt=myData[0].time)
    if(fov is None):
        fov = pydarn.radar.radFov.fov(site=site, rsep=myData[0].prm.rsep,
                                      ngates=myData[0].prm.nrang + 1,
                                      nbeams=site.maxbeam, coords=coords,
                                      date_time=myData[0].time)

    gs_flg, lines = [], []
    if fill: verts, intensities = [], []
    else: verts, intensities = [[], []], [[], []]

    # loop through gates with scatter
    for myBeam in myData:
        for k in range(0, len(myBeam.fit.slist)):
            if myBeam.fit.slist[k] not in fov.gates: continue
            r = myBeam.fit.slist[k]

            if fill:
                x1, y1 = myMap(fov.lonFull[myBeam.bmnum, r],
                               fov.latFull[myBeam.bmnum, r])
                x2, y2 = myMap(fov.lonFull[myBeam.bmnum, r + 1],
                               fov.latFull[myBeam.bmnum, r + 1])
                x3, y3 = myMap(fov.lonFull[myBeam.bmnum + 1, r + 1],
                               fov.latFull[myBeam.bmnum + 1, r + 1])
                x4, y4 = myMap(fov.lonFull[myBeam.bmnum + 1, r],
                               fov.latFull[myBeam.bmnum + 1, r])

                # save the polygon vertices
                verts.append(((x1, y1), (x2, y2), (x3, y3), (x4, y4),
                              (x1, y1)))

                # save the param to use as a color scale
                if(param == 'velocity'):
                    intensities.append(myBeam.fit.v[k])
                elif(param == 'power'):
                    intensities.append(myBeam.fit.p_l[k])
                elif(param == 'width'):
                    intensities.append(myBeam.fit.w_l[k])
                elif(param == 'elevation' and myBeam.prm.xcf):
                    intensities.append(myBeam.fit.elv[k])
                elif(param == 'phi0' and myBeam.prm.xcf):
                    intensities.append(myBeam.fit.phi0[k])

            else:
                x1, y1 = myMap(fov.lonCenter[myBeam.bmnum, r],
                               fov.latCenter[myBeam.bmnum, r])
                verts[0].append(x1)
                verts[1].append(y1)

                x2, y2 = myMap(fov.lonCenter[myBeam.bmnum, r + 1],
                               fov.latCenter[myBeam.bmnum, r + 1])

                theta = math.atan2(y2 - y1, x2 - x1)

                x2, y2 = x1 + myBeam.fit.v[k] / velscl * (-1.0) * \
                    math.cos(theta) * dist, y1 + myBeam.fit.v[k] / velscl * \
                    (-1.0) * math.sin(theta) * dist

                lines.append(((x1, y1), (x2, y2)))
                # save the param to use as a color scale
                if(param == 'velocity'):
                    intensities[0].append(myBeam.fit.v[k])
                elif(param == 'power'):
                    intensities[0].append(myBeam.fit.p_l[k])
                elif(param == 'width'):
                    intensities[0].append(myBeam.fit.w_l[k])
                elif(param == 'elevation' and myBeam.prm.xcf):
                    intensities[0].append(myBeam.fit.elv[k])
                elif(param == 'phi0' and myBeam.prm.xcf):
                    intensities[0].append(myBeam.fit.phi0[k])

                if(myBeam.fit.p_l[k] > 0):
                    intensities[1].append(myBeam.fit.p_l[k])
                else:
                    intensities[1].append(0.)
            if(gsct):
                gs_flg.append(myBeam.fit.gflg[k])

    # do the actual overlay
    if(fill):
        # if we have data
        if(verts != []):
            if(gsct == 0):
                inx = numpy.arange(len(verts))
            else:
                inx = numpy.where(numpy.array(gs_flg) == 0)
                x = PolyCollection(numpy.array(verts)[numpy.where(
                                   numpy.array(gs_flg) == 1)], facecolors='.3',
                                   linewidths=0, zorder=5, alpha=alpha)
                myFig.gca().add_collection(x, autolim=True)

            pcoll = PolyCollection(numpy.array(verts)[inx],
                                   edgecolors='face', linewidths=0,
                                   closed=False, zorder=4, alpha=alpha,
                                   cmap=cmap, norm=norm)
            # set color array to intensities
            pcoll.set_array(numpy.array(intensities)[inx])
            myFig.gca().add_collection(pcoll, autolim=True)
            return intensities, pcoll
    else:
        # if we have data
        if(verts != [[], []]):
            if(gsct == 0):
                inx = numpy.arange(len(verts[0]))
            else:
                inx = numpy.where(numpy.array(gs_flg) == 0)
                # plot the ground scatter as open circles
                x = myFig.scatter(numpy.array(verts[0])[numpy.where(
                                  numpy.array(gs_flg) == 1)],
                                  numpy.array(verts[1])[numpy.where(
                                      numpy.array(gs_flg) == 1)],
                                  s=.1 * numpy.array(intensities[1])[
                                  numpy.where(numpy.array(gs_flg) == 1)],
                                  zorder=5, marker='o', linewidths=.5,
                                  facecolors='w', edgecolors='k')
                myFig.gca().add_collection(x, autolim=True)

            # plot the i-s as filled circles
            ccoll = myFig.gca().scatter(numpy.array(verts[0])[inx],
                                        numpy.array(verts[1])[inx],
                                        s=.1 * numpy.array(
                                        intensities[1])[inx], zorder=10,
                                        marker='o', linewidths=.5,
                                        edgecolors='face', cmap=cmap,
                                        norm=norm)

            # set color array to intensities
            ccoll.set_array(numpy.array(intensities[0])[inx])
            myFig.gca().add_collection(ccoll)
            # plot the velocity vectors
            lcoll = LineCollection(numpy.array(lines)[inx], linewidths=.5,
                                   zorder=12, cmap=cmap, norm=norm)
            lcoll.set_array(numpy.array(intensities[0])[inx])
            myFig.gca().add_collection(lcoll)

            return intensities, lcoll
Esempio n. 30
0
def overlayFan(myData,myMap,myFig,param,coords='geo',gsct=0,site=None,\
                fov=None,gs_flg=[],fill=True,velscl=1000.,dist=1000.,
                cmap=None,norm=None,alpha=1):

  """A function of overlay radar scan data on a map

  **Args**:
    * **myData (:class:`pydarn.sdio.radDataTypes.scanData` or :class:`pydarn.sdio.radDataTypes.beamData` or list of :class:`pydarn.sdio.radDataTypes.beamData` objects)**: a radar beam object, a radar scanData object, or simply a list of radar beams
    * **myMap**: the map we are plotting on
    * **[param]**: the parameter we are plotting
    * **[coords]**: the coordinates we are plotting in
    * **[param]**: the parameter to be plotted, valid inputs are 'velocity', 'power', 'width', 'elevation', 'phi0'.  default = 'velocity
    * **[gsct]**: a flag indicating whether we are distinguishing ground scatter.  default = 0
    * **[intensities]**: a list of intensities (used for colorbar)
    * **[fov]**: a radar fov object
    * **[gs_flg]**: a list of gs flags, 1 per range gate
    * **[fill]**: a flag indicating whether to plot filled or point RB cells.  default = True
    * **[velscl]**: the velocity to use as baseline for velocity vector length, only applicable if fill = 0.  default = 1000
    * **[lines]**: an array to have the endpoints of velocity vectors.  only applicable if fill = 0.  default = []
    * **[dist]**: the length in map projection coords of a velscl length velocity vector.  default = 1000. km
  **OUTPUTS**:
    NONE

  **EXAMPLE**:
    ::
      
      overlayFan(aBeam,myMap,param,coords,gsct=gsct,site=sites[i],fov=fovs[i],\
                            verts=verts,intensities=intensities,gs_flg=gs_flg)

  Written by AJ 20121004
  """
  
  if(site == None):
    site = pydarn.radar.site(radId=myData[0].stid, dt=myData[0].time)
  if(fov == None):
    fov = pydarn.radar.radFov.fov(site=site,rsep=myData[0].prm.rsep,\
    ngates=myData[0].prm.nrang+1,nbeams= site.maxbeam,coords=coords) 
  
  if(isinstance(myData,pydarn.sdio.beamData)): myData = [myData]
  
  gs_flg,lines = [],[]
  if fill: verts,intensities = [],[]
  else: verts,intensities = [[],[]],[[],[]]
  
  #loop through gates with scatter
  for myBeam in myData:
    for k in range(0,len(myBeam.fit.slist)):
      if myBeam.fit.slist[k] not in fov.gates: continue
      r = myBeam.fit.slist[k]

      if fill:
        x1,y1 = myMap(fov.lonFull[myBeam.bmnum,r],fov.latFull[myBeam.bmnum,r])
        x2,y2 = myMap(fov.lonFull[myBeam.bmnum,r+1],fov.latFull[myBeam.bmnum,r+1])
        x3,y3 = myMap(fov.lonFull[myBeam.bmnum+1,r+1],fov.latFull[myBeam.bmnum+1,r+1])
        x4,y4 = myMap(fov.lonFull[myBeam.bmnum+1,r],fov.latFull[myBeam.bmnum+1,r])

        #save the polygon vertices
        verts.append(((x1,y1),(x2,y2),(x3,y3),(x4,y4),(x1,y1)))
        
        #save the param to use as a color scale
        if(param == 'velocity'): intensities.append(myBeam.fit.v[k])
        elif(param == 'power'): intensities.append(myBeam.fit.p_l[k])
        elif(param == 'width'): intensities.append(myBeam.fit.w_l[k])
        elif(param == 'elevation' and myBeam.prm.xcf): intensities.append(myBeam.fit.elv[k])
        elif(param == 'phi0' and myBeam.prm.xcf): intensities.append(myBeam.fit.phi0[k])
        
      else:
        x1,y1 = myMap(fov.lonCenter[myBeam.bmnum,r],fov.latCenter[myBeam.bmnum,r])
        verts[0].append(x1)
        verts[1].append(y1)
        
        x2,y2 = myMap(fov.lonCenter[myBeam.bmnum,r+1],fov.latCenter[myBeam.bmnum,r+1])
        
        theta = math.atan2(y2-y1,x2-x1)
        
        x2,y2 = x1+myBeam.fit.v[k]/velscl*(-1.0)*math.cos(theta)*dist,y1+myBeam.fit.v[k]/velscl*(-1.0)*math.sin(theta)*dist
        
        lines.append(((x1,y1),(x2,y2)))
        #save the param to use as a color scale
        if(param == 'velocity'): intensities[0].append(myBeam.fit.v[k])
        elif(param == 'power'): intensities[0].append(myBeam.fit.p_l[k])
        elif(param == 'width'): intensities[0].append(myBeam.fit.w_l[k])
        elif(param == 'elevation' and myBeam.prm.xcf): intensities[0].append(myBeam.fit.elv[k])
        elif(param == 'phi0' and myBeam.prm.xcf): intensities[0].append(myBeam.fit.phi0[k])
        
        if(myBeam.fit.p_l[k] > 0): intensities[1].append(myBeam.fit.p_l[k])
        else: intensities[1].append(0.)
      if(gsct): gs_flg.append(myBeam.fit.gflg[k])
      

  #do the actual overlay
  if(fill):
    #if we have data
    if(verts != []):
      if(gsct == 0):
        inx = numpy.arange(len(verts))
      else:
        inx = numpy.where(numpy.array(gs_flg)==0)
        x = PolyCollection(numpy.array(verts)[numpy.where(numpy.array(gs_flg)==1)],
          facecolors='.3',linewidths=0,zorder=5,alpha=alpha)
        myFig.gca().add_collection(x, autolim=True)
        
      pcoll = PolyCollection(numpy.array(verts)[inx],
        edgecolors='face',linewidths=0,closed=False,zorder=4,
        alpha=alpha,cmap=cmap,norm=norm)
      #set color array to intensities
      pcoll.set_array(numpy.array(intensities)[inx])
      myFig.gca().add_collection(pcoll, autolim=True)
      return intensities,pcoll
  else:
    #if we have data
    if(verts != [[],[]]):
      if(gsct == 0):
        inx = numpy.arange(len(verts[0]))
      else:
        inx = numpy.where(numpy.array(gs_flg)==0)
        #plot the ground scatter as open circles
        x = myFig.scatter(numpy.array(verts[0])[numpy.where(numpy.array(gs_flg)==1)],\
            numpy.array(verts[1])[numpy.where(numpy.array(gs_flg)==1)],\
            s=.1*numpy.array(intensities[1])[numpy.where(numpy.array(gs_flg)==1)],\
            zorder=5,marker='o',linewidths=.5,facecolors='w',edgecolors='k')
        myFig.gca().add_collection(x, autolim=True)
        
      #plot the i-s as filled circles
      ccoll = myFig.gca().scatter(numpy.array(verts[0])[inx],numpy.array(verts[1])[inx], \
              s=.1*numpy.array(intensities[1])[inx],zorder=10,marker='o',linewidths=.5, \
              edgecolors='face',cmap=cmap,norm=norm)
      
      #set color array to intensities
      ccoll.set_array(numpy.array(intensities[0])[inx])
      myFig.gca().add_collection(ccoll)
      #plot the velocity vectors
      lcoll = LineCollection(numpy.array(lines)[inx],linewidths=.5,zorder=12,cmap=cmap,norm=norm)
      lcoll.set_array(numpy.array(intensities[0])[inx])
      myFig.gca().add_collection(lcoll)

      return intensities,lcoll
Esempio n. 31
0
def do_plot(plotfile, component1, component2, outFile, minval1, maxval1,
            minval2, maxval2, ncontours, eps, dpi, xmin, xmax, ymin, ymax,
            label1, label2):

    print minval1, maxval1, minval2, maxval2

    #--------------------------------------------------------------------------
    # construct the output file name
    #--------------------------------------------------------------------------
    if (outFile == ""):
        outFile = "compare_" + component1 + "_" + component2

        if (not eps):
            outFile += ".png"

        else:
            outFile += ".eps"

    else:
        # make sure the proper extension is used
        if (not eps):
            if (not string.rfind(outFile, ".png") > 0):
                outFile = outFile + ".png"

        else:
            if (not string.rfind(outFile, ".eps") > 0):
                outFile = outFile + ".eps"

    #--------------------------------------------------------------------------
    # read in the data from the plotfile
    #--------------------------------------------------------------------------
    (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile)

    time = fsnapshot.fplotfile_get_time(plotfile)

    (xmin, xmax, ymin, ymax, zmin, zmax) = \
        fsnapshot.fplotfile_get_limits(plotfile)

    x = xmin + numpy.arange((nx), dtype=numpy.float64) * (xmax - xmin) / nx
    y = ymin + numpy.arange((ny), dtype=numpy.float64) * (ymax - ymin) / ny

    if (not nz == -1):
        print "ERROR: 2-d support only"
        sys.exit(2)

    # read in the first components
    data1 = numpy.zeros((nx, ny), dtype=numpy.float64)
    (data1, err1) = fsnapshot.fplotfile_get_data_2d(plotfile, component1,
                                                    data1)

    data2 = numpy.zeros((nx, ny), dtype=numpy.float64)
    (data2, err2) = fsnapshot.fplotfile_get_data_2d(plotfile, component2,
                                                    data2)

    if not err1 == 0 and err2 == 0:
        sys.exit(2)

    data1 = numpy.transpose(data1)
    data2 = numpy.transpose(data2)

    extent = [xmin, xmax, ymin, ymax]

    #--------------------------------------------------------------------------
    # find data limits, etc.
    #--------------------------------------------------------------------------
    if (not xmin == None):
        extent[0] = xmin

    if (not xmax == None):
        extent[1] = xmax

    if (not ymin == None):
        extent[2] = ymin

    if (not ymax == None):
        extent[3] = ymax

    if (minval1 == None):
        minval1 = numpy.min(data1)

    if (maxval1 == None):
        maxval1 = numpy.max(data1)

    if (minval2 == None):
        minval2 = numpy.min(data2)

    if (maxval2 == None):
        maxval2 = numpy.max(data2)

    levels1 = numpy.linspace(minval1, maxval1, ncontours, endpoint=True)
    levels2 = numpy.linspace(minval2, maxval2, ncontours, endpoint=True)

    print levels1

    #--------------------------------------------------------------------------
    # make the figure
    #--------------------------------------------------------------------------
    cs1 = pylab.contour(x, y, data1, ncontours, colors='b', levels=levels1)
    cs2 = pylab.contour(x, y, data2, ncontours, colors='r', levels=levels2)

    # make the labels -- see http://www.scipy.org/Cookbook/Matplotlib/Legend
    # for this technique
    lines = []
    labels = []

    if (not label1 == None):
        line1 = matplotlib.lines.Line2D(range(10),
                                        range(10),
                                        linestyle='-',
                                        color='b')
        lines.append(line1)
        labels.append(label1)

    if (not label2 == None):
        line2 = matplotlib.lines.Line2D(range(10),
                                        range(10),
                                        linestyle='-',
                                        color='r')
        lines.append(line2)
        labels.append(label2)

    pylab.legend(lines, labels)

    formatter = matplotlib.ticker.ScalarFormatter(useMathText=True)
    #pylab.clabel(cs, fontsize=9, inline=1)#, fmt=formatter)

    pylab.axis(extent)

    ax = pylab.gca()
    ax.set_aspect("equal")

    fig1 = ax.get_figure()

    ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True))
    ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True))

    pylab.xlabel("x")
    pylab.ylabel("y")

    if (not eps):
        pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.5)
    else:
        pylab.savefig(outFile, bbox_inches='tight', pad_inches=0.5)
Esempio n. 32
0
def do_plot(plotfile1, plotfile2, plotfile3, component, outFile, minval,
            maxval, color1, color2, color3, ncontours, eps, dpi, xmin, xmax,
            ymin, ymax, label1, label2, label3):

    #--------------------------------------------------------------------------
    # construct the output file name
    #--------------------------------------------------------------------------
    if (outFile == ""):
        outFile = "compare_" + component

        if (not eps):
            outFile += ".png"

        else:
            outFile += ".eps"

    else:
        # make sure the proper extension is used
        if (not eps):
            if (not string.rfind(outFile, ".png") > 0):
                outFile = outFile + ".png"

        else:
            if (not string.rfind(outFile, ".eps") > 0):
                outFile = outFile + ".eps"

    #--------------------------------------------------------------------------
    # read in the data from the plotfiles
    #--------------------------------------------------------------------------
    (nx1, ny1, nz1) = fsnapshot.fplotfile_get_size(plotfile1)

    time = fsnapshot.fplotfile_get_time(plotfile1)

    (xmin1, xmax1, ymin1, ymax1, zmin1, zmax1) = \
        fsnapshot.fplotfile_get_limits(plotfile1)

    if (not nz1 == -1):
        print "ERROR: 2-d support only"
        sys.exit(2)

    (nx2, ny2, nz2) = fsnapshot.fplotfile_get_size(plotfile2)

    time = fsnapshot.fplotfile_get_time(plotfile2)

    (xmin2, xmax2, ymin2, ymax2, zmin2, zmax2) = \
        fsnapshot.fplotfile_get_limits(plotfile2)

    if not (nx1 == nx2 and ny1 == ny2 and xmin1 == xmin2 and xmax1 == xmax2
            and ymin1 == ymin2 and ymax1 == ymax2):
        sys.exit("ERROR: grids don't match")

    if not plotfile3 == None:
        (nx3, ny3, nz3) = fsnapshot.fplotfile_get_size(plotfile3)

        time = fsnapshot.fplotfile_get_time(plotfile3)

        (xmin3, xmax3, ymin3, ymax3, zmin3, zmax3) = \
            fsnapshot.fplotfile_get_limits(plotfile3)

        if not (nx1 == nx3 and ny1 == ny3 and xmin1 == xmin3 and xmax1 == xmax3
                and ymin1 == ymin3 and ymax1 == ymax3):
            sys.exit("ERROR: grids don't match")

    x = xmin1 + numpy.arange(
        (nx1), dtype=numpy.float64) * (xmax1 - xmin1) / nx1
    y = ymin1 + numpy.arange(
        (ny1), dtype=numpy.float64) * (ymax1 - ymin1) / ny1

    # read in the components
    data1 = numpy.zeros((nx1, ny1), dtype=numpy.float64)
    (data1, err1) = fsnapshot.fplotfile_get_data_2d(plotfile1, component,
                                                    data1)

    data2 = numpy.zeros((nx2, ny2), dtype=numpy.float64)
    (data2, err2) = fsnapshot.fplotfile_get_data_2d(plotfile2, component,
                                                    data2)

    if not err1 == 0 and err2 == 0:
        sys.exit("ERRORS while reading data")

    data1 = numpy.transpose(data1)
    data2 = numpy.transpose(data2)

    if not plotfile3 == None:
        data3 = numpy.zeros((nx3, ny3), dtype=numpy.float64)
        (data3,
         err3) = fsnapshot.fplotfile_get_data_2d(plotfile3, component, data3)

        if not err3 == 0:
            sys.exit("ERRORS while reading data")

        data3 = numpy.transpose(data3)

    extent = [xmin1, xmax1, ymin1, ymax1]

    #--------------------------------------------------------------------------
    # find data limits, etc.
    #--------------------------------------------------------------------------
    if (not xmin == None):
        extent[0] = xmin

    if (not xmax == None):
        extent[1] = xmax

    if (not ymin == None):
        extent[2] = ymin

    if (not ymax == None):
        extent[3] = ymax

    if (minval == None):
        minval = min(numpy.min(data1), numpy.min(data2))

    if (maxval == None):
        maxval = max(numpy.max(data1), numpy.max(data2))

    if not plotfile3 == None:
        if (minval == None):
            minval = min(minval, numpy.min(data3))

        if (maxval == None):
            maxval = max(maxval, numpy.max(data3))

    levels = numpy.linspace(minval, maxval, ncontours, endpoint=True)

    #--------------------------------------------------------------------------
    # make the figure
    #--------------------------------------------------------------------------

    # left half of data1
    xx = numpy.array(x[0:nx1 / 2 + 1])
    yy = numpy.array(y)
    dd = numpy.array(data1[:, 0:nx1 / 2 + 1])

    cs1 = pylab.contour(xx, yy, dd, ncontours, colors=color1, levels=levels)

    xx = numpy.array(x[nx2 / 2:])
    yy = numpy.array(y)
    dd = numpy.array(data2[:, nx2 / 2:])

    cs2 = pylab.contour(xx, yy, dd, ncontours, colors=color2, levels=levels)

    if not plotfile3 == None:
        xx = numpy.array(x[0:nx1 / 2 + 1])
        yy = numpy.array(y)
        dd = numpy.array(data3[:, 0:nx1 / 2 + 1])

        cs3 = pylab.contour(xx,
                            yy,
                            dd,
                            ncontours,
                            colors=color3,
                            levels=levels)

    # make the labels -- see http://www.scipy.org/Cookbook/Matplotlib/Legend
    # for this technique
    lines = []
    labels = []

    if (not label1 == None):
        line1 = matplotlib.lines.Line2D(range(10),
                                        range(10),
                                        linestyle='-',
                                        color=color1)
        lines.append(line1)
        labels.append(label1)

    if (not label2 == None):
        line2 = matplotlib.lines.Line2D(range(10),
                                        range(10),
                                        linestyle='-',
                                        color=color2)
        lines.append(line2)
        labels.append(label2)

    if not plotfile3 == None:
        if (not label3 == None):
            line3 = matplotlib.lines.Line2D(range(10),
                                            range(10),
                                            linestyle='-',
                                            color=color3)
            lines.append(line3)
            labels.append(label3)

    pylab.legend(lines, labels)

    formatter = matplotlib.ticker.ScalarFormatter(useMathText=True)
    #pylab.clabel(cs, fontsize=9, inline=1)#, fmt=formatter)

    pylab.axis(extent)

    ax = pylab.gca()
    ax.set_aspect("equal")

    fig1 = ax.get_figure()

    ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True))
    ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True))

    pylab.xlabel("x")
    pylab.ylabel("y")

    if (not eps):
        pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.5)
    else:
        pylab.savefig(outFile, bbox_inches='tight', pad_inches=0.5)
            bbox_transform = plt.gcf().transFigure)

fig.suptitle('CTRNN trace comparison between evolved and target networks',fontsize=20)
fig.tight_layout()
plt.subplots_adjust(top=0.85,bottom=0.1)
plt.show()


# And here are the network's responses to a $sin(t/25)$ wave:

# In[114]:

fig,ax = plt.subplots(figsize=(9,6))

lines = []
lines.append(ax.plot(fixedStd['0:200'][0]['neur1'],linewidth=4, label='target')[0])

for key in sortedKeys:
    lines.append(ax.plot(ctrnnStd[key][0]['neur1'],linewidth=3,linestyle='--',label=key)[0])

plt.ylabel('Output')
plt.xlabel('Time')

inputAx = ax.twinx()
line5=inputAx.plot(fixedStd['0:200'][0]['input0'],color='gray',linestyle=':',label='input',linewidth=3)
labs= [line.get_label() for line in lines]

legend = ax.legend(lines,labs,loc="lower right")
legend.set_frame_on(True)
legend.get_frame().set_facecolor('#EEEEEE')
legend.get_frame().set_edgecolor('grey')