Try to improve transpose

This commit is contained in:
kojix2 2020-07-31 18:54:40 +09:00
parent 5557c4c1d0
commit 9d6337df1c

View File

@ -19,7 +19,7 @@ module Uplot
opt.on('-o', '--output', TrueClass) { |v| @output = v } opt.on('-o', '--output', TrueClass) { |v| @output = v }
opt.on('-d', '--delimiter VAL', String) { |v| @delimiter = v } opt.on('-d', '--delimiter VAL', String) { |v| @delimiter = v }
opt.on('-H', '--headers', TrueClass) { |v| @headers = v } opt.on('-H', '--headers', TrueClass) { |v| @headers = v }
opt.on('-T', '--transpose', TrueClass) { |v| @transpose = v} opt.on('-T', '--transpose', TrueClass) { |v| @transpose = v }
opt.on('-t', '--title VAL', String) { |v| @params[:title] = v } opt.on('-t', '--title VAL', String) { |v| @params[:title] = v }
opt.on('-w', '--width VAL', Numeric) { |v| @params[:width] = v } opt.on('-w', '--width VAL', Numeric) { |v| @params[:width] = v }
opt.on('-h', '--height VAL', Numeric) { |v| @params[:height] = v } opt.on('-h', '--height VAL', Numeric) { |v| @params[:height] = v }
@ -93,16 +93,25 @@ module Uplot
end end
end end
# Note: How can I transpose different sized ruby arrays?
# https://stackoverflow.com/questions/26016632/how-can-i-transpose-different-sized-ruby-arrays
def transpose2(arr) # Should be renamed
arr[0].zip(*arr[1..-1])
end
def preprocess(input) def preprocess(input)
data = CSV.parse(input, col_sep: @delimiter) data = CSV.parse(input, col_sep: @delimiter)
headers = nil
if @transpose
if @headers if @headers
headers = data.shift headers = []
data = data.transpose unless @transpose data.each { |series| headers << series.shift } # each but destructive like map
[data, headers]
else
data = data.transpose unless @transpose
[data, nil]
end end
else
headers = data.shift if @headers
data = transpose2(data)
end
[data, headers]
end end
def barplot(data, headers) def barplot(data, headers)