-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work with the distance matrix is moved to separate handlers
- Loading branch information
1 parent
5010922
commit 968f361
Showing
6 changed files
with
148 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) 2023, Sofia Vivdich and Anastasiia Kornilova | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import copy | ||
import numpy as np | ||
import zope.interface | ||
|
||
from src.services.distance.interface import IProcessor | ||
from src.utils.distances_utils import dfs | ||
from src.utils.pcd_utils import color_pcd_by_two_groups | ||
from src.utils.pcd_utils import visualize_pcd | ||
|
||
|
||
@zope.interface.implementer(IProcessor) | ||
class ExtractionLargestConnectedComponentProcessor: | ||
def process(self, distance_matrix, points, trace): | ||
"""Extraction the largest connected component of a graph using the dfs algorithm""" | ||
|
||
num_vertices = len(points) | ||
half_num_vertices = num_vertices // 2 | ||
visited_vertices = np.array([False for i in range(num_vertices)], dtype=bool) | ||
for i in range(num_vertices): | ||
visited_vertices = dfs(distance_matrix, i) | ||
if visited_vertices.sum() >= half_num_vertices: | ||
break | ||
|
||
not_visited_vertices = [ | ||
vertex for vertex, is_visited in enumerate(visited_vertices) if not is_visited | ||
] | ||
|
||
#Visualization of the extracted connectivity component against the background of the entire cloud | ||
visualize_pcd(color_pcd_by_two_groups(points, not_visited_vertices)) | ||
|
||
trace_copy = copy.deepcopy(trace) | ||
for index in sorted(not_visited_vertices, reverse=True): | ||
del trace_copy[index] | ||
|
||
return ( | ||
distance_matrix[visited_vertices][:, visited_vertices], | ||
points[visited_vertices], | ||
trace_copy, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright (c) 2023, Sofia Vivdich and Anastasiia Kornilova | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import zope.interface | ||
|
||
|
||
class IProcessor(zope.interface.Interface): | ||
def process(distance_matrix, points, trace): | ||
"""Extracting only those points that have certain properties | ||
based on the distance matrix. | ||
The result of processing is a distance matrix, a set of points | ||
and a trace that contains only those points that satisfy the desired property. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright (c) 2023, Sofia Vivdich and Anastasiia Kornilova | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import copy | ||
import numpy as np | ||
import zope.interface | ||
|
||
from src.services.distance.interface import IProcessor | ||
|
||
|
||
@zope.interface.implementer(IProcessor) | ||
class RemovingIsolatedPointsProcessor: | ||
def process(self, distance_matrix, points, trace): | ||
"""Removing isolated points that have all 0s in the distance matrix except the diagonal element""" | ||
|
||
mask_isolated = np.all(distance_matrix - np.eye(distance_matrix.shape[0]) == 0, axis=1) | ||
isolated_points = np.array([i for i in range(len(points))], dtype=int)[ | ||
mask_isolated | ||
] | ||
|
||
trace_copy = copy.deepcopy(trace) | ||
for index in sorted(isolated_points, reverse=True): | ||
del trace_copy[index] | ||
|
||
mask_not_isolated = np.any(distance_matrix - np.eye(distance_matrix.shape[0]) != 0, axis=1) | ||
|
||
return ( | ||
distance_matrix[mask_not_isolated][:, mask_not_isolated], | ||
points[mask_not_isolated], | ||
trace_copy, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters