-
-
Save Dysp/34dbaa317a90c5636ee068c1f049238b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubyXL' | |
# This is a class. It does a lot of things on which I will elaborate later. | |
class Kincom | |
def initialize(file, name: 'N/A', | |
arm: nil, | |
stop_index: 0.5, | |
prestress: 0.025, | |
begin_percentage: 0.025, | |
time_values: [0, 30, 50, 100, 200]) | |
@filename = find_filename(file) | |
@subject = name | |
@arm = arm | |
@stop_index = stop_index | |
@prestress = prestress | |
@begin_percentage = begin_percentage | |
@time_values = time_values | |
xlsx_sheets = load(file) | |
data = extract_values(xlsx_sheets) | |
calculate(data) | |
end | |
def load(file) | |
book = RubyXL::Parser.parse(file) | |
book.worksheets | |
end | |
def extract_values(sheets) | |
array = [] | |
sheets.each do |sheet| | |
array << sheet.map { |row| row[1].value.to_f } | |
end | |
array | |
end | |
def find_filename(file) | |
File.basename(file) | |
end | |
def calculate(data) | |
right_array = Array[] | |
left_array = Array[] | |
data.each do |a| | |
if positive?(a) == true | |
right_kick = true | |
normalized_data = normalize(a, right_kick) | |
elsif positive?(a) == false | |
right_kick = false | |
normalized_data = normalize(a, right_kick) | |
else | |
puts 'Fejl.' # Raise exception... | |
end | |
analyzed_data = rfd_calculation(normalized_data) | |
right_array << analyzed_data if right_kick == true | |
left_array << analyzed_data if right_kick == false | |
end | |
@right = right_array.each.with_index(1).map { |kick_hash, index| ["r#{index}", kick_hash] }.to_h | |
@left = left_array.each.with_index(1).map { |kick_hash, index| ["l#{index}", kick_hash] }.to_h | |
end | |
def baseline(data) | |
baseline = Array.new(data.first(100)) | |
# Should make a check to make sure no value changes more than 2,5 %, | |
# which would mean no baseline could be found | |
baseline.inject(:+).quo(baseline.size) | |
end | |
def mvc(data, baseline) | |
mvc = data.max | |
mvc - baseline | |
end | |
def rfd_calculation(data) | |
baseline = baseline(data) | |
mvc = mvc(data, baseline) | |
mvc_newton = (mvc * 1000) / 4.88 | |
mvc_nm = mvc_newton * @arm | |
# prestress = prestress?(data) | |
ms = rfd(data, baseline, mvc) | |
ms_dif = find_rfd_dif(ms) | |
ms_dif_n = find_dif_n(ms_dif) | |
ms_dif_nm = find_dif_nm(ms_dif_n) | |
ms_dif_nms = find_dif_nms(ms_dif_nm) | |
ms_dif_nmpc = find_dif_nmpc(ms_dif_nm, mvc_nm) | |
return { mvc: mvc, mvc_newton: mvc_newton, mvc_nm: mvc_nm, | |
baseline: baseline, ms: ms, ms_dif: ms_dif, | |
ms_dif_n: ms_dif_n, ms_dif_nm: ms_dif_nm, | |
ms_dif_nms: ms_dif_nms, ms_dif_nmpc: ms_dif_nmpc } | |
end | |
def find_dif_nmpc(ms_dif_nm, mvc_nm) | |
ms_dif_nm.map { |ms| (ms / mvc_nm) * 100 } | |
end | |
def find_dif_nms(ms_dif_nm) | |
ms_dif_nm.map.with_index(1) { |a, i| a / @time_values[i] } | |
end | |
def find_dif_nm(ms_dif_n) | |
ms_dif_n.map { |ms| ms * @arm } | |
end | |
def find_dif_n(ms_dif) | |
ms_dif.map { |ms| (ms * 1000) / 4.88 } | |
end | |
def find_rfd_dif(ms) | |
array = [] | |
null_value = ms.first | |
ms[1..-1].each { |m| array << m - null_value } | |
return array | |
end | |
def rfd(data, baseline, mvc) | |
percentage_value = baseline + (mvc * @begin_percentage) | |
stop = data.length * @stop_index | |
data.reject!.each_with_index { |v, i| break if i >= stop; v < percentage_value } | |
array = [] | |
@time_values.each do |t| | |
array << data.fetch(t, nil) | |
end | |
end | |
def prestress?(data) | |
# Should check for prestress | |
end | |
def positive?(array) | |
if array.inject(:+).quo(array.size) > 0 | |
return true | |
else | |
return false | |
end | |
end | |
def normalize(array, right_kick) | |
array.reverse! | |
volt = array.drop(array.length / 3) | |
volt.reverse! | |
if right_kick == true | |
volt.map! { |x| x + array.min.abs } | |
elsif right_kick == false | |
volt.map! { |x| (x - array.max.abs) * -1 } | |
else | |
puts 'lol' | |
end | |
end | |
end | |
puts 'testing...' | |
test = Kincom.new('test.xlsx', name: 'Okay', arm: 25) | |
p test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment