-
Notifications
You must be signed in to change notification settings - Fork 12
/
stat_extract.rb
executable file
·172 lines (132 loc) · 3.88 KB
/
stat_extract.rb
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env ruby
require 'csv'
require 'date'
require 'json'
# A way to pretty print-ish an object
def jp(object)
puts JSON.generate(object)
#puts JSON.pretty_generate(object)
#puts object.inspect
rescue
puts "Unabled to display #{object.class} classes"
end
def parse_csv(filename)
CSV.parse(File.read(filename), headers: true).map do |row|
data = {}
row.to_hash.each do |key, value|
next if key.nil? || value.nil?
new_key = key.downcase.gsub(" ", "_").to_sym
new_key = :license if new_key == :os_license
value = value.upcase if new_key == :researcher
value = Date.strptime(value, '%m/%d/%Y') if [:initial_release, :last_active_date].include?(new_key)
data[new_key] = value
end
data
end
end
def breakdown_by_language(data)
languages = Hash.new(0)
data.each do |tool|
clean_lang = tool[:language] || "Missing"
clean_lang.split(",").each do |lang|
languages[lang.strip] += 1
end
end
languages
end
def breakdown_by_svc(data)
svc_hash = Hash.new(0)
data.each do |tool|
svc = tool[:version_control] || "Missing"
svc_hash[svc] += 1
puts svc
end
svc_hash
end
def breakdown_by_license(data)
licenses = Hash.new(0)
data.each do |tool|
licenses[tool[:license] || "Missing"] += 1
end
licenses
end
def breakdown_by_start_year(data)
years = Hash.new(0)
data.each do |tool|
next unless tool[:initial_release]
years[tool[:initial_release].year] += 1
end
years
end
def language_by_year(data)
years = {}
data.each do |tool|
next unless tool[:initial_release] && tool[:language]
clean_lang = tool[:language] || "Missing"
clean_lang.split(",").each do |lang|
years[tool[:initial_release].year] ||= Hash.new(0)
years[tool[:initial_release].year][lang.strip] += 1
end
end
years
end
def license_by_year(data)
years = {}
data.each do |tool|
next unless tool[:initial_release] && tool[:license]
years[tool[:initial_release].year] ||= Hash.new(0)
years[tool[:initial_release].year][tool[:license]] += 1
end
years
end
def length_of_project(data)
length = []
data.each do |tool|
next unless tool[:initial_release] && tool[:last_active_date]
length << (tool[:last_active_date] - tool[:initial_release]).to_i
end
length.sort
end
data = parse_csv('data/tool_list_research.csv')
length = length_of_project(data)
median = (length[(length.size - 1) / 2] + length[length.size / 2]) / 2
#jp breakdown_by_license(data)
#jp breakdown_by_language(data)
#jp breakdown_by_start_year(data)
#jp language_by_year(data)
#jp license_by_year(data)
#jp breakdown_by_svc(data)
#puts "Shortest Project Length (days): #{length.min}"
#puts "Average Project Length (days): #{length.inject(&:+) / length.size}"
#puts "Median Project Length (days): #{median}"
#puts "Longest Project Length (days): #{length.max}"
def csv_of_language_by_year(data)
result = language_by_year(data)
langs = result.map { |yr, langs| langs.map { |k, v| k }}.flatten.uniq
rows = [['', langs].flatten] + result.map { |yr, ln| [yr, langs.map { |l| ln[l] || 0 }].flatten }
rows.map { |r| r.join(",") }.join("\n")
end
def csv_of_license_by_year(data)
result = license_by_year(data)
langs = result.map { |yr, langs| langs.map { |k, v| k }}.flatten.uniq
rows = [['', langs].flatten] + result.map { |yr, ln| [yr, langs.map { |l| ln[l] || 0 }].flatten }
rows.map { |r| r.join(",") }.join("\n")
end
def csv_of_languages(data)
res = breakdown_by_language(data)
res.map{|l,c| [l,c].join(",")}.join("\n")
end
def csv_of_licenses(data)
res = breakdown_by_license(data)
res.map{|l,c| [l,c].join(",")}.join("\n")
end
def csv_of_svc(data)
res = breakdown_by_svc(data)
res.map{|l,c| [l,c].join(",")}.join("\n")
end
#puts csv_of_license_by_year(data)
#puts csv_of_language_by_year(data)
#puts csv_of_languages(data)
#puts csv_of_licenses(data)
#puts csv_of_svc(data)
puts data.inspect