kaplan_meier 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/kaplan_meier.gemspec +36 -0
- data/lib/kaplan_meier/survival.rb +110 -0
- data/lib/kaplan_meier/version.rb +3 -0
- data/lib/kaplan_meier.rb +6 -0
- metadata +102 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 22b752531bfcfd551c6faa4484fff906aa938dd4
|
|
4
|
+
data.tar.gz: 74360ad770530dd57933d8ad5db8b685d3561f25
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 41a6c1663a0467a96fc763e82ecd0bcd47f78a57112861acaff58b472a6f9ee50477e7b09cd9b9e10ef85976e10585d152b319e8a42933309663f3739b25805f
|
|
7
|
+
data.tar.gz: ed0202e12f3a97e6cf7f585025cd8ad20b9c10b7b34b861a009599ef7642b72ace5add4b4e2b650e7a4f89ea5c9ed0d4b3198f18dad641b009cc3b73d77b986c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
kaplan_meier (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.3)
|
|
10
|
+
rake (10.5.0)
|
|
11
|
+
rspec (3.7.0)
|
|
12
|
+
rspec-core (~> 3.7.0)
|
|
13
|
+
rspec-expectations (~> 3.7.0)
|
|
14
|
+
rspec-mocks (~> 3.7.0)
|
|
15
|
+
rspec-core (3.7.1)
|
|
16
|
+
rspec-support (~> 3.7.0)
|
|
17
|
+
rspec-expectations (3.7.0)
|
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
+
rspec-support (~> 3.7.0)
|
|
20
|
+
rspec-mocks (3.7.0)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.7.0)
|
|
23
|
+
rspec-support (3.7.1)
|
|
24
|
+
|
|
25
|
+
PLATFORMS
|
|
26
|
+
ruby
|
|
27
|
+
|
|
28
|
+
DEPENDENCIES
|
|
29
|
+
bundler (~> 1.16)
|
|
30
|
+
kaplan_meier!
|
|
31
|
+
rake (~> 10.0)
|
|
32
|
+
rspec (~> 3.0)
|
|
33
|
+
|
|
34
|
+
BUNDLED WITH
|
|
35
|
+
1.16.0
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 seigel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# KaplanMeier
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kaplan_meier`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
This current intent of this library is to support doing a Kaplan-Meier Survival Analysis and generating survival plots for subjects that have a brain tumour diagnosis. This is in support of research being conducted to help create better treatments and improve outcomes.
|
|
6
|
+
|
|
7
|
+
This does not mean it is only useful for that, but it is the initial driver of the interface for better or for worse.
|
|
8
|
+
|
|
9
|
+
Please dive in and suggest changes through issues and pull requests. The latter getting more attention.
|
|
10
|
+
|
|
11
|
+
I will be using a couple of sites to validate the approach. The current two sources I am using as truth are:
|
|
12
|
+
|
|
13
|
+
* [Example from graphpad](https://s3.amazonaws.com/cdn.graphpad.com/faq/1757/file/P4-Survival%20analysis.pdf)
|
|
14
|
+
* [Survival Analysis From Stats Direct](https://statsdirect.com/help/survival_analysis/kaplan.htm)
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Add this line to your application's Gemfile:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem 'kaplan_meier'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
And then execute:
|
|
25
|
+
|
|
26
|
+
$ bundle
|
|
27
|
+
|
|
28
|
+
Or install it yourself as:
|
|
29
|
+
|
|
30
|
+
$ gem install kaplan_meier
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
TODO: Improve these but taking the example data and results from graphpad, here is the current way it would be used.
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
# At some point I will try and generate decent docs. If you look at the code
|
|
38
|
+
# it is using named parameters.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
km = KaplanMeier.new
|
|
42
|
+
km.add(46, 1, 1)
|
|
43
|
+
km.add(64, 0, 1)
|
|
44
|
+
km.add(78)
|
|
45
|
+
km.add(124)
|
|
46
|
+
km.add(130, 0, 1)
|
|
47
|
+
km.add(150, 0, 2)
|
|
48
|
+
|
|
49
|
+
km.raw_probabilities
|
|
50
|
+
km.raw_probabilities(as_percent: true)
|
|
51
|
+
km.probabilities
|
|
52
|
+
km.probabilities(as_percent: true)
|
|
53
|
+
|
|
54
|
+
km.range
|
|
55
|
+
km.values_for_range
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Development
|
|
59
|
+
|
|
60
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
61
|
+
|
|
62
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
63
|
+
|
|
64
|
+
## Contributing
|
|
65
|
+
|
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/seigel/kaplan_meier.
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "kaplan_meier"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "kaplan_meier/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "kaplan_meier"
|
|
8
|
+
spec.version = KaplanMeier::VERSION
|
|
9
|
+
spec.authors = ["seigel"]
|
|
10
|
+
spec.email = ["kaplan_meier@paddlethis.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Library to perform the Kaplan-Meier Survival Analysis.}
|
|
13
|
+
spec.description = %q{Evolving library to support analysing data for subjects with brain tumours, and driving graphical representations.}
|
|
14
|
+
spec.homepage = "https://github.com/seigel/kaplan_meier."
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
if spec.respond_to?(:metadata)
|
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
21
|
+
else
|
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
23
|
+
"public gem pushes."
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
|
28
|
+
end
|
|
29
|
+
spec.bindir = "exe"
|
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
31
|
+
spec.require_paths = ["lib"]
|
|
32
|
+
|
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
36
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
require "kaplan_meier/version"
|
|
2
|
+
|
|
3
|
+
module KaplanMeier
|
|
4
|
+
class Survival
|
|
5
|
+
|
|
6
|
+
def initialize
|
|
7
|
+
@data = {}
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def add(time, event_count = 1, censored_count = 0)
|
|
11
|
+
leaf = @data[time]
|
|
12
|
+
if leaf.nil?
|
|
13
|
+
@data[time] = KaplanMeierCoord.new(time, event_count, censored_count)
|
|
14
|
+
else
|
|
15
|
+
leaf.add_count(event_count: event_count, censored_count: censored_count)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initial_at_risk_count
|
|
20
|
+
self.to_a.length
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def time_periods
|
|
24
|
+
@data.keys.sort
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_a
|
|
28
|
+
@data.keys.sort.map do |k|
|
|
29
|
+
@data[k].to_a
|
|
30
|
+
end.flatten
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def raw_probabilities(as_percent: false)
|
|
34
|
+
return [] if @data.keys.length < 1
|
|
35
|
+
self.probabilities.map {|i| as_percent ? (i[:probability] * 100.0).round(2) : i[:probability]}
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def probabilities(as_percent: false)
|
|
39
|
+
return [] if @data.keys.length < 1
|
|
40
|
+
result = [{time: 0, probability: 1}]
|
|
41
|
+
time_points = self.to_a
|
|
42
|
+
n = time_points.length
|
|
43
|
+
percent = 1
|
|
44
|
+
(0...n).each do |i|
|
|
45
|
+
percent = (1.0 - (time_points[i][:event].to_f / (n - i))) * percent
|
|
46
|
+
result << {time: time_points[i][:time_point], probability: as_percent ? (percent * 100.0).round(2) : percent}
|
|
47
|
+
end
|
|
48
|
+
result
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def range(start_time: 0, end_time: nil)
|
|
52
|
+
(start_time..(end_time || time_periods[-1])).to_a
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def values_for_range(start_time: 0, end_time: nil, as_percent: false)
|
|
56
|
+
return [] if @data.keys.length < 1
|
|
57
|
+
result = []
|
|
58
|
+
range_array = range(start_time: start_time, end_time: end_time)
|
|
59
|
+
periods = probabilities(as_percent: as_percent)
|
|
60
|
+
current_period = periods.shift
|
|
61
|
+
range_value = range_array.shift
|
|
62
|
+
until range_value.nil?
|
|
63
|
+
if periods[0] && range_value >= periods[0][:time]
|
|
64
|
+
current_period = periods.shift
|
|
65
|
+
next
|
|
66
|
+
end
|
|
67
|
+
result << current_period[:probability]
|
|
68
|
+
range_value = range_array.shift
|
|
69
|
+
end
|
|
70
|
+
result
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class KaplanMeierCoord
|
|
77
|
+
def initialize(time_point, event_count = 1, censored_count = 0)
|
|
78
|
+
@time_point = time_point
|
|
79
|
+
@event_count = event_count
|
|
80
|
+
@censored_count = censored_count
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def add_event_count(event_count)
|
|
84
|
+
@event_count += event_count
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def add_censored_count(censored_count)
|
|
88
|
+
@censored_count += censored_count
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def add_count(event_count: 0, censored_count: 0)
|
|
92
|
+
add_event_count(event_count)
|
|
93
|
+
add_censored_count(censored_count)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def to_s
|
|
97
|
+
"@#{@time_point}: #{@event_count} event(s). #{@censored_count} censored."
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def to_a
|
|
101
|
+
result = []
|
|
102
|
+
@event_count.times do
|
|
103
|
+
result << {time_point: @time_point, event: 1}
|
|
104
|
+
end
|
|
105
|
+
@censored_count.times do
|
|
106
|
+
result << {time_point: @time_point, event: 0}
|
|
107
|
+
end
|
|
108
|
+
result
|
|
109
|
+
end
|
|
110
|
+
end
|
data/lib/kaplan_meier.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kaplan_meier
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- seigel
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-02-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: Evolving library to support analysing data for subjects with brain tumours,
|
|
56
|
+
and driving graphical representations.
|
|
57
|
+
email:
|
|
58
|
+
- kaplan_meier@paddlethis.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- ".gitignore"
|
|
64
|
+
- ".rspec"
|
|
65
|
+
- ".travis.yml"
|
|
66
|
+
- Gemfile
|
|
67
|
+
- Gemfile.lock
|
|
68
|
+
- LICENSE.txt
|
|
69
|
+
- README.md
|
|
70
|
+
- Rakefile
|
|
71
|
+
- bin/console
|
|
72
|
+
- bin/setup
|
|
73
|
+
- kaplan_meier.gemspec
|
|
74
|
+
- lib/kaplan_meier.rb
|
|
75
|
+
- lib/kaplan_meier/survival.rb
|
|
76
|
+
- lib/kaplan_meier/version.rb
|
|
77
|
+
homepage: https://github.com/seigel/kaplan_meier.
|
|
78
|
+
licenses:
|
|
79
|
+
- MIT
|
|
80
|
+
metadata:
|
|
81
|
+
allowed_push_host: https://rubygems.org
|
|
82
|
+
post_install_message:
|
|
83
|
+
rdoc_options: []
|
|
84
|
+
require_paths:
|
|
85
|
+
- lib
|
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
requirements: []
|
|
97
|
+
rubyforge_project:
|
|
98
|
+
rubygems_version: 2.6.13
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: Library to perform the Kaplan-Meier Survival Analysis.
|
|
102
|
+
test_files: []
|