Skip to content

Commit 17e474a

Browse files
committed
double speed of expand subpaths
1 parent 0f1c622 commit 17e474a

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed

pathpy/classes/paths.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ def expand_subpaths(self):
559559
for s in range(path_length - k + 1):
560560
# Add frequency as a subpath to *first* entry of occurrence
561561
# counter
562-
self.paths[k][path[s:s + k + 1]] += np.array([frequency, 0])
562+
path_slice = path[s:s + k + 1]
563+
self.paths[k][path_slice][0] += frequency
563564

564565
def add_path_tuple(self, path, expand_subpaths=True, frequency=np.array([0, 1])):
565566
"""Adds a tuple of elements as a path. If the elements are not strings,
@@ -583,8 +584,9 @@ def add_path_tuple(self, path, expand_subpaths=True, frequency=np.array([0, 1]))
583584
assert path, 'Error: paths needs to contain at least one element'
584585

585586
for x in path:
586-
if self.separator in x:
587-
raise PathpyError('Error: Node name contains separator character. Choose different separator.')
587+
if isinstance(x, str) and self.separator in x:
588+
raise PathpyError('Error: Node name contains separator character. '
589+
'Choose different separator.')
588590

589591
path_str = path if isinstance(path, str) else tuple(map(str, path))
590592

@@ -629,8 +631,9 @@ def add_path_ngram(self, ngram, separator=',', expand_subpaths=True, frequency=N
629631
"""
630632
path = tuple(ngram.split(separator))
631633
for x in path:
632-
if self.separator in x:
633-
raise PathpyError('Error: Node name contains separator character. Choose different separator.')
634+
if isinstance(x, str) and self.separator in x:
635+
raise PathpyError('Error: Node name contains separator character.'
636+
'Choose different separator.')
634637

635638
path_length = len(path) - 1
636639

tests/test_DAG.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,16 @@ def test_dag_path_mapping_to_many(dag_object):
8888
dag.topsort()
8989

9090
mapping = {
91-
'a': {1, 2},
92-
'b': {2, 5},
93-
'c': {5},
94-
'e': {1, 2},
95-
'f': {2, 3},
96-
'g': {2, 5},
97-
'h': {1},
98-
'i': {1, 5},
91+
'a': {1, 2}, 'b': {2, 5},
92+
'c': {5}, 'e': {1, 2},
93+
'f': {2, 3}, 'g': {2, 5},
94+
'h': {1}, 'i': {1, 5},
9995
'j': {4}
10096
}
10197
paths_mapped2 = pp.path_extraction.paths_from_dag(dag, node_mapping=mapping)
10298

103-
assert paths_mapped2.observation_count is None
99+
assert paths_mapped2.observation_count == 55
100+
assert set(paths_mapped2.nodes()) == {'1', '2', '3', '4', '5'}
104101

105102

106103
edges1, types1 = [(1, 2), (1, 3), (2, 3)], ({1}, {2}, {3})
@@ -150,27 +147,6 @@ def test_remove_edge(dag_object: pp.DAG):
150147
assert 'e' in dag_object.isolate_nodes()
151148

152149

153-
def test_set_path_expansion(dag_object: pp.DAG):
154-
from collections import defaultdict
155-
mapping = {'a': {1, 2},
156-
'b': {2, 5},
157-
'c': {5},
158-
'e': {1, 2},
159-
'f': {2, 3},
160-
'g': {2, 5},
161-
'h': {1},
162-
'i': {1, 5},
163-
'j': {4}
164-
}
165-
166-
path_count = defaultdict(lambda: 0)
167-
# for root in dag_object.roots:
168-
# for set_path in dag_object.routes_from_node(root):
169-
170-
171-
172-
173-
174150

175151

176152

tests/test_Path.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,5 +250,13 @@ def test_pickle(random_paths, tmpdir):
250250
assert paths.observation_count == paths.observation_count
251251

252252

253+
# def test_expand_subpaths(random_paths):
254+
# paths = random_paths(2000, 0, 400)
255+
# paths.expand_subpaths()
256+
#
257+
#
258+
# import pytest
259+
# pytest.main(__file__)
260+
253261

254262

0 commit comments

Comments
 (0)