text:
"""
Copyright (C) 2019 NVIDIA Corporation.  All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license
(https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
"""

import torch
from torch.utils.data import Dataset

import numpy as np
import time
import utils as utils
import os
import cv2

from datasets.scannet_scene import ScanNetScene
from datasets.plane_dataset import *

class PlaneDataset(PlaneDatasetSingle):
    def __init__(self, options, config, split, random=True, image_only=False, load_semantics=False, load_boundary=False, write_invalid_indices=False):
        super().__init__(options, config, split, random, load_semantics=load_semantics, load_boundary=load_boundary)
        self.image_only = image_only
        self.load_semantics = load_semantics
        self.load_boundary = load_boundary
        self.write_invalid_indices = write_invalid_indices
        return

    def __getitem__(self, index):
        t = int(time.time() * 1000000)
        np.random.seed(((t & 0xff000000) >> 24) +
                       ((t & 0x00ff0000) >> 8) +
                       ((t & 0x0000ff00) << 8) +
                       ((t & 0x000000ff) << 24))
        if self.random:
            index = np.random.randint(len(self.sceneImageIndices))
        else:
            index = index % len(self.sceneImageIndices)
            if self.options.testingIndex >= 0 and index != self.options.testingIndex:
                return 0
            pass

        sceneIndex, imageIndex = self.sceneImageIndices[index]
        scene = self.scenes[sceneIndex]
        # print("DBG11")

        while True:
            if self.random:
                index = np.random.randint(len(self.sceneImageIndices))
            else:
                index = (index + 1) % len(self.sceneImageIndices)
                pass
            # print("DBG12")
            sceneIndex, imageIndex = self.sceneImageIndices[index]
            scene = self.scenes[sceneIndex]
            # print("DBG12_1")
            if imageIndex + self.options.frameGap < len(scene.imagePaths):
                imageIndex_2 = imageIndex + self.options.frameGap
            else:
                imageIndex_2 = imageIndex - self.options.frameGap
                pass
            # print("DBG12_2")
            if (sceneIndex * 10000 + imageIndex_2) in self.invalid_indices:
                continue

            try:
                # print("DBG12_3")
                image_1, planes_1, plane_info_1, segmentation_1, depth_1, camera_1, extrinsics_1, semantics_1 = scene[imageIndex]
                # print("DBG12_4")
            except Exception as e:
                print("ERROR : "+str(e))
                # print(self.options)
                if self.write_invalid_indices:
                    print('invalid')
                    print(str(index) + ' ' + str(sceneIndex) + ' ' + str(imageIndex) + '\n', file=open(self.dataFolder + '/invalid_indices_' + self.split + '.txt', 'a'))
                    return 1
                continue

            if self.write_invalid_indices:
                return 0

            info_1 = [image_1, planes_1, plane_info_1, segmentation_1, depth_1, camera_1, extrinsics_1, semantics_1]

            try:
                image_2, planes_2, plane_info_2, segmentation_2, depth_2, camera_2, extrinsics_2, semantics_2 = scene[imageIndex_2]
            except:
                continue
            info_2 = [image_2, planes_2, plane_info_2, segmentation_2, depth_2, camera_2, extrinsics_2, semantics_2]
            break
        if self.image_only:
            data_pair = []
            for info in [info_1, info_2]:
                image, planes, plane_info, segmentation, depth, camera, extrinsics, semantics = info
                image = cv2.resize(image, (depth.shape[1], depth.shape[0]))
                image, window, scale, padding = utils.resize_image(
                    image,
                    min_dim=self.config.IMAGE_MAX_DIM,
                    max_dim=self.config.IMAGE_MAX_DIM,
                    padding=self.config.IMAGE_PADDING)

                image = utils.mold_image(image.astype(np.float32), self.config)
                image = torch.from_numpy(image.transpose(2, 0, 1)).float()
                depth = np.concatenate([np.zeros((80, 640)), depth, np.zeros((80, 640))], axis=0)
                data_pair += [image, depth.astype(np.float32), camera]
                continue
            return data_pair
        data_pair = []
        extrinsics_pair = []
        for info in [info_1, info_2]:
            # print("DBG13")
            image, planes, plane_info, segmentation, depth, camera, extrinsics, semantics = info

            image = cv2.resize(image, (depth.shape[1], depth.shape[0]))

            instance_masks = []
            class_ids = []
            parameters = []

            if len(planes) > 0:
                if 'joint' in self.config.ANCHOR_TYPE:
                    distances = np.linalg.norm(np.expand_dims(planes, 1) - self.config.ANCHOR_PLANES, axis=-1)
                    plane_anchors = distances.argmin(-1)
                elif self.config.ANCHOR_TYPE == 'Nd':
                    plane_offsets = np.linalg.norm(planes, axis=-1)
                    plane_normals = planes / np.expand_dims(plane_offsets, axis=-1)
                    distances_N = np.linalg.norm(np.expand_dims(plane_normals, 1) - self.config.ANCHOR_NORMALS, axis=-1)
                    normal_anchors = distances_N.argmin(-1)
                    distances_d = np.abs(np.expand_dims(plane_offsets, -1) - self.config.ANCHOR_OFFSETS)
                    offset_anchors = distances_d.argmin(-1)
                elif 'normal' in self.config.ANCHOR_TYPE or self.config.ANCHOR_TYPE == 'patch':
                    plane_offsets = np.linalg.norm(planes, axis=-1)
                    plane_normals = planes / np.expand_dims(plane_offsets, axis=-1)
                    distances_N = np.linalg.norm(np.expand_dims(plane_normals, 1) - self.config.ANCHOR_NORMALS, axis=-1)
                    normal_anchors = distances_N.argmin(-1)
                    pass
                pass

            for planeIndex, plane in enumerate(planes):
                # print("DBG14")
                m = segmentation == planeIndex
                if m.sum() < 1:
                    continue
                instance_masks.append(m)
                if self.config.ANCHOR_TYPE == 'none':
                    class_ids.append(1)
                    parameters.append(np.concatenate([plane, np.zeros(1)], axis=0))
                elif 'joint' in self.config.ANCHOR_TYPE:
                    class_ids.append(plane_anchors[planeIndex] + 1)
                    residual = plane - self.config.ANCHOR_PLANES[plane_anchors[planeIndex]]
                    parameters.append(np.concatenate([residual, np.array([0, plane_info[planeIndex][-1]])], axis=0))
                elif self.config.ANCHOR_TYPE == 'Nd':
                    class_ids.append(normal_anchors[planeIndex] * len(self.config.ANCHOR_OFFSETS) + offset_anchors[planeIndex] + 1)
                    normal = plane_normals[planeIndex] - self.config.ANCHOR_NORMALS[normal_anchors[planeIndex]]
                    offset = plane_offsets[planeIndex] - self.config.ANCHOR_OFFSETS[offset_anchors[planeIndex]]
                    parameters.append(np.concatenate([normal, np.array([offset])], axis=0))
                elif 'normal' in self.config.ANCHOR_TYPE:
                    class_ids.append(normal_anchors[planeIndex] + 1)
                    normal = plane_normals[planeIndex] - self.config.ANCHOR_NORMALS[normal_anchors[planeIndex]]
                    parameters.append(np.concatenate([normal, np.array([plane_info[planeIndex][-1]])], axis=0))
                else:
                    assert(False)
                    pass
                continue

            parameters = np.array(parameters)
            mask = np.stack(instance_masks, axis=2)
            class_ids = np.array(class_ids, dtype=np.int32)

            image, image_metas, gt_class_ids, gt_boxes, gt_masks, gt_parameters = load_image_gt(self.config, index, image, depth, mask, class_ids, parameters, augment=self.split == 'train')

            ## RPN Targets
            rpn_match, rpn_bbox = build_rpn_targets(image.shape, self.anchors,
                                                    gt_class_ids, gt_boxes, self.config)

            ## If more instances than fits in the array, sub-sample from them.
            if gt_boxes.shape[0] > self.config.MAX_GT_INSTANCES:
                ids = np.random.choice(
                    np.arange(gt_boxes.shape[0]), self.config.MAX_GT_INSTANCES, replace=False)
                gt_class_ids = gt_class_ids[ids]
                gt_boxes = gt_boxes[ids]
                gt_masks = gt_masks[:, :, ids]
                gt_parameters = gt_parameters[ids]
                pass

            ## Add to batch
            rpn_match = rpn_match[:, np.newaxis]
            image = utils.mold_image(image.astype(np.float32), self.config)

            depth = np.concatenate([np.zeros((80, 640)), depth, np.zeros((80, 640))], axis=0)
            segmentation = np.concatenate([np.full((80, 640), fill_value=-1, dtype=np.int32), segmentation, np.full((80, 640), fill_value=-1, dtype=np.int32)], axis=0)
            # print("DBG15")
            ## Convert
            image = torch.from_numpy(image.transpose(2, 0, 1)).float()
            image_metas = torch.from_numpy(image_metas)
            rpn_match = torch.from_numpy(rpn_match)
            rpn_bbox = torch.from_numpy(rpn_bbox).float()
            gt_class_ids = torch.from_numpy(gt_class_ids)
            gt_boxes = torch.from_numpy(gt_boxes).float()
            gt_masks = torch.from_numpy(gt_masks.astype(np.float32)).transpose(1, 2).transpose(0, 1)
            plane_indices = torch.from_numpy(gt_parameters[:, -1]).long()
            gt_parameters = torch.from_numpy(gt_parameters[:, :-1]).float()
            data_pair += [image, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, depth.astype(np.float32), extrinsics.astype(np.float32), planes.astype(np.float32), segmentation, plane_indices]

            if self.load_semantics or self.load_boundary:
                semantics = np.concatenate([np.full((80, 640), fill_value=-1, dtype=np.int32), semantics, np.full((80, 640), fill_value=-1, dtype=np.int32)], axis=0)
                data_pair[-1] = semantics
                pass

            extrinsics_pair.append(extrinsics)
            continue
        # print("DBG16")
        transformation = np.matmul(extrinsics_pair[1], np.linalg.inv(extrinsics_pair[0]))
        rotation = transformation[:3, :3]
        translation = transformation[:3, 3]
        axis, angle = utils.rotationMatrixToAxisAngle(rotation)

        data_pair.append(np.concatenate([translation, axis, np.array([angle])], axis=0).astype(np.float32))

        correspondence = np.zeros((len(info_1[1]), len(info_2[1])), dtype=np.float32)
        for planeIndex_1, plane_info_1 in enumerate(info_1[2]):
            for planeIndex_2, plane_info_2 in enumerate(info_2[2]):
                if plane_info_1[-1] == plane_info_2[-1]:
                    correspondence[planeIndex_1][planeIndex_2] = 1
                    pass
                continue
            continue
        data_pair.append(info_1[1].astype(np.float32))
        data_pair.append(info_2[1].astype(np.float32))
        data_pair.append(correspondence)
        data_pair.append(camera.astype(np.float32))
        # print("DBG17")
        return data_pair


text:
import sys, os
base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../')
sys.path.append(base_dir)
sys.path.append(os.path.join(base_dir, 'design_search'))

from copy import deepcopy
import numpy as np

from design_search import presimulate, simulate, build_normalized_robot, make_initial_graph
import pyrobotdesign as rd

def make_sim_fn(task, robot, robot_init_pos):
    sim = rd.BulletSimulation(task.time_step)
    task.add_terrain(sim)
    # Rotate 180 degrees around the y axis, so the base points to the right
    sim.add_robot(robot, robot_init_pos, rd.Quaterniond(0.0, 0.0, 1.0, 0.0))
    return sim

def finalize_robot(robot):
    for link in robot.links:
        link.label = ""
        link.joint_label = ""
        if link.shape == rd.LinkShape.NONE:
            link.shape = rd.LinkShape.CAPSULE
            link.length = 0.1
            link.radius = 0.025
            link.color = [1.0, 0.0, 1.0]
        if link.joint_type == rd.JointType.NONE:
            link.joint_type = rd.JointType.FIXED
            link.joint_color = [1.0, 0.0, 1.0]

def build_robot(grammar_file = "grammar_apr30.dot", rule_sequence="0,7,1,13,1,2,16,12,13,6,4,19,4,17,5,3,2,16,4,5,18,9,8,9,9,8"):
    if not os.path.exists(grammar_file):
        data_dir = os.path.abspath(os.path.join(__file__, '../../../../data/designs/'))
        grammar_file = os.path.join(data_dir, grammar_file)
    graphs = rd.load_graphs(grammar_file)
    rules = [rd.create_rule_from_graph(g) for g in graphs]

    # rule_sequence = [int(s.strip(",")) for s in args.rule_sequence]
    # change rule_sequence type:
    rule_sequence = [int(s) for s in rule_sequence.split(",")]

    graph = make_initial_graph()
    for r in rule_sequence:
        matches = rd.find_matches(rules[r].lhs, graph)
        if matches:
            graph = rd.apply_rule(rules[r], graph, matches[0])

    robot = build_normalized_robot(graph)
    finalize_robot(robot)

    return robot

def get_robot_state(sim, robot_id):
    base_tf = np.zeros((4, 4), order = 'f')
    sim.get_link_transform(robot_id, 0, base_tf)
    base_R = deepcopy(base_tf[0:3, 0:3])
    base_pos = deepcopy(base_tf[0:3, 3])

    # anguler velocity first and linear velocity next
    base_vel = np.zeros(6, order = 'f')
    sim.get_link_velocity(robot_id, 0, base_vel)

    n_dofs = sim.get_robot_dof_count(robot_id)

    joint_pos = np.zeros(n_dofs, order = 'f')
    sim.get_joint_positions(robot_id, joint_pos)

    joint_vel = np.zeros(n_dofs, order = 'f')
    sim.get_joint_velocities(robot_id, joint_vel)

    state = np.hstack((base_R.flatten(), base_pos, base_vel, joint_pos, joint_vel))

    return state

def presimulate(robot):
    """Find an initial position that will place the robot on the ground behind the
    x=0 plane, and check if the robot collides in its initial configuration."""
    temp_sim = rd.BulletSimulation()
    temp_sim.add_robot(robot, np.zeros(3), rd.Quaterniond(0.0, 0.0, 1.0, 0.0))
    temp_sim.step()
    robot_idx = temp_sim.find_robot_index(robot)
    lower = np.zeros(3)
    upper = np.zeros(3)
    temp_sim.get_robot_world_aabb(robot_idx, lower, upper)
    return [-upper[0], -lower[1], 0.0], temp_sim.robot_has_collision(robot_idx)

ext: py

max_stars_repo_name: liusida/RoboGrammar

max_issues_repo_name: liusida/RoboGrammar

max_forks_repo_name: liusida/RoboGrammar

reason: import numpy


text:
# Created by Qingzhi Ma at 20/02/2020
# All right reserved
# Department of Computer Science
# the University of Warwick
# Q.Ma.2@warwick.ac.uk
import re
from itertools import chain

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
from matplotlib.pyplot import axis, legend

def read_results(file, b_remove_null=True, split_char="\s"):
    """read the group by value and the corresponding aggregate within
    a given range, used to compare the accuracy.the

    Output: a dict contating the

    Args:
        file (file): path to the file
    """

    key_values = {}
    with open(file) as f:
        # print("Start reading file " + file)
        index = 1
        for line in f:
            # ignore empty lines

            if line != "":
                if line.strip():
                    # print(line)
                    key_value = line.replace(
                        "(", " ").replace(")", " ").replace(";", "").replace("\n", "").replace(" ","")  # .replace(",", "")
                    # print(key_value)
                    # self.logger.logger.info(key_value)
                    key_value = re.split(split_char, key_value)
                    # print("key_value", key_value)
                    # print(','.join(key_value[:-1]))

                    for i in range(2):
                        if key_value[i] == 'NULL':
                            key_value[i] = ""
                    # print(line)
                    # print(key_value)
                    if key_value[-1]=="":
                        key_value[-1]="0.0"
                    key_values[','.join(key_value[:-1])] = float(key_value[-1])

    # if ('NULL' in key_values) and b_remove_null:
    #     key_values.pop('NULL', None)
    # if ('0' in key_values) and b_remove_null:
    #     key_values.pop('0', None)

    # cnt=0
    # for key in key_values:
    #     cnt+=1
    #     print(key, key_values[key])
    #     if (cnt==100):
    #         return

    return key_values

def compare_dicts(true: dict, pred: dict) -> dict:
    """compare the error between two dicts

    Args:
        true (dict): truth
        pred (dict): prediction

    Returns:
        dict: relative error
    """
    res = []
    cnt=0
    for key in true:
        if key not in pred:
            pred[key]=0
            cnt+=1
        res.append(abs(((true[key]-pred[key])/true[key])))
        # print(key)
    # print(res)
    # plt.hist(res,bins=50)
    # plt.show()
    if cnt!=0:
        print("there are ", cnt, " missing values.")
    return res

def plot_count():
    """ plot count
    """
    mdn = read_results("mdn40/count1_ss1t_gg4.txt", split_char=",")
    truth = read_results("mdn40/count1truth.txt")
    kde = read_results("mdn40/count1gg10.txt")
    res0 = compare_dicts(truth, mdn)
    res1 = compare_dicts(truth, kde)
    res0 = [res * 100 for res in res0]
    res1 = [res * 100 for res in res1]
    plt.hist(res0, bins=50, color="r", alpha=0.2, label="MDN")
    plt.hist(res1, bins=50, color="b", alpha=0.6, label="DBEst")
    plt.legend()
    plt.title("Histogram of relative error for COUNT")
    plt.ylabel("Frequency")
    plt.xlabel("Relative error")
    fmt = '%.2f%%'
    xticks = mtick.FormatStrFormatter(fmt)
    plt.gca().xaxis.set_major_formatter(xticks)
    # plt.gca().xaxis.set_major_formatter(mtick.PercentFormatter())

    plt.text(10, 3, "MDN error " + str(sum(res0) / len(res0)) + "%")
    plt.text(10, 2, "DBEst error " + str(sum(res1) / len(res1)) + "%")
    plt.show()

def plt501():
    """ plot for 501 group.
    """
    mdn = read_results("mdn501/sum1_ss1t_gg4.txt", split_char=",")
    truth = read_results("groundtruth/sum1.result")
    kde = read_results("DBEst/sum1.txt")
    res0 = compare_dicts(truth, mdn)
    res1 = compare_dicts(truth, kde)
    res0 = [res * 100 for res in res0]
    res1 = [res * 100 for res in res1]
    plt.hist(res0, bins=50, color="r", alpha=0.2, label="MDN")
    plt.hist(res1, bins=50, color="b", alpha=0.6, label="DBEst")
    plt.legend()
    plt.title("Histogram of relative error for COUNT")
    plt.ylabel("Frequency")
    plt.xlabel("Relative error")
    fmt = '%.2f%%'
    xticks = mtick.FormatStrFormatter(fmt)
    plt.gca().xaxis.set_major_formatter(xticks)
    # plt.gca().xaxis.set_major_formatter(mtick.PercentFormatter())

    plt.text(10, 3, "MDN error " + str(sum(res0) / len(res0)) + "%")
    plt.text(10, 2, "DBEst error " + str(sum(res1) / len(res1)) + "%")

    print("MDN error " + str(sum(res0) / len(res0)) + "%")
    print("DBEst error " + str(sum(res1) / len(res1)) + "%")

    plt.show()

def plt_workload(agg_func="avg", suffix="_ss1t_gg4.txt", b_plot=True, b_merge_result_for_group=False, b_two_methods=False):
    mdn_errors = []
    kde_errors = []
    # prapare the files.
    for i in range(1, 11):
        prefix = agg_func+str(i)
        # print(prefix)
        truth = read_results(
            "experiments/results/groundtruth/1t2cols/"+prefix+".txt")
        # print("experiments/results/DBEst/1t2cols/2_5g/" +
        #                    prefix+suffix)
        mdn = read_results("experiments/results/mdn/1t2cols/20g/" +
                           prefix+suffix, split_char=",")

        mdn_error = compare_dicts(truth, mdn)
        mdn_errors.append(mdn_error)
        if b_two_methods:
            kde = read_results(
                "experiments/results/deepdb/10g/"+prefix+".txt", split_char=",")
            kde_error = compare_dicts(truth, kde)
            kde_errors.append(kde_error)

    # mdn_errors = mdn_errors*100
    # if b_two_methods:
    #     kde_errors = kde_errors*100

    if b_plot:
        if b_merge_result_for_group:
            mdn_errors = np.array(mdn_errors)
            mdn_errors = np.mean(mdn_errors, axis=0)

            if b_two_methods:
                kde_errors = np.array(kde_errors)
                kde_errors = np.mean(kde_errors, axis=0)
        else:
            mdn_errors = np.array(list(chain.from_iterable(mdn_errors)))
            if b_two_methods:
                kde_errors = np.array(list(chain.from_iterable(kde_errors)))

        mdn_errors = mdn_errors*100
        if b_two_methods:
            kde_errors = kde_errors*100

        plt.hist(mdn_errors, bins=50, color="r", alpha=0.2, label="DBEst-MDN")
        if b_two_methods:
            plt.hist(kde_errors, bins=50, color="b", alpha=0.6, label="DBEst")
        plt.legend()
        plt.title("Histogram of relative error for " + agg_func.upper())
        plt.ylabel("Frequency")
        plt.xlabel("Relative error")
        fmt = '%.2f%%'
        xticks = mtick.FormatStrFormatter(fmt)
        plt.gca().xaxis.set_major_formatter(xticks)
        # plt.gca().xaxis.set_major_formatter(mtick.PercentFormatter())

        plt.text(10, 150, "DBEst-MDN error " +
                 "{0:.2f}".format(np.mean(mdn_errors)) + "%")
        if b_two_methods:
            plt.text(10, 180, "DBEst error " +
                     "{0:.2f}".format(np.mean(kde_errors)) + "%")

        plt.show()
    print(agg_func)
    print("MDN error " + str(np.mean(mdn_errors)) + "%")
    if b_two_methods:
        print("DBEst error " + str(np.mean(kde_errors)) + "%")
    if b_two_methods:
        return np.mean(mdn_errors), np.mean(kde_errors)
    else:
        return np.mean(mdn_errors)

def plt_501_bar_chart_error(suffix=".txt"):
    fontsize = 10
    mdn_count, kde_count = plt_workload(
        agg_func="count", suffix=suffix, b_plot=False)
    mdn_sum, kde_sum = plt_workload(
        agg_func="sum", suffix=suffix, b_plot=False)
    mdn_avg, kde_avg = plt_workload(
        agg_func="avg", suffix=suffix, b_plot=False)
    labels = ["COUNT", "SUM", "AVG", "OVERALL"]

    mdn = [mdn_count, mdn_sum, mdn_avg]
    kde = [kde_count, kde_sum, kde_avg]
    mdn.append(sum(mdn)/3.0)
    kde.append(sum(kde)/3.0)
    mdn = np.array(mdn)*100
    kde = np.array(kde)*100

    x_points = np.arange(len(labels))
    width = 0.35
    fig, ax = plt.subplots(figsize=(4.5, 3.5))
    rect_mdn = ax.bar(x_points-width/2, mdn, width,
                      label="DBEst-MDN", alpha=0.5)
    rect_kde = ax.bar(x_points+width/2, kde, width, label="DBEst", alpha=0.8)
    ax.set_xticks(x_points)
    ax.set_xticklabels(labels)
    ax.yaxis.set_major_formatter(mtick.PercentFormatter(decimals=1))
    ax.set_ylabel("Relative Error (%)", fontsize=fontsize)
    ax.set_xlabel("Aggregate function", fontsize=fontsize)
    ax.legend(loc="lower left", fontsize=fontsize)
    autolabel(rect_kde, ax)
    autolabel(rect_mdn, ax)

    # ax.set_ylim(0, 5)

    fig.tight_layout()
    plt.show()

def autolabel(rects, ax):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{0:.2f}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

if __name__ == "__main__":

    plt_workload(agg_func="count", suffix=".txt", b_plot=False,
                 b_merge_result_for_group=False, b_two_methods=False)
    plt_workload(agg_func="sum", suffix=".txt", b_plot=False,
                 b_merge_result_for_group=False, b_two_methods=False)
    plt_workload(agg_func="avg", suffix=".txt", b_plot=False,
                 b_merge_result_for_group=False, b_two_methods=False)

max_stars_repo_name: qingzma/DBEstClient

max_issues_repo_name: Forever-MrX/DBEstClient

max_forks_repo_name: qingzma/DBEstClient


text:
import difflib,os
import numpy as np
import scipy.io.wavfile as wav
from tqdm import tqdm
from scipy.fftpack import fft
from random import shuffle
from keras import backend as K
from utils import audio
from utils.mylogger import log
from hparams import hparams as hp
import librosa

class DataFeeder():
    '''
    属性：
        wav_lst :    ['data_aishell/wav/dev/S0724/BAC009S0724W0121.wav' , ...]
        pny_lst :    [['guang3','zhou1','shi4','fang2','di4','chan3','zhong1','jie4','xie2','hui4','fen1','xi1'] , ...]
        han_lst :    ['广州市房地产中介协会分析']
        am_vocab :   ['_', 'yi1', ...]
        pny_vocab :  ['<PAD>', 'yi1', ...]
        han_vocab :  ['<PAD>', '一', ...]
    '''
    def __init__(self, args):
        self.data_type = args.data_type  # train test dev
        self.data_path = args.data_path  # 存放数据的顶层目录
        self.thchs30 = args.thchs30      # 是否使用thchs30
        self.aishell = args.aishell
        self.prime = args.prime
        self.stcmd = args.stcmd
        self.data_length = args.data_length # 使用多少数据训练 None表示全部
        self.batch_size = args.batch_size   # batch大小
        self.shuffle = args.shuffle         # 是否打乱训练数据
        self.feature_type = args.feature_type
        self.AM = args.AM
        self.LM = args.LM
        self.source_init()

    def source_init(self):
        print('get source list...')
        read_files = []
        if self.data_type == 'train':
            if self.thchs30 == True:
                read_files.append('thchs_train.txt')
            if self.aishell == True:
                read_files.append('aishell_train.txt')
            if self.prime == True:
                read_files.append('prime.txt')
            if self.stcmd == True:
                read_files.append('stcmd.txt')
        elif self.data_type == 'dev':
            if self.thchs30 == True:
                read_files.append('thchs_dev.txt')
            if self.aishell == True:
                read_files.append('aishell_dev.txt')
        elif self.data_type == 'test':
            if self.thchs30 == True:
                read_files.append('thchs_test.txt')
            if self.aishell == True:
                read_files.append('aishell_test.txt')
        self.wav_lst = []
        self.pny_lst = []
        self.han_lst = []
        for file in read_files:
            print('load ', file, ' data...')
            sub_file = 'datasets/' + file
            with open(sub_file, 'r', encoding='utf8') as f:
                data = f.readlines()
            for line in tqdm(data):
                wav_file, pny, han = line.split('\t')
                self.wav_lst.append(wav_file)
                self.pny_lst.append(pny.split(' '))
                self.han_lst.append(han.strip('\n'))
        if self.data_length:
            self.wav_lst = self.wav_lst[:self.data_length]
            self.pny_lst = self.pny_lst[:self.data_length]
            self.han_lst = self.han_lst[:self.data_length]
        if self.AM:
            print('make am vocab...')
            self.am_vocab = self.mk_am_vocab(self.pny_lst)
        if self.LM:
            print('make lm pinyin vocab...')
            self.pny_vocab = self.mk_lm_pny_vocab(self.pny_lst)
            print('make lm hanzi vocab...')
            self.han_vocab = self.mk_lm_han_vocab(self.han_lst)

    def get_am_batch(self):
        # shuffle只是对index打乱，没有对原始数据打乱，所以wav_lst[i]和pny_lst[i]还是一一对应的
        shuffle_list = [i for i in range(len(self.wav_lst))]
        while 1:
            if self.shuffle == True:
                shuffle(shuffle_list)
            # len(self.wav_lst) // self.batch_size的值 表示一个epoch里有多少step才能把所有数据过一遍
            for i in range(len(self.wav_lst) // self.batch_size):
                wav_data_lst = []    # wav_data_lst里放的是batch_size个频谱图 wav_lst里放的是音频文件地址
                label_data_lst = []
                begin = i * self.batch_size
                end = begin + self.batch_size
                sub_list = shuffle_list[begin:end]
                for index in sub_list:
                    # TODO：计算频谱图
                    if self.feature_type == 'spec':
                        fbank,n_frames = compute_spec(self.data_path + self.wav_lst[index])
                    elif self.feature_type == 'mel':
                        fbank, n_frames = compute_mel2(self.data_path + self.wav_lst[index])
                    else:
                        fbank, n_frames = compute_mfcc2(self.data_path + self.wav_lst[index])
                    # TODO：把语谱图时间维度pad成8的倍数
                    pad_fbank = np.zeros((fbank.shape[0] // 8 * 8 + 8, fbank.shape[1])) # 保证是8的倍数，因为几层cnn后要求维度能被8整除
                    pad_fbank[:fbank.shape[0], :] = fbank
                    label = self.pny2id(self.pny_lst[index], self.am_vocab)
                    label_ctc_len = self.ctc_len(label)
                    # 假设num_mel =90 那么最多只能预测十个字的句子？
                    if pad_fbank.shape[0] // 8 >= label_ctc_len:# ctc要求解码长度必须小于等于原始输入长度
                        wav_data_lst.append(pad_fbank)
                        label_data_lst.append(label)
                    else:
                        print(self.data_path + self.wav_lst[index])
                        raise Exception('data not allowed')
                # TODO：对语谱图时间维度进行第二次pad，pad成本次batch中最长的长度
                pad_wav_data, input_length = self.wav_padding(wav_data_lst)
                pad_label_data, label_length = self.label_padding(label_data_lst)
                inputs = {'the_inputs': pad_wav_data,
                          'the_labels': pad_label_data,
                          'input_length': input_length.reshape(-1,1),
                          # 注意这个input_length不是原始频谱图的长度而是经过几层cnn后隐层输出的长度（传给ctc）
                          # 而且是语谱图第一次pad后的长度//8而不是第二次最长pad后//8，尽量保证靠近真实长度//8的值
                          'label_length': label_length.reshape(-1,1),# batch中的每个句子的真实长度
                          }
                # outputs = {'ctc': np.zeros(pad_wav_data.shape[0], )} # 没看懂为什么有这个
                print('genarate one batch mel data')
                yield inputs
        pass

    def get_lm_batch(self):
        batch_num = len(self.pny_lst) // self.batch_size
        for k in range(batch_num):
            begin = k * self.batch_size
            end = begin + self.batch_size
            input_batch = self.pny_lst[begin:end]
            label_batch = self.han_lst[begin:end]
            max_len = max([len(line) for line in input_batch])
            input_batch = np.array(
                [self.pny2id(line, self.pny_vocab) + [0] * (max_len - len(line)) for line in input_batch])
            label_batch = np.array(
                [self.han2id(line, self.han_vocab) + [0] * (max_len - len(line)) for line in label_batch])
            yield (input_batch, label_batch)

    def pny2id(self, line, vocab):
        ids = []
        for pny in line :
            if pny in vocab:
                ids.append(vocab.index(pny))
            else:
                if 'UnkTok' in vocab:
                    ids.append(vocab.index('UnkTok'))
                else:
                    ids.append(vocab.index('_')-1)

        return ids

    def han2id(self, line, vocab):
        ids = []
        for han in line:
            if han in vocab:
                ids.append(vocab.index(han))
            else:
                ids.append(vocab.index('UnkTok'))

        return ids

    def wav_padding(self, wav_data_lst):
        # wav_data_lst里data是pad_fbank不是原始fbank所以后面//8一定能整除，这个训练的时候为什么不能直接在网络中获取呢？？
        wav_lens = [len(data) for data in wav_data_lst] # len(data)实际上就是求语谱图的第一维的长度，也就是n_frames
        # print(wav_lens)
        wav_max_len = max(wav_lens)
        wav_lens = np.array([leng // 8 for leng in wav_lens])
        new_wav_data_lst = np.zeros((len(wav_data_lst), wav_max_len, hp.num_mels, 1)) # 之前固定200是n-fft，len(wav_data_lst)是batch_size
        for i in range(len(wav_data_lst)):
            new_wav_data_lst[i, :wav_data_lst[i].shape[0], :, 0] = wav_data_lst[i]
        # print('new_wav_data_lst',new_wav_data_lst.shape,wav_lens.shape)
        return new_wav_data_lst, wav_lens

    def label_padding(self, label_data_lst):
        label_lens = np.array([len(label) for label in label_data_lst])
        max_label_len = max(label_lens)
        new_label_data_lst = np.zeros((len(label_data_lst), max_label_len))
        for i in range(len(label_data_lst)):
            new_label_data_lst[i][:len(label_data_lst[i])] = label_data_lst[i]
        return new_label_data_lst, label_lens

    def mk_am_vocab(self, data):
        vocab = []
        for line in tqdm(data):
            line = line
            for pny in line:
                if pny not in vocab:
                    vocab.append(pny)
        vocab.append('_')
        return vocab

    def mk_lm_pny_vocab(self, data):
        vocab = ['<PAD>']
        for line in tqdm(data):
            for pny in line:
                if pny not in vocab:
                    vocab.append(pny)
        vocab.append('UnkTok')
        return vocab

    def mk_lm_han_vocab(self, data):
        vocab = ['<PAD>']
        for line in tqdm(data):
            line = ''.join(line.split(' '))#能将‘你好吗’直接拆成三个字
            for han in line:
                if han not in vocab:
                    vocab.append(han)
        vocab.append('UnkTok')
        return vocab

    def ctc_len(self, label):
        add_len = 0
        label_len = len(label)
        for i in range(label_len - 1):
            if label[i] == label[i + 1]:
                add_len += 1 # 这里+1是因为ctc会在重复字符之间填充
        return label_len + add_len

class DataFeeder_wavnet(DataFeeder):
    def __init__(self,args):
        super().__init__(args)

    def get_am_batch(self):
        shuffle_list = [i for i in range(len(self.wav_lst))]
        while 1:
            if self.shuffle == True:
                shuffle(shuffle_list)
            # len(self.wav_lst) // self.batch_size的值 表示一个epoch里有多少step才能把所有数据过一遍
            for i in range(len(self.wav_lst) // self.batch_size):
                wav_data_lst = []  # wav_data_lst里放的是batch_size个频谱图 wav_lst里放的是音频文件地址
                label_data_lst = []
                begin = i * self.batch_size
                end = begin + self.batch_size
                sub_list = shuffle_list[begin:end]
                for index in sub_list:
                    # TODO：计算频谱图
                    if self.feature_type == 'spec':
                        fbank, n_frames = compute_spec(self.data_path + self.wav_lst[index])
                    elif self.feature_type == 'mel':
                        fbank, n_frames = compute_mel(self.data_path + self.wav_lst[index])
                    else:
                        fbank, n_frames = compute_mfcc2(self.data_path + self.wav_lst[index])

                    pad_fbank = fbank
                    label = self.pny2id(self.pny_lst[index], self.am_vocab)
                    label_ctc_len = self.ctc_len(label)
                    # 假设num_mel =90 那么最多只能预测十个字的句子？
                    if pad_fbank.shape[0] >= label_ctc_len:  # ctc要求解码长度必须小于等于原始输入长度
                        wav_data_lst.append(pad_fbank)
                        label_data_lst.append(label)
                    else:
                        print(self.data_path + self.wav_lst[index])
                        raise Exception('data not allowed')
                # TODO：对mfcc时间维度进行pad，pad成本次batch中最长的长度
                pad_wav_data, input_length = self.wav_padding(wav_data_lst)
                pad_label_data, label_length = self.label_padding(label_data_lst)
                inputs = {'the_inputs': pad_wav_data,
                          'the_labels': pad_label_data,
                          'input_length': input_length.reshape(-1, 1),
                          # 注意这个input_length不是原始频谱图的长度而是经过几层cnn后隐层输出的长度（传给ctc）
                          # 而且是语谱图第一次pad后的长度//8而不是第二次最长pad后//8，尽量保证靠近真实长度//8的值
                          'label_length': label_length.reshape(-1, 1),  # batch中的每个句子的真实长度
                          }
                # outputs = {'ctc': np.zeros(pad_wav_data.shape[0], )} # 没看懂为什么有这个
                print('genarate one batch mfcc data')
                yield inputs
        pass

    def wav_padding(self, wav_data_lst):
        wav_lens = np.asarray([len(data) for data in wav_data_lst])  # len(data)实际上就是求语谱图的第一维的长度，也就是n_frames
        wav_max_len = max(wav_lens)
        new_wav_data_lst = np.zeros(
            (len(wav_data_lst), wav_max_len, hp.num_mfccs))  # 之前固定200是n-fft，len(wav_data_lst)是batch_size
        for i in range(len(wav_data_lst)):
            new_wav_data_lst[i, :wav_data_lst[i].shape[0], :] = wav_data_lst[i]
        return new_wav_data_lst, wav_lens

class DataFeeder_transformer(DataFeeder):
    def __init__(self,args):
        super().__init__(args)

    def mk_lm_han_vocab(self,data):
        vocab = ['<PAD>','<GO>','<EOS>']
        for line in tqdm(data):
            line = ''.join(line.split(' '))  # 能将‘你好吗’直接拆成三个字
            for han in line:
                if han not in vocab:
                    vocab.append(han)
        return vocab

    def get_lm_batch(self):
        encoder_inputs = [[self.pny_vocab.index(word) for word in line] for line in self.pny_lst]
        decoder_inputs = [[self.han_vocab.index('<GO>')] + [self.han_vocab.index(word) for word in ''.join(line.split(' '))] for line in self.han_lst]
        decoder_targets = [[self.han_vocab.index(word) for word in ''.join(line.split(' '))] + [self.han_vocab.index('<EOS>')] for line in self.han_lst]

        batch_num = len(encoder_inputs) // self.batch_size
        for k in range(batch_num):
            begin = k * self.batch_size
            end = begin + self.batch_size
            en_input_batch = encoder_inputs[begin:end]
            de_input_batch = decoder_inputs[begin:end]
            de_label_batch = decoder_targets[begin:end]
            max_en_len = max([len(line) for line in en_input_batch])
            max_de_len = max([len(line) for line in de_input_batch])
            en_input_batch = np.array(
                [line + [0] * (max_en_len - len(line)) for line in en_input_batch]
            )
            de_input_batch = np.array(
                [line + [0] * (max_de_len - len(line)) for line in de_input_batch]
            )
            de_label_batch = np.array(
                [line + [0] * (max_de_len - len(line)) for line in de_label_batch]
            )
            yield en_input_batch, de_input_batch, de_label_batch

def compute_mfcc2(file):
    wav = audio.load_wav(file)
    # mfcc = p.mfcc(wav,numcep=hp.num_mfccs) # n_frames*n_mfcc
    mfcc = librosa.feature.mfcc(wav,sr=hp.sample_rate,n_mfcc=hp.num_mfccs)  # n_mfcc * n_frames
    n_frames = mfcc.shape[1]
    return (mfcc.T,n_frames)

def compute_mfcc(file):
    wav = audio.load_wav(file)
    mfcc = audio.mfcc(wav).astype(np.float32)
    n_frames = mfcc.shape[1]
    return (mfcc.T,n_frames)

def compute_mel2(file):
    wav = audio.load_wav(file)
    # mel = audio.melspectrogram(wav).astype(np.float32)
    mel = librosa.feature.melspectrogram(wav,sr=hp.sample_rate,n_mels=hp.num_mels,hop_length=256)  # [shape=(n_mels, t)]
    n_frames = mel.shape[1]
    return (mel.T,n_frames)

def compute_mel(file):
    wav = audio.load_wav(file)
    mel = audio.melspectrogram(wav).astype(np.float32)
    n_frames = mel.shape[1]
    return (mel.T,n_frames)

def compute_spec(file):
    wav = audio.load_wav(file)    # np.ndarray [shape=(n,) or (2, n)] 2表示双声道
    spectrogram = audio.spectrogram(wav).astype(np.float32) # np.ndarray [shape=(num_freq, num_frames), dtype=float32]
    n_frames = spectrogram.shape[1]
    return (spectrogram.T, n_frames) # 注意转置后[shape=( num_frames, num_freq), dtype=float32]

# # 获取信号的时频图
# def compute_fbank(file):
#     x = np.linspace(0, 400 - 1, 400, dtype=np.int64)
#     w = 0.54 - 0.46 * np.cos(2 * np.pi * (x) / (400 - 1))  # 汉明窗
#     fs, wavsignal = wav.read(file) # fs是采样率
#     # wav波形 加时间窗以及时移10ms
#     time_window = 25  # 单位ms 假设采样率16000那么采样点就是400，对应下面p_end = p_start + 400
#     preemphasis = 0.97
#     wav_arr = np.array(wavsignal)
#     range0_end = int(len(wavsignal) / fs * 1000 - time_window) // 10 + 1 # 计算循环终止的位置，也就是最终生成的窗数
#     data_input = np.zeros((range0_end, 200), dtype=np.float)  # 用于存放最终的频率特征数据
#     data_line = np.zeros((1, 400), dtype=np.float)
#     for i in range(0, range0_end):
#         p_start = i * 160 # 窗移是每次160个点，也就是10ms
#         p_end = p_start + 400 # 400就是n-fft，如果采样率是16000那么n-fft就等于窗长
#         data_line = wav_arr[p_start:p_end]
#         data_line = data_line * w  # 加窗
#         data_line = np.abs(fft(data_line))
#         data_input[i] = data_line[0:200]  # 设置为400除以2的值（即200）是取一半数据，因为是对称的
#     data_input = np.log(data_input + 1)
#     # data_input = data_input[::]
#     # TODO：增加norm和预加重
#     return data_input  # [shape=( num_frames, num_freq), dtype=float32]

# word error rate------------------------------------
def GetEditDistance(str1, str2):
    leven_cost = 0
    s = difflib.SequenceMatcher(None, str1, str2)
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        if tag == 'replace':
            leven_cost += max(i2-i1, j2-j1)
        elif tag == 'insert':
            leven_cost += (j2-j1)
        elif tag == 'delete':
            leven_cost += (i2-i1)
    return leven_cost

# 定义解码器------------------------------------
def decode_ctc(num_result, num2word):
    result = num_result[:, :, :]
    in_len = np.zeros((1), dtype = np.int32)
    in_len[0] = result.shape[1]
    r = K.ctc_decode(result, in_len, greedy = True, beam_width=10, top_paths=1)
    r1 = K.get_value(r[0][0])
    r1 = r1[0]
    text = []
    for i in r1:
        text.append(num2word[i])
    return r1, text

max_stars_repo_name: stephen-song/Speech-Recognition

max_issues_repo_name: stephen-song/Speech-Recognition

max_forks_repo_name: stephen-song/Speech-Recognition

reason: import numpy,import scipy,from scipy


text:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import warnings
from .priors import JointAutoregressiveHierarchicalPriors
from .our_utils import *
from compressai.layers import *
from .waseda import Cheng2020Anchor

class InvCompress(Cheng2020Anchor):
    def __init__(self, N=192, **kwargs):
        super().__init__(N=N)
        self.g_a = None
        self.g_s = None
        self.enh = EnhModule(64)
        self.inv = InvComp(M=N)
        self.attention = AttModule(N)

    def g_a_func(self, x):
        x = self.enh(x)
        x = self.inv(x)
        x = self.attention(x)
        return x

    def g_s_func(self, x):
        x = self.attention(x, rev = True)
        x = self.inv(x, rev=True)
        x = self.enh(x, rev=True)
        return x

    def forward(self, x):
        y = self.g_a_func(x)
        z = self.h_a(y)
        z_hat, z_likelihoods = self.entropy_bottleneck(z)
        params = self.h_s(z_hat)

        y_hat = self.gaussian_conditional.quantize(
            y, "noise" if self.training else "dequantize"
        )
        ctx_params = self.context_prediction(y_hat)
        gaussian_params = self.entropy_parameters(
            torch.cat((params, ctx_params), dim=1)
        )
        scales_hat, means_hat = gaussian_params.chunk(2, 1)
        _, y_likelihoods = self.gaussian_conditional(y, scales_hat, means=means_hat)
        x_hat = self.g_s_func(y_hat)

        return {
            "x_hat": x_hat,
            "likelihoods": {"y": y_likelihoods, "z": z_likelihoods}
        }

    @classmethod
    def from_state_dict(cls, state_dict):
        """Return a new model instance from `state_dict`."""
        N = state_dict["h_a.0.weight"].size(0)
        net = cls(N)
        net.load_state_dict(state_dict)
        return net

    def compress(self, x):
        if next(self.parameters()).device != torch.device("cpu"):
            warnings.warn(
                "Inference on GPU is not recommended for the autoregressive "
                "models (the entropy coder is run sequentially on CPU)."
            )

        y = self.g_a_func(x)
        z = self.h_a(y)

        z_strings = self.entropy_bottleneck.compress(z)
        z_hat = self.entropy_bottleneck.decompress(z_strings, z.size()[-2:])

        params = self.h_s(z_hat)

        s = 4  # scaling factor between z and y
        kernel_size = 5  # context prediction kernel size
        padding = (kernel_size - 1) // 2

        y_height = z_hat.size(2) * s
        y_width = z_hat.size(3) * s

        y_hat = F.pad(y, (padding, padding, padding, padding))

        y_strings = []
        for i in range(y.size(0)):
            string = self._compress_ar(
                y_hat[i : i + 1],
                params[i : i + 1],
                y_height,
                y_width,
                kernel_size,
                padding,
            )
            y_strings.append(string)

        return {"strings": [y_strings, z_strings], "shape": z.size()[-2:], "y": y}

    def decompress(self, strings, shape):
        assert isinstance(strings, list) and len(strings) == 2

        if next(self.parameters()).device != torch.device("cpu"):
            warnings.warn(
                "Inference on GPU is not recommended for the autoregressive "
                "models (the entropy coder is run sequentially on CPU)."
            )

        z_hat = self.entropy_bottleneck.decompress(strings[1], shape)
        params = self.h_s(z_hat)

        s = 4  # scaling factor between z and y
        kernel_size = 5  # context prediction kernel size
        padding = (kernel_size - 1) // 2

        y_height = z_hat.size(2) * s
        y_width = z_hat.size(3) * s

        y_hat = torch.zeros(
            (z_hat.size(0), self.M, y_height + 2 * padding, y_width + 2 * padding),
            device=z_hat.device,
        )

        for i, y_string in enumerate(strings[0]):
            self._decompress_ar(
                y_string,
                y_hat[i : i + 1],
                params[i : i + 1],
                y_height,
                y_width,
                kernel_size,
                padding,
            )

        y_hat = F.pad(y_hat, (-padding, -padding, -padding, -padding))
        x_hat = self.g_s_func(y_hat).clamp_(0, 1)
        return {"x_hat": x_hat}

max_stars_repo_name: Geunwoo-Jeon/InvCompress

max_issues_repo_name: Geunwoo-Jeon/InvCompress

max_forks_repo_name: Geunwoo-Jeon/InvCompress


text:
"""
test_utils
----------

"""

import numpy as np

from ..utils.sampling_utils import create_times_randompoints,\
    create_times_randomregimes, create_times_randombursts
from ..utils.util_operations import join_regimes, format_as_regular_ts,\
    apply_gaussianconvolved_ts
from ..utils.sliding_utils import sliding_embeded_transf
from ..utils.fit_utils import general_multiscale_fit,\
    fit_loglogleastsquares

def test():
    create_times_randompoints(100)
    create_times_randomregimes(100, [.1, .2])
    ts0 = create_times_randombursts(np.array([0.]), [(100, 1.), (5, .01)])
    ts1 = create_times_randombursts(np.array([0.]), [(100, 1.)])

    #### Operations
    # Parameters
    tss = [ts0, ts1]
    regimes = [.4, 2.]
    intervals = (0, 100, .2)

    times, values = join_regimes(tss, regimes)
    gridtimes, gridvalues = format_as_regular_ts(times, values, intervals)
    apply_gaussianconvolved_ts(gridvalues, 5, .2)

    ### Sliding utils
    #################
    D, step = 2, 1
    tau = 3

    sliding_embeded_transf(values, tau, D,  step)
    try:
        tau = .4
        boolean = False
        sliding_embeded_transf(values, tau, D,  step)
        boolean = True
    except:
        if boolean:
            raise Exception()
    try:
        tau = len(values)
        boolean = False
        sliding_embeded_transf(values, tau, D,  step)
        boolean = True
    except:
        if boolean:
            raise Exception()

    ### Fit utils
    ##############
    x = np.random.random(1000).cumsum()
    y = x + np.random.randn(1000)*0.01
    x = x + x.min()+0.001
    y = y + y.min()+0.001
    assert(np.all(x > 0))
    assert(np.all(y > 0))
    general_multiscale_fit(x, y, 'loglogLSQ')
    fit_loglogleastsquares(x, y)


text:
"""This module implements an operator for visualizing the state of
the vehicle

IMPORTANT: modified to accomodate multiple cameras and obstacle detection +
error evaluation for each camera. other pipeline components have been
temporarily omitted for sake of simplicity."""

from collections import deque
from functools import partial

import erdos

import numpy as np

import pygame
from pygame.locals import K_n

import pylot.utils
from pylot.drivers.sensor_setup import RGBCameraSetup
from pylot.perception.camera_frame import CameraFrame
from pylot.planning.world import World

DEFAULT_VIS_TIME = 30000.0

class VisualizerOperator(erdos.Operator):
    """ The `VisualizerOperator` allows developers to see the current state
    of the entire pipeline by visualizing it on a pygame instance.

    This receives input data from almost the entire pipeline and renders the
    results of the operator currently chosen by the developer on the screen.

    TODO: currently, b/c of the 15 stream limit, limit this to obstacle
    error stream visuals only -> eventually, merge with visualizer_operator_old.py
    to include all streams
    """
    """
    def __init__(self, pose_stream, front_camera_stream, back_camera_stream, bird_camera_stream, tl_camera_stream,
                 prediction_camera_stream, depth_camera_stream,
                 point_cloud_stream, segmentation_stream, imu_stream,
                 obstacles_stream, front_obstacles_error, back_obstacles_error, traffic_lights_stream,
                 tracked_obstacles_stream, lane_detection_stream,
                 prediction_stream, waypoints_stream, control_stream,
                 display_control_stream, pygame_display, flags):
    """
    def __init__(self, pose_stream, front_camera_stream, bird_camera_stream, tl_camera_stream,
                 prediction_camera_stream, depth_camera_stream,
                 point_cloud_stream, segmentation_stream, imu_stream,
                 obstacles_stream, front_obstacles_error, front_perfect_obstacles,
                 traffic_lights_stream,
                 tracked_obstacles_stream, lane_detection_stream,
                 prediction_stream, waypoints_stream, control_stream,
                 display_control_stream, pygame_display, flags):
        visualize_streams = []

        self._pose_msgs = deque()
        pose_stream.add_callback(
            partial(self.save, msg_type="Pose", queue=self._pose_msgs))
        visualize_streams.append(pose_stream)

        #rgb_camera_streams = [front_camera_stream, back_camera_stream]
        rgb_camera_streams = [front_camera_stream]
        self._bgr_msg_queues = []
        for i in range(len(rgb_camera_streams)):
            self._bgr_msg_queues.append(deque())

            rgb_camera_stream = rgb_camera_streams[i]
            msg_name = "RGB" + str(i)
            rgb_camera_stream.add_callback(
                partial(self.save, msg_type=msg_name, queue=self._bgr_msg_queues[i]))
            visualize_streams.append(rgb_camera_stream)

        #obstacles_error_streams = [front_obstacles_error, back_obstacles_error]
        obstacles_error_streams = [front_obstacles_error]
        self._obstacle_error_msg_queues = []
        for i in range(len(obstacles_error_streams)):
            self._obstacle_error_msg_queues.append(deque())

            obstacles_error_stream = obstacles_error_streams[i]
            msg_name = "ObstacleError" + str(i)
            obstacles_error_stream.add_callback(
                partial(self.save, msg_type=msg_name, queue=self._obstacle_error_msg_queues[i]))
            visualize_streams.append(obstacles_error_stream)

        #perfect_obstacles_streams = [front_perfect_obstacles, back_perfect_obstacles]
        perfect_obstacles_streams = [front_perfect_obstacles]
        self._perfect_obstacles_msg_queues = []
        for i in range(len(perfect_obstacles_streams)):
            self._perfect_obstacles_msg_queues.append(deque())

            perfect_obstacles_stream = perfect_obstacles_streams[i]
            msg_name = "PerfectObstacle" + str(i)
            perfect_obstacles_stream.add_callback(
                partial(self.save, msg_type=msg_name, queue=self._perfect_obstacles_msg_queues[i]))
            visualize_streams.append(perfect_obstacles_stream)

        self._bird_msgs = deque()
        bird_camera_stream.add_callback(
            partial(self.save, msg_type="BirdEye", queue=self._bird_msgs))
        visualize_streams.append(bird_camera_stream)

        self._control_msgs = deque()
        control_stream.add_callback(
            partial(self.save, msg_type="Control", queue=self._control_msgs))
        visualize_streams.append(control_stream)

        # Register a watermark callback on all the streams to be visualized.
        erdos.add_watermark_callback(visualize_streams, [], self.on_watermark)

        # Add a callback on a control stream to figure out what to display.
        display_control_stream.add_callback(self.change_display)
        self._logger = erdos.utils.setup_logging(self.config.name,
                                                 self.config.log_file_name)
        self.display = pygame_display

        # Set the font.
        fonts = [x for x in pygame.font.get_fonts() if 'mono' in x]
        default_font = 'ubuntumono'
        mono = default_font if default_font in fonts else fonts[0]
        mono = pygame.font.match_font(mono)
        self.font = pygame.font.Font(mono, 14)

        # Array of keys to figure out which message to display.
        self.current_display = 0
        self.display_array = []
        self.window_titles = []
        if flags.visualize_detected_obstacles: # eventually refactor + combine w/ other flags
            for i in range(len(obstacles_error_streams)):
                display_name = "ObstacleError" + str(i)
                self.display_array.append(display_name)
                self.window_titles.append("Detected obstacles")

            self.display_array.append("BirdEye")
            self.window_titles.append("Bird's Eye View")

        assert len(self.display_array) == len(self.window_titles), \
            "The display and titles differ."

        # Save the flags.
        self._flags = flags

    @staticmethod
    def connect(pose_stream, front_camera_stream, bird_camera_stream, tl_camera_stream,
                prediction_camera_stream, depth_stream, point_cloud_stream,
                segmentation_stream, imu_stream, obstacles_stream,
                front_obstacles_error, front_perfect_obstacles,
                traffic_lights_stream, tracked_obstacles_stream,
                lane_detection_stream, prediction_stream, waypoints_stream,
                control_stream, display_control_stream):
        return []

    def destroy(self):
        self._logger.warn('destroying {}'.format(self.config.name))

    def save(self, msg, msg_type, queue):
        self._logger.debug("@{}: Received {} message.".format(
            msg.timestamp, msg_type))
        queue.append(msg)

    def change_display(self, display_message):
        if display_message.data == K_n:
            self.current_display = (self.current_display + 1) % len(
                self.display_array)
            self._logger.debug("@{}: Visualizer changed to {}".format(
                display_message.timestamp, self.current_display))

    def get_message(self, queue, timestamp, name):
        msg = None
        if queue:
            while len(queue) > 0:
                retrieved_msg = queue.popleft()
                if retrieved_msg.timestamp == timestamp:
                    msg = retrieved_msg
                    break
            if not msg:
                self._logger.warning(
                    "@{}: message for {} was not found".format(
                        timestamp, name))
        return msg

    def render_text(self, pose, control, timestamp):
        # Generate the text to be shown on the box.
        info_text = [
            "Display  : {}".format(self.window_titles[self.current_display]),
            "Timestamp: {}".format(timestamp.coordinates[0]),
        ]

        # Add information from the pose.
        if pose:
            info_text += [
                "Location : {:.1f}, {:.1f}, {:.1f}".format(
                    *tuple(pose.transform.location.as_numpy_array())),
                "Rotation : {:.1f}, {:.1f}, {:.1f}".format(
                    *tuple(pose.transform.rotation.as_numpy_array())),
                "Speed    : {:.2f} m/s".format(pose.forward_speed),
            ]

        # Add information from the control message
        if control:
            info_text += [
                "Throttle : {:.2f}".format(control.throttle),
                "Steer    : {:.2f}".format(control.steer),
                "Brake    : {:.2f}".format(control.brake),
                "Reverse  : {:.2f}".format(control.reverse),
            ]

        # Display the information box.
        info_surface = pygame.Surface(
            (220, self._flags.camera_image_height // 3))
        info_surface.set_alpha(100)
        self.display.blit(info_surface, (0, 0))

        # Render the text.
        v_offset = 10
        for line in info_text:
            if v_offset + 18 > self._flags.camera_image_height:
                break
            surface = self.font.render(line, True, (255, 255, 255))
            self.display.blit(surface, (8, v_offset))
            v_offset += 18
        pygame.display.flip()

    def on_watermark(self, timestamp):
        self._logger.debug("@{}: received watermark.".format(timestamp))
        if timestamp.is_top:
            return
        pose_msg = self.get_message(self._pose_msgs, timestamp, "Pose")

        bgr_msgs = []
        for i in range(len(self._bgr_msg_queues)):
            msg_name = "BGR" + str(i)
            bgr_msg = self.get_message(self._bgr_msg_queues[i], timestamp, msg_name)
            bgr_msgs.append(bgr_msg)

        obstacle_error_msgs = []
        for i in range(len(self._obstacle_error_msg_queues)):
            msg_name = "ObstacleError" + str(i)
            obstacle_error_msg = self.get_message(self._obstacle_error_msg_queues[i], timestamp, msg_name)
            obstacle_error_msgs.append(obstacle_error_msg)

        perfect_obstacles_msgs = []
        for i in range(len(self._perfect_obstacles_msg_queues)):
            msg_name = "PerfectObstacle" + str(i)
            perfect_obstacles_msg = self.get_message(self._perfect_obstacles_msg_queues[i], timestamp, msg_name)
            perfect_obstacles_msgs.append(perfect_obstacles_msg)

        bird_msg = self.get_message(self._bird_msgs, timestamp, "BirdEye")

        control_msg = self.get_message(self._control_msgs, timestamp,
                                       "Control")
        if pose_msg:
            ego_transform = pose_msg.data.transform
        else:
            ego_transform = None

        # Add the visualizations on world.
        if self._flags.visualize_pose:
            self._visualize_pose(ego_transform)

        sensor_to_display = self.display_array[self.current_display]
        for i in range(len(bgr_msgs)):
            bgr_msg = bgr_msgs[i]
            obstacle_error_msg = obstacle_error_msgs[i]
            perfect_obstacles_msg = perfect_obstacles_msgs[i]

            sensor_name = "ObstacleError" + str(i)
            if sensor_to_display == sensor_name and bgr_msg and obstacle_error_msg and perfect_obstacles_msg:
                bgr_msg.frame.annotate_with_bounding_boxes(timestamp,
                                                       obstacle_error_msg.obstacles,
                                                       transform=ego_transform,
                                                       bbox_color=[255, 0, 0]) # BLUE

                bgr_msg.frame.annotate_with_bounding_boxes(timestamp,
                                                        perfect_obstacles_msg.obstacles,
                                                        transform=ego_transform,
                                                        bbox_color=[0, 0, 255]) # RED
                bgr_msg.frame.visualize(self.display, timestamp=timestamp)
                break

        if (sensor_to_display == "BirdEye" and bird_msg):
            bird_msg.frame.visualize(self.display, timestamp=timestamp)

        self.render_text(pose_msg.data, control_msg, timestamp)

    def run(self):
        # Run method is invoked after all operators finished initializing.
        # Thus, we're sure the world is up-to-date here.
        if (self._flags.visualize_pose or self._flags.visualize_imu
                or (self._flags.visualize_waypoints
                    and self._flags.draw_waypoints_on_world)):
            from pylot.simulation.utils import get_world
            _, self._world = get_world(self._flags.simulator_host,
                                       self._flags.simulator_port,
                                       self._flags.simulator_timeout)

    def _visualize_pose(self, ego_transform):
        # Draw position. We add 0.5 to z to ensure that the point is above
        # the road surface.
        loc = (ego_transform.location +
               pylot.utils.Location(0, 0, 0.5)).as_simulator_location()
        self._world.debug.draw_point(loc, size=0.2, life_time=DEFAULT_VIS_TIME)

    def _visualize_imu(self, msg):
        transform = msg.transform
        # Acceleration measured in ego frame, not global
        # z acceleration not useful for visualization so set to 0
        rotation_transform = pylot.utils.Transform(
            location=pylot.utils.Location(0, 0, 0),
            rotation=transform.rotation)
        rotated_acceleration = rotation_transform.transform_locations(
            [pylot.utils.Location(msg.acceleration.x, msg.acceleration.y,
                                  0)])[0]

        # Construct arrow.
        begin_acc = transform.location + pylot.utils.Location(z=0.5)
        end_acc = begin_acc + pylot.utils.Location(rotated_acceleration.x,
                                                   rotated_acceleration.y, 0)

        # draw arrow
        self._logger.debug("Acc: {}".format(rotated_acceleration))
        self._world.debug.draw_arrow(begin_acc.as_simulator_location(),
                                     end_acc.as_simulator_location(),
                                     arrow_size=0.1,
                                     life_time=0.1)


text:
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import find_lane_pixels_convolution

def find_good_indexis_in_window(out_img, window_idx, nonzerox, nonzeroy, window_height, current_pos, margin=100,
                                visu=False):
    '''
    Calculate good (==nonzero) indices inside sliding window defined by input parameters
    :param out_img: binary image the same shape as input wrapped image on which result will be visualized if visu parameter set to True
    :param window_idx: current idx of window for y position calculation
    :param nonzerox: x indices of the elements that are non-zero
    :param nonzeroy: y indices of the elements that are non-zero
    :param window_height: height of the sliding window
    :param current_pos: current x position
    :param margin: width of the windows +/- margin
    :param visu: draw sliding window onto out_img
    :return: nonzero  indices inside sliding window
    '''
    # Identify window boundaries in x and y
    height = out_img.shape[0]
    win_y_low = height - (window_idx + 1) * window_height
    win_y_high = height - window_idx * window_height
    win_x_low = current_pos - margin
    win_x_high = current_pos + margin

    if visu == True:
        # Draw the windows on the visualization image
        cv2.rectangle(out_img, (win_x_low, win_y_low), (win_x_high, win_y_high), (0, 255, 0), 2)

    # Identify the nonzero pixels in x and y within the window #
    good_inds = \
        ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_x_low) & (
        nonzerox < win_x_high)).nonzero()[0]

    return good_inds, out_img

def find_lane_pixels(binary_warped, visu=False):
    '''
    find lane lines pixels using histogram results
    as starting point and then looking for nonzero points
    in each sliding window
    '''
    # Take a histogram of the bottom half of the image
    binary_filtered, histogram = get_hist(binary_warped)
    midpoint = np.int(histogram.shape[0] // 2)
    # Find the peak of the left and right halves of the histogram
    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    # HYPERPARAMETERS
    # number of sliding windows
    nwindows = 9
    # width of the windows +/- margin
    margin = 100
    # minimum number of pixels found to recenter window
    minpix = 50

    # Set height of windows - based on nwindows above and image shape
    window_height = np.int(binary_filtered.shape[0] // nwindows)
    # Identify the x and y positions of all nonzero pixels in the image
    nonzero = binary_filtered.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    # Current positions to be updated later for each window in nwindows
    leftx_current = leftx_base
    rightx_current = rightx_base

    # Create empty lists to receive left and right lane pixel indices
    left_lane_inds = []
    right_lane_inds = []

    if visu == True:
        # Create an output image to draw on and visualize the result
        out_img = np.dstack((binary_warped * 255, binary_warped * 255, binary_warped * 255))
    else:
        out_img = binary_warped

    # Step through the windows one by one
    for window_idx in range(nwindows):

        # Identify window boundaries in x and y (and right and left)
        good_left_inds, out_img = find_good_indexis_in_window(out_img, window_idx, nonzerox, nonzeroy, window_height,
                                                              leftx_current, margin, visu)
        good_right_inds, out_img = find_good_indexis_in_window(out_img, window_idx, nonzerox, nonzeroy, window_height,
                                                               rightx_current, margin, visu)

        # Append these indices to the lists
        left_lane_inds.append(good_left_inds)
        right_lane_inds.append(good_right_inds)

        # If you found > minpix pixels, recenter next window on their mean position
        if len(good_left_inds) > minpix:
            leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
        if len(good_right_inds) > minpix:
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    # Concatenate the arrays of indices (previously was a list of lists of pixels)
    try:
        left_lane_inds = np.concatenate(left_lane_inds)
        right_lane_inds = np.concatenate(right_lane_inds)
    except ValueError:
        # Avoids an error if the above is not implemented fully
        pass

    # Extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds]
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    return leftx, lefty, rightx, righty, out_img

def calculate_polynomial_coefficients(binary_warped, lefty, leftx, righty, rightx, ym_per_pix=1, xm_per_pix=1):
    '''
    return polynomial coefficient in pixels and meters (real world)
    '''
    # Fit a second order polynomial to each using `np.polyfit`
    left_fit_cr = np.polyfit(lefty * ym_per_pix, leftx * xm_per_pix, 2)
    right_fit_cr = np.polyfit(righty * ym_per_pix, rightx * xm_per_pix, 2)

    left_fit = np.polyfit(lefty, leftx, 2)
    right_fit = np.polyfit(righty, rightx, 2)

    # Generate x and y values for plotting
    ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0])
    try:
        left_fitx = left_fit[0] * ploty ** 2 + left_fit[1] * ploty + left_fit[2]
        right_fitx = right_fit[0] * ploty ** 2 + right_fit[1] * ploty + right_fit[2]
    except TypeError:
        # Avoids an error if `left` and `right_fit` are still none or incorrect
        print('The function failed to fit a line!')
        left_fitx = 1 * ploty ** 2 + 1 * ploty
        right_fitx = 1 * ploty ** 2 + 1 * ploty
    return left_fitx, right_fitx, left_fit, right_fit, left_fit_cr, right_fit_cr

def fit_polynomial(binary_warped, visu=False):
    '''
    pipeline for finding lane pixels onto binary wrapped image
    '''
    # Find our lane pixels first
    leftx, lefty, rightx, righty, out_img = find_lane_pixels(binary_warped, visu)
    # uncomment the function below and comment the find_lane_pixels function to see result of convolution approach instead
    # leftx, lefty, rightx, righty, out_img = find_lane_pixels_convolution.find_lane_pixels_convolution(binary_warped)

    left_fitx, right_fitx, left_fit, right_fit, left_fit_cr, right_fit_cr = calculate_polynomial_coefficients(
        binary_warped, lefty, leftx, righty, rightx)

    ## Visualization ##
    # Colors in the left and right lane regions
    if visu == True:
        # visu wrapped image with sliding windows (result from find_lane_pixels)
        plt.imshow(out_img.astype(np.uint8))
        plt.show()

        out_img = np.dstack((binary_warped * 255, binary_warped * 255, binary_warped * 255))

        out_img[lefty, leftx] = [255, 0, 0]
        out_img[righty, rightx] = [0, 0, 255]

        return left_fitx, right_fitx, left_fit, right_fit, out_img

    return left_fitx, right_fitx, left_fit, right_fit, binary_warped

def visu_histogram(binary_warped):
    '''
    plot histogram
    '''
    binary_filtered, histogram = get_hist(binary_warped)

    # Visualize
    fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(20, 10))
    ax1.set_title('Binary warped image')
    ax1.imshow(binary_filtered)

    ax2.set_title('Histogram')
    ax2.plot(histogram)

    plt.show()

    return

def get_hist(binary_warped):
    '''
     calculate histogram
     '''
    [height, width] = binary_warped.shape
    histogram = np.sum(binary_warped[height // 2:height, :], axis=0)
    return binary_warped, histogram

def visualize_polynomials(img_ref, combined_binary, left_fitx, right_fitx):
    # Visualize
    fig, ((ax1, ax2)) = plt.subplots(ncols=2, nrows=1, figsize=(20, 10))
    ax1.set_title('Original image')
    ax1.imshow(img_ref)

    ploty = np.linspace(0, combined_binary.shape[0] - 1, combined_binary.shape[0])
    ax2.plot(left_fitx, ploty, color='green')
    ax2.plot(right_fitx, ploty, color='green')

    ax2.set_title('Transformed image')
    ax2.imshow(combined_binary.astype(np.uint8))

    plt.show()

    return

def transform_inverse(warped, distorted_img, left_fitx, right_fitx, mat_inv):
    '''
     Apply inverse transformation on lane pixels and draw resulting polygon on
     original image
     '''
    img_size = (distorted_img.shape[1], distorted_img.shape[0])
    # Create an image to draw the lines on
    warp_zero = np.zeros_like(warped).astype(np.uint8)
    color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

    # Recast the x and y points into usable format for cv2.fillPoly()
    ploty = np.linspace(0, warped.shape[0] - 1, warped.shape[0])
    pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
    pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
    pts = np.hstack((pts_left, pts_right))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0))

    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, mat_inv, img_size, flags=cv2.INTER_LINEAR)
    # Combine the result with the original image
    result = cv2.addWeighted(distorted_img, 1, newwarp, 0.3, 0)
    return result

def find_lane_pixels_test():
    '''
    This is test function. Reads intermediate results stored at the previous step.
    '''
    arrays_names = glob.glob('../test_images/warped/test*.npy')
    images_names = glob.glob('../test_images/test*.jpg')
    mat_names = glob.glob('../test_images/warped/M_test*.npy')
    mat_inv_names = glob.glob('../test_images/warped/Minv_test*.npy')
    for fname, img_name, mat_name, mat_inv_name in zip(arrays_names, images_names, mat_names, mat_inv_names):
        img_bin_orig = np.load(fname)
        img_undist = cv2.imread(img_name)
        mat = np.load(mat_name)
        mat_inv = np.load(mat_inv_name)
        img_bin = img_bin_orig[:, :, 0] / 255
        left_fitx, right_fitx, left_fit, right_fit, binary_lanes = fit_polynomial(img_bin, visu=True)
        # 4. Transform back
        result = transform_inverse(img_bin, img_undist, left_fitx, right_fitx, mat_inv)
        # Plots the left and right polynomials on the lane lines
        visualize_polynomials(result, binary_lanes, left_fitx, right_fitx)

def test_histogram():
    '''
    Calculate and visualize histograms calculated on images from the previous step
    :return:
    '''
    arrays_names = glob.glob('../test_images/warped/test*.npy')
    for fname in arrays_names:
        img = np.load(fname)
        img_bin = img[:, :, 0] / 255
        visu_histogram(img_bin)

if __name__ == '__main__':
    # uncomment function below to see histograms
    # test_histogram()
    find_lane_pixels_test()

max_stars_repo_name: anastasia-spb/CarND-Advanced-Lane-Lines

max_issues_repo_name: anastasia-spb/CarND-Advanced-Lane-Lines

max_forks_repo_name: anastasia-spb/CarND-Advanced-Lane-Lines


text:
#include the Numpy library
import numpy as np

#define the main() function
def main():

	i = 0	#declare an integer i, set to 0
	x = 119.0	#declare a float x,with val 119

	for i in range(120):	#loops i from 0 through
		if ((i%2)==0):		#if i is even
			x += 3.0		#add 3 to x
		else:				#if i odd
			x -= 5.0		#x = x-5

	s = "%3.2e" % x			#make a string that shows x with scientific notation (e)
							#3 nbr zith .2 nbs apres decimal

	print (s)

#now the rest of the program
if __name__ == "__main__":
	main()

#continue


text:
import numpy
import cv2
# 使用矩阵来创建图像
img_1 = numpy.ndarray(shape=(500, 500, 3), dtype=numpy.uint8)

img_py = [[[255, 255, 0] for y in range(250)] for x in range(500)]    # BGR
img_2 = numpy.array(img_py)

# print(img_2[slice(0, 2, 1), 0, 1 ], img_2[0][0])
# print(img_2[0:2:1, 0, 1 ], img_2[0][0])
# print(img_1)

img_1[0, 0, 0] = 0
img_1[0, 0, 1] = 255
img_1[0, 0, 2] = 0

cv2.imwrite("1.png", img_1)
cv2.imwrite("2.png", img_2)
# 加载图像

img_3 = cv2.imread("00002.png")

# img_3[:, :, 2] = 0
# img_3[:, :, 1] = 0

img_3[ img_3[:,:, 0] > 100] = 0

cv2.imwrite("3.png", img_3)


text:
import scipy.io as sio
import numpy as np
from PIL import Image
import tensorflow as tf

label_colours_19 = [[128, 64, 128], [244, 35, 231], [69, 69, 69]
                # 0 = road, 1 = sidewalk, 2 = building
                ,[102, 102, 156], [190, 153, 153], [153, 153, 153]
                # 3 = wall, 4 = fence, 5 = pole
                ,[250, 170, 29], [219, 219, 0], [106, 142, 35]
                # 6 = traffic light, 7 = traffic sign, 8 = vegetation
                ,[152, 250, 152], [69, 129, 180], [219, 19, 60]
                # 9 = terrain, 10 = sky, 11 = person
                ,[255, 0, 0], [0, 0, 142], [0, 0, 69]
                # 12 = rider, 13 = car, 14 = truck
                ,[0, 60, 100], [0, 79, 100], [0, 0, 230]
                # 15 = bus, 16 = train, 17 = motocycle
                ,[119, 10, 32]]
                # 18 = bicycle

label_colours_14 = [[0,0,0], [69, 69, 69], [0, 0, 142],
                # 0 = Misc , 1 = Building, 2 = Car
                [0, 79, 100], [153, 153, 153], [128, 64, 128],
                # 3 = GuardRAil, 4 = Pole, 5 = Road
                [69, 129, 180], [152, 250, 152], [250, 170, 29],
                # 6 = Sky       7 = Terrain      8 = TrafficLight
                [219, 219, 0],  [0, 255, 0]  , [0, 0, 69],
                # 9 = TrafficSign 10 = Tree 11=Truck
                [0, 60, 100],  [106, 142, 35]]
                # 12 = Van 13 = Vegetation

label_colours_12 = [[0,0,0], [170,	85,	255], [244, 35, 232]
                # 0 = unlabeled, 1 = road, 2 = sidewalk,
                ,[152,	251,	152], [250,	170,	30], [220,	220,	0]
                # 3 = pole, 4 = traffic light, 5 = traffic sign
                ,[220,	20,	60], [155,	40,	0], [0,	0,	255]
                # 6 = person   7 = bicycle,     8 = car
                ,[0,	255,	255], [69, 129, 180], [0,	0,	130]
                # 9 = truck      , 10 = bus,    11 = motorcycle
                ]

def decode_labels(mask, img_shape, num_classes):
    if num_classes == 12:
        color_table = label_colours_12
    elif num_classes == 14:
        color_table = label_colours_14
    elif num_classes == 19:
        color_table = label_colours_19
    else:
        print ('ERROR<tools.py> No valid label color available!!!')

    color_mat = tf.constant(color_table, dtype=tf.float32)
    onehot_output = tf.one_hot(mask, depth=num_classes)
    onehot_output = tf.reshape(onehot_output, (-1, num_classes))
    pred = tf.matmul(onehot_output, color_mat)
    pred = tf.reshape(pred, (1, img_shape[0], img_shape[1], 3))

    return pred

def prepare_label(input_batch, new_size, num_classes, one_hot=True):
    with tf.name_scope('label_encode'):
        input_batch = tf.compat.v1.image.resize_nearest_neighbor(input_batch, new_size) # as labels are integer numbers, need to use NN interp.
        input_batch = tf.squeeze(input_batch, squeeze_dims=[3]) # reducing the channel dimension.
        if one_hot:
            input_batch = tf.one_hot(input_batch, depth=num_classes)

    return input_batch

max_stars_repo_name: Andreas-Pfeuffer/LSTM-ICNet

max_issues_repo_name: Andreas-Pfeuffer/LSTM-ICNet

max_forks_repo_name: Andreas-Pfeuffer/LSTM-ICNet

reason: import numpy,import scipy


text:
import itertools
from typing import Any
from typing import Dict
from typing import Optional
from typing import Union

import dask.dataframe as dd
import holoviews as hv
import numpy as np
import pandas as pd
from bokeh.models import HoverTool
from sid.colors import get_colors
from sid.policies import compute_pseudo_effect_sizes_of_policies

DEFAULT_FIGURE_KWARGS = {
    "height": 400,
    "width": 600,
    "line_width": 12,
    "title": "Gantt Chart of Policies",
}

def plot_policy_gantt_chart(
    policies,
    effects=False,
    colors="categorical",
    fig_kwargs=None,
):
    """Plot a Gantt chart of the policies."""
    if fig_kwargs is None:
        fig_kwargs = {}
    fig_kwargs = {**DEFAULT_FIGURE_KWARGS, **fig_kwargs}

    if isinstance(policies, dict):
        df = (
            pd.DataFrame(policies)
            .T.reset_index()
            .rename(columns={"index": "name"})
            .astype({"start": "datetime64", "end": "datetime64"})
            .drop(columns="policy")
        )
    elif isinstance(policies, pd.DataFrame):
        df = policies
    else:
        raise ValueError("'policies' should be either a dict or pandas.DataFrame.")

    if effects:
        effect_kwargs = effects if isinstance(effects, dict) else {}
        effects = compute_pseudo_effect_sizes_of_policies(
            policies=policies, **effect_kwargs
        )
        effects_s = pd.DataFrame(
            [{"policy": name, "effect": effects[name]["mean"]} for name in effects]
        ).set_index("policy")["effect"]
        df = df.merge(effects_s, left_on="name", right_index=True)
        df["alpha"] = (1 - df["effect"] + 0.1) / 1.1
    else:
        df["alpha"] = 1

    df = df.reset_index()
    df = _complete_dates(df)
    df = _add_color_to_gantt_groups(df, colors)
    df = _add_positions(df)

    hv.extension("bokeh", logo=False)

    segments = hv.Segments(
        df,
        [
            hv.Dimension("start", label="Date"),
            hv.Dimension("position", label="Affected contact model"),
            "end",
            "position",
        ],
    )
    y_ticks_and_labels = list(zip(*_create_y_ticks_and_labels(df)))

    tooltips = [("Name", "@name")]
    if effects:
        tooltips.append(("Effect", "@effect"))
    hover = HoverTool(tooltips=tooltips)

    gantt = segments.opts(
        color="color",
        alpha="alpha",
        tools=[hover],
        yticks=y_ticks_and_labels,
        **fig_kwargs,
    )

    return gantt

def _complete_dates(df):
    """Complete dates."""
    for column in ("start", "end"):
        df[column] = pd.to_datetime(df[column])
    df["start"] = df["start"].fillna(df["start"].min())
    df["end"] = df["end"].fillna(df["end"].max())
    return df

def _add_color_to_gantt_groups(df, colors):
    """Add a color for each affected contact model."""
    colors_ = itertools.cycle(get_colors(colors, 4))
    acm_to_color = dict(zip(df["affected_contact_model"].unique(), colors_))
    df["color"] = df["affected_contact_model"].replace(acm_to_color)

    return df

def _add_positions(df):
    """Add positions.

    This functions computes the positions of policies, displayed as segments on the time
    line. For example, if two policies affecting the same contact model have an
    overlapping time windows, the segments are stacked and drawn onto different
    horizontal lines.

    """
    min_position = 0

    def _add_within_group_positions(df):
        """Add within group positions."""
        nonlocal min_position
        position = pd.Series(data=min_position, index=df.index)
        for i in range(1, len(df)):
            start = df.iloc[i]["start"]
            end = df.iloc[i]["end"]
            is_overlapping = (
                (df.iloc[:i]["start"] <= start) & (start <= df.iloc[:i]["end"])
            ) | ((df.iloc[:i]["start"] <= end) & (end <= df.iloc[:i]["end"]))
            if is_overlapping.any():
                possible_positions = set(range(min_position, i + min_position + 1))
                positions_of_overlapping = set(position.iloc[:i][is_overlapping])
                position.iloc[i] = min(possible_positions - positions_of_overlapping)

        min_position = max(position) + 1

        return position

    positions = df.groupby("affected_contact_model", group_keys=False).apply(
        _add_within_group_positions
    )
    df["position_local"] = positions
    df["position"] = df.groupby(
        ["affected_contact_model", "position_local"], sort=True
    ).ngroup()

    return df

def _create_y_ticks_and_labels(df):
    """Create the positions and their related labels for the y axis."""
    pos_per_group = df.groupby("position", as_index=False).first()
    mean_pos_per_group = (
        pos_per_group.groupby("affected_contact_model")["position"].mean().reset_index()
    )
    return mean_pos_per_group["position"], mean_pos_per_group["affected_contact_model"]

ERROR_MISSING_CHANNEL = (
    "'channel_infected_by_contact' is necessary to plot infection rates by contact "
    "models. Re-run the simulation and pass `saved_columns={'channels': "
    "'channel_infected_by_contact'}` to `sid.get_simulate_func`."
)

DEFAULT_IR_PER_CM_KWARGS = {
    "width": 600,
    "height": 400,
    "tools": ["hover"],
    "title": "Contribution of Contact Models to Infections",
    "xlabel": "Date",
    "ylabel": "Contact Model",
    "invert_yaxis": True,
    "colorbar": True,
    "cmap": "YlOrBr",
}

def plot_infection_rates_by_contact_models(
    df_or_time_series: Union[pd.DataFrame, dd.core.DataFrame],
    show_reported_cases: bool = False,
    unit: str = "share",
    fig_kwargs: Optional[Dict[str, Any]] = None,
) -> hv.HeatMap:
    """Plot infection rates by contact models.

    Parameters
    ----------
    df_or_time_series : Union[pandas.DataFrame, dask.dataframe.core.DataFrame]
        The input can be one of the following two.

        1. It is a :class:`dask.dataframe.core.DataFrame` which holds the time series
           from a simulation.
        2. It can be a :class:`pandas.DataFrame` which is created with
           :func:`prepare_data_for_infection_rates_by_contact_models`. It allows to
           compute the data for various simulations with different seeds and use the
           average over all seeds.

    show_reported_cases : bool, optional
        A boolean to select between reported or real cases of infections. Reported cases
        are identified via testing mechanisms.
    unit : str
        The arguments specifies the unit shown in the figure.

        - ``"share"`` means that daily units represent the share of infection caused
          by a contact model among all infections on the same day.

        - ``"population_share"`` means that daily units represent the share of
          infection caused by a contact model among all people on the same day.

        - ``"incidence"`` means that the daily units represent incidence levels per
          100,000 individuals.
    fig_kwargs : Optional[Dict[str, Any]], optional
        Additional keyword arguments which are passed to ``heatmap.opts`` to style the
        plot. The keyword arguments overwrite or extend the default arguments.

    Returns
    -------
    heatmap : hv.HeatMap
        The heatmap object.

    """
    fig_kwargs = (
        DEFAULT_IR_PER_CM_KWARGS
        if fig_kwargs is None
        else {**DEFAULT_IR_PER_CM_KWARGS, **fig_kwargs}
    )

    if _is_data_prepared_for_heatmap(df_or_time_series):
        df = df_or_time_series
    else:
        df = prepare_data_for_infection_rates_by_contact_models(
            df_or_time_series, show_reported_cases, unit
        )

    hv.extension("bokeh", logo=False)

    heatmap = hv.HeatMap(df)
    plot = heatmap.opts(**fig_kwargs)

    return plot

def _is_data_prepared_for_heatmap(df):
    """Is the data prepared for the heatmap plot."""
    return (
        isinstance(df, pd.DataFrame)
        and df.columns.isin(["date", "channel_infected_by_contact", "share"]).all()
        and not df["channel_infected_by_contact"]
        .isin(["not_infected_by_contact"])
        .any()
    )

def prepare_data_for_infection_rates_by_contact_models(
    time_series: dd.core.DataFrame,
    show_reported_cases: bool = False,  # noqa: U100
    unit: str = "share",
) -> pd.DataFrame:
    """Prepare the data for the heatmap plot.

    Parameters
    ----------
    time_series : dask.dataframe.core.DataFrame
        The time series of a simulation.
    show_reported_cases : bool, optional
        A boolean to select between reported or real cases of infections. Reported cases
        are identified via testing mechanisms.
    unit : str
        The arguments specifies the unit shown in the figure.

        - ``"share"`` means that daily units represent the share of infection caused
          by a contact model among all infections on the same day.

        - ``"population_share"`` means that daily units represent the share of
          infection caused by a contact model among all people on the same day.

        - ``"incidence"`` means that the daily units represent incidence levels per
          100,000 individuals.

    Returns
    -------
    time_series : pandas.DataFrame
        The time series with the prepared data for the plot.

    """
    if isinstance(time_series, pd.DataFrame):
        time_series = dd.from_pandas(time_series, npartitions=1)
    elif not isinstance(time_series, dd.core.DataFrame):
        raise ValueError("'time_series' must be either pd.DataFrame or dask.dataframe.")

    if "channel_infected_by_contact" not in time_series:
        raise ValueError(ERROR_MISSING_CHANNEL)

    if show_reported_cases:
        time_series = _adjust_channel_infected_by_contact_to_new_known_cases(
            time_series
        )

    counts = (
        time_series[["date", "channel_infected_by_contact"]]
        .groupby(["date", "channel_infected_by_contact"])
        .size()
        .reset_index()
        .rename(columns={0: "n"})
    )

    if unit == "share":
        out = counts.query(
            "channel_infected_by_contact != 'not_infected_by_contact'"
        ).assign(
            share=lambda x: x["n"]
            / x.groupby("date")["n"].transform("sum", meta=("n", "f8")),
        )

    elif unit == "population_share":
        out = counts.assign(
            share=lambda x: x["n"]
            / x.groupby("date")["n"].transform("sum", meta=("n", "f8")),
        ).query("channel_infected_by_contact != 'not_infected_by_contact'")

    elif unit == "incidence":
        out = counts.query(
            "channel_infected_by_contact != 'not_infected_by_contact'"
        ).assign(share=lambda x: x["n"] * 7 / 100_000)

    else:
        raise ValueError(
            "'unit' should be one of 'share', 'population_share' or 'incidence'"
        )

    out = out.drop(columns="n").compute()

    return out

def _adjust_channel_infected_by_contact_to_new_known_cases(df):
    """Adjust channel of infections by contacts to new known cases.

    Channel of infections are recorded on the date an individual got infected which is
    not the same date an individual is tested positive with a PCR test.

    This function adjusts ``"channel_infected_by_contact"`` such that the infection
    channel is shifted to the date when an individual is tested positive.

    """
    channel_of_infection_by_contact = _find_channel_of_infection_for_individuals(df)
    df = _patch_channel_infected_by_contact(df, channel_of_infection_by_contact)
    return df

def _find_channel_of_infection_for_individuals(df):
    """Find the channel of infected by contact for each individual."""
    df["channel_infected_by_contact"] = df["channel_infected_by_contact"].cat.as_known()

    df["channel_infected_by_contact"] = df[
        "channel_infected_by_contact"
    ].cat.remove_categories(["not_infected_by_contact"])

    df = df.dropna(subset=["channel_infected_by_contact"])

    df = df["channel_infected_by_contact"].compute()

    return df

def _patch_channel_infected_by_contact(df, s):
    """Patch channel of infections by contact to only show channels for known cases."""
    df = df.drop(columns="channel_infected_by_contact")

    df = df.merge(s.to_frame(name="channel_infected_by_contact"), how="left")

    df["channel_infected_by_contact"] = df["channel_infected_by_contact"].mask(
        ~df["new_known_case"], np.nan
    )

    df["channel_infected_by_contact"] = (
        df["channel_infected_by_contact"]
        .cat.add_categories("not_infected_by_contact")
        .fillna("not_infected_by_contact")
    )

    return df


text:
from uv_data import UVData
from spydiff import import_difmap_model
from stats import LnLikelihood, LnPrior
from model import Model
from emcee import PTSampler, utils
import scipy as sp
import numpy as np

mdl_dir = '/home/ilya/vlbi_errors/bin_q'
mdl_file = '0235+164.q1.2008_09_02.mdl'
uvfits = '/home/ilya/vlbi_errors/bin_q/0235+164.q1.2008_09_02.uvf_difmap'

uvdata = UVData(uvfits)
comps = import_difmap_model(mdl_file, mdl_dir)
eg = comps[0]
eg.add_prior(flux=(sp.stats.uniform.logpdf, [0., 10.], dict(),),
             bmaj=(sp.stats.uniform.logpdf, [0, 1.], dict(),),
             x=(sp.stats.uniform.logpdf, [-1., 1.], dict(),),
             y=(sp.stats.uniform.logpdf, [-1., 1.], dict(),),
             e=(sp.stats.uniform.logpdf, [0, 1.], dict(),),
             bpa=(sp.stats.uniform.logpdf, [0, np.pi], dict(),))
model = Model(stokes='I')
model.add_component(eg)
ndim = model.size
nwalkers = 32
ntemps = 20

lnlik = LnLikelihood(uvdata, model)
lnpr = LnPrior(model)

p0 = utils.sample_ball(model.p, [0.3, 0.1, 0.1, 0.003, 0.03, 0.1],
                       size=ntemps*nwalkers).reshape((ntemps, nwalkers, ndim))

betas = np.exp(np.linspace(0, -(ntemps - 1) * 0.5 * np.log(2), ntemps))
ptsampler = PTSampler(ntemps, nwalkers, ndim, lnlik, lnpr, betas=betas)

# Burning in
print "Burnin"
for p, lnprob, lnlike in ptsampler.sample(p0, iterations=1000):
    pass
ptsampler.reset()

print "Production"
for p, lnprob, lnlike in ptsampler.sample(p, lnprob0=lnprob,
                                          lnlike0=lnlike,
                                          iterations=10000, thin=10):
    pass

# # 0-temperature chain
# mu0 = np.mean(np.mean(ptsampler.chain[0,...], axis=0), axis=0)
# # array([ 3.49944349, -0.00425058,  0.02648386,  0.06396026,  0.58487231,
# #         1.57089506])
#
# # (11427.220089952611, 5.9308987859385525)
# import corner
# samples = ptsampler.flatchain[0, ::3, 3:5]
fig = corner.corner(samples, range=((0, 0.15), (0, 1)),
                    labels=[r'$bmaj$', r'$e$'], show_titles=True,
                    title_kwargs={'fontsize': 16}, quantiles=[0.16, 0.5, 0.84],
                    label_kwargs={'fontsize': 16}, title_fmt=".3f")
fig.savefig('_corner.png', bbox_inches='tight', dpi=200)

max_stars_repo_name: akutkin/SACA

max_issues_repo_name: akutkin/SACA

max_forks_repo_name: ipashchenko/ve