def test_fill_2(self):
        distance_matrix_with_gaps = DistanceMatrix(
            np.array([
                [0, 1, 3, math.inf, math.inf],
                [1, 0, 2, 3, math.inf],
                [3, 2, 0, 1, 3],
                [math.inf, 3, 1, 0, 2],
                [math.inf, math.inf, 3, 2, 0],
            ]))
        distance_matrix_correct_solution = DistanceMatrix(
            np.array([
                [0, 1, 3, 4, 6],
                [1, 0, 2, 3, 5],
                [3, 2, 0, 1, 3],
                [4, 3, 1, 0, 2],
                [6, 5, 3, 2, 0],
            ]))
        sdf = ShortestDistancesFillerMy()
        distance_matrix_result_my = sdf.fill(distance_matrix_with_gaps)
        sdf = ShortestDistancesFillerIGraph()
        distance_matrix_result_igraph = sdf.fill(distance_matrix_with_gaps)

        self.assertEqual(distance_matrix_result_my,
                         distance_matrix_correct_solution)
        self.assertEqual(distance_matrix_result_igraph,
                         distance_matrix_correct_solution)
Esempio n. 2
0
 def get_distance_matrix_with_gaps_old(self) -> DistanceMatrix:
     distance_matrix_with_gaps = np.copy(self.chromosome.distance_matrix.distance_matrix_nparray)
     if self.full:
         return DistanceMatrix(distance_matrix_with_gaps)
     n = distance_matrix_with_gaps.shape[0]
     for x in range(0, n):
         for y in range(x + 1, n):
             if not [x, y] in self.not_gaps:
                 distance_matrix_with_gaps[x, y] = math.inf
                 distance_matrix_with_gaps[y, x] = math.inf
     return DistanceMatrix(distance_matrix_with_gaps)
Esempio n. 3
0
    def get_distance_matrix_with_gaps(self) -> DistanceMatrix:
        n = self.size
        distance_matrix_with_gaps = np.zeros((n, n))
        distance_matrix_with_gaps.fill(math.inf)
        if self.full:
            return DistanceMatrix(distance_matrix_with_gaps)

        for i in range(0, n):
            distance_matrix_with_gaps[i, i] = 0

        for not_gap in self.not_gaps:
            val = self.chromosome.distance_matrix.distance_matrix_nparray[not_gap[0], not_gap[1]]
            distance_matrix_with_gaps[not_gap[0], not_gap[1]] = val
            distance_matrix_with_gaps[not_gap[1], not_gap[0]] = val

        return DistanceMatrix(distance_matrix_with_gaps)
Esempio n. 4
0
    def from_points(cls, points):
        if points.ndim != 2:
            raise ValueError('"points" should have two dimensions.')
        if points.shape[0] < 3:
            raise ValueError('"points" should contain at least 3 points.')
        if points.shape[1] != 3:
            raise ValueError('"points" should be X*3 (x,y,z).')

        return cls(points, points.shape[0], DistanceMatrix.from_points(points))
Esempio n. 5
0
    def test_get_flatten_upper_triangular_matrix_by_coordinates(self):

        distance_matrix_base = np.array([
            [0, 1, 2, 3],
            [1, 0, 1, 2],
            [2, 1, 0, 1],
            [3, 2, 1, 0],
        ])
        distance_matrix = DistanceMatrix(distance_matrix_base)

        coordinates = [[0, 1], [0, 3], [2, 3]]
        flatten_upper_triangular_matrix_by_coordinates = distance_matrix.get_futm_by_coordinates(
            coordinates)

        correct_flatten_upper_triangular_matrix_by_coordinates = [
            1.0, 3.0, 1.0
        ]
        self.assertEqual(
            flatten_upper_triangular_matrix_by_coordinates,
            correct_flatten_upper_triangular_matrix_by_coordinates)
    def test_fill_1(self):
        distance_matrix_with_gaps = DistanceMatrix(
            np.array([
                [0, 3, 4],
                [3, 0, math.inf],
                [4, math.inf, 0],
            ]))
        distance_matrix_correct_solution = DistanceMatrix(
            np.array([
                [0, 3, 4],
                [3, 0, 7],
                [4, 7, 0],
            ]))
        sdf = ShortestDistancesFillerMy()
        distance_matrix_result_my = sdf.fill(distance_matrix_with_gaps)
        sdf = ShortestDistancesFillerIGraph()
        distance_matrix_result_igraph = sdf.fill(distance_matrix_with_gaps)

        self.assertEqual(distance_matrix_result_my,
                         distance_matrix_correct_solution)
        self.assertEqual(distance_matrix_result_igraph,
                         distance_matrix_correct_solution)
 def compose_distance_matrix(self, gaps_values):
     n = self.size
     d = self.dm_with_known_distances.copy()
     gap_cur = 0
     for x in range(0, n):
         for y in range(x + 1, n):
             if d[x, y] == math.inf:
                 d[x, y] = gaps_values[gap_cur]
                 d[y, x] = gaps_values[gap_cur]
                 gap_cur += 1
     if gap_cur != len(gaps_values):
         raise ValueError('Did not use all provided values!')
     return DistanceMatrix(d)
Esempio n. 8
0
    def test_get_flatten_upper_triangular_matrix_except_ordered_coordinates_wrong_order(
            self):
        distance_matrix_base = np.array([
            [0, 1, 2, 3],
            [1, 0, 1, 2],
            [2, 1, 0, 1],
            [3, 2, 1, 0],
        ])
        distance_matrix = DistanceMatrix(distance_matrix_base)

        coordinates = [[0, 3], [0, 1], [2, 3]]
        self.assertRaises(IndexError,
                          distance_matrix.get_futm_except_ordered_coordinates,
                          coordinates)
Esempio n. 9
0
    def test_get_flatten_upper_triangular_matrix(self):
        points = np.array([
            [1, 0, 0],
            [2, 0, 0],
            [3, 0, 0],
            [4, 0, 0],
        ])
        distance_matrix = DistanceMatrix.from_points(points)
        flatten_upper_triangular_matrix = distance_matrix.get_futm()

        correct_flatten_upper_triangular_matrix = [
            1.0, 2.0, 3.0, 1.0, 2.0, 1.0
        ]
        self.assertEqual(flatten_upper_triangular_matrix,
                         correct_flatten_upper_triangular_matrix)
Esempio n. 10
0
    def from_files(cls, path: str):
        name = os.path.basename(os.path.normpath(path))

        points_path = os.path.join(path, 'points.npy')
        dm_path = os.path.join(path, 'dm.npy')
        if os.path.isfile(points_path):
            points = np.load(points_path)
            chromosome = Chromosome.from_points(points)
        else:
            dm = np.load(dm_path)
            size = dm.shape[0]
            chromosome = Chromosome(np.full((size, 3), 0), size, DistanceMatrix(dm))

        not_gaps_path = os.path.join(path, 'not_gaps.npy')
        not_gaps = np.load(not_gaps_path)

        return cls(chromosome, not_gaps.tolist(), name=name)
Esempio n. 11
0
    def test___init__(self):
        points = np.array([
            [1, 0, 0],
            [2, 0, 0],
            [3, 0, 0],
            [4, 0, 0],
        ])
        distance_matrix = DistanceMatrix.from_points(points)

        correct_distance_matrix = np.array([
            [0, 1, 2, 3],
            [1, 0, 1, 2],
            [2, 1, 0, 1],
            [3, 2, 1, 0],
        ])
        self.assertEqual(distance_matrix.size, 4)
        self.assertTrue(
            np.array_equal(distance_matrix.distance_matrix_nparray,
                           correct_distance_matrix))
Esempio n. 12
0
    def fill(self,
             distance_matrix_with_gaps: DistanceMatrix) -> DistanceMatrix:

        #g = Graph.Adjacency((distance_matrix_with_gaps.distance_matrix_nparray != math.inf).tolist())
        g = igraph.Graph.Weighted_Adjacency(
            distance_matrix_with_gaps.distance_matrix_nparray.tolist(),
            mode=igraph.ADJ_UNDIRECTED)
        # g.vs["label"] = g.vs["name"] = range(0, 10)
        # visual_style = {}
        # visual_style["vertex_label"] = g.vs["name"]
        # visual_style["edge_width"] = g.es["weight"]
        # igraph.plot(g, **visual_style)

        distance_matrix_with_gaps_filled_list = g.shortest_paths_dijkstra(
            weights='weight')

        distance_matrix_with_gaps_filled = np.array(
            distance_matrix_with_gaps_filled_list)

        return DistanceMatrix(distance_matrix_with_gaps_filled)
    def test_compose_distance_matrix(self):
        distance_matrix_base = np.array([
            [0, 1, 2, 3],
            [1, 0, 1, 2],
            [2, 1, 0, 1],
            [3, 2, 1, 0],
        ])
        distance_matrix = DistanceMatrix(distance_matrix_base)
        not_gap_coordinates = [[0, 1], [0, 3], [2, 3]]
        not_gap_values = [1.0, 3.0, 1.0]
        gap_values = [2.0, 1.0, 2.0] # genome

        fitness_calculator = FitnessCalculator(
            score_calculator=None,
            mds=None,
            not_gaps_values=not_gap_values,
            not_gaps_coordinates=not_gap_coordinates,
            size=distance_matrix.size
        )
        result = fitness_calculator.compose_distance_matrix(gap_values)
        self.assertEqual(result, distance_matrix)
Esempio n. 14
0
 def test___init__points_2D(self):
     points_2d = np.array([
         [1, 1],
     ])
     with self.assertRaises(ValueError):
         DistanceMatrix(points_2d)
Esempio n. 15
0
    def fill(self,
             distance_matrix_with_gaps: DistanceMatrix) -> DistanceMatrix:

        distance_matrix_with_gaps_filled = self.floyd_warshall(
            distance_matrix_with_gaps.distance_matrix_nparray)
        return DistanceMatrix(distance_matrix_with_gaps_filled)