2
2
from sklearn .metrics import pairwise_distances
3
3
4
4
5
- def pmf (X ,N = None ,d = None ,delta_c = None ,return_grid = True ):
5
+ def pmf (X ,N = None ,d = None ,delta_c = None ,return_grid = True , lims = None ):
6
6
"""
7
7
Take a cloud X of P points in Q dimensions (i.e., X.shape->(P,Q)) and compute
8
8
a discrete-support probability mass function on a Q-dimensional grid.
@@ -25,6 +25,9 @@ def pmf(X,N=None,d=None,delta_c=None,return_grid=True):
25
25
If not given, computed as the greatest width divided by N.
26
26
return_grid : bool, default=True
27
27
If True, return the support of the N**Q grid points.
28
+ lims : array of 2-tuples
29
+ Spatial limits of the computed probability distribution
30
+ function. Must have Q 2-tuples
28
31
29
32
Returns
30
33
-------
@@ -52,30 +55,36 @@ def pmf(X,N=None,d=None,delta_c=None,return_grid=True):
52
55
raise ValueError ('N and d parameters cannot be given at the same time.' )
53
56
# if (d is None) and (N is None):
54
57
# raise ValueError('Either N or d must be given as inputs.')
58
+ if lims is not None :
59
+ if len (list (lims ))!= X .shape [1 ]:
60
+ raise ValueError ('lims must contain the same number of 2-tuples as the dimensions of the points in the cloud (%d).' % X .shape [1 ])
61
+ if any ([len (tuple )!= 2 for tuple in lims ]):
62
+ raise ValueError ('lims must only contain 2-tuples.' )
63
+
64
+ # Retrieving parameters of the points cloud
65
+ (N_points ,dims ) = X .shape
66
+ # N_points = X.shape[0]
67
+ # dims = X.shape[1]
55
68
56
69
# Treating input
70
+ if lims is None :
71
+ lims = []
72
+ for dim in range (dims ):
73
+ lims .append ( (np .min (X [:,dim ]),np .max (X [:,dim ])) )
57
74
if (d is not None ) and (N is None ):
58
- N = np .max ( ( X .max (axis = 1 ) - X . min ( axis = 1 )) / d )
75
+ N = int ( np .ceil ( np .max ([ np . abs ( lim [ 1 ] - lim [ 0 ]) for lim in lims ]) / d ) )
59
76
if (d is None ) and (N is None ):
60
77
N = 20
61
78
if delta_c is None :
62
79
delta_c = np .max ( (X .max (axis = 1 )- X .min (axis = 1 )))/ N
63
80
64
- # Retrieving parameters of the points cloud
65
- (N_points ,dims ) = X .shape
66
- # N_points = X.shape[0]
67
- # dims = X.shape[1]
68
-
69
81
# Generating grid
70
- lims = []
71
- for dim in range (dims ):
72
- lims .append ( (np .min (X [:,dim ]),np .max (X [:,dim ])) )
73
82
dim_arrays = []
74
83
for dim in range (dims ):
75
- if d is not None :
76
- dim_arrays .append (np .arange (start = lims [dim ][0 ],stop = lims [dim ][1 ]+ d ,step = d ))
77
- elif N is not None :
78
- dim_arrays .append (np .linspace (start = lims [dim ][0 ],stop = lims [dim ][1 ],num = N ))
84
+ # if d is not None:
85
+ # dim_arrays.append(np.arange(start=lims[dim][0],stop=lims[dim][1]+d,step=d))
86
+ # elif N is not None:
87
+ dim_arrays .append (np .linspace (start = lims [dim ][0 ],stop = lims [dim ][1 ],num = N ))
79
88
grid = np .meshgrid (* dim_arrays )
80
89
# Creating the points of the grid
81
90
Y = np .vstack (map (np .ravel , grid )).transpose ()
@@ -86,7 +95,7 @@ def pmf(X,N=None,d=None,delta_c=None,return_grid=True):
86
95
# Computing the PMF
87
96
flat_pmf = (pd < delta_c ).sum (axis = 0 )
88
97
flat_pmf = flat_pmf / flat_pmf .sum ()
89
- pmf = np .reshape (flat_pmf ,newshape = [N for i in range (dims )])
98
+ pmf = np .reshape (flat_pmf ,newshape = [int ( N ) for i in range (dims )])
90
99
91
100
if return_grid :
92
101
return grid , pmf ;
0 commit comments