plot_statistics 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/plot_statistics +8 -16
- data/lib/plot_statistics/circle.rb +6 -6
- data/lib/plot_statistics/clam.rb +2 -2
- data/lib/plot_statistics/clam_plot.rb +18 -8
- data/lib/plot_statistics/monte_carlo.rb +7 -7
- data/lib/plot_statistics/output.rb +18 -0
- data/lib/plot_statistics.rb +1 -0
- data/plot_statistics.gemspec +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/bin/plot_statistics
CHANGED
@@ -8,33 +8,25 @@ rescue LoadError
|
|
8
8
|
require 'plot_statistics'
|
9
9
|
end
|
10
10
|
|
11
|
-
ARGV.each do |
|
12
|
-
|
13
|
-
|
14
|
-
file
|
11
|
+
ARGV.each do |filename|
|
12
|
+
file = File.new(filename)
|
13
|
+
lines = file.readlines
|
14
|
+
file.close
|
15
15
|
|
16
16
|
puts "\n"
|
17
17
|
puts '##########################'
|
18
|
-
puts "# #{
|
18
|
+
puts "# #{filename}"
|
19
19
|
puts '##########################'
|
20
20
|
puts "\n"
|
21
21
|
|
22
|
-
lines = file.readlines
|
23
|
-
|
24
22
|
clams = lines.map do |line|
|
25
23
|
x, y = line.strip.split("\t")
|
26
|
-
PlotStatistics::Clam.new(:x => x
|
24
|
+
PlotStatistics::Clam.new(:x => x, :y => y)
|
27
25
|
end
|
28
26
|
|
29
|
-
puts "\nActual plot started at: #{Time.now}\n"
|
30
|
-
|
31
27
|
actual_plot = PlotStatistics::ClamPlot.new(clams)
|
32
|
-
puts actual_plot.inspect
|
33
|
-
|
34
|
-
puts "\nMonte Carlo plots started at: #{Time.now}\n"
|
35
|
-
|
36
28
|
monte_carlo = PlotStatistics::MonteCarlo.new(actual_plot.number_of_clams)
|
37
|
-
puts monte_carlo.inspect
|
38
29
|
|
39
|
-
|
30
|
+
output_filename = filename + '.ripleys_k.csv'
|
31
|
+
PlotStatistics::Output.new(:clam_plot => actual_plot, :monte_carlo => monte_carlo).write_to_file(output_filename)
|
40
32
|
end
|
@@ -26,7 +26,7 @@ class PlotStatistics
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def proportion_for_two_sides_out
|
29
|
-
if
|
29
|
+
if corners_outside_circle.size == 3
|
30
30
|
1 - ( Math.acos( distance_from_bound.pop / radius ) + Math.acos( distance_from_bound.pop / radius ) + Math::PI / 2 ) / (Math::PI * 2)
|
31
31
|
else
|
32
32
|
1 - ( 2 * Math.acos( distance_from_bound.pop / radius ) + 2 * Math.acos( distance_from_bound.pop / radius ) ) / (Math::PI * 2)
|
@@ -35,14 +35,14 @@ class PlotStatistics
|
|
35
35
|
|
36
36
|
def find_distance_from_bounds
|
37
37
|
[
|
38
|
-
|
39
|
-
|
40
|
-
clam.x,
|
41
|
-
clam.y
|
38
|
+
100 - clam.y, # distance from top
|
39
|
+
100 - clam.x, # distance from right
|
40
|
+
clam.x, # distance from left
|
41
|
+
clam.y # distance from bottom
|
42
42
|
]
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
45
|
+
def corners_outside_circle
|
46
46
|
ClamPlot::PLOT_CORNERS.map do |corner|
|
47
47
|
distance = Math.sqrt((corner.first - clam.x) ** 2 + (corner.last - clam.y) ** 2)
|
48
48
|
next if distance < radius
|
data/lib/plot_statistics/clam.rb
CHANGED
@@ -2,7 +2,7 @@ class PlotStatistics
|
|
2
2
|
class ClamPlot
|
3
3
|
attr_accessor :clams, :stats
|
4
4
|
|
5
|
-
AREA_OF_PLOT =
|
5
|
+
AREA_OF_PLOT = 1 # this is in meters everything else is in centimeters
|
6
6
|
MAX_RADIUS = 50
|
7
7
|
PLOT_CORNERS = [[0,0], [0,100], [100,0], [100,100]]
|
8
8
|
|
@@ -18,8 +18,20 @@ class PlotStatistics
|
|
18
18
|
new(clams)
|
19
19
|
end
|
20
20
|
|
21
|
+
def self.create_regular(distance=5)
|
22
|
+
clams = []
|
23
|
+
(0..100).each do |x|
|
24
|
+
(0..100).each do |y|
|
25
|
+
mod_x = x % distance
|
26
|
+
mod_y = x % distance
|
27
|
+
clams << Clam.new(:x => x, :y => y) if mod_x == 0 && mod_y == 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
new(clams)
|
31
|
+
end
|
32
|
+
|
21
33
|
def number_of_clams
|
22
|
-
clams.size
|
34
|
+
clams.size.to_f
|
23
35
|
end
|
24
36
|
|
25
37
|
def setup_clam_distances(clams)
|
@@ -39,18 +51,16 @@ class PlotStatistics
|
|
39
51
|
def calculate_stats
|
40
52
|
(1..MAX_RADIUS).each do |radius|
|
41
53
|
|
42
|
-
sums = clams.inject(0) do |sum, clam|
|
54
|
+
sums = clams.inject(0.0) do |sum, clam|
|
43
55
|
clams_inside_circle = clam.distances.select { |distance| distance <= radius }
|
44
56
|
|
45
57
|
circle_proportion = Circle.new(:clam => clam, :radius => radius).proportion_inside_plot
|
46
|
-
reciprocal_proportion = 1 / circle_proportion
|
58
|
+
reciprocal_proportion = 1.0 / circle_proportion
|
47
59
|
|
48
|
-
clams_inside_circle.
|
49
|
-
( reciprocal_proportion * 1.0 ) / ( number_of_clams ** 2 )
|
50
|
-
end
|
60
|
+
reciprocal_proportion * clams_inside_circle.size
|
51
61
|
end
|
52
62
|
|
53
|
-
k_t = AREA_OF_PLOT * sums
|
63
|
+
k_t = AREA_OF_PLOT * sums / (number_of_clams ** 2)
|
54
64
|
l_t = radius - Math.sqrt( k_t / Math::PI)
|
55
65
|
|
56
66
|
stats.k_ts << k_t
|
@@ -2,7 +2,7 @@ class PlotStatistics
|
|
2
2
|
class MonteCarlo
|
3
3
|
attr_accessor :plots, :stats
|
4
4
|
|
5
|
-
def initialize(number_of_clams, number_of_plots=100)
|
5
|
+
def initialize(number_of_clams, number_of_plots=100.0)
|
6
6
|
@plots = (1..number_of_plots).map { ClamPlot.create_random number_of_clams }
|
7
7
|
@stats = OpenStruct.new(:mean => OpenStruct.new(:k_ts => [], :l_ts => []),
|
8
8
|
:upper_limit => OpenStruct.new(:k_ts => [], :l_ts => []),
|
@@ -20,8 +20,8 @@ class PlotStatistics
|
|
20
20
|
|
21
21
|
def calculate_limits
|
22
22
|
(0...ClamPlot::MAX_RADIUS).each do |radius|
|
23
|
-
threshold_k = standard_deviation_k(radius) * 2
|
24
|
-
threshold_l = standard_deviation_l(radius) * 2
|
23
|
+
threshold_k = standard_deviation_k(radius) * 2.0
|
24
|
+
threshold_l = standard_deviation_l(radius) * 2.0
|
25
25
|
|
26
26
|
stats.upper_limit.k_ts << (stats.mean.k_ts[radius] + threshold_k)
|
27
27
|
stats.upper_limit.l_ts << (stats.mean.l_ts[radius] + threshold_l)
|
@@ -31,7 +31,7 @@ class PlotStatistics
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def standard_deviation_k(radius)
|
34
|
-
sum = plots.inject(0) do |sum, plot|
|
34
|
+
sum = plots.inject(0.0) do |sum, plot|
|
35
35
|
(plot.stats.k_ts[radius] - stats.mean.k_ts[radius]) ** 2
|
36
36
|
end
|
37
37
|
|
@@ -39,7 +39,7 @@ class PlotStatistics
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def standard_deviation_l(radius)
|
42
|
-
sum = plots.inject(0) do |sum, plot|
|
42
|
+
sum = plots.inject(0.0) do |sum, plot|
|
43
43
|
(plot.stats.l_ts[radius] - stats.mean.l_ts[radius]) ** 2
|
44
44
|
end
|
45
45
|
|
@@ -51,7 +51,7 @@ class PlotStatistics
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def average_k(radius)
|
54
|
-
sum = plots.inject(0) do |sum, plot|
|
54
|
+
sum = plots.inject(0.0) do |sum, plot|
|
55
55
|
plot.stats.k_ts[radius]
|
56
56
|
end
|
57
57
|
|
@@ -59,7 +59,7 @@ class PlotStatistics
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def average_l(radius)
|
62
|
-
sum = plots.inject(0) do |sum, plot|
|
62
|
+
sum = plots.inject(0.0) do |sum, plot|
|
63
63
|
plot.stats.l_ts[radius]
|
64
64
|
end
|
65
65
|
|
@@ -3,7 +3,25 @@ class PlotStatistics
|
|
3
3
|
attr_accessor :clam_plot, :monte_carlo
|
4
4
|
|
5
5
|
def initialize(params={})
|
6
|
+
@clam_plot = params[:clam_plot]
|
7
|
+
@monte_carlo = params[:monte_carlo]
|
8
|
+
end
|
9
|
+
|
10
|
+
def write_to_file(filename)
|
11
|
+
FasterCSV.open(filename, 'w') do |csv|
|
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)']
|
6
13
|
|
14
|
+
(0...ClamPlot::MAX_RADIUS).each do |position|
|
15
|
+
radius = position + 1
|
16
|
+
csv << [
|
17
|
+
radius,
|
18
|
+
clam_plot.stats.k_ts[position], clam_plot.stats.l_ts[position],
|
19
|
+
monte_carlo.stats.mean.k_ts[position], monte_carlo.stats.mean.l_ts[position],
|
20
|
+
monte_carlo.stats.upper_limit.k_ts[position], monte_carlo.stats.lower_limit.k_ts[position],
|
21
|
+
monte_carlo.stats.upper_limit.l_ts[position], monte_carlo.stats.lower_limit.l_ts[position]
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
7
25
|
end
|
8
26
|
end
|
9
27
|
end
|
data/lib/plot_statistics.rb
CHANGED
@@ -5,4 +5,5 @@ class PlotStatistics
|
|
5
5
|
require File.dirname(__FILE__) + '/plot_statistics/clam'
|
6
6
|
require File.dirname(__FILE__) + '/plot_statistics/clam_plot'
|
7
7
|
require File.dirname(__FILE__) + '/plot_statistics/monte_carlo'
|
8
|
+
require File.dirname(__FILE__) + '/plot_statistics/output'
|
8
9
|
end
|
data/plot_statistics.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{plot_statistics}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.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-03-
|
12
|
+
s.date = %q{2010-03-19}
|
13
13
|
s.default_executable = %q{plot_statistics}
|
14
14
|
s.description = %q{This is a gem to do a Ripley's K analysis}
|
15
15
|
s.email = %q{acvwilson@gmail.com}
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: plot_statistics
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
|
-
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Asa Wilson
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-19 00:00:00 -04:00
|
18
18
|
default_executable: plot_statistics
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|