pysbm 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pysbm/__init__.py +8 -0
- pysbm/additional_types.py +451 -0
- pysbm/parallel_execution.py +121 -0
- pysbm/sbm/__init__.py +76 -0
- pysbm/sbm/exceptions.py +6 -0
- pysbm/sbm/hierarchical_inference.py +366 -0
- pysbm/sbm/inference.py +2102 -0
- pysbm/sbm/model_selection.py +609 -0
- pysbm/sbm/nxpartitiongraphbased.py +2315 -0
- pysbm/sbm/objective_function_iclex.py +1407 -0
- pysbm/sbm/objective_function_newman_group_size.py +1512 -0
- pysbm/sbm/objective_functions.py +1060 -0
- pysbm/sbm/partition.py +901 -0
- pysbm/sbm/peixotos_flat_sbm.py +1788 -0
- pysbm/sbm/peixotos_flat_sbm_full.py +446 -0
- pysbm/sbm/peixotos_hierarchical_sbm.py +1596 -0
- pysbm/sbm/peixotos_hierarchical_sbm_full.py +247 -0
- pysbm/sbm/peixotos_hierarchical_sbm_tools.py +199 -0
- pysbm/sbm/peixotos_hierarchical_sbm_tools_full.py +64 -0
- pysbm/test_ground.py +634 -0
- pysbm/test_ground_new.py +2073 -0
- pysbm-0.0.1.dist-info/METADATA +84 -0
- pysbm-0.0.1.dist-info/RECORD +26 -0
- pysbm-0.0.1.dist-info/WHEEL +5 -0
- pysbm-0.0.1.dist-info/licenses/LICENSE.txt +429 -0
- pysbm-0.0.1.dist-info/top_level.txt +1 -0
pysbm/__init__.py
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) 2026 Thorben Funke
|
|
2
|
+
# SPDX-License-Identifier: CC-BY-SA-4.0
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Module for additional needed types
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import copy
|
|
9
|
+
import functools
|
|
10
|
+
import heapq as hp
|
|
11
|
+
import random
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SetDict:
|
|
15
|
+
"""
|
|
16
|
+
Type which implements the following operations with a runtime better than O(n):
|
|
17
|
+
- adding of items (once)
|
|
18
|
+
- removal of items
|
|
19
|
+
- random access
|
|
20
|
+
additional the operation copy() is needed.
|
|
21
|
+
This implementation is based on a post of Amber on stackoverflow:
|
|
22
|
+
http://stackoverflow.com/questions/15993447/python-data-structure-for-efficient-add-remove-and-random-choice
|
|
23
|
+
Accessed 5.5.2017 10:11
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
self.item_to_position = {}
|
|
28
|
+
self.items = []
|
|
29
|
+
|
|
30
|
+
def add(self, item):
|
|
31
|
+
"""Add a new item"""
|
|
32
|
+
if item in self.item_to_position:
|
|
33
|
+
raise KeyError()
|
|
34
|
+
# return
|
|
35
|
+
self.items.append(item)
|
|
36
|
+
self.item_to_position[item] = len(self.items) - 1
|
|
37
|
+
|
|
38
|
+
def remove(self, item):
|
|
39
|
+
"""Remove an item"""
|
|
40
|
+
position = self.item_to_position.pop(item)
|
|
41
|
+
last_item = self.items.pop()
|
|
42
|
+
if position != len(self.items):
|
|
43
|
+
self.items[position] = last_item
|
|
44
|
+
self.item_to_position[last_item] = position
|
|
45
|
+
|
|
46
|
+
def choose_random(self):
|
|
47
|
+
"""Return a random element"""
|
|
48
|
+
return random.choice(self.items)
|
|
49
|
+
|
|
50
|
+
def copy(self):
|
|
51
|
+
"""Create a copy"""
|
|
52
|
+
new = SetDict()
|
|
53
|
+
new.item_to_position = self.item_to_position.copy()
|
|
54
|
+
new.items = self.items[:]
|
|
55
|
+
return new
|
|
56
|
+
|
|
57
|
+
def __repr__(self):
|
|
58
|
+
return str(self.items)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class ListDict:
|
|
62
|
+
"""
|
|
63
|
+
Type which implements the following operations with a runtime better than O(n):
|
|
64
|
+
- adding of items (even multiple times the same)
|
|
65
|
+
- removal of items
|
|
66
|
+
- random access
|
|
67
|
+
additional the operation copy() is needed.
|
|
68
|
+
This implementation is based on a post of Amber on stackoverflow:
|
|
69
|
+
http://stackoverflow.com/questions/15993447/python-data-structure-for-efficient-add-remove-and-random-choice
|
|
70
|
+
Accessed 5.5.2017 10:11
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
def __init__(self):
|
|
74
|
+
self.item_to_position = {}
|
|
75
|
+
self.items = []
|
|
76
|
+
|
|
77
|
+
def add(self, item):
|
|
78
|
+
"""Add a new item"""
|
|
79
|
+
self.items.append(item)
|
|
80
|
+
if item in self.item_to_position:
|
|
81
|
+
self.item_to_position[item].add(len(self.items) - 1)
|
|
82
|
+
else:
|
|
83
|
+
self.item_to_position[item] = {len(self.items) - 1}
|
|
84
|
+
|
|
85
|
+
def remove(self, item):
|
|
86
|
+
"""Remove an item"""
|
|
87
|
+
positions = self.item_to_position[item]
|
|
88
|
+
item_position = positions.pop()
|
|
89
|
+
|
|
90
|
+
# if set is empty remove entry from dict
|
|
91
|
+
if not positions:
|
|
92
|
+
del self.item_to_position[item]
|
|
93
|
+
|
|
94
|
+
# exchange entry in items list with the last entry of the list and
|
|
95
|
+
# keep track of this change in the corresponding item_to_position set
|
|
96
|
+
last_item = self.items.pop()
|
|
97
|
+
if item_position != len(self.items):
|
|
98
|
+
self.items[item_position] = last_item
|
|
99
|
+
self.item_to_position[last_item].add(item_position)
|
|
100
|
+
self.item_to_position[last_item].remove(len(self.items))
|
|
101
|
+
|
|
102
|
+
def choose_random(self):
|
|
103
|
+
"""Return a random element"""
|
|
104
|
+
return random.choice(self.items)
|
|
105
|
+
|
|
106
|
+
def copy(self):
|
|
107
|
+
"""Create a copy"""
|
|
108
|
+
new = ListDict()
|
|
109
|
+
new.item_to_position = copy.deepcopy(self.item_to_position)
|
|
110
|
+
new.items = self.items[:]
|
|
111
|
+
return new
|
|
112
|
+
|
|
113
|
+
def __repr__(self):
|
|
114
|
+
return str(self.items)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class WeightedListDict:
|
|
118
|
+
"""
|
|
119
|
+
Type which implements the following operations with a runtime better than:
|
|
120
|
+
- adding of items (even multiple times the same -> reduced to weight)
|
|
121
|
+
- removal of items
|
|
122
|
+
- choose random element (with probabilities proportional to the weights)
|
|
123
|
+
additional the operation copy() is needed.
|
|
124
|
+
|
|
125
|
+
This implementation saves space by only counting the times an element is added to the list.
|
|
126
|
+
Additionally this allows different weights for the element. Nevertheless it's slow down
|
|
127
|
+
the runtime of random access because concerning the different weights costs time.
|
|
128
|
+
|
|
129
|
+
This implementation is based on a post of Amber on stackoverflow:
|
|
130
|
+
http://stackoverflow.com/questions/15993447/python-data-structure-for-efficient-add-remove-and-random-choice
|
|
131
|
+
Accessed 5.5.2017 10:11
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def __init__(self):
|
|
135
|
+
self.item_to_position = {}
|
|
136
|
+
self.items = []
|
|
137
|
+
self.weights = []
|
|
138
|
+
# we save the total way for an easy implementation of random access
|
|
139
|
+
self.total_weight = 0
|
|
140
|
+
|
|
141
|
+
def add(self, item, weight=1):
|
|
142
|
+
"""Add a new item"""
|
|
143
|
+
self.total_weight += weight
|
|
144
|
+
if item in self.item_to_position:
|
|
145
|
+
self.weights[self.item_to_position[item]] += weight
|
|
146
|
+
else:
|
|
147
|
+
self.items.append(item)
|
|
148
|
+
self.weights.append(weight)
|
|
149
|
+
self.item_to_position[item] = len(self.items) - 1
|
|
150
|
+
|
|
151
|
+
def remove(self, item, weight=1):
|
|
152
|
+
"""Remove an item"""
|
|
153
|
+
position = self.item_to_position[item]
|
|
154
|
+
self.weights[position] -= weight
|
|
155
|
+
self.total_weight -= weight
|
|
156
|
+
|
|
157
|
+
# Element was completely removed from the list
|
|
158
|
+
# WARNING the actual zero check only works perfect for ints,
|
|
159
|
+
# -> floats have to be checked with abs < epsilon
|
|
160
|
+
if self.weights[position] == 0:
|
|
161
|
+
del self.item_to_position[item]
|
|
162
|
+
|
|
163
|
+
# exchange entry if last entry was removed, remove only 0 weight entry
|
|
164
|
+
last_item = self.items.pop()
|
|
165
|
+
if position != len(self.items):
|
|
166
|
+
self.items[position] = last_item
|
|
167
|
+
self.weights[position] = self.weights.pop()
|
|
168
|
+
self.item_to_position[last_item] = position
|
|
169
|
+
else:
|
|
170
|
+
self.weights.pop()
|
|
171
|
+
|
|
172
|
+
def choose_random(self):
|
|
173
|
+
"""Return a random element"""
|
|
174
|
+
# first get random weight where we will stop
|
|
175
|
+
random_weight = random.random() * self.total_weight
|
|
176
|
+
for position, actual_weight in enumerate(self.weights):
|
|
177
|
+
random_weight -= actual_weight
|
|
178
|
+
if random_weight < 0:
|
|
179
|
+
return self.items[position]
|
|
180
|
+
|
|
181
|
+
def copy(self):
|
|
182
|
+
"""Create a copy"""
|
|
183
|
+
new = WeightedListDict()
|
|
184
|
+
new.item_to_position = self.item_to_position.copy()
|
|
185
|
+
new.items = self.items[:]
|
|
186
|
+
new.weights = self.weights[:]
|
|
187
|
+
return new
|
|
188
|
+
|
|
189
|
+
#
|
|
190
|
+
# def __getitem__(self, item):
|
|
191
|
+
# return self.weights[self.item_to_position[item]]
|
|
192
|
+
|
|
193
|
+
def __repr__(self):
|
|
194
|
+
return str(list(zip(self.items, self.weights)))
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
# noinspection PyPep8Naming
|
|
198
|
+
class nHeap:
|
|
199
|
+
"""
|
|
200
|
+
Data Structure for saving the max n Elements (tuples (value, item_data))
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
def __init__(self, max_elements):
|
|
204
|
+
# create min heap
|
|
205
|
+
self._data = []
|
|
206
|
+
hp.heapify(self.data)
|
|
207
|
+
|
|
208
|
+
self.max_elements = max_elements
|
|
209
|
+
|
|
210
|
+
def push_try(self, data):
|
|
211
|
+
"""
|
|
212
|
+
Add element if data contains not more than max_elements else exchange
|
|
213
|
+
the lowest element if new element is greater
|
|
214
|
+
"""
|
|
215
|
+
if len(self._data) < self.max_elements:
|
|
216
|
+
hp.heappush(self.data, data)
|
|
217
|
+
elif self._data[0][0] < data[0]:
|
|
218
|
+
hp.heapreplace(self.data, data)
|
|
219
|
+
|
|
220
|
+
@property
|
|
221
|
+
def data(self):
|
|
222
|
+
"""Returns the contained data"""
|
|
223
|
+
return self._data
|
|
224
|
+
|
|
225
|
+
def __getitem__(self, key):
|
|
226
|
+
return self.data[key]
|
|
227
|
+
|
|
228
|
+
def __iter__(self):
|
|
229
|
+
yield from self._data
|
|
230
|
+
|
|
231
|
+
def __len__(self):
|
|
232
|
+
return len(self.data)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class Node:
|
|
236
|
+
"""
|
|
237
|
+
Node with data and link to next node
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
def __init__(self, data=None, next_node=None):
|
|
241
|
+
self._data = data
|
|
242
|
+
self._next_node = next_node
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def next_node(self):
|
|
246
|
+
"""Return reference to next node"""
|
|
247
|
+
return self._next_node
|
|
248
|
+
|
|
249
|
+
@next_node.setter
|
|
250
|
+
def next_node(self, new_node):
|
|
251
|
+
"""Set reference to next node"""
|
|
252
|
+
self._next_node = new_node
|
|
253
|
+
|
|
254
|
+
@property
|
|
255
|
+
def data(self):
|
|
256
|
+
"""Return contained value"""
|
|
257
|
+
return self._data
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class LinkedList:
|
|
261
|
+
"""
|
|
262
|
+
Implementation of a simple linked list
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
def __init__(self):
|
|
266
|
+
self.head = None
|
|
267
|
+
self.tail = None
|
|
268
|
+
|
|
269
|
+
def is_empty(self):
|
|
270
|
+
"""Check if List is empty"""
|
|
271
|
+
return self.head is None
|
|
272
|
+
|
|
273
|
+
def append(self, data, as_first_element=False):
|
|
274
|
+
"""Append new data to linked list"""
|
|
275
|
+
# if list is Empty...
|
|
276
|
+
if self.head is None:
|
|
277
|
+
node = Node(data)
|
|
278
|
+
self.head = node
|
|
279
|
+
self.tail = node
|
|
280
|
+
elif as_first_element:
|
|
281
|
+
node = Node(data, self.head)
|
|
282
|
+
self.head = node
|
|
283
|
+
else:
|
|
284
|
+
node = Node(data)
|
|
285
|
+
self.tail.next_node = node
|
|
286
|
+
self.tail = node
|
|
287
|
+
|
|
288
|
+
def get_data(self):
|
|
289
|
+
"""Get first data"""
|
|
290
|
+
return self.head.data
|
|
291
|
+
|
|
292
|
+
def merge(self, another_list):
|
|
293
|
+
"""Merge two lists"""
|
|
294
|
+
if another_list.get_data() >= self.get_data():
|
|
295
|
+
# ensure that tail is real tail...
|
|
296
|
+
while self.tail.next_node is not None:
|
|
297
|
+
self.tail = self.tail.next_node
|
|
298
|
+
self.tail.next_node = another_list.head
|
|
299
|
+
self.tail = another_list.tail
|
|
300
|
+
another_list.change_head(self.head)
|
|
301
|
+
else:
|
|
302
|
+
another_list.merge(self)
|
|
303
|
+
|
|
304
|
+
def change_head(self, node):
|
|
305
|
+
"""Change head - needed for merge"""
|
|
306
|
+
self.head = node
|
|
307
|
+
|
|
308
|
+
def pop(self):
|
|
309
|
+
"""Return first data and remove it from the list"""
|
|
310
|
+
if self.head is None:
|
|
311
|
+
raise IndexError
|
|
312
|
+
value = self.head.data
|
|
313
|
+
self.head = self.head.next_node
|
|
314
|
+
return value
|
|
315
|
+
|
|
316
|
+
def __iter__(self):
|
|
317
|
+
return LinkedListIterator(self)
|
|
318
|
+
|
|
319
|
+
def __repr__(self):
|
|
320
|
+
return str(list(self))
|
|
321
|
+
|
|
322
|
+
# implement operations needed for the desired comparison
|
|
323
|
+
def __eq__(self, other):
|
|
324
|
+
if isinstance(other, self.__class__):
|
|
325
|
+
return self.head == other.head
|
|
326
|
+
return NotImplemented
|
|
327
|
+
|
|
328
|
+
def __ne__(self, other):
|
|
329
|
+
return not self.__eq__(other)
|
|
330
|
+
|
|
331
|
+
def __hash__(self):
|
|
332
|
+
return hash(self.head)
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
class LinkedListIterator:
|
|
336
|
+
"""Iterator over a LinkedList"""
|
|
337
|
+
|
|
338
|
+
# pylint: disable=too-few-public-methods
|
|
339
|
+
# this class is only an iterator and don't need any additional methods
|
|
340
|
+
def __init__(self, linked_list):
|
|
341
|
+
self.next_node = linked_list.head
|
|
342
|
+
|
|
343
|
+
def __iter__(self):
|
|
344
|
+
return self
|
|
345
|
+
|
|
346
|
+
def next(self):
|
|
347
|
+
"""
|
|
348
|
+
Returns data and set pointer to next node or raise StopIteration
|
|
349
|
+
if end of list is reached
|
|
350
|
+
"""
|
|
351
|
+
if self.next_node is None:
|
|
352
|
+
raise StopIteration()
|
|
353
|
+
else:
|
|
354
|
+
value = self.next_node.data
|
|
355
|
+
self.next_node = self.next_node.next_node
|
|
356
|
+
return value
|
|
357
|
+
|
|
358
|
+
__next__ = next
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
@functools.total_ordering
|
|
362
|
+
class Observed:
|
|
363
|
+
"""Object containing a value and a list of interested observers"""
|
|
364
|
+
|
|
365
|
+
def __init__(self, value):
|
|
366
|
+
self._value = value
|
|
367
|
+
self.observers = []
|
|
368
|
+
|
|
369
|
+
def add(self, observer):
|
|
370
|
+
"""Add a new observer"""
|
|
371
|
+
self.observers.append(observer)
|
|
372
|
+
|
|
373
|
+
def update_ref(self, new_observed):
|
|
374
|
+
"""Change observed object for all interested observers"""
|
|
375
|
+
for observer in self.observers:
|
|
376
|
+
observer.update_ref(new_observed)
|
|
377
|
+
|
|
378
|
+
@property
|
|
379
|
+
def value(self):
|
|
380
|
+
"""Return the contained value"""
|
|
381
|
+
return self._value
|
|
382
|
+
|
|
383
|
+
@value.setter
|
|
384
|
+
def value(self, value):
|
|
385
|
+
"""Change the contained value (dos not call an update of observers)"""
|
|
386
|
+
self._value = value
|
|
387
|
+
|
|
388
|
+
def __eq__(self, other):
|
|
389
|
+
if isinstance(other, self.__class__):
|
|
390
|
+
return self.value == other.value
|
|
391
|
+
return NotImplemented
|
|
392
|
+
|
|
393
|
+
def __le__(self, other):
|
|
394
|
+
if isinstance(other, self.__class__):
|
|
395
|
+
return self.value <= other.value
|
|
396
|
+
return NotImplemented
|
|
397
|
+
|
|
398
|
+
def __hash__(self):
|
|
399
|
+
return hash(self.value)
|
|
400
|
+
|
|
401
|
+
def __repr__(self):
|
|
402
|
+
return str(self.value) + " :-> " + str(self.observers)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
@functools.total_ordering
|
|
406
|
+
class Observer:
|
|
407
|
+
"""Class watching at an observed object"""
|
|
408
|
+
|
|
409
|
+
def __init__(self, observed):
|
|
410
|
+
self.observed = observed
|
|
411
|
+
self.observed.add(self)
|
|
412
|
+
|
|
413
|
+
def update_ref(self, new_observed):
|
|
414
|
+
"""Update observed object"""
|
|
415
|
+
self.observed = new_observed
|
|
416
|
+
self.observed.add(self)
|
|
417
|
+
|
|
418
|
+
def get_value(self):
|
|
419
|
+
"""Return the value of the observed object"""
|
|
420
|
+
return self.observed.value
|
|
421
|
+
|
|
422
|
+
def get_observed(self):
|
|
423
|
+
"""Return the observed object"""
|
|
424
|
+
return self.observed
|
|
425
|
+
|
|
426
|
+
def update_observed(self, new_observed):
|
|
427
|
+
"""
|
|
428
|
+
Call the observed object to update all interested objects to change to
|
|
429
|
+
the new observed object
|
|
430
|
+
"""
|
|
431
|
+
self.observed.update_ref(new_observed)
|
|
432
|
+
|
|
433
|
+
def set_value(self, value):
|
|
434
|
+
"""Change value of the observed object"""
|
|
435
|
+
self.observed.value = value
|
|
436
|
+
|
|
437
|
+
def __eq__(self, other):
|
|
438
|
+
if isinstance(other, self.__class__):
|
|
439
|
+
return self.observed == other.observed
|
|
440
|
+
return NotImplemented
|
|
441
|
+
|
|
442
|
+
def __le__(self, other):
|
|
443
|
+
if isinstance(other, self.__class__):
|
|
444
|
+
return self.observed <= other.observed
|
|
445
|
+
return NotImplemented
|
|
446
|
+
|
|
447
|
+
def __hash__(self):
|
|
448
|
+
return hash(self.observed)
|
|
449
|
+
|
|
450
|
+
def __repr__(self):
|
|
451
|
+
return str(self.get_value())
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) 2026 Thorben Funke
|
|
2
|
+
# SPDX-License-Identifier: CC-BY-SA-4.0
|
|
3
|
+
|
|
4
|
+
import concurrent.futures
|
|
5
|
+
import os
|
|
6
|
+
import pickle
|
|
7
|
+
import time
|
|
8
|
+
|
|
9
|
+
TEMP_DIRECTORY = "_temp"
|
|
10
|
+
TEMP_FILE_NAMES = "results_"
|
|
11
|
+
|
|
12
|
+
SUCCESS_MESSAGE_AND_SAVE_EVERY_X_PERCENT = 5
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def parallel_execution(
|
|
16
|
+
function_to_call,
|
|
17
|
+
arguments_dict,
|
|
18
|
+
max_workers=1,
|
|
19
|
+
maximum_time_per_function_call=10,
|
|
20
|
+
save_temporary_results=True,
|
|
21
|
+
save_method=None,
|
|
22
|
+
save_memory=False,
|
|
23
|
+
):
|
|
24
|
+
# create directory for storing temp files
|
|
25
|
+
if save_temporary_results and not os.path.exists(TEMP_DIRECTORY):
|
|
26
|
+
os.makedirs(TEMP_DIRECTORY)
|
|
27
|
+
|
|
28
|
+
file_paths = []
|
|
29
|
+
with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) as executor:
|
|
30
|
+
result_by_key = {}
|
|
31
|
+
keys_with_error = []
|
|
32
|
+
success_message_percentile = SUCCESS_MESSAGE_AND_SAVE_EVERY_X_PERCENT / 100
|
|
33
|
+
next_finished_message = success_message_percentile
|
|
34
|
+
success_message_threshold = success_message_percentile
|
|
35
|
+
number_of_function_calls_finished = 0
|
|
36
|
+
|
|
37
|
+
futures_to_key = {
|
|
38
|
+
executor.submit(function_to_call, *arguments_dict[key]): key
|
|
39
|
+
for key in arguments_dict
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
print(
|
|
43
|
+
"Started parallel execution at",
|
|
44
|
+
time.ctime(),
|
|
45
|
+
"\n",
|
|
46
|
+
len(arguments_dict),
|
|
47
|
+
"number of functions to call",
|
|
48
|
+
)
|
|
49
|
+
try:
|
|
50
|
+
for future in concurrent.futures.as_completed(
|
|
51
|
+
futures_to_key,
|
|
52
|
+
timeout=maximum_time_per_function_call * len(arguments_dict),
|
|
53
|
+
):
|
|
54
|
+
key = futures_to_key[future]
|
|
55
|
+
|
|
56
|
+
try:
|
|
57
|
+
result = future.result()
|
|
58
|
+
except Exception as exc:
|
|
59
|
+
print(f"{key!r} generated an exception: {exc}")
|
|
60
|
+
keys_with_error.append(key)
|
|
61
|
+
else:
|
|
62
|
+
result_by_key[key] = result
|
|
63
|
+
|
|
64
|
+
del futures_to_key[future]
|
|
65
|
+
|
|
66
|
+
if len(result_by_key) / len(arguments_dict) > success_message_threshold:
|
|
67
|
+
if save_temporary_results:
|
|
68
|
+
file_path = (
|
|
69
|
+
TEMP_DIRECTORY
|
|
70
|
+
+ "/"
|
|
71
|
+
+ TEMP_FILE_NAMES
|
|
72
|
+
+ str(round(next_finished_message * 100))
|
|
73
|
+
)
|
|
74
|
+
if save_method is None:
|
|
75
|
+
try:
|
|
76
|
+
with open(file_path, "wb") as file:
|
|
77
|
+
pickle.dump(result_by_key, file)
|
|
78
|
+
except pickle.PickleError:
|
|
79
|
+
print("Save not worked")
|
|
80
|
+
else:
|
|
81
|
+
file_paths.append(file_path)
|
|
82
|
+
print("Saved file:", file_path)
|
|
83
|
+
else:
|
|
84
|
+
file_path = save_method(result_by_key, file_path)
|
|
85
|
+
file_paths.append(file_path)
|
|
86
|
+
print("Saved file:", file_path)
|
|
87
|
+
if save_memory:
|
|
88
|
+
number_of_function_calls_finished += len(result_by_key)
|
|
89
|
+
# delete saved results
|
|
90
|
+
del result_by_key
|
|
91
|
+
result_by_key = {}
|
|
92
|
+
success_message_threshold = 0
|
|
93
|
+
else:
|
|
94
|
+
number_of_function_calls_finished = len(result_by_key)
|
|
95
|
+
|
|
96
|
+
success_message_threshold += success_message_percentile
|
|
97
|
+
print(
|
|
98
|
+
round(
|
|
99
|
+
number_of_function_calls_finished
|
|
100
|
+
/ len(arguments_dict)
|
|
101
|
+
* 100
|
|
102
|
+
),
|
|
103
|
+
"% completed at",
|
|
104
|
+
time.ctime(),
|
|
105
|
+
)
|
|
106
|
+
next_finished_message += success_message_percentile
|
|
107
|
+
except concurrent.futures.TimeoutError:
|
|
108
|
+
keys_with_error.extend(
|
|
109
|
+
set(futures_to_key.values()).difference(result_by_key.keys())
|
|
110
|
+
)
|
|
111
|
+
print(
|
|
112
|
+
str(len(keys_with_error))
|
|
113
|
+
+ " function calls of"
|
|
114
|
+
+ str(function_to_call)
|
|
115
|
+
+ "did not finished in a total of "
|
|
116
|
+
+ str(maximum_time_per_function_call * len(arguments_dict))
|
|
117
|
+
+ "seconds. Not finished argument keys: "
|
|
118
|
+
+ str(keys_with_error)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
return result_by_key, keys_with_error
|
pysbm/sbm/__init__.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) 2026 Thorben Funke
|
|
2
|
+
# SPDX-License-Identifier: CC-BY-SA-4.0
|
|
3
|
+
|
|
4
|
+
from .exceptions import NoFreeNodeException as NoFreeNodeException
|
|
5
|
+
from .hierarchical_inference import (
|
|
6
|
+
HierarchicalInference as HierarchicalInference,
|
|
7
|
+
)
|
|
8
|
+
from .hierarchical_inference import (
|
|
9
|
+
PeixotoHierarchicalInference as PeixotoHierarchicalInference,
|
|
10
|
+
)
|
|
11
|
+
from .inference import *
|
|
12
|
+
from .model_selection import *
|
|
13
|
+
from .nxpartitiongraphbased import (
|
|
14
|
+
NxHierarchicalPartition as NxHierarchicalPartition,
|
|
15
|
+
)
|
|
16
|
+
from .nxpartitiongraphbased import (
|
|
17
|
+
NxPartitionGraphBased as NxPartitionGraphBased,
|
|
18
|
+
)
|
|
19
|
+
from .nxpartitiongraphbased import (
|
|
20
|
+
NxPartitionGraphBasedWithMoveCounter as NxPartitionGraphBasedWithMoveCounter,
|
|
21
|
+
)
|
|
22
|
+
from .objective_function_iclex import (
|
|
23
|
+
IntegratedCompleteLikelihoodExact as IntegratedCompleteLikelihoodExact,
|
|
24
|
+
)
|
|
25
|
+
from .objective_function_iclex import (
|
|
26
|
+
IntegratedCompleteLikelihoodExactJeffrey as IntegratedCompleteLikelihoodExactJeffrey,
|
|
27
|
+
)
|
|
28
|
+
from .objective_function_iclex import (
|
|
29
|
+
IntegratedCompleteLikelihoodExactUniform as IntegratedCompleteLikelihoodExactUniform,
|
|
30
|
+
)
|
|
31
|
+
from .objective_function_newman_group_size import (
|
|
32
|
+
NewmanReinertDegreeCorrected as NewmanReinertDegreeCorrected,
|
|
33
|
+
)
|
|
34
|
+
from .objective_function_newman_group_size import (
|
|
35
|
+
NewmanReinertNonDegreeCorrected as NewmanReinertNonDegreeCorrected,
|
|
36
|
+
)
|
|
37
|
+
from .objective_functions import (
|
|
38
|
+
DegreeCorrectedMicrocanonicalEntropy as DegreeCorrectedMicrocanonicalEntropy,
|
|
39
|
+
)
|
|
40
|
+
from .objective_functions import (
|
|
41
|
+
DegreeCorrectedUnnormalizedLogLikelihood as DegreeCorrectedUnnormalizedLogLikelihood,
|
|
42
|
+
)
|
|
43
|
+
from .objective_functions import (
|
|
44
|
+
ObjectiveFunction as ObjectiveFunction,
|
|
45
|
+
)
|
|
46
|
+
from .objective_functions import (
|
|
47
|
+
TraditionalMicrocanonicalEntropy as TraditionalMicrocanonicalEntropy,
|
|
48
|
+
)
|
|
49
|
+
from .objective_functions import (
|
|
50
|
+
TraditionalUnnormalizedLogLikelihood as TraditionalUnnormalizedLogLikelihood,
|
|
51
|
+
)
|
|
52
|
+
from .partition import *
|
|
53
|
+
from .peixotos_flat_sbm import (
|
|
54
|
+
LogLikelihoodOfFlatMicrocanonicalDegreeCorrectedSbmWrapper as LogLikelihoodOfFlatMicrocanonicalDegreeCorrectedSbmWrapper,
|
|
55
|
+
)
|
|
56
|
+
from .peixotos_flat_sbm import (
|
|
57
|
+
LogLikelihoodOfFlatMicrocanonicalDegreeCorrectedUniform as LogLikelihoodOfFlatMicrocanonicalDegreeCorrectedUniform,
|
|
58
|
+
)
|
|
59
|
+
from .peixotos_flat_sbm import (
|
|
60
|
+
LogLikelihoodOfFlatMicrocanonicalDegreeCorrectedUniformHyperprior as LogLikelihoodOfFlatMicrocanonicalDegreeCorrectedUniformHyperprior,
|
|
61
|
+
)
|
|
62
|
+
from .peixotos_flat_sbm import (
|
|
63
|
+
LogLikelihoodOfFlatMicrocanonicalNonDegreeCorrected as LogLikelihoodOfFlatMicrocanonicalNonDegreeCorrected,
|
|
64
|
+
)
|
|
65
|
+
from .peixotos_hierarchical_sbm import (
|
|
66
|
+
LogLikelihoodOfHierarchicalMicrocanonicalDegreeCorrectedSbmWrapper as LogLikelihoodOfHierarchicalMicrocanonicalDegreeCorrectedSbmWrapper,
|
|
67
|
+
)
|
|
68
|
+
from .peixotos_hierarchical_sbm import (
|
|
69
|
+
LogLikelihoodOfHierarchicalMicrocanonicalDegreeCorrectedUniform as LogLikelihoodOfHierarchicalMicrocanonicalDegreeCorrectedUniform,
|
|
70
|
+
)
|
|
71
|
+
from .peixotos_hierarchical_sbm import (
|
|
72
|
+
LogLikelihoodOfHierarchicalMicrocanonicalDegreeCorrectedUniformHyperprior as LogLikelihoodOfHierarchicalMicrocanonicalDegreeCorrectedUniformHyperprior,
|
|
73
|
+
)
|
|
74
|
+
from .peixotos_hierarchical_sbm import (
|
|
75
|
+
LogLikelihoodOfHierarchicalMicrocanonicalNonDegreeCorrected as LogLikelihoodOfHierarchicalMicrocanonicalNonDegreeCorrected,
|
|
76
|
+
)
|