Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom clustering now takes position matrix instead of distance^2 matrix #87

Open
wants to merge 48 commits into
base: custom_cluster
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
f1f52ed
neighbor spelling (assuming we're sticking to American spelling!)
AdamOrmondroyd May 19, 2022
1c40b21
custom clustering now takes position matrix instead of distance^2 mat…
AdamOrmondroyd May 23, 2022
634a4b8
corrected order of dimensions of position_matrix
AdamOrmondroyd May 24, 2022
d98cd08
renamed calculate_similarity_matrix() to calculate_position_matrix()
AdamOrmondroyd May 24, 2022
a04231a
renamed calculate_similarity_matrix() to calculate_position2_matrix()
AdamOrmondroyd May 24, 2022
d9571a3
Merge branch 'ormorod-custom_cluster' of github.com:Ormorod/PolyChord…
AdamOrmondroyd May 24, 2022
9568985
corrected index in default_cluster
AdamOrmondroyd May 25, 2022
92b3809
added new dimension to cluster input as position_matrix is not square…
AdamOrmondroyd May 26, 2022
db02cc2
changed cluster interfaces to add extra dimension for position_matrix
AdamOrmondroyd May 26, 2022
876de66
Merge branch 'master' into ormorod-custom_cluster
williamjameshandley May 26, 2022
f5f8c93
Updated CI to work on pull request to all branches
williamjameshandley May 26, 2022
eeb83ac
changed cluster interfaces to add extra dimension for position_matrix
AdamOrmondroyd May 26, 2022
4d68c9d
changed distance2_matrix to no longer be square
AdamOrmondroyd May 26, 2022
b6885cb
added extra dimension to cluster argument for non-square distance2_ma…
AdamOrmondroyd May 26, 2022
4f638f2
Merge branch 'ormorod-custom_cluster' of github.com:Ormorod/PolyChord…
AdamOrmondroyd May 26, 2022
7da5423
renamed m to nDims, n to nPoints, and position_matrix (and distance2_…
AdamOrmondroyd May 26, 2022
fa5c42d
changed neighbour to ~~correct~~ UK spelling
AdamOrmondroyd May 26, 2022
5184835
forgot to change the interface in clustering itself
AdamOrmondroyd May 26, 2022
3ed846c
another clustering interface using the wrong dimension of points
AdamOrmondroyd May 26, 2022
e4622f5
swapped indices over
AdamOrmondroyd May 26, 2022
1b90073
Merge branch 'ormorod-custom_cluster' of github.com:Ormorod/PolyChord…
AdamOrmondroyd May 26, 2022
23d8db8
Revert "swapped indices over"
AdamOrmondroyd May 26, 2022
4a9bf0a
now uses c indexing in c and python, and fortran indexing in fortran
AdamOrmondroyd May 26, 2022
2f0c339
Merge branch 'PolyChord:master' into master
AdamOrmondroyd May 28, 2022
f68dcb3
similarity matrix renamed to distance^2 in comments
AdamOrmondroyd Jun 13, 2022
da78633
wrap_cluster() adds one to the cluster list, as Python algos number c…
AdamOrmondroyd Jun 30, 2022
512fca1
Merge branch 'ormorod-custom_cluster' of github.com:Ormorod/PolyChord…
AdamOrmondroyd Jun 30, 2022
be3467a
example cluster now returns -1
AdamOrmondroyd Jun 30, 2022
eb412c3
Merge branch 'master' into ormorod-custom_cluster
AdamOrmondroyd Jul 29, 2022
9fbf7a3
Merge branch 'custom_cluster' of github.com:PolyChord/PolyChordLite i…
AdamOrmondroyd Jul 29, 2022
14efda1
example to test custom clustering
AdamOrmondroyd Jul 29, 2022
3029e60
equal weights of each Gaussian peak, corrected comments
AdamOrmondroyd Jul 29, 2022
88572e3
remove mutual_nearest_neighbours function
AdamOrmondroyd Jul 29, 2022
b49fb80
replace sklearn dependence with compute_knn
AdamOrmondroyd Jul 29, 2022
f8dc320
compute_knn() computed the full nn ordering, then returned the first …
AdamOrmondroyd Jul 29, 2022
4d95077
realised the for loop in the copied knn clustering is actually a whil…
AdamOrmondroyd Jul 29, 2022
2d99c89
Removed unused num_clusters_old from native clustering
AdamOrmondroyd Jul 29, 2022
1a561de
comment reminds that clustering algorithm should be 0-indexed
AdamOrmondroyd Jul 29, 2022
f67543d
updated run_pypolychord.ipynb with py2nb
AdamOrmondroyd Jul 29, 2022
8954de3
correct docstring to clarify cluster function must number clusters fr…
AdamOrmondroyd Sep 22, 2022
ebf3b91
further correction to clustering in docstring
AdamOrmondroyd Sep 22, 2022
e1102fb
change cluster function example comments to docstring
AdamOrmondroyd Sep 22, 2022
4d3adcc
remove unnecessary np.sqrt import
AdamOrmondroyd Sep 22, 2022
70628a5
Correct root=root -> root=0 in make_resume_file (#92)
AdamOrmondroyd Feb 7, 2023
6cf0fc4
PWD->CURDIR
williamjameshandley Feb 16, 2023
3084a81
Updated PWD to CURDIR in setup.py
williamjameshandley Feb 16, 2023
1854372
Update python3.6 CI (#96)
AdamOrmondroyd Feb 20, 2023
cfc77c4
Merge branch 'master' into ormorod-custom_cluster
AdamOrmondroyd Feb 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pypolychord/_pypolychord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,24 @@ void dumper(int ndead, int nlive, int npars, double* live, double* dead, double*
/* Callback to the cluster */
static PyObject *python_cluster = NULL;

void cluster(double* distance2_matrix, int* cluster_list, int n)
void cluster(double* position_matrix, int* cluster_list, int m, int n)
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
{
/* create a python version of distance2_matrix */
npy_intp shape[] = {n,n};
PyObject *array_distance2_matrix = PyArray_SimpleNewFromData(2, shape, NPY_DOUBLE, distance2_matrix);
if (array_distance2_matrix ==NULL) throw PythonException();
PyArray_CLEARFLAGS(reinterpret_cast<PyArrayObject*>(array_distance2_matrix), NPY_ARRAY_WRITEABLE);
/* create a python version of position_matrix */
npy_intp shape[] = {m,n};
PyObject *array_position_matrix = PyArray_SimpleNewFromData(2, shape, NPY_DOUBLE, position_matrix);
if (array_position_matrix ==NULL) throw PythonException();
PyArray_CLEARFLAGS(reinterpret_cast<PyArrayObject*>(array_position_matrix), NPY_ARRAY_WRITEABLE);

/* create a python version of cluster_list */
npy_intp shape1[] = {n};
PyObject *array_cluster_list = PyArray_SimpleNewFromData(1, shape1, NPY_INT, cluster_list);
if (array_cluster_list==NULL) {Py_DECREF(array_distance2_matrix); throw PythonException();}
if (array_cluster_list==NULL) {Py_DECREF(array_position_matrix); throw PythonException();}

/* Compute cluster_list from the cluster */
PyObject_CallFunctionObjArgs(python_cluster,array_distance2_matrix,array_cluster_list,NULL);
PyObject_CallFunctionObjArgs(python_cluster,array_position_matrix,array_cluster_list,NULL);

/* Garbage collect */
Py_DECREF(array_cluster_list); Py_DECREF(array_distance2_matrix);
Py_DECREF(array_cluster_list); Py_DECREF(array_position_matrix);
}


Expand Down
12 changes: 6 additions & 6 deletions pypolychord/polychord.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def default_dumper(live, dead, logweights, logZ, logZerr):
pass


def default_cluster(distance2_matrix):
return np.zeros(distance2_matrix.shape[0],dtype=int)
def default_cluster(position_matrix):
return np.zeros(position_matrix.shape[1],dtype=int)


def run_polychord(loglikelihood, nDims, nDerived, settings,
Expand Down Expand Up @@ -112,8 +112,8 @@ def run_polychord(loglikelihood, nDims, nDerived, settings,

Parameters
----------
distance2_matrix: numpy.array
squared distances between points. Shape (nPoints, nPoints)
position_matrix: numpy.array
AdamOrmondroyd marked this conversation as resolved.
Show resolved Hide resolved
positions of points. Shape (nDims, nPoints)
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Expand Down Expand Up @@ -197,8 +197,8 @@ def wrap_loglikelihood(theta, phi):
def wrap_prior(cube, theta):
theta[:] = prior(cube)

def wrap_cluster(distance2_matrix, cluster_list):
cluster_list[:] = cluster(distance2_matrix)
def wrap_cluster(position_matrix, cluster_list):
cluster_list[:] = cluster(position_matrix)

# Run polychord from module library
_pypolychord.run(wrap_loglikelihood,
Expand Down
4 changes: 2 additions & 2 deletions run_pypolychord.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def dumper(live, dead, logweights, logZ, logZerr):

#| Optional cluster function allow user-defined clustering

def cluster(distance2_matrix):
npoints = distance2_matrix.shape[0]
def cluster(position_matrix):
npoints = position_matrix.shape[1]
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
clusters = np.ones(npoints, dtype=int)

# <do some clustering algorithm to assign clusters>
Expand Down
10 changes: 5 additions & 5 deletions src/polychord/calculate.f90
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ end function calculate_posterior_point
!! The final term can be written as a data_array^T data_array, and the first
!! two are easy to write. We can therefore calculate this in two lines with
!! instrisic functions
function calculate_similarity_matrix(data_array) result(similarity_matrix)
function calculate_distance2_matrix(data_array) result(distance2_matrix)

real(dp), intent(in), dimension(:,:) :: data_array

real(dp), dimension(size(data_array,2),size(data_array,2)) :: similarity_matrix
real(dp), dimension(size(data_array,2),size(data_array,2)) ::distance2_matrix

integer :: i


similarity_matrix = spread( &
distance2_matrix = spread( &
[ ( dot_product(data_array(:,i),data_array(:,i)), i=1,size(data_array,2) ) ], &
dim=2,ncopies=size(data_array,2) )

similarity_matrix = similarity_matrix + transpose(similarity_matrix) - 2d0 * matmul( transpose(data_array),data_array )
distance2_matrix = distance2_matrix + transpose(distance2_matrix) - 2d0 * matmul( transpose(data_array),data_array )

end function calculate_similarity_matrix
end function calculate_distance2_matrix



Expand Down
32 changes: 20 additions & 12 deletions src/polychord/clustering.f90
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ module cluster_module
function do_clustering(settings,RTI,cluster,sub_dimensions)
use settings_module, only: program_settings
use run_time_module, only: run_time_info,add_cluster
use calculate_module, only: calculate_similarity_matrix
use calculate_module, only: calculate_distance2_matrix
use KNN_clustering, only: NN_clustering
implicit none

Expand All @@ -266,10 +266,10 @@ function do_clustering(settings,RTI,cluster,sub_dimensions)
integer,dimension(:),optional,intent(in) :: sub_dimensions

interface
function cluster(distance2_matrix) result(cluster_list)
function cluster(position_matrix) result(cluster_list)
import :: dp
real(dp), intent(in), dimension(:,:) :: distance2_matrix
integer, dimension(size(distance2_matrix,1)) :: cluster_list
real(dp), intent(in), dimension(:,:) :: position_matrix
integer, dimension(size(position_matrix,1)) :: cluster_list
AdamOrmondroyd marked this conversation as resolved.
Show resolved Hide resolved
end function
end interface

Expand All @@ -278,14 +278,16 @@ function cluster(distance2_matrix) result(cluster_list)


! Similarity matrix
real(dp),dimension(sum(RTI%nlive),sum(RTI%nlive)) :: distance2_matrix
real(dp),dimension(settings%nDims,sum(RTI%nlive)) :: position_matrix
integer,dimension(sum(RTI%nlive)) :: clusters

integer :: num_clusters
integer :: num_old_clusters

! number of live points
integer :: nlive
! number of dimensions
integer :: nDims

integer :: i_cluster

Expand All @@ -295,24 +297,30 @@ function cluster(distance2_matrix) result(cluster_list)
! Get the number of old clusters
num_old_clusters=RTI%ncluster

! get the number of dimensions
nDims = settings%nDims

i_cluster = 1
do while(i_cluster<=num_old_clusters)

nlive = RTI%nlive(i_cluster) ! Get the number of live points in a temp variable

if(nlive>2) then
! Calculate the similarity matrix for this cluster
! get the position matrix for this cluster
if(present(sub_dimensions)) then
distance2_matrix(:nlive,:nlive) =&
calculate_similarity_matrix(RTI%live(sub_dimensions,:nlive,i_cluster))
position_matrix(:nDims, :nlive) =&
RTI%live(sub_dimensions,:nlive,i_cluster)
else
distance2_matrix(:nlive,:nlive) =&
calculate_similarity_matrix(RTI%live(settings%h0:settings%h1,:nlive,i_cluster))
position_matrix(:nDims, :nlive) =&
RTI%live(settings%h0:settings%h1,:nlive,i_cluster)
end if

clusters(:nlive) = cluster(distance2_matrix(:nlive,:nlive))
clusters(:nlive) = cluster(position_matrix(:nDims, :nlive))

! default to KNN clustering
if (any(clusters(:nlive)<=0)) then
clusters(:nlive) = NN_clustering(distance2_matrix(:nlive,:nlive))
clusters(:nlive) = NN_clustering(&
calculate_distance2_matrix(position_matrix(:nDims, :nlive)))
end if
num_clusters = maxval(clusters(:nlive))

Expand Down
6 changes: 3 additions & 3 deletions src/polychord/interfaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void run_polychord(
double (*c_loglikelihood_ptr)(double*,int,double*,int),
void (*c_prior_ptr)(double*,double*,int),
void (*c_dumper_ptr)(int,int,int,double*,double*,double*,double,double),
void (*c_cluster_ptr)(double*,int*,int),
void (*c_cluster_ptr)(double*,int*,int,int),
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
Settings s);
void run_polychord(
double (*c_loglikelihood_ptr)(double*,int,double*,int),
Expand Down Expand Up @@ -75,7 +75,7 @@ void run_polychord(
double (*loglikelihood)(double*,int,double*,int),
void (*prior)(double*,double*,int),
void (*dumper)(int,int,int,double*,double*,double*,double,double),
void (*c_cluster_ptr)(double*,int*,int),
void (*c_cluster_ptr)(double*,int*,int,int),
Settings, MPI_Comm &comm);
void run_polychord(
double (*loglikelihood)(double*,int,double*,int),
Expand All @@ -102,4 +102,4 @@ void run_polychord(
double default_loglikelihood(double*,int,double*,int);
void default_prior(double*,double*,int);
void default_dumper(int,int,int,double*,double*,double*,double,double);
void default_cluster(double*,int*,int);
void default_cluster(double*,int*,int,int);