Skip to content

Commit 02fa8b4

Browse files
release 0.0.2
1 parent e51ebc9 commit 02fa8b4

10 files changed

+82
-25
lines changed

cloudtropy/entropy_functions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
def testfunc():
66
return 1;
77

8-
def entropy(X,base=2,N=None,d=None,delta_c=None):
8+
def entropy(X,base=2,N=None,d=None,delta_c=None,lims=None):
99
"""
1010
Take a cloud X of P points in Q dimensions (i.e., X.shape->(P,Q)) and compute
1111
the entropy of a discrete-support probability mass function on a Q-dimensional grid.
@@ -44,5 +44,5 @@ def entropy(X,base=2,N=None,d=None,delta_c=None):
4444
Journal of Physics Communications 3, no. 9 (2019): 095011.
4545
"""
4646

47-
p = pmf(X,N=N,d=d,delta_c=delta_c,return_grid=False)
47+
p = pmf(X,N=N,d=d,delta_c=delta_c,return_grid=False,lims=lims)
4848
return entpy(np.ravel(p),base=base);

cloudtropy/pmfs.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from sklearn.metrics import pairwise_distances
33

44

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):
66
"""
77
Take a cloud X of P points in Q dimensions (i.e., X.shape->(P,Q)) and compute
88
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):
2525
If not given, computed as the greatest width divided by N.
2626
return_grid : bool, default=True
2727
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
2831
2932
Returns
3033
-------
@@ -52,30 +55,36 @@ def pmf(X,N=None,d=None,delta_c=None,return_grid=True):
5255
raise ValueError('N and d parameters cannot be given at the same time.')
5356
# if (d is None) and (N is None):
5457
# 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]
5568

5669
# 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])) )
5774
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 ))
5976
if (d is None) and (N is None):
6077
N=20
6178
if delta_c is None:
6279
delta_c = np.max( (X.max(axis=1)-X.min(axis=1)))/N
6380

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-
6981
# Generating grid
70-
lims = []
71-
for dim in range(dims):
72-
lims.append( (np.min(X[:,dim]),np.max(X[:,dim])) )
7382
dim_arrays = []
7483
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))
7988
grid = np.meshgrid(*dim_arrays)
8089
# Creating the points of the grid
8190
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):
8695
# Computing the PMF
8796
flat_pmf = (pd<delta_c).sum(axis=0)
8897
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)])
9099

91100
if return_grid:
92101
return grid, pmf;

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
]
1414

1515
setup(name='cloudtropy',
16-
version='0.0.1',
16+
version='0.0.2',
1717
description='Empirical probability mass functions and entropies of N-dimensional clouds of points.',
1818
author='Pedro Ramaciotti Morales',
1919
author_email='[email protected]',
2020
url = 'https://github.com/pedroramaciotti/Cloudtropy',
2121
download_url = 'https://github.com/pedroramaciotti/Cloudtropy/archive/0.0.1.tar.gz',
22-
keywords = ['entropy','probabilities'],
22+
keywords = ['entropy','probabilities','entropy of points'],
2323
packages=find_packages(),
2424
data_files=[('', ['LICENSE'])],
2525
install_requires=INSTALL_REQUIRES)

tests/all.png

-7.1 KB
Loading

tests/contour_simple.png

-4.93 KB
Loading

tests/parameters_test.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
import sys
5+
sys.path.append('..')
6+
import cloudtropy
7+
8+
##################
9+
# Simulated data #
10+
##################

tests/scatter.png

-2.6 KB
Loading

tests/surf.png

372 Bytes
Loading

tests/test1.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from sklearn.metrics import pairwise_distances
32
import matplotlib.pyplot as plt
43

54
from mpl_toolkits import mplot3d
@@ -8,7 +7,7 @@
87

98

109
import sys
11-
sys.path.append('..')
10+
sys.path.append('../')
1211
import cloudtropy
1312

1413

@@ -19,7 +18,7 @@
1918
lims = (-2,6)
2019

2120
scale = 0.2
22-
X = np.random.uniform(low=lims[0],high=lims[1],size=(10000,2)) # back
21+
X = np.random.uniform(low=lims[0],high=lims[1],size=(10000,2)) # background
2322
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([0,0])] )
2423
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([4,0])] )
2524
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([0,4])] )
@@ -31,10 +30,13 @@
3130
N_grid = 80
3231
delta_c = 0.35
3332

34-
grid,pmf = cloudtropy.pmf(X,N=N_grid,delta_c=delta_c)
35-
entropy = cloudtropy.entropy(X,base=2,N=N_grid,delta_c=delta_c)
33+
# grid,pmf = cloudtropy.pmf(X,N=N_grid,delta_c=delta_c,lims=[(-2,6),(-2,6)])
34+
grid,pmf = cloudtropy.pmf(X,d=0.1,delta_c=delta_c,lims=[(-2,6),(-2,6)])
35+
entropy = cloudtropy.entropy(X,base=2,N=N_grid,delta_c=delta_c,lims=[(-3,7),(-3,7)])
36+
37+
38+
print(cloudtropy.entropy(X,base=2,d=0.1,delta_c=delta_c,lims=[(-3,7),(-3,7)]))
3639

37-
entropy_readme = cloudtropy.entropy(X)
3840

3941
############## All in one
4042

tests/uniform_distribution.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
from mpl_toolkits import mplot3d
5+
6+
# from scipy.stats import entropy
7+
8+
9+
import sys
10+
sys.path.append('..')
11+
import cloudtropy
12+
13+
14+
# data
15+
16+
gen_dim = 2
17+
gen_N = 300
18+
lims = (-2,6)
19+
20+
scale = 0.2
21+
X = np.random.uniform(low=lims[0],high=lims[1],size=(10000,2)) # background
22+
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([0,0])] )
23+
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([4,0])] )
24+
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([0,4])] )
25+
X = np.concatenate([X,scale*np.random.randn(gen_N,gen_dim)+np.array([4,4])] )
26+
27+
28+
# input parameters
29+
30+
N_grid = 80
31+
delta_c = 0.35
32+
33+
grid,pmf = cloudtropy.pmf(X,N=N_grid,delta_c=delta_c)
34+
entropy = cloudtropy.entropy(X,base=2,N=N_grid,delta_c=delta_c)
35+
36+
entropy_readme = cloudtropy.entropy(X)

0 commit comments

Comments
 (0)