Skip to content

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

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

Open
wants to merge 48 commits into
base: custom_cluster
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pypolychord/_pypolychord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static PyObject *python_cluster = NULL;
void cluster(double* points, int* cluster_list, int nDims, int nPoints)
{
/* create a python version of points */
npy_intp shape[] = {nDims,nPoints};
npy_intp shape[] = {nPoints,nDims};
PyObject *array_points = PyArray_SimpleNewFromData(2, shape, NPY_DOUBLE, points);
if (array_points ==NULL) throw PythonException();
PyArray_CLEARFLAGS(reinterpret_cast<PyArrayObject*>(array_points), NPY_ARRAY_WRITEABLE);
Expand Down
4 changes: 2 additions & 2 deletions pypolychord/polychord.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def default_dumper(live, dead, logweights, logZ, logZerr):


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


def run_polychord(loglikelihood, nDims, nDerived, settings,
Expand Down Expand Up @@ -113,7 +113,7 @@ def run_polychord(loglikelihood, nDims, nDerived, settings,
Parameters
----------
points: numpy.array
positions of points. Shape (nDims, nPoints)
positions of points. Shape (nPoints, nDims)

Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion run_pypolychord.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def dumper(live, dead, logweights, logZ, logZerr):
#| Optional cluster function allow user-defined clustering

def cluster(points):
npoints = points.shape[1]
npoints = points.shape[0]
clusters = np.ones(npoints, dtype=int)

# <do some clustering algorithm to assign clusters>
Expand Down
4 changes: 2 additions & 2 deletions src/polychord/c_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,5 @@ void default_prior(double* cube, double* theta, int nDims)

void default_dumper(int,int,int,double*,double*,double*,double,double) {}

void default_cluster(double* points, int* cluster_list, int m, int n)
{ for(int i=0;i<n;i++) cluster_list[i] = 0; }
void default_cluster(double* points, int* cluster_list, int nDims, int nPoints)
{ for(int i=0;i<nPoints;i++) cluster_list[i] = 0; }
20 changes: 10 additions & 10 deletions src/polychord/calculate.f90
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,32 @@ function calculate_posterior_point(settings,point,logweight,evidence,volume) res
end function calculate_posterior_point


!> This function computes the similarity matrix of an array of data.
!> This function computes the distance^2 matrix of an array of data.
!!
!! Assume that the data_array can be considered an indexed array of vectors
!! Assume that the points can be considered an indexed array of vectors
!! V = ( v_i : i=1,n )
!!
!! The similarity matrix can be expressed very neatly as
!! The distance^2 matrix can be expressed very neatly as
!! d_ij = (v_i-v_j) . (v_i-v_j)
!! = v_i.v_i + v_j.v_j - 2 v_i.v_j
!!
!! The final term can be written as a data_array^T data_array, and the first
!! The final term can be written as a points^T points, and the first
!! two are easy to write. We can therefore calculate this in two lines with
!! instrisic functions
function calculate_distance2_matrix(data_array) result(distance2_matrix)
function calculate_distance2_matrix(points) result(distance2_matrix)

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

real(dp), dimension(size(data_array,2),size(data_array,2)) ::distance2_matrix
real(dp), dimension(size(points,1),size(points,1)) ::distance2_matrix

integer :: i


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

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

end function calculate_distance2_matrix

Expand Down
14 changes: 7 additions & 7 deletions src/polychord/clustering.f90
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function do_clustering(settings,RTI,cluster,sub_dimensions)
function cluster(points) result(cluster_list)
import :: dp
real(dp), intent(in), dimension(:,:) :: points
integer, dimension(size(points,2)) :: cluster_list
integer, dimension(size(points,1)) :: cluster_list
end function
end interface

Expand Down Expand Up @@ -308,19 +308,19 @@ function cluster(points) result(cluster_list)
if(nlive>2) then
! get the position matrix for this cluster
if(present(sub_dimensions)) then
points(:nDims, :nlive) =&
RTI%live(sub_dimensions,:nlive,i_cluster)
points(:nlive, :nDims) =&
transpose(RTI%live(sub_dimensions,:nlive,i_cluster))
else
points(:nDims, :nlive) =&
RTI%live(settings%h0:settings%h1,:nlive,i_cluster)
points(:nlive, :nDims) =&
transpose(RTI%live(settings%h0:settings%h1,:nlive,i_cluster))
end if

clusters(:nlive) = cluster(points(:nDims, :nlive))
clusters(:nlive) = cluster(points(:nlive, :nDims))

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

Expand Down
18 changes: 9 additions & 9 deletions src/polychord/interfaces.F90
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end subroutine dumper
function cluster(points) result(cluster_list)
import :: dp
real(dp), intent(in), dimension(:,:) :: points
integer, dimension(size(points,2)) :: cluster_list
integer, dimension(size(points,1)) :: cluster_list
end function
end interface

Expand Down Expand Up @@ -132,7 +132,7 @@ end subroutine dumper
contains
function cluster(points) result(cluster_list)
real(dp), intent(in), dimension(:,:) :: points
integer, dimension(size(points,2)) :: cluster_list
integer, dimension(size(points,1)) :: cluster_list
cluster_list = 0
end function
end subroutine run_polychord_no_cluster
Expand Down Expand Up @@ -390,11 +390,11 @@ subroutine c_dumper(ndead, nlive, npars, live, dead, logweights, logZ, logZerr)
end subroutine c_dumper
end interface
interface
subroutine c_cluster(points,cluster_list,m,n) bind(c)
subroutine c_cluster(points,cluster_list,nDims,nPoints) bind(c)
use iso_c_binding
integer(c_int), intent(in), value :: m, n
real(c_double), intent(in), dimension(m,n) :: points
integer(c_int), intent(out), dimension(n) :: cluster_list
integer(c_int), intent(in), value :: nDims, nPoints
real(c_double), intent(in), dimension(nPoints,nDims) :: points
integer(c_int), intent(out), dimension(nPoints) :: cluster_list
end subroutine c_cluster
end interface

Expand Down Expand Up @@ -564,12 +564,12 @@ end subroutine dumper
function cluster(points) result(cluster_list)
implicit none
real(dp), intent(in), dimension(:,:) :: points
integer, dimension(size(points,2)) :: cluster_list
integer, dimension(size(points,1)) :: cluster_list

integer(c_int) :: c_npoints, c_ndims
real(c_double), dimension(size(points,1),size(points,2)) :: c_points
integer(c_int), dimension(size(points,2)) :: c_cluster_list
c_ndims = size(points,1)
integer(c_int), dimension(size(points,1)) :: c_cluster_list
c_ndims = size(points,2)
c_npoints = size(c_cluster_list)
c_points = points
call f_cluster_ptr(c_points,c_cluster_list,c_ndims,c_npoints)
Expand Down
2 changes: 1 addition & 1 deletion src/polychord/nested_sampling.F90
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ end subroutine dumper
function cluster(points) result(cluster_list)
import :: dp
real(dp), intent(in), dimension(:,:) :: points
integer, dimension(size(points,2)) :: cluster_list
integer, dimension(size(points,1)) :: cluster_list
end function
end interface

Expand Down