plot_statistics 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/bivariate_ripleys_k +39 -0
- data/bin/{plot_statistics → univariate_ripleys_k} +4 -3
- data/lib/plot_statistics/clam.rb +4 -0
- data/lib/plot_statistics/clam_plot.rb +57 -14
- data/lib/plot_statistics/monte_carlo.rb +45 -38
- data/lib/plot_statistics/output.rb +21 -1
- data/plot_statistics.gemspec +5 -5
- metadata +8 -6
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.4.0
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'plot_statistics'
|
5
|
+
rescue LoadError
|
6
|
+
require 'rubygems'
|
7
|
+
gem 'plot_statistics'
|
8
|
+
require 'plot_statistics'
|
9
|
+
end
|
10
|
+
|
11
|
+
ARGV.each do |filename|
|
12
|
+
filename = "../High Aggregated Plot 1.txt"
|
13
|
+
file = File.new(filename)
|
14
|
+
lines = file.readlines
|
15
|
+
file.close
|
16
|
+
|
17
|
+
puts "Analyzing #{filename}"
|
18
|
+
printf "Importing Clams"
|
19
|
+
live_clams = []
|
20
|
+
dead_clams = []
|
21
|
+
lines.each do |line|
|
22
|
+
line = line.strip.split("\t")
|
23
|
+
next if line.empty?
|
24
|
+
x, y, number_eaten = line
|
25
|
+
(5 - number_eaten).times { printf "."; live_clams << PlotStatistics::Clam.new(:x => x, :y => y) }
|
26
|
+
number_eaten.to_i.times { printf "."; dead_clams << PlotStatistics::Clam.new(:x => x, :y => y) }
|
27
|
+
end.compact
|
28
|
+
|
29
|
+
puts "Running Ripley's K analysis of plot"
|
30
|
+
actual_plot = PlotStatistics::ClamPlot.new(live_clams, dead_clams).bivariate_analysis
|
31
|
+
|
32
|
+
puts "Running Monte Carlo Simulations of plot"
|
33
|
+
monte_carlo = PlotStatistics::MonteCarlo.new_bivariate(actual_plot).bivariate_analysis
|
34
|
+
|
35
|
+
output_filename = filename + '.ripleys_k.csv'
|
36
|
+
PlotStatistics::Output.new(:clam_plot => actual_plot, :monte_carlo => monte_carlo).to_bivariate_file(output_filename)
|
37
|
+
|
38
|
+
puts "Done™"
|
39
|
+
end
|
@@ -9,6 +9,7 @@ rescue LoadError
|
|
9
9
|
end
|
10
10
|
|
11
11
|
ARGV.each do |filename|
|
12
|
+
filename = "../Clam distributions/Cabbage/Cabbage_3.txt"
|
12
13
|
file = File.new(filename)
|
13
14
|
lines = file.readlines
|
14
15
|
file.close
|
@@ -24,13 +25,13 @@ ARGV.each do |filename|
|
|
24
25
|
end.compact
|
25
26
|
|
26
27
|
puts "Running Ripley's K analysis of plot"
|
27
|
-
actual_plot = PlotStatistics::ClamPlot.new(clams)
|
28
|
+
actual_plot = PlotStatistics::ClamPlot.new(clams).univariate_analysis
|
28
29
|
|
29
30
|
puts "Running Monte Carlo Simulations of plot"
|
30
|
-
monte_carlo = PlotStatistics::MonteCarlo.
|
31
|
+
monte_carlo = PlotStatistics::MonteCarlo.new_univariate(actual_plot.number_of_clams).univariate_analysis
|
31
32
|
|
32
33
|
output_filename = filename + '.ripleys_k.csv'
|
33
|
-
PlotStatistics::Output.new(:clam_plot => actual_plot, :monte_carlo => monte_carlo).
|
34
|
+
PlotStatistics::Output.new(:clam_plot => actual_plot, :monte_carlo => monte_carlo).to_univariate_file(output_filename)
|
34
35
|
|
35
36
|
puts "Done™"
|
36
37
|
end
|
data/lib/plot_statistics/clam.rb
CHANGED
@@ -1,16 +1,29 @@
|
|
1
1
|
class PlotStatistics
|
2
2
|
class ClamPlot
|
3
|
-
attr_accessor :clams, :stats
|
3
|
+
attr_accessor :clams, :dead_clams, :stats
|
4
4
|
|
5
5
|
AREA_OF_PLOT = 10_000
|
6
6
|
MAX_RADIUS = 50
|
7
7
|
PLOT_CORNERS = [[0,0], [0,100], [100,0], [100,100]]
|
8
8
|
|
9
|
-
def initialize(clams)
|
9
|
+
def initialize(clams, dead_clams=[])
|
10
10
|
@clams = clams
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
@dead_clams = dead_clams
|
12
|
+
end
|
13
|
+
|
14
|
+
def univariate_analysis
|
15
|
+
self.stats = OpenStruct.new(:k_ts => [], :l_ts => [])
|
16
|
+
setup_clam_distances
|
17
|
+
calculate_univariate_stats
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def bivariate_analysis
|
22
|
+
self.stats = OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => [])
|
23
|
+
setup_clam_distances
|
24
|
+
setup_clam_distances(:dead_clams)
|
25
|
+
calculate_bivariate_stats
|
26
|
+
self
|
14
27
|
end
|
15
28
|
|
16
29
|
def self.create_random(number_of_clams)
|
@@ -29,20 +42,29 @@ class PlotStatistics
|
|
29
42
|
(0..100).each do |x|
|
30
43
|
(0..100).each do |y|
|
31
44
|
mod_x = x % distance
|
32
|
-
mod_y =
|
45
|
+
mod_y = y % distance
|
33
46
|
clams << Clam.new(:x => x, :y => y) if mod_x == 0 && mod_y == 0
|
34
47
|
end
|
35
48
|
end
|
36
49
|
new(clams)
|
37
50
|
end
|
38
51
|
|
52
|
+
def self.create_random_from(clam_plot)
|
53
|
+
number_dead = clam_plot.dead_clams.size
|
54
|
+
all_clams = clam_plot.dead_clams | clam_plot.clams
|
55
|
+
dead_clams = all_clams.shuffle.slice(0...number_dead)
|
56
|
+
live_clams = all_clams - dead_clams
|
57
|
+
new(live_clams, dead_clams)
|
58
|
+
end
|
59
|
+
|
39
60
|
def number_of_clams
|
40
61
|
clams.size.to_f
|
41
62
|
end
|
42
63
|
|
43
|
-
def setup_clam_distances(clams)
|
44
|
-
|
45
|
-
|
64
|
+
def setup_clam_distances(clam_type='clams')
|
65
|
+
reset_clam_distances
|
66
|
+
send(clam_type).each_with_index do |reference_clam, i|
|
67
|
+
send(clam_type)[(i + 1)..-1].each do |other_clam|
|
46
68
|
distance = distance_between_clams(reference_clam, other_clam)
|
47
69
|
reference_clam.distances << distance
|
48
70
|
other_clam.distances << distance
|
@@ -54,19 +76,32 @@ class PlotStatistics
|
|
54
76
|
Math.sqrt((clam1.x - clam2.x) ** 2 + (clam1.y - clam2.y) ** 2)
|
55
77
|
end
|
56
78
|
|
57
|
-
def
|
79
|
+
def calculate_bivariate_stats
|
80
|
+
(1..MAX_RADIUS).each do |radius|
|
81
|
+
|
82
|
+
live_k_t = calculate_k_t(radius)
|
83
|
+
dead_k_t = calculate_k_t(radius, :dead_clams)
|
84
|
+
l_t = calculate_bivariate_l_t(live_k_t, dead_k_t, radius)
|
85
|
+
|
86
|
+
stats.k_ts << live_k_t
|
87
|
+
stats.dead_k_ts << dead_k_t
|
88
|
+
stats.l_ts << l_t
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def calculate_univariate_stats
|
58
93
|
(1..MAX_RADIUS).each do |radius|
|
59
94
|
|
60
95
|
k_t = calculate_k_t(radius)
|
61
|
-
l_t =
|
96
|
+
l_t = calculate_univariate_l_t(k_t, radius)
|
62
97
|
|
63
98
|
stats.k_ts << k_t
|
64
99
|
stats.l_ts << l_t
|
65
100
|
end
|
66
101
|
end
|
67
102
|
|
68
|
-
def calculate_k_t(radius)
|
69
|
-
sums =
|
103
|
+
def calculate_k_t(radius, clam_type='clams')
|
104
|
+
sums = send(clam_type).inject(0.0) do |sum, clam|
|
70
105
|
clams_inside_circle = clam.distances.select { |distance| distance <= radius }
|
71
106
|
|
72
107
|
circle_proportion = Circle.new(:clam => clam, :radius => radius).proportion_inside_plot
|
@@ -79,8 +114,16 @@ class PlotStatistics
|
|
79
114
|
AREA_OF_PLOT * sums / (number_of_clams ** 2)
|
80
115
|
end
|
81
116
|
|
82
|
-
def
|
117
|
+
def calculate_univariate_l_t(k_t, radius)
|
83
118
|
radius - Math.sqrt( k_t / Math::PI)
|
84
119
|
end
|
120
|
+
|
121
|
+
def calculate_bivariate_l_t(live_k_t, dead_k_t, radius)
|
122
|
+
radius - Math.sqrt( (live_k_t - dead_k_t).abs / Math::PI)
|
123
|
+
end
|
124
|
+
|
125
|
+
def reset_clam_distances
|
126
|
+
(clams | dead_clams).each { |clam| clam.reset_distances }
|
127
|
+
end
|
85
128
|
end
|
86
129
|
end
|
@@ -2,68 +2,75 @@ class PlotStatistics
|
|
2
2
|
class MonteCarlo
|
3
3
|
attr_accessor :plots, :stats
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@plots =
|
7
|
-
@stats = OpenStruct.new(:mean => OpenStruct.new(:k_ts => [], :l_ts => []),
|
8
|
-
:upper_limit => OpenStruct.new(:k_ts => [], :l_ts => []),
|
9
|
-
:lower_limit => OpenStruct.new(:k_ts => [], :l_ts => []))
|
10
|
-
calculate_means
|
11
|
-
calculate_limits
|
5
|
+
def initialize(params={})
|
6
|
+
@plots = params[:plots]
|
12
7
|
end
|
13
8
|
|
14
|
-
def
|
9
|
+
def self.new_univariate(number_of_clams, number_of_plots=500.0)
|
10
|
+
plots = (1..number_of_plots).map { ClamPlot.create_random(number_of_clams).univariate_analysis }
|
11
|
+
new(:plots => plots)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.new_bivariate(actual_plot, number_of_plots=500.0)
|
15
|
+
plots = (1..number_of_plots).map { ClamPlot.create_random_from(actual_plot).bivariate_analysis }
|
16
|
+
new(:plots => plots)
|
17
|
+
end
|
18
|
+
|
19
|
+
def bivariate_analysis
|
20
|
+
self.stats = OpenStruct.new(:mean => OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => []),
|
21
|
+
:upper_limit => OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => []),
|
22
|
+
:lower_limit => OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => []))
|
23
|
+
calculate_means([:k_ts, :dead_k_ts, :l_ts])
|
24
|
+
calculate_limits([:k_ts, :dead_k_ts, :l_ts])
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def univariate_analysis
|
29
|
+
self.stats = OpenStruct.new(:mean => OpenStruct.new(:k_ts => [], :l_ts => []),
|
30
|
+
:upper_limit => OpenStruct.new(:k_ts => [], :l_ts => []),
|
31
|
+
:lower_limit => OpenStruct.new(:k_ts => [], :l_ts => []))
|
32
|
+
calculate_means([:k_ts, :l_ts])
|
33
|
+
calculate_limits([:k_ts, :l_ts])
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def calculate_means(means_for)
|
15
38
|
(0...ClamPlot::MAX_RADIUS).each do |radius|
|
16
|
-
|
17
|
-
|
39
|
+
means_for.each do |stat|
|
40
|
+
stats.mean.send(stat) << average_stat(radius, stat)
|
41
|
+
end
|
18
42
|
end
|
19
43
|
end
|
20
44
|
|
21
|
-
def calculate_limits
|
45
|
+
def calculate_limits(limits_for)
|
22
46
|
(0...ClamPlot::MAX_RADIUS).each do |radius|
|
23
|
-
|
24
|
-
|
47
|
+
limits_for.each do |stat|
|
48
|
+
threshold = standard_deviation_stat(radius, stat) * 2.0
|
25
49
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
stats.lower_limit.l_ts << (stats.mean.l_ts[radius] - threshold_l)
|
50
|
+
stats.upper_limit.send(stat) << (stats.mean.send(stat)[radius] + threshold)
|
51
|
+
stats.lower_limit.send(stat) << (stats.mean.send(stat)[radius] - threshold)
|
52
|
+
end
|
30
53
|
end
|
31
54
|
end
|
32
55
|
|
33
|
-
def
|
56
|
+
def standard_deviation_stat(radius, stat)
|
34
57
|
sum = plots.inject(0.0) do |sum, plot|
|
35
|
-
(plot.stats.
|
58
|
+
(plot.stats.send(stat)[radius] - stats.mean.send(stat)[radius]) ** 2
|
36
59
|
end
|
37
60
|
|
38
61
|
Math.sqrt(sum / number_of_plots)
|
39
62
|
end
|
40
63
|
|
41
|
-
def
|
64
|
+
def average_stat(radius, stat)
|
42
65
|
sum = plots.inject(0.0) do |sum, plot|
|
43
|
-
|
66
|
+
plot.stats.send(stat)[radius]
|
44
67
|
end
|
45
68
|
|
46
|
-
|
69
|
+
sum / number_of_plots
|
47
70
|
end
|
48
71
|
|
49
72
|
def number_of_plots
|
50
73
|
plots.size
|
51
74
|
end
|
52
|
-
|
53
|
-
def average_k(radius)
|
54
|
-
sum = plots.inject(0.0) do |sum, plot|
|
55
|
-
plot.stats.k_ts[radius]
|
56
|
-
end
|
57
|
-
|
58
|
-
sum / number_of_plots
|
59
|
-
end
|
60
|
-
|
61
|
-
def average_l(radius)
|
62
|
-
sum = plots.inject(0.0) do |sum, plot|
|
63
|
-
plot.stats.l_ts[radius]
|
64
|
-
end
|
65
|
-
|
66
|
-
sum / number_of_plots
|
67
|
-
end
|
68
75
|
end
|
69
76
|
end
|
@@ -7,7 +7,7 @@ class PlotStatistics
|
|
7
7
|
@monte_carlo = params[:monte_carlo]
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def to_univariate_file(filename)
|
11
11
|
FasterCSV.open(filename, 'w') do |csv|
|
12
12
|
csv << ['Radius', 'K(t)', 'L(t)', 'Mean K(t)', 'Mean L(t)', 'Upper K(t)', 'Lower K(t)', 'Upper L(t)', 'Lower L(t)']
|
13
13
|
|
@@ -23,5 +23,25 @@ class PlotStatistics
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
def to_bivariate_file(filename)
|
28
|
+
FasterCSV.open(filename, 'w') do |csv|
|
29
|
+
csv << ['Radius', 'Live K(t)', 'Dead K(t)', 'L(t)', 'Mean Live K(t)', 'Mean Dead K(t)', 'Mean L(t)', 'Upper Live K(t)', 'Lower Live K(t)', 'Upper Dead K(t)', 'Lower Dead K(t)', 'Upper L(t)', 'Lower L(t)']
|
30
|
+
|
31
|
+
(0...ClamPlot::MAX_RADIUS).each do |position|
|
32
|
+
radius = position + 1
|
33
|
+
csv << [
|
34
|
+
radius,
|
35
|
+
clam_plot.stats.k_ts[position], clam_plot.stats.dead_k_ts[position],
|
36
|
+
clam_plot.stats.l_ts[position],
|
37
|
+
monte_carlo.stats.mean.k_ts[position], monte_carlo.stats.mean.dead_k_ts[position],
|
38
|
+
monte_carlo.stats.mean.l_ts[position],
|
39
|
+
monte_carlo.stats.upper_limit.k_ts[position], monte_carlo.stats.lower_limit.k_ts[position],
|
40
|
+
monte_carlo.stats.upper_limit.dead_k_ts[position], monte_carlo.stats.lower_limit.dead_k_ts[position],
|
41
|
+
monte_carlo.stats.upper_limit.l_ts[position], monte_carlo.stats.lower_limit.l_ts[position]
|
42
|
+
]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
26
46
|
end
|
27
47
|
end
|
data/plot_statistics.gemspec
CHANGED
@@ -5,15 +5,14 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{plot_statistics}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Asa Wilson"]
|
12
|
-
s.date = %q{2010-
|
13
|
-
s.default_executable = %q{plot_statistics}
|
12
|
+
s.date = %q{2010-09-27}
|
14
13
|
s.description = %q{This is a gem to do a Ripley's K analysis}
|
15
14
|
s.email = %q{acvwilson@gmail.com}
|
16
|
-
s.executables = ["
|
15
|
+
s.executables = ["bivariate_ripleys_k", "univariate_ripleys_k"]
|
17
16
|
s.extra_rdoc_files = [
|
18
17
|
"LICENSE",
|
19
18
|
"README.markdown"
|
@@ -25,7 +24,8 @@ Gem::Specification.new do |s|
|
|
25
24
|
"README.markdown",
|
26
25
|
"Rakefile",
|
27
26
|
"VERSION",
|
28
|
-
"bin/
|
27
|
+
"bin/bivariate_ripleys_k",
|
28
|
+
"bin/univariate_ripleys_k",
|
29
29
|
"lib/plot_statistics.rb",
|
30
30
|
"lib/plot_statistics/circle.rb",
|
31
31
|
"lib/plot_statistics/clam.rb",
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
7
|
+
- 4
|
8
8
|
- 0
|
9
|
-
version: 1.
|
9
|
+
version: 1.4.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Asa Wilson
|
@@ -14,8 +14,8 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
18
|
-
default_executable:
|
17
|
+
date: 2010-09-27 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -48,7 +48,8 @@ dependencies:
|
|
48
48
|
description: This is a gem to do a Ripley's K analysis
|
49
49
|
email: acvwilson@gmail.com
|
50
50
|
executables:
|
51
|
-
-
|
51
|
+
- bivariate_ripleys_k
|
52
|
+
- univariate_ripleys_k
|
52
53
|
extensions: []
|
53
54
|
|
54
55
|
extra_rdoc_files:
|
@@ -61,7 +62,8 @@ files:
|
|
61
62
|
- README.markdown
|
62
63
|
- Rakefile
|
63
64
|
- VERSION
|
64
|
-
- bin/
|
65
|
+
- bin/bivariate_ripleys_k
|
66
|
+
- bin/univariate_ripleys_k
|
65
67
|
- lib/plot_statistics.rb
|
66
68
|
- lib/plot_statistics/circle.rb
|
67
69
|
- lib/plot_statistics/clam.rb
|