forked from RubyPeters/Angular-Ripleys-K
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngularRipleysKForDemoData.m
148 lines (97 loc) · 4.34 KB
/
AngularRipleysKForDemoData.m
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% AngularRipleysK - Main analysis code
%
% Ruby Peters, King's College London, 2016.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% User Inputs
clear
MainDirectory = '/Users/user/Documents/work/Angular-Ripleys-K/'; % Please enter the main directory in which your cropped ROIs are saved.
cd(MainDirectory);
r=200; % Please enter the Radius over which to perform the analysis. Default r=200nm
LateralCropping = 200; % This will crop edges to compensate for edge effects. LateralCropping must be >=r. Default 200 nm.
AngleIncrement = 5; % Angular increment (in degrees) to perform the analysis. Default = 5 degrees.
Shift = 1; % 1 for True and 0 for False (depending on if you want to shift H(a) Values - default 1)
%% Main script
FileName = ['data.txt'];
InputData = importdata(FileName);
OutputData=InputData;
yMax = max(OutputData(:,2));
xMax = max(OutputData(:,1));
AreaData = xMax*yMax;
CentreRegion=[[(xMax./2) (yMax./2)]];
yRoi = yMax-2*LateralCropping;
xRoi = xMax-2*LateralCropping;
AreaRoi = xRoi*yRoi;
xCoord = OutputData(:,1);
yCoord = OutputData(:,2);
NumberPoints = size(OutputData,1);
Tested_X = [];
Tested_Y = [];
for t = 1:NumberPoints
if xCoord(t)>=LateralCropping && xCoord(t)<=(xRoi+LateralCropping) && yCoord(t)>=LateralCropping && yCoord(t)<=(yRoi+LateralCropping)
Tested_X = [Tested_X ; xCoord(t)];
Tested_Y = [Tested_Y ; yCoord(t)];
end
end
for r=200
AngleBins = (0:AngleIncrement:(360-AngleIncrement))';
CumKValues = zeros(size(AngleBins,1),1);
CumShiftedKValues = zeros(size(AngleBins,1),1);
AreaSegment = pi*(r^2)*AngleIncrement/360;
for j = 1:size(Tested_X,1)
x = Tested_X(j);
y = Tested_Y(j);
r_xCoord = xCoord;
r_yCoord = yCoord;
tDistances = sqrt ((r_xCoord-x).^2 + (r_yCoord-y).^2);
r_xCoord(j) = [];
r_yCoord(j) = [];
tDistances(j) = [];
bad_inds = tDistances >= r;
r_xCoord(bad_inds) = [];
r_yCoord(bad_inds) = [];
tDistances(bad_inds) = [];
% Calculate angles of each vector with the reference vertical vector
% of length r
NormRef = r;
DotProduct = (r_yCoord-y)*r;
Norm = tDistances;
Angles = acos((DotProduct./(Norm*NormRef)))*180/pi;
neg_inds = r_xCoord-x<=0;
Angles(neg_inds) = 360-Angles(neg_inds);
KValues = sum(AngleBins<=Angles.' & (AngleBins+AngleIncrement)>Angles.', 2);
if Shift == 1
[ShiftedKValues] = ShiftMaxima(KValues);
CumShiftedKValues = CumShiftedKValues + ShiftedKValues;
end
CumKValues = CumKValues + KValues;
end
AverageKValues = CumKValues/size(Tested_X,1);
if Shift == 1
AverageShiftedKValues = CumShiftedKValues/size(Tested_X,1);
end
%% Normalisation of Values by comparison with analytical solution of CSR
NormalisedKValues = 360/AngleIncrement*AreaData/NumberPoints*AverageKValues;
if Shift == 1
NormalisedShiftedKValues = 360/AngleIncrement*AreaData/NumberPoints*AverageShiftedKValues;
ShiftedLValues=sqrt(NormalisedShiftedKValues/pi);
ShiftedHValues=ShiftedLValues-r;
end
%% L and H values
LValues = sqrt(NormalisedKValues/pi);
HValues = LValues-r;
%% Set saving feature
Hvaluesdata = [AngleBins HValues ShiftedHValues];
FileName2 = ['OutputData.txt'];
dlmwrite(FileName2,Hvaluesdata); %OutputData will inclue Angluar Bin ranges, H(a) Values and Shifted H(a) values.
if Shift == 1
figure
plot(AngleBins,ShiftedHValues,'-k','Linewidth',1.5);set(gca,'box','on');
axis('square'); xlabel('Angle ({\circ})'); ylabel('Shifted H Values'); xlim([0 355])
savefig('Shifted_H_versus_AngleBins.fig')
close
end
cd(MainDirectory)
end