forked from JABull1066/ExtendedCorrelationFunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntheticDataset_II.py
153 lines (123 loc) · 5.68 KB
/
SyntheticDataset_II.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
from helperFunctions import *
import numpy as np
import seaborn as sns
import os
import pandas as pd
import matplotlib.pyplot as plt
sns.set(font_scale=5)
#%% Make synthetic data which correlates pairwise but not three-wise
np.random.seed(1536)
clusterCentres = np.asarray([[0.2,0.2],[0.5,0.8],[0.8,0.2]])
labelsPresent = [['$C_1$','$C_2$'],['$C_1$','$C_3$'],['$C_2$','$C_3$']]
points = np.empty(shape=(0,2))
labels = []
# background noise
nBackground = 0
for l in ['$C_1$','$C_2$','$C_3$']:
points = np.vstack([points, np.random.rand(nBackground,2)])
labels.extend([l for v in range(nBackground)])
sigma = 0.05
nPoints = 25
for j in range(3):
mu = clusterCentres[j]
l = labelsPresent[j]
points = np.vstack([points, mu + sigma*np.random.randn(2*nPoints,2)])
labels.extend([l[0] for v in range(nPoints)])
labels.extend([l[1] for v in range(nPoints)])
pc_pairwise = generatePointCloud('Pairwise Correlation Only', points*1000,domain=([[0,1000],[0,1000]]))
pc_pairwise.addLabels('Celltype', 'categorical', labels)
#%% Make synthetic data which correlates pairwise AND three-wise in the same way
clusterCentres = np.asarray([[0.2,0.2],[0.5,0.8],[0.8,0.2]])
labelsPresent = [['$C_1$','$C_2$','$C_3$'],['$C_1$','$C_2$','$C_3$'],['$C_1$','$C_2$','$C_3$']]
points = np.empty(shape=(0,2))
labels = []
# background noise
nBackground = 0
for l in ['$C_1$','$C_2$','$C_3$']:
points = np.vstack([points, np.random.rand(nBackground,2)])
labels.extend([l for v in range(nBackground)])
sigma = 0.05
nPoints = 25
for j in range(3):
mu = clusterCentres[j]
l = labelsPresent[j]
points = np.vstack([points, mu + sigma*np.random.randn(3*nPoints,2)])
labels.extend([l[0] for v in range(nPoints)])
labels.extend([l[1] for v in range(nPoints)])
labels.extend([l[2] for v in range(nPoints)])
pc_threewise = generatePointCloud('Three-way correlation', points*1000,domain=([[0,1000],[0,1000]]))
pc_threewise.addLabels('Celltype', 'categorical', labels)
#%% Visualise point clouds
visualisePointCloud(pc_pairwise,'Celltype',markerSize=100)
visualisePointCloud(pc_threewise,'Celltype',markerSize=100)
#%% Synthetic Dataset II - Pairwise cross-PCFs
for pc in [pc_pairwise, pc_threewise]:
plt.figure(figsize=(20,20))
plt.gca().axhline(1,c='k',linestyle=':',lw=4)
for cellpairs in [['$C_1$','$C_2$'],['$C_1$','$C_3$'],['$C_2$','$C_3$']]:
maxR = 1000
annulusStep = 10
annulusWidth = 50
r, pcf, contributions = pairCorrelationFunction(pc, 'Celltype', cellpairs, maxR=maxR,annulusStep=annulusStep,annulusWidth=annulusWidth)
label = '$g_{C_'+cellpairs[0][3]+' C_'+cellpairs[1][3]+'}(r)$'
plt.plot(r,pcf,lw=5,label=label)
plt.xlabel('Radius, $r$ ($\mu$m)')
plt.ylim([0,15])
plt.xlim([0,1000])
plt.title(pc.name)
plt.legend()
#%% Synthetic Dataset II - Neighbourhood correlation functions
maxR = 300
for pc in [pc_pairwise, pc_threewise]:
circles, triplets = neighbourhoodCorrelationFunction(pc,'Celltype',['$C_1$','$C_2$','$C_3$'],maxR=maxR)
order = np.arange(len(circles))
np.random.shuffle(order)
drawCircles = False
if drawCircles:
visualisePointCloud(pc,'Celltype',markerSize=100)
for i in range(len(order)):
circle = circles[order[i]]
col = plt.cm.plasma(circle[2]/maxR)
ec = [v for v in col]
ec[3] = 0.25
circle = plt.Circle((circle[0], circle[1]), circle[2], ec=col, fc=[0,0,0,0],zorder=-1)
plt.gca().add_patch(circle)
plt.gca().axis('equal')
circles = np.asarray(circles)
# Use bootstrapping to get predicted number of circles under CSR
nA = 2*nPoints + nBackground
nB = 2*nPoints + nBackground
nC = 2*nPoints + nBackground
redoBoostrap = False
# Set this flag to true if you want to regenerate the distribution under CSR
# Otherwise we load in precalculated values for speed
if redoBoostrap:
bootstrappedRadii = []
nBootstrap = 1000000
for i in range(nBootstrap):
if i % 10000 == 0:
print(i)
points_temp = np.random.rand(3,2)
pc_temp = generatePointCloud('temp',points_temp,domain=[[0,1],[0,1]])
pc_temp.addLabels('Celltype','categorical',['$C_1$','$C_2$','$C_3$'])
circles_temp, triplets_temp = neighbourhoodCorrelationFunction(pc_temp,'Celltype',['$C_1$','$C_2$','$C_3$'],maxR=2)
bootstrappedRadii.append(circles_temp[0][2])
bootstrappedRadii = [v*1000 for v in bootstrappedRadii] # As this was generated in mm, not mu m
vals_bootstrap, rs = np.histogram(bootstrappedRadii,bins=bins)
vals_bootstrap = nA*nB*nC*vals_bootstrap/nBootstrap
else:
vals_observed, rs = np.histogram(circles,bins=bins)
vals_bootstrap = np.asarray([0.375,1.375,4.875,17.625,39.25,65.875,103.75,143.75,208.875,276.5,364.625,464,578.25,713.75,856.125,997.625,1151.5,1325.88,1489.88,1684.62,1875.88,2075.88,2280.38,2495.5,2682.62,2887.62,3072.5,3305.75,3487.88,3617])
plt.figure(figsize=(18,18))
plt.plot(rs[1:],vals_bootstrap,label='Expectation (CSR)',lw=5)
plt.plot(rs[1:],vals_observed,label='Observation',lw=5)
plt.xlabel('$r$ (mm)')
plt.ylabel('Number')
plt.legend()
plt.title(pc.name)
plt.figure(figsize=(18,18))
plt.plot(rs[1:],vals_observed/vals_bootstrap,lw=5)
plt.gca().axhline(1,c='k',linestyle=':',lw=5)
plt.xlabel('$r$ ($\mu$m)')
plt.ylabel('NCF$_{C_1 C_2 C_3}(r)$')
plt.title(pc.name)