Save results to file in particular data frame? - ruby-on-rails

I have some problems when I am trying to save the output to a text file:
def self.visual_model(object_or_ticker)
predicted_values = DataVisual::test_model(object_or_ticker, opts={})
myStr = predicted_values
aFile = File.new("mydata.txt2", "w")
aFile.write(myStr)
aFile.close
return predicted_values
end
predicted_values is an array, like this:
{:revenues=>[1, 2, 3, 4, 5], :cost=>[-8, -9, -8, 7, 3], :gross_profit=>[27, 26, 25, 25, 23]}
I want to save the text file as the following frame:
revenues 1, 2, 3, 4, 5
cost -8, -9, -8, 7, 3
gross_profit 27, 26, 25, 25, 23
Or like this:
revenues cost gross_profit
1 -8 27
2 -9 26
3 -8 25
4 7 25
5 3 23

output_string = ""
predicted_values.each do |key, value|
output_string += "#{key.to_s.ljust(15," ")}#{value.join(", ")}\n"
end
File.open("predicted_values.txt", 'w') { |file| file.write(output_string) }

Related

Kaminari not respecting .per() setting

#items = #cats + #dogs + #birds
#pag_items = Kaminari.paginate_array(#items).page(params[:page]).per(9)
puts #pag_items.count
6
The #items array has 23 items.
Why is the #pag_items array only holding 6 items?
If i set it to 5, or 4 it will hold that many. But more than 6 it won't.
Thanks!
You probably has max_per_page setting enabled:
Kaminari.configure { |s| s.max_per_page = 6 }
#items = (1..23).to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
#pag_items = Kaminari.paginate_array(#items).page(1).per(9)
# => [1, 2, 3, 4, 5, 6]

How to read nested table

I am new to lua and want to learn about nested table
This is what I have been trying:
t = {};
t[1] = 22, {1, 22, 3};
t[2] = 44, {4, 5, 6};
t[3] = 66, {4, 5, 6};
for k, v in ipairs(t) do
print(k,v)
end
This does not work for me.
This is the result I want:
Example:
1 2 5
2 5 66
3 6 33
4 2 1
5 4 12
6 4 3
7 2 44
8 3 1
9 2 44
10 3 99
How do I read a nested table like this and
how do I table.insert in the right column?
I believe the misunderstanding lies in this code:
t[1] = 22, {1, 22, 3}
The 2nd value, the one after the comma, {1, 22, 3} is being assigned to nothing, it is dropped in the ether.
Perhaps what you meant was a table of tables:
t[1] = {22, {1, 22, 3}}
This would create an entry at [1] in t that is itself..a table
t[1][1] == 22
t[1][2] == {1, 22, 3}
Then to iterate this setup you could use:
local t = {}
t[1] = {22, {1, 22, 3}}
t[2] = {44, {4, 5, 6}}
t[3] = {66, {4, 5, 6}}
for _,entry in ipairs(t) do
local key = entry[1]
io.write(key .. ' ')
for _,value in ipairs(entry[2]) do
io.write(value .. ' ')
end
io.write('\n')
end
But to be honest, I'm not sure what you're asking as your "expected output" is significantly different than your sample data set.
Replying to Qualmos' comment in payo's answer:
local t = {}
t[1] = {22,3}
t[2] = {44,6}
t[3] = {66,63}
for _,v in pairs(table) do
print(_,v[1],v[2])
end
Would print something like this:
1,22,3
2,44,6
3,66,63
Btw, you can make the table look like this:
local t = {
{22,3};
{44,6};
{66,63};
}

ruby array (enumerable) method to select and reject into 2 arrays in 1 operation

# this code works
list = (0..20).to_a
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
odd = list.select { |x| x.odd? }
# => [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
list.reject! { |x| x.odd? }
# => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# but can i emulate this type of functionality with an enumerable method?
set = [1,5,10]
# => [1, 5, 10]
one, five, ten = set
# => [1, 5, 10]
one
# => 1
five
# => 5
ten
# => 10
# ------------------------------------------------
# method I am looking for ?
list = (0..20).to_a
odd, even = list.select_with_reject { |x| x.odd? }
# put the matching items into the first variable
# and the non-matching into the second
Sure, you can do:
odd, even = list.partition &:odd?
odd = []
even = []
list = [1..20]
list.each {|x| x.odd? ? odd << x : even << x }
As pguardiario said, the partition method is the most direct way. You could also use Set#divide:
require 'set'
list = (1..10).to_a
odd, even = Set.new(list).divide(&:odd?).to_a.map{|x| x.to_a}
You could try below:
odd,even = (0..20).group_by(&:odd?).values
p odd,even
Output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Rails array with weeks and year

I want to list all weeknumbers together with year.
This is what I have:
start # 2012-05-10
ende # 2013-06-20
while start < ende
weeks << start.cweek
start += 1.week
end
List all weeknumbers:
#kws.each do |w|
w
end
I need some inspiration how to assign the corresponding year to each weeknumber..
So that I get 22 / 2012 23 / 2012 etc..
Thanks for help..
When in your while loop, you can also store the year, and one easy way is just as an array of arrays.
Then in your each loop later you can get access to both:
start = Date.new( 2012, 5, 10 )
ende = Date.new( 2013, 6, 20 )
weeks = []
while start < ende
weeks << [start.cweek, start.year] # <-- enhanced
start += 1.week
end
weeks.each do |w,y| # <-- take two arguments in the block
puts "#{w} / #{y}" # and print them both out
end
Results:
=>
19 / 2012
20 / 2012
21 / 2012
22 / 2012
23 / 2012
24 / 2012
25 / 2012
...
22 / 2013
23 / 2013
24 / 2013
Create a hash instead with key as a year and value as an array of week numbers
start # 2012-05-10
ende # 2013-06-20
weeks ={}
while start < ende
weeks[start.year] = [] unless weeks[start.year]
weeks[start.year] << start.cweek
start += 1.week
end
p weeks
and you get o/p
=> {2012=>[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],
2013=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24]}
(10.weeks.ago.to_date..Date.today.to_date).map(&:beginning_of_week).uniq

How to Line Chart this data?

I would like to create line chart for following data. I would also like to have the ability to hover over each data point to look at the x and y value at that data point. I have the following data structure:
x[0] = [23 4 2 2 4 4 5 3 334 2]
y[0] = [6 24 1 2 2 5 1 3 8 0]
x[1] = [5 6 8 6 3 4 6 3 3]
y[1] = [9 7 8 6 3 4 1 9 2]
x[2] = [6 9 9 6 2 5 8 3]
y[2] = [1 0 2 5 6 2 1 5]
... so that I will have 3 lines on the same chart.
I played with "Seer" without much success. Can anyone provide any recommendations / examples / references for plotting similar data using Seer or anything else?
Thanks.
Give the lazy_high_charts gem a try.
#app/views/layouts/appliction.*
= javascript_include_tag 'highcharts.js'
#Gemfile
gem 'lazy_high_charts'
# my_controller#my_action
x_0 = [23, 4, 2, 2, 4, 4, 5, 3, 334, 2]
y_0= [6, 24, 1, 2, 2, 5, 1, 3, 8, 0]
x_1 = [5, 6, 8, 6, 3, 4, 6, 3, 3]
y_1 = [9, 7, 8, 6, 3, 4, 1, 9, 2]
x_2 = [6, 9, 9, 6, 2, 5, 8, 3]
y_2 = [1, 0, 2, 5, 6, 2, 1, 5]
data_0 = x_0.zip(y_0)
data_1 = x_1.zip(y_1)
data_2 = x_2.zip(y_2)
#h = LazyHighCharts::HighChart.new('graph') do |f|
f.series(:name => "xy0", :data => data_0)
f.series(:name => "xy1", :data => data_1)
f.series(:name => "xy3", :data => data_2)
f.chart({:defaultSeriesType=>"line" })
f.yAxis(:title => { :text => "y axis values" } )
f.xAxis(:title => { :text => "x axis values"} )
f.title(:text => "XY Graph")
f.plotOptions({}) # override the default values that lazy_high_charts puts there
f.legend({}) # override the default values
end
#app/views/my_controller/my_action
= high_chart("chart", #h)
Caveat:
HighCharts is only free for non-commercial use. That may or may not be a dealbreaker for you.
I've really liked jQuery flot for this kind of thing:
http://code.google.com/p/flot/
Check out the example here:
http://flot.googlecode.com/svn/trunk/README.txt
In your controller or view, you can use Ruby's zip to zip together arrays of x and y values if you need to:
> a = [1,2,3]
=> [1, 2, 3]
> b = [5,6,7]
=> [5, 6, 7]
> a.zip(b)
=> [[1, 5], [2, 6], [3, 7]]

Resources