-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbelief_propagation.py
1244 lines (870 loc) · 32.1 KB
/
belief_propagation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms import bipartite
import pandas as pd
from sklearn.neighbors import KernelDensity
from sklearn.covariance import GraphicalLasso
import copy
def FG_from_glasso(data, alpha):
""" Compute a factor graph using graphical lasso and clique factorization
Parameters
----------
data: pandas DataFrame
Input trajectory for graphical lasso.
alpha: float
Penalty parameter for graphical lasso.
Returns
-------
networkx Graph
Factor graph inheriting variable names from the input DataFrame.
"""
lasso = GraphicalLasso(alpha=alpha).fit(data)
inv_cov = lasso.precision_
lasso_net = np.abs(inv_cov) > 0 # entries greater than 0
lasso_net = lasso_net.astype('int')
np.fill_diagonal(lasso_net,0)
graph = nx.from_numpy_matrix(lasso_net)
col = data.columns
label_dict = {i:col[i] for i in range(len(col))}
graph = nx.relabel_nodes(graph,label_dict)
max_cliques = nx.find_cliques(graph)
clique_list = []
for clique in max_cliques:
clique_list.append(clique)
FG = nx.Graph()
FG.add_nodes_from(list(graph.nodes), bipartite=0)
factors = []
edges = []
index = 1
for clique in clique_list:
factor = "$f_" + str(index) +"$"
factors.append(factor)
for node in clique:
edges.append((factor, node))
index += 1
FG.add_nodes_from(factors, bipartite=1)
FG.add_edges_from(edges)
return FG
def show_FG_components(FG, figsize=None, dpi=None, savename=None):
""" Display a factor graph structure, showing each connected component.
Parameters
----------
FG: networkx Graph
Factor graph where node type is distinguisehd with the 'bipartite' attribute.
figsize: tuple
Width and height of the output figures.
dpi: int
DPI of the output figures.
savename: str
Name to save the figure when the graph is a single component.
Returns
-------
None
This function only shows matplotlib figures.
"""
components = [FG.subgraph(c).copy() for c in nx.connected_components(FG)]
for C in components:
plt.figure(figsize=figsize,dpi=dpi)
pos = nx.kamada_kawai_layout(C)
var = [x for x,y in C.nodes(data=True) if y['bipartite']==0]
func = [x for x,y in C.nodes(data=True) if y['bipartite']==1]
nx.draw_networkx_nodes(C, nodelist = var, pos=pos, node_size=900)
nx.draw_networkx_nodes(C, nodelist = func, pos=pos, node_shape='s', node_size=900)
nx.draw_networkx(C,pos=pos,with_labels=True,node_size=0, font_color='w')
if savename != None:
plt.savefig(savename)
plt.show()
def outer_multi(*arrays):
""" Compute the outer product of a set of arrays.
Parameters
----------
arrays: numpy arrays
Set of arrays to be multiplied.
Returns
-------
numpy array
N-dim tensor where N is the number of input arrays.
"""
prod = arrays[0]
for i in range(1,len(arrays)):
prod = np.tensordot(prod,arrays[i], axes=((),()))
return prod
def marginalize(tensor, axis):
""" Marginalize out all but one axes of a probability tensor.
Parameters
----------
tensor: numpy array
Probability tensor to be marginalized.
axis: int
Axis that will not be marginalized.
Returns
-------
numpy array
Marginalized probability array with shape (n,) where n is the length of axis.
"""
sum_dims = [i for i in range(len(tensor.shape))]
sum_dims.remove(axis)
sum_dims = tuple(sum_dims)
return np.sum(tensor, axis=sum_dims)
def standardize_df(df, return_stats=False):
""" Standardize each column of a DataFrame.
Parameters
----------
df: pandas DataFrame
DataFrame to be standardized.
return_stats: bool
Option to return the original means and standard deviations.
Returns
-------
pandas DataFrame
Standardized DataFrame.
pandas Series (optional)
Means of the original DataFrame.
pandas Series (optional)
Standard deviations of the original DataFrame.
"""
df_mean = np.mean(df)
df_std = np.std(df)
standardized = (df - df_mean) / df_std
if return_stats:
return standardized, df_mean, df_std
else:
return standardized
def set_bandwidth(n, d, kernel, weights=None, rule='scott', print_bw=False):
""" Calculates the bandwidth consistent with Scott or Silverman's
rules of thumb for bandwidth selection.
Parameters
----------
n : int
Number of data points.
d : int
Dimensionality of the data.
kernel : str
Kernel name for kernel density estimation.
weights : list of floats or numpy array
The weights associated with each data point after reweighting an enhanced
sampling trajectory.
rule : str
Rule used for bandwidth selection.
Returns
-------
float
Bandwidth from the rules of thumb (they're the same for 2D KDE).
"""
if kernel == 'epanechnikov':
bw_constant = 2.2
else:
bw_constant = 1
if type(weights) != type(None):
weights = np.array(weights)
n = np.sum(weights)**2 / np.sum(weights**2)
if rule == 'scott':
bandwidth = bw_constant*n**(-1/(d+4))
elif rule == 'silverman':
bandwidth = bw_constant*(n*(d+2)/4)**(-1/(d+4))
else:
print('Please use scott or silverman as the rule for bandwidth selection.')
if print_bw:
print('Selected bandwidth: ' + str(bandwidth)+ '\n')
return bandwidth
class Factor:
""" Handles the joint probability modeling for each factor and stores a grid of their values.
Attributes
----------
dims: list
The factor's neighbors in the order they appear in the probability tensors.
data: pandas DataFrame
Data used to define grids and to fit KDE.
bins: int
Number of bins for the KDE and factor grids.
kernel: str
Kernel type used in KDE.
weights: numpy array
Weights used for weighted KDE. Used for modeling biased simualtions.
rule: str
Rule for KDE bandwidth selection.
"""
def __init__(self, dims, data, bins=20, kernel='gaussian', weights=None, rule='scott'):
self.dims = dims
self.bins = bins
self.bounds = self.bin_boundaries(data)
self.midpoints = [(b[1:]+b[:-1])/2 for b in self.bounds]
self.KD = self.KDE_fit(data, kernel, weights, rule)
self.joint_grid = self.KDE_grid()
self.factor_grid = self.joint_grid
def bin_boundaries(self,data):
""" Defines gridpoints along each dimension of the input data.
Parameters
----------
data: pandas DataFrame
Data used to calculate the gridpoints.
Returns
-------
list of numpy arrays
List of gridpoints for the dimensions in self.dims.
"""
bounds = []
for col in self.dims:
bounds.append(np.linspace(np.min(data[col]),np.max(data[col]),self.bins+1))
return bounds
def KDE_fit(self, data, kernel='gaussian', weights=None, rule='scott'):
""" Fit KDE to input data.
Parameters
----------
data: pandas DataFrame
Data used for KDE fitting.
kernel: str
Kernel type used in KDE.
weights: numpy array
Weights used for weighted KDE. Used for modeling biased simualtions.
rule: str
Rule for KDE bandwidth selection.
Returns
-------
sklearn KernelDensity
Object containing the KDE fit.
"""
shape = np.shape(data[self.dims])
n = shape[0]
if len(shape) == 1:
d = 1
else:
d = shape[1]
bw = set_bandwidth(n, d, kernel, weights, rule)
KD = KernelDensity(bandwidth=bw,kernel=kernel)
KD.fit(data[self.dims], sample_weight=weights)
return KD
def KDE_grid(self):
""" Calculate the KDE probability at each point in the factor's grid.
Returns
-------
numpy array
Probability tensor calculated using the KDE values on the factor's gridpoints.
"""
points = np.array(np.meshgrid(*self.midpoints,indexing='ij')).reshape(len(self.midpoints),-1).T
samp = self.KD.score_samples(points)
samp = samp.reshape([self.bins]*len(self.dims))
p = np.exp(samp)/np.sum(np.exp(samp))
return p
class MessagePasser:
"""Class containing the factor graph and handling message passing operations.
Attributes
----------
FG: bipartite networkx Graph
Factor graph used for message passing.
data: pandas DataFrame
Data used for KDE fitting.
bins: int
Number of bins for the KDE and factor grids.
kernel: str
Kernel type used in KDE.
rule: str
Rule for KDE bandwidth selection.
"""
def __init__(self, FG, data, bins=20, kernel='gaussian', rule='scott'):
self.FG = FG
self.bins = bins
self.binned = self.digitize_df(data)
self.data, self.orig_mean, self.orig_std = standardize_df(data,return_stats=True)
self.kernel = kernel
self.rule = rule
self.funcs = self.get_func_nodes()
self.vars = self.get_var_nodes()
self.init_factors()
self.init_messages()
def get_var_nodes(self):
"""Creates a list of all variable nodes.
Returns
-------
list
List of variable nodes in self.FG.
"""
return [x for x,y in self.FG.nodes(data=True) if y['bipartite']==0]
def get_func_nodes(self):
"""Creates a list of all factor nodes.
Returns
-------
list
List of factor nodes in self.FG.
"""
return [x for x,y in self.FG.nodes(data=True) if y['bipartite']==1]
def digitize_df(self, df):
"""Digitizes a DataFrame creating a new DataFrame replacing data points with their bins.
Parameters
----------
df: pandas DataFrame
DataFrame to be digitized.
Returns
-------
pandas DataFrame
DataFrame storing binning for each data point.
"""
df_array = df.to_numpy()
num_col = df_array.shape[1]
binned_cols = []
for i in range(num_col):
col = df_array[:,i]
grid = np.linspace(np.min(col), np.max(col), self.bins+1)
binned = np.digitize(col, grid[:-1])-1
binned_cols.append(binned)
return pd.DataFrame(np.column_stack(binned_cols),columns=df.columns)
def init_factors(self):
"""Initializes the Factor objects for each factor node, fitting KDE for each factor.
Returns
-------
None
"""
factor_properties = {}
for node in self.funcs:
factor_properties[node] = {}
neighbors = list(self.FG[node])
F = Factor(neighbors, self.data, bins=self.bins, kernel=self.kernel, rule=self.rule)
factor_properties[node]['factor'] = F
nx.set_node_attributes(self.FG,factor_properties)
def init_messages(self):
"""Initializes the messages for message passing.
Returns
-------
None
"""
factor_properties = dict(self.FG.nodes())
for node in self.FG.nodes:
messages = dict(self.FG[node])
for edge in messages:
messages[edge] = np.ones(self.bins)
factor_properties[node]['messages'] = messages
nx.set_node_attributes(self.FG, factor_properties)
def cross_entropy(self, q, dims):
"""Calculates the cross entropy between a model q and the observed data.
Parameters
----------
q: numpy array
Model probability.
dims: list
Dimensions used to access columns of self.data.
Returns
-------
float
Value of the cross entropy.
"""
indices = tuple(self.binned[dims].to_numpy().T)
cross_entropy = -np.mean(np.log(q[indices]))
return cross_entropy
def factor_message_update(self, node):
"""Updates a single factor node and its outgoing messages.
Parameters
----------
node: str
Name of the node to be updated.
Retutns
-------
None
"""
factor = self.FG.nodes[node]['factor']
neighbors = factor.dims
incoming = []
for n in neighbors:
incoming.append(self.FG.nodes[n]['messages'][node])
f_grid = factor.joint_grid/outer_multi(*incoming)
factor.factor_grid = f_grid
for i in range(len(incoming)):
m = [x if j!=i else x/x for j,x in enumerate(incoming)]
self.FG.nodes[node]['messages'][neighbors[i]] = marginalize(f_grid*outer_multi(*m),i)
def var_message_update(self, node):
"""Updates a single variable node and its outgoing messages.
Parameters
----------
node: str
Name of the node to be updated.
Returns
-------
None
"""
neighbors = list(self.FG[node])
incoming = {n:self.FG.nodes[n]['messages'][node] for n in neighbors}
product = np.product(list(incoming.values()),axis=0)
product = product / np.sum(product)
for n in neighbors:
self.FG.nodes[node]['messages'][n] = product / incoming[n]
def factor_update_cycle(self, node):
"""Updates a single factor node, all adjacent variable nodes, and their outgoing messages.
Parameters
----------
node: str
Name of the factor node to be updated.
Returns
-------
None
"""
factor = self.FG.nodes[node]['factor']
neighbors = factor.dims
self.factor_message_update(node)
for n in neighbors:
self.var_message_update(n)
def factor_CE(self, node):
"""Calculates the contribution to the model's cross entropy from one factor.
Parameters
----------
node: str
Name of the factor to calculate cross entropy.
Returns
-------
float
The cross entropy from one factor node.
"""
F = self.FG.nodes[node]['factor']
vals = F.factor_grid
return self.cross_entropy(vals, F.dims)
def global_CE(self):
"""Calculates the cross entropy of the factorized joint probability model.
Returns
-------
float
Model cross entropy.
"""
CE = 0
for f in self.funcs:
CE += self.factor_CE(f)
return CE
class FactorPeriodic(Factor):
"""Factor class with manually set grid range.
Attributes
----------
dims: list
The factor's neighbors in the order they appear in the probability tensors.
data: pandas DataFrame
Data used to define grids and to fit KDE.
period_range: list
List of standardized ranges (min, max) for each dimension.
bins: int
Number of bins for the KDE and factor grids.
kernel: str
Kernel type used in KDE.
weights: numpy array
Weights used for weighted KDE. Used for modeling biased simualtions.
rule: str
Rule for KDE bandwidth selection.
"""
def __init__(self, dims, data, period_range, bins=21, kernel='gaussian',
weights=None, rule='scott'):
self.dims = dims
self.bins = bins
self.gridpoints = [np.linspace(*p, bins) for p in period_range]
self.KD = self.KDE_fit(data, kernel, weights, rule)
self.joint_grid = self.KDE_grid()
self.factor_grid = self.joint_grid
def KDE_grid(self):
""" Calculate the KDE probability at each point in the factor's grid.
Returns
-------
numpy array
Probability tensor calculated using the KDE values on the factor's gridpoints.
"""
points = np.array(np.meshgrid(*self.gridpoints,indexing='ij')).reshape(len(self.gridpoints),-1).T
samp = self.KD.score_samples(points)
samp = samp.reshape([self.bins]*len(self.dims))
p = np.exp(samp)/np.sum(np.exp(samp))
return p
class MessagePasserPeriodic(MessagePasser):
"""Class containing the factor graph and handling message passing operations.
Attributes
----------
FG: bipartite networkx Graph
Factor graph used for message passing.
data: pandas DataFrame
Data used for KDE fitting.
bins: int
Number of bins for the KDE and factor grids.
period_range: tuple
Min and max for the variables. Currently uses the same range for each variable.
kernel: str
Kernel type used in KDE.
rule: str
Rule for KDE bandwidth selection.
"""
def __init__(self, FG, data, bins=21, period_range=(-np.pi,np.pi), kernel='gaussian',
rule='scott'):
self.FG = FG
self.range = period_range
self.bins = bins
self.binned = self.digitize_df(data)
self.data, self.orig_mean, self.orig_std = standardize_df(data,return_stats=True)
self.kernel = kernel
self.rule = rule
self.funcs = self.get_func_nodes()
self.vars = self.get_var_nodes()
self.init_factors()
self.init_messages()
def init_factors(self):
"""Initializes the Factor objects for each factor node, fitting KDE for each factor.
Returns
-------
None
"""
factor_properties = {}
for node in self.funcs:
factor_properties[node] = {}
neighbors = list(self.FG[node])
period_range = [(self.range-self.orig_mean[n])/self.orig_std[n] for n in neighbors]
F = FactorPeriodic(neighbors, self.data, period_range=period_range, bins=self.bins,
kernel=self.kernel, rule=self.rule)
factor_properties[node]['factor'] = F
nx.set_node_attributes(self.FG,factor_properties)
def digitize_df(self, df):
"""Digitizes a DataFrame creating a new DataFrame replacing data points with their bins.
Parameters
----------
df: pandas DataFrame
DataFrame to be digitized.
Returns
-------
pandas DataFrame
DataFrame storing binning for each data point using the range self.range.
"""
df_array = df.to_numpy()
num_col = df_array.shape[1]
binned_cols = []
for i in range(num_col):
col = df_array[:,i]
grid = np.linspace(*self.range, self.bins+1)
binned = np.digitize(col, grid[:-1])-1
binned_cols.append(binned)
return pd.DataFrame(np.column_stack(binned_cols),columns=df.columns)
def show_2d_update(MP,node,figsize=(25,5),levels=30,titlesize=20):
"""Update a single factor and show how it and its associated probability change.
Parameters
----------
MP: MessagePasser or MessagePasserPeriodic
Object to be updated.
node: str
Name of the factor in the MessagePasser to update.
figsize: tuple
Size of the output figure.
levels: int
Number of contour levels on each subplot.
titlesize: float
Font size for each subplot's title.
Returns
-------
None
"""
F = MP.FG.nodes[node]['factor']
neighbors = F.dims
incoming = []
for n in neighbors:
incoming.append(MP.FG.nodes[n]['messages'][node])
fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1,5,figsize=figsize)
ax1.contourf((F.factor_grid*outer_multi(*incoming)).T,levels=levels,cmap='Spectral_r')
ax1.set_title('initial model prob',fontsize=titlesize)
init_factor = F.factor_grid
ax2.contourf(F.joint_grid.T,levels=levels,cmap='Spectral_r')
ax2.set_title('KDE',fontsize=titlesize)
MP.factor_update_cycle(node)
ax3.contourf((F.factor_grid*outer_multi(*incoming)).T,levels=levels,cmap='Spectral_r')
ax3.set_title('final model prob',fontsize=titlesize)
ax4.contourf(init_factor.T,levels=levels,cmap='Spectral_r')
ax4.set_title('initial factor',fontsize=titlesize)
ax5.contourf(F.factor_grid.T,levels=levels,cmap='Spectral_r')
ax5.set_title('final factor',fontsize=titlesize)
plt.show()
def update_show_if_2d(MP,node,figsize=(25,5),levels=30,titlesize=20):
"""Update a single factor and show how it and its associated probability change if it is 2D.
Parameters
----------
MP: MessagePasser or MessagePasserPeriodic
Object to be updated.
node: str
Name of the factor in the MessagePasser to update.
figsize: tuple
Size of the output figure.
levels: int
Number of contour levels on each subplot.
titlesize: float
Font size for each subplot's title.
Returns
-------
None
"""
F = MP.FG.nodes[node]['factor']
if len(F.dims) == 2:
show_2d_update(MP, node, figsize, levels, titlesize)
else:
print('Updated factor ' + str(node) + ' with dimension ' + str(len(F.dims)) )
MP.factor_update_cycle(node)
def periodic_padding(grid):
"""Create a periodic boundary around a tensor such that the ends are adjacent.
For example a 1D tensor will return [grid[-1],grid[0],...,grid[-1],grid[0]]
Parameters
----------
grid: numpy array
Any dimensional tensor to add a periodic boundary to.
Returns
-------
numpy array
Tensor with a periodic boundary added.
"""
shape = grid.shape
padded = np.zeros(np.array(shape)+2)
length = len(shape)
center_ind = tuple([slice(1,-1,1)]*length)
full_ind = tuple([slice(None)]*length)
padded[center_ind] = grid
for i in range(length):
pad_edge_ind = list(center_ind)
pad_edge_ind[i] = 0
pad_edge_ind = tuple(pad_edge_ind)
orig_edge_ind = list(full_ind)
orig_edge_ind[i] = -1
orig_edge_ind = tuple(orig_edge_ind)
padded[pad_edge_ind] = grid[orig_edge_ind]
pad_edge_ind = list(center_ind)
pad_edge_ind[i] = -1
pad_edge_ind = tuple(pad_edge_ind)
orig_edge_ind = list(full_ind)
orig_edge_ind[i] = 0
orig_edge_ind = tuple(orig_edge_ind)
padded[pad_edge_ind] = grid[orig_edge_ind]
return padded
def periodic_grad(grid, *varargs, **kwargs):
"""Calculate the gradient of a tensor representing a periodic function
where the first and last bin are adjacent.
Parameters
----------
grid: numpy array
Tensor to have its gradient calculated.
varargs : list of scalar, optional
N scalars specifying the sample distances for each dimension.
kwargs
Additional keywords passed to np.gradient. See numpy documentation for more details.
Returns
-------
numpy array or list
Gradient of the input tensor considering periodic boundaries. List if dimension >= 2.
"""
periodic_grad = np.gradient(periodic_padding(grid), *varargs, **kwargs)
center_ind = tuple([slice(1,-1,1)]*len(grid.shape))
if type(periodic_grad) == list:
for dim in range(len(periodic_grad)):
periodic_grad[dim] = periodic_grad[dim][center_ind]
else:
periodic_grad = periodic_grad[center_ind]
return periodic_grad
def bias_from_p(p, T, cutoff=0, gamma=None):
"""Calculate a bias potential from a probability tensor.
Parameters
----------
p: numpy array
Probability tensor to convert to bias.
T: float
Temperature.
cutoff: float
Set all probability below this point to 0.
gamma: float or None
Gamma used for well-tempered bias.
Returns
-------
numpy array
Tensor containing the bias potential.
"""
kb = 8.31446261815324e-3
p[p<cutoff] = 0
E = -kb*T*np.ma.log(p)
V = -(E-np.max(E))
V[V.mask] = 0
if gamma !=None:
V *= (1-1/gamma)
return V
def clean_names(names):
"""Clean factor names to remove formatting characters.
Parameters
----------
names: list of str
Names to be cleaned.
Returns
-------
list of str
List of strings without formatting characters.
"""
new_names = copy.copy(names)
for i in range(len(names)):
name = new_names[i]
name = name.replace('_','')
name = name.replace('$','')
name = name.replace('\\','')
new_names[i] = name
return new_names
def write_header(file, F, bias_name):