Esempio n. 1
0
def draw(name, p, clf, X, y, step, ):
    stepx = step
    stepy = step
    x_min, y_min = np.amin(X, 0)
    x_max, y_max = np.amax(X, 0)
    x_min -= stepx
    x_max += stepx
    y_min -= stepy
    y_max += stepy
    xx, yy = np.meshgrid(np.arange(x_min, x_max, stepx),
                         np.arange(y_min, y_max, stepy))

    mesh_dots = np.c_[xx.ravel(), yy.ravel()]
    zz = np.apply_along_axis(lambda t: clf.predict(t), 1, mesh_dots)
    zz = np.array(zz).reshape(xx.shape)

    plt.figure(figsize=(10, 10))
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    x0, y0 = X[y == -1].T
    x1, y1 = X[y == 1].T

    plt.pcolormesh(xx, yy, zz, cmap=ListedColormap(['#FFAAAA', '#AAAAFF']))
    plt.scatter(x0, y0, color='red', s=100)
    plt.scatter(x1, y1, color='blue', s=100)

    sup_ind = clf.get_non_bound_indices()
    X_sup = X[sup_ind]
    x_sup, y_sup = X_sup.T

    plt.scatter(x_sup, y_sup, color='white', marker='x', s=60)
    plt.suptitle(p)
    plt.savefig(name + '_' + p['name'] + '.png')
    plt.show()
Esempio n. 2
0
 def __init__(self, trajectoryID, points):
     self.trajectoryID = trajectoryID
     self.max_x = 0
     self.max_y = 0
     self.min_x = 0
     self.min_y = 0
     self.xs = []
     self.ys = []
     # compute max and minimum of the trajectory
     for el in points:
         self.xs.append(el.x)
         self.ys.append(el.y)
     self.max_x = np.amax(np.array(self.xs))
     self.min_x = np.amin(np.array(self.xs))
     self.max_y = np.amax(np.array(self.ys))
     self.min_y = np.amin(np.array(self.ys))
Esempio n. 3
0
    def __init__(self, trajectoryID, classification, generetedPoint, realPoint):
        self.trajectoryID = trajectoryID
        self.classification = classification
        self.generetedPoint = generetedPoint
        self.realPoint = realPoint
        self.max_x = 0
        self.max_y = 0
        self.min_x = 0
        self.min_y = 0
        self.xs_real = []
        self.ys_real = []
        self.xs_generated = []
        self.ys_generated = []

        # compute max and minimum of the individual
        xs = []
        ys = []
        for el in self.realPoint:
            xs.append(el.x)
            ys.append(el.y)
            if el.x not in self.xs_real:
                self.xs_real.append(el.x)
            if el.y not in self.ys_real:
                self.ys_real.append(el.y)
        for el in self.generetedPoint:
            xs.append(el.x)
            ys.append(el.y)
            if el.x not in self.xs_generated:
                self.xs_generated.append(el.x)
            if el.y not in self.ys_generated:
                self.ys_generated.append(el.y)
        self.max_x = np.amax(np.array(xs))
        self.min_x = np.amin(np.array(xs))
        self.max_y = np.amax(np.array(ys))
        self.min_y = np.amin(np.array(ys))

        # compute mse points this trajectory
        distances = []
        for i in range(len(self.generetedPoint)):
            a = np.array((self.xs_real[i], self.ys_real[i]))
            b = np.array((self.xs_generated[i], self.ys_generated[i]))
            value = np.linalg.norm(a - b) * 100000
            distances.append(pow(value, 2))

        self.array = np.array(distances)
        self.MSD = (np.sum(self.array)) / len(self.array)
def analiseTra(json_file, trajectories_label):
    for trajectory in trajectories_label:

        lat = []
        lng = []
        for el in json_file[trajectory]["trajectory"]:
            lat.append(el[0])
            lng.append(el[1])

        # transform real in lat and lng

        lat_real_h = []
        lng_real_h = []

        for el in json_file[trajectory]["real"]:
            lat_real_h.append(el[0])
            lng_real_h.append(el[1])

        # transform generated in lat and lng
        lat_generated = []
        lng_generated = []
        for el in json_file[trajectory]["generated"]:
            lat_generated.append(el[0])
            lng_generated.append(el[1])

        min_lat = []
        min_lat.append(np.amin(np.array(lat)))
        min_lat.append(np.amin(np.array(lat_real_h)))
        min_lat.append(np.amin(np.array(lat_generated)))
        real_total_min_lat = np.amin(np.array(min_lat))

        min_lon = []
        min_lon.append(np.amin(np.array(lng)))
        min_lon.append(np.amin(np.array(lng_real_h)))
        min_lon.append(np.amin(np.array(lng_generated)))
        real_total_min_lng = np.amin(np.array(min_lon))

        max_lat = []
        max_lat.append(np.amax(np.array(lat)))
        max_lat.append(np.amax(np.array(lat_real_h)))
        max_lat.append(np.amax(np.array(lat_generated)))
        real_total_max_lat = np.amax(np.array(max_lat))

        max_lon = []
        max_lon.append(np.amax(np.array(lng)))
        max_lon.append(np.amax(np.array(lng_real_h)))
        max_lon.append(np.amax(np.array(lng_generated)))
        real_total_max_lng = np.amax(np.array(max_lon))

    return real_total_min_lat, real_total_min_lng, real_total_max_lat, real_total_max_lng
def find_max_min_coordinates(real, generated, trajectory):
    # logging.debug("converting JSON list to list for the library")
    # transform trajectory in lat and lng
    lat = []
    lng = []
    for el in trajectory:
        lat.append(el[0])
        lng.append(el[1])

    # transform real in lat and lng
    lat_real = []
    lng_real = []
    for el in real:
        lat_real.append(el[0])
        lng_real.append(el[1])

    # transform generated in lat and lng
    lat_generated = []
    lng_generated = []
    for el in generated:
        lat_generated.append(el[0])
        lng_generated.append(el[1])

    # logging.debug("plotting points")
    min_lat = []
    min_lat.append(np.amin(np.array(lat)))
    min_lat.append(np.amin(np.array(lat_real)))
    min_lat.append(np.amin(np.array(lat_generated)))
    real_total_min_lat = np.amin(np.array(min_lat))

    min_lon = []
    min_lon.append(np.amin(np.array(lng)))
    min_lon.append(np.amin(np.array(lng_real)))
    min_lon.append(np.amin(np.array(lng_generated)))
    real_total_min_lng = np.amin(np.array(min_lon))

    max_lat = []
    max_lat.append(np.amax(np.array(lat)))
    max_lat.append(np.amax(np.array(lat_real)))
    max_lat.append(np.amax(np.array(lat_generated)))
    real_total_max_lat = np.amax(np.array(max_lat))

    max_lon = []
    max_lon.append(np.amax(np.array(lng)))
    max_lon.append(np.amax(np.array(lng_real)))
    max_lon.append(np.amax(np.array(lng_generated)))
    real_total_max_lng = np.amax(np.array((max_lon)))

    return real_total_min_lat, real_total_min_lng, real_total_max_lat, real_total_max_lng
def find_max_fitnes(path):
    name = path + "tgcfs.EA.Agents-fitness.csv"
    try:
        with open(name) as f:
            lis = [line.split() for line in f]
            lis = lis[1:]
            # need to find all the last position
            fit = []
            for el in lis:
                fit.append(float(el[len(el) - 1].replace(",","")))

            max_finale = np.amax(np.array(fit))
            value = np.ceil(max_finale)
            return value

    except Exception:
        pass
Esempio n. 7
0
def detect_surface(profile):
    """Automatic detection of surface (begin of snowpack).

    :param profile: The profile to detect surface in.
    :return: Distance where surface was detected.
    :rtype: float
    """

    # Cut off ca. 1 mm
    distance = profile.samples.distance.values[250:]
    force = profile.samples.force.values[250:]

    force = downsample(force, 20)
    distance = downsample(distance, 20)

    force = smooth(force, 242)

    y_grad = np.gradient(force)
    y_grad = downsample(y_grad, 3)
    x_grad = downsample(distance, 3)

    max_force = np.amax(force)

    try:
        for i in np.arange(100, x_grad.size):
            std = np.std(y_grad[:i - 1])
            mean = np.mean(y_grad[:i - 1])
            if y_grad[i] >= 5 * std + mean:
                surface = x_grad[i]
                break

        if i == x_grad.size - 1:
            surface = max_force

        log.info('Detected surface at {:.3f} mm in profile {}'.format(
            surface, profile))
        return surface

    except ValueError:
        log.warning('Failed to detect surface')
        return max_force
Esempio n. 8
0
                        agent = []
                        classifier = []
                        result = []
                        for el in v:
                            # select only with the
                            agent.append(el[0])
                            classifier.append(el[1])
                            result.append(el[2])

                        # need to order per agent

                        classifier_array = np.array(classifier)
                        agent_array = np.array(agent)

                        min_cla = np.amin(classifier_array)
                        max_cla = np.amax(classifier_array)
                        unique_element_classifier = np.unique(classifier_array)
                        # print(unique_element_classifier)
                        # print(len(unique_element_classifier))
                        unique_element_agent = np.unique(agent_array)
                        # print(unique_element_agent)
                        # print(len(unique_element_agent))

                        dif_cla = max_cla - min_cla

                        real_classifier = np.zeros(
                            len(unique_element_classifier))

                        # (maxEnd - minEnd) * ((value - minStart) / (maxStart - minStart)) + minEnd;

                        value_agent = agent[0]
Esempio n. 9
0
 def get_max_y(self):
     return np.amax(np.array(self.max_y)) + 0.000001
def manyTrajectories(json_file, trajectories_label, numb, bigOrSmall):
    total_list_different_trajectories = {}

    total_trajectories = []

    total_info_trajectories = []
    total_info_fitness = []

    count = 0

    for trajectory in trajectories_label:

        # transform trajectory in lat and lng
        lat_real = json_file[trajectory]["trajectory"][1][0]
        lng_real = json_file[trajectory]["trajectory"][1][1]

        key = str(lat_real) + str(lng_real)

        if key not in total_list_different_trajectories:
            total_list_different_trajectories.update({key: count})
            count += 1

            lat = []
            lng = []
            for el in json_file[trajectory]["trajectory"]:
                lat.append(el[0])
                lng.append(el[1])

            # transform real in lat and lng
            lat_real_h = []
            lng_real_h = []
            for el in json_file[trajectory]["real"]:
                lat_real_h.append(el[0])
                lng_real_h.append(el[1])

            total_trajectories.append(
                {"lats_tra": lat, "lng_tra": lng, "lats_real": lat_real_h, "lng_real": lng_real_h})

            # transform generated in lat and lng
            lat_generated = []
            lng_generated = []
            for el in json_file[trajectory]["generated"]:
                lat_generated.append(el[0])
                lng_generated.append(el[1])


            fit = json_file[trajectory]["classification"]

            vect_generated = []
            vect_generated.append((lat_generated, lng_generated))
            total_info_trajectories.append(vect_generated)
            vect_fitness = []
            vect_fitness.append(fit)
            total_info_fitness.append(vect_fitness)
        else:
            position = total_list_different_trajectories[key]

            # transform generated in lat and lng
            lat_generated = []
            lng_generated = []
            for el in json_file[trajectory]["generated"]:
                lat_generated.append(el[0])
                lng_generated.append(el[1])

            fit = json_file[trajectory]["classification"]

            total_info_trajectories[position].append((lat_generated, lng_generated))
            total_info_fitness[position].append(fit)



    plt.figure(figsize=(12, 10))
    plt.autoscale(enable=False, tight=True)

    sns.set_style("darkgrid")

    for i in range(len(total_trajectories)):
        j = i + 1
        if len(total_trajectories) == 1:
            plt.subplot(1, 1, j)
        elif len(total_trajectories) == 2:
            plt.subplot(1, 2, j)
        elif len(total_trajectories) == 3:
            plt.subplot(1, 3, j)
        elif len(total_trajectories) == 5:
            plt.subplot(2, 3, j)
        elif len(total_trajectories) == 10:
            plt.subplot(3, 4, j)
        elif len(total_trajectories) == 20:
            plt.subplot(4, 5, j)
        elif len(total_trajectories) == 30:
            plt.subplot(5, 6, j)
        elif len(total_trajectories) == 40:
            plt.subplot(6, 7, j)
        else:
            raise Exception("Num trajectories not set")


        lats = total_trajectories[i]["lats_tra"]
        lngs = total_trajectories[i]["lng_tra"]

        lat_real = total_trajectories[i]["lats_real"]
        lng_real = total_trajectories[i]["lng_real"]

        lat_g = []
        lng_g = []
        for el in total_info_trajectories[i]:
            lat_g.append(el[0])
            lng_g.append(el[1])

        fitness = total_info_fitness[i]
        max = 150

        min_lat = []
        max_lat = []
        min_lng = []
        max_lng = []
        min_lat.append(np.amin(np.array(lat_g)))
        max_lat.append(np.amax(np.array(lat_g)))
        min_lng.append(np.amin(np.array(lng_g)))
        max_lng.append(np.amax(np.array(lng_g)))
        min_lat.append(np.amin(np.array(lats)))
        max_lat.append(np.amax(np.array(lats)))
        min_lng.append(np.amin(np.array(lngs)))
        max_lng.append(np.amax(np.array(lngs)))
        min_lat.append(np.amin(np.array(lat_real)))
        max_lat.append(np.amax(np.array(lat_real)))
        min_lng.append(np.amin(np.array(lng_real)))
        max_lng.append(np.amax(np.array(lng_real)))
        real_min_lat = np.amin(np.array(min_lat)) - 0.0002
        real_max_lat = np.amax(np.array(max_lat)) + 0.0002
        real_min_lng = np.amin(np.array(min_lng)) - 0.0002
        real_max_lng = np.amax(np.array(max_lng)) + 0.0002

        # plt.axis([real_min_lat, real_max_lat, real_min_lng, real_max_lng])
        # plt.xlim(real_min_lat, real_max_lat)
        # plt.ylim(real_min_lng, real_max_lng)
        plt.axis('off')

        # trajectory
        plt.plot(lats, lngs, color='b', marker='o', markersize=1)

        # print real point
        for i in range(len(lat_real)):
            plt.plot(lat_real[i], lng_real[i], color='k', marker='o', markersize=1)

        # plt.scatter(lat_g, lng_g, c=[fitness], marker='o', vmin=0., vmax=max, cmap='autumn', s=1)
        plt.scatter(lat_g, lng_g, color='red', marker='o', s=1)


        # print generated point
        # for k in range(len(lat_g)):
        #     plt.plot(lat_g[k], lng_g[k], color='r', marker='o', markersize=1)
    if bigOrSmall:
        name_to_save = '_tmp%05d.png' % numb
    else:
        name_to_save = '_tmpTop%05d.png' % numb
    save_name = path + name_to_save
    plt.savefig(save_name, dpi=None, facecolor='w', edgecolor='w', orientation='portrait', papertype=None,
                format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None)
    plt.close()
                    trajectories_label, json_file = reanInfo(path + name)

                    total_min_lat = []
                    total_min_lng = []
                    total_max_lat = []
                    total_max_lng = []
                    for ell in trajectories_label:
                        real_total_min_lat, real_total_min_lng, real_total_max_lat, real_total_max_lng = find_max_min_coordinates(json_file[ell]["real"], json_file[ell]["generated"], json_file[ell]["trajectory"])
                        total_min_lat.append(real_total_min_lat)
                        total_min_lng.append(real_total_min_lng)
                        total_max_lat.append(real_total_max_lat)
                        total_max_lng.append(real_total_max_lng)

                    min_lat = np.amin(np.array(total_min_lat)) - 0.0001
                    min_lng = np.amin(np.array(total_min_lng)) - 0.0001
                    max_lat = np.amax(np.array(total_max_lat)) + 0.0001
                    max_lng = np.amax(np.array(total_max_lng)) + 0.0001

                logging.debug("Boundaries found!")

            logging.debug("Creating graphs...")
            for i in tqdm.tqdm(range(len(names))):
                name = names[i]
                # name = "trajectory-generatedPoints-" + str(numb) + "-" + str(numb) + ".zip"
                # numb = int(name.replace("trajectory-generatedPoints-", "").replace(".zip", "").split("-")[0])
                if bigOrSmall:
                    name_to_check = "trajectory-generatedPoints-"
                else:
                    name_to_check = "trajectory-generate-aSs-"
                numb = int(name.replace(name_to_check, "").replace(".zip", "").split("-")[0])
                # logging.debug("Analysing " + str(path) + str(name))
Esempio n. 12
0
def manyTrajectories(json_file, trajectories_label):
    total_list_different_trajectories = {}
    real_id_tra = []

    total_trajectories = []

    total_info_trajectories = []
    total_info_fitness = []

    count = 0

    for trajectory in trajectories_label:

        # transform trajectory in lat and lng
        lat_real = json_file[trajectory]["trajectory"][1][0]
        lng_real = json_file[trajectory]["trajectory"][1][1]

        key = str(lat_real) + str(lng_real)

        if key not in total_list_different_trajectories:
            total_list_different_trajectories.update({key: count})
            real_id_tra.append(json_file[trajectory]["id"])
            count += 1

            lat = []
            lng = []
            for el in json_file[trajectory]["trajectory"]:
                lat.append(el[0])
                lng.append(el[1])

            # transform real in lat and lng
            lat_real_h = []
            lng_real_h = []
            for el in json_file[trajectory]["real"]:
                lat_real_h.append(el[0])
                lng_real_h.append(el[1])

            total_trajectories.append({
                "lats_tra": lat,
                "lng_tra": lng,
                "lats_real": lat_real_h,
                "lng_real": lng_real_h
            })

            # transform generated in lat and lng
            lat_generated = []
            lng_generated = []
            for el in json_file[trajectory]["generated"]:
                lat_generated.append(el[0])
                lng_generated.append(el[1])

            fit = json_file[trajectory]["classification"]

            vect_generated = []
            vect_generated.append((lat_generated, lng_generated))
            total_info_trajectories.append(vect_generated)
            vect_fitness = []
            vect_fitness.append(fit)
            total_info_fitness.append(vect_fitness)
        else:
            position = total_list_different_trajectories[key]

            # transform generated in lat and lng
            lat_generated = []
            lng_generated = []
            for el in json_file[trajectory]["generated"]:
                lat_generated.append(el[0])
                lng_generated.append(el[1])

            fit = json_file[trajectory]["classification"]

            total_info_trajectories[position].append(
                (lat_generated, lng_generated))
            total_info_fitness[position].append(fit)

    plt.figure(figsize=(12, 10))
    plt.autoscale(enable=False, tight=True)

    sns.set_style("darkgrid")
    for i in range(len(total_trajectories)):
        j = i + 1
        # plt.subplot(3, 4, j)
        plt.subplot(3, 4, j)

        lats = total_trajectories[i]["lats_tra"]
        lngs = total_trajectories[i]["lng_tra"]

        lat_real = total_trajectories[i]["lats_real"]
        lng_real = total_trajectories[i]["lng_real"]

        lat_g = []
        lng_g = []
        for el in total_info_trajectories[i]:
            lat_g.append(el[0])
            lng_g.append(el[1])

        fitness = total_info_fitness[i]
        max = 150

        min_lat = []
        max_lat = []
        min_lng = []
        max_lng = []
        min_lat.append(np.amin(np.array(lat_g)))
        max_lat.append(np.amax(np.array(lat_g)))
        min_lng.append(np.amin(np.array(lng_g)))
        max_lng.append(np.amax(np.array(lng_g)))
        min_lat.append(np.amin(np.array(lats)))
        max_lat.append(np.amax(np.array(lats)))
        min_lng.append(np.amin(np.array(lngs)))
        max_lng.append(np.amax(np.array(lngs)))
        min_lat.append(np.amin(np.array(lat_real)))
        max_lat.append(np.amax(np.array(lat_real)))
        min_lng.append(np.amin(np.array(lng_real)))
        max_lng.append(np.amax(np.array(lng_real)))
        real_min_lat = np.amin(np.array(min_lat)) - 0.0002
        real_max_lat = np.amax(np.array(max_lat)) + 0.0002
        real_min_lng = np.amin(np.array(min_lng)) - 0.0002
        real_max_lng = np.amax(np.array(max_lng)) + 0.0002

        plt.axis([real_min_lat, real_max_lat, real_min_lng, real_max_lng])
        # plt.xlim(real_min_lat, real_max_lat)
        # plt.ylim(real_min_lng, real_max_lng)
        plt.axis('off')

        # trajectory
        plt.plot(lats, lngs, color='b', marker='o', markersize=1)

        # print real point
        plt.plot(lat_real, lng_real, color='k', marker='o', markersize=1)

        plt.scatter(lat_g,
                    lng_g,
                    c=[fitness],
                    marker='o',
                    vmin=0.,
                    vmax=max,
                    cmap='cool',
                    s=1)

        # print generated point
        # for k in range(len(lat_g)):
        #     plt.plot(lat_g[k], lng_g[k], color='r', marker='o', markersize=1)

    plt.show()
    plt.close()