Skip to content

Commit d4d2111

Browse files
committed
improve performance of path weight calcs
It is more efficient to increase the weight of an edge/path which has a default np.array([0, 0]) by direct indexing insted of creating a new array and adding it ca 30% faster removed mutable default argument
1 parent 17e474a commit d4d2111

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,5 @@ prof/*
110110
# ==================
111111

112112
.vscode/
113-
docs/_build/*
113+
docs/_build/
114114
.pytest_cache/v/cache/lastfailed

pathpy/classes/paths.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def expand_subpaths(self):
562562
path_slice = path[s:s + k + 1]
563563
self.paths[k][path_slice][0] += frequency
564564

565-
def add_path_tuple(self, path, expand_subpaths=True, frequency=np.array([0, 1])):
565+
def add_path_tuple(self, path, expand_subpaths=True, frequency=(0, 1)):
566566
"""Adds a tuple of elements as a path. If the elements are not strings,
567567
a conversion to strings will be made. This function can be used to
568568
to set custom subpath statistics, via the frequency tuple (see below).
@@ -605,7 +605,7 @@ def add_path_tuple(self, path, expand_subpaths=True, frequency=np.array([0, 1]))
605605
for i in range(s, s + k + 1):
606606
subpath += (path_str[i],)
607607
# add subpath weight to first component of occurrences
608-
self.paths[k][subpath] += np.array([frequency[1], 0])
608+
self.paths[k][subpath][0] += frequency[1]
609609

610610
def add_path_ngram(self, ngram, separator=',', expand_subpaths=True, frequency=None):
611611
"""Adds the path(s) of a single n-gram to the path statistics object.
@@ -639,20 +639,20 @@ def add_path_ngram(self, ngram, separator=',', expand_subpaths=True, frequency=N
639639

640640
# add the occurrences as *longest* path to the second component of the numpy array
641641
if frequency is not None:
642-
self.paths[path_length][path] += np.array([0, frequency])
642+
self.paths[path_length][path][1] += frequency
643643
else:
644-
self.paths[path_length][path] += np.array([0, 1])
644+
self.paths[path_length][path][1] += 1
645645

646646
if expand_subpaths:
647647
max_length = min(self.max_subpath_length + 1, len(path) - 1)
648648
if frequency is not None:
649649
for k in range(max_length):
650650
for s in range(path_length - k + 1):
651-
self.paths[k][path[s:s + k + 1]] += np.array([frequency, 0])
651+
self.paths[k][path[s:s + k + 1]][0] += frequency
652652
else:
653653
for k in range(max_length):
654654
for s in range(path_length - k + 1):
655-
self.paths[k][path[s:s + k + 1]] += np.array([1, 0])
655+
self.paths[k][path[s:s + k + 1]][0] += 1
656656

657657
@staticmethod
658658
def contained_paths(p, node_filter):

pathpy/path_extraction/temporal_paths.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
from collections import defaultdict
2626
import sys
27-
import numpy as np
2827

2928
from pathpy import Paths
3029
from pathpy.utils import Log
@@ -173,7 +172,7 @@ class Paths, which can subsequently be used to generate higher-order network
173172
path = (x[0][0],)
174173
for edge in x:
175174
path += (edge[1],)
176-
p.paths[len(x)][path] += np.array([0, 1])
175+
p.paths[len(x)][path][1] += 1
177176

178177
# expand sub paths of longest paths
179178
p.expand_subpaths()

0 commit comments

Comments
 (0)