Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

open svg on OSX #12

Merged
merged 3 commits into from

2 participants

@vpereira

just a quick fix to make it work.

@vpereira

ok, i improved the read method. It's kind of style, but I think it looks better now.

@vpereira

i tried to mimic R and improve the ruby style (it's relative, i know).

@clbustos clbustos merged commit 7a79b66 into clbustos:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 5, 2013
  1. @vpereira

    open svg on mac osx

    vpereira authored
Commits on Jan 6, 2013
  1. @vpereira

    added Bundle depds

    vpereira authored
  2. @vpereira

    improved the csv read method

    vpereira authored
This page is out of date. Refresh to see the latest.
View
8 Gemfile
@@ -0,0 +1,8 @@
+source :rubygems
+
+gem 'minitest'
+gem 'rdoc'
+gem 'mocha', '0.10.5' #:require=>'mocha/setup'
+gem 'shoulda','2.11.3'
+gem 'shoulda-matchers','1.1.0'
+gem 'hoe'
View
32 Gemfile.lock
@@ -0,0 +1,32 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.2.10)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ hoe (3.4.0)
+ rake (>= 0.8, < 11.0)
+ i18n (0.6.1)
+ json (1.7.6)
+ metaclass (0.0.1)
+ minitest (4.3.3)
+ mocha (0.10.5)
+ metaclass (~> 0.0.1)
+ multi_json (1.5.0)
+ rake (10.0.3)
+ rdoc (3.12)
+ json (~> 1.4)
+ shoulda (2.11.3)
+ shoulda-matchers (1.1.0)
+ activesupport (>= 3.0.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ hoe
+ minitest
+ mocha (= 0.10.5)
+ rdoc
+ shoulda (= 2.11.3)
+ shoulda-matchers (= 1.1.0)
View
6 lib/statsample/analysis/suite.rb
@@ -62,7 +62,11 @@ def show_svg(svg)
require 'tmpdir'
fn=Dir.tmpdir+"/image_#{Time.now.to_f}.svg"
File.open(fn,"w") {|fp| fp.write svg}
- `xdg-open '#{fn}'`
+ if RUBY_PLATFORM =~/darwin/
+ %x(open -a safari #{fn})
+ else
+ %x(xdg-open #{fn})
+ end
end
def boxplot(*args)
show_svg(old_boxplot(*args).to_svg)
View
19 lib/statsample/converter/csv.rb
@@ -8,6 +8,25 @@ class CSV < SpreadsheetBase
CSV_klass=::CSV
end
class << self
+
+ def read19(filename,ignore_lines=0,csv_opts=Hash.new)
+ #default first line is header
+ csv_opts.merge!(:headers=>true, :header_converters => :symbol)
+ csv = CSV_klass::Table.new(CSV_klass::read(filename,'r',csv_opts))
+ csv_headers = if csv_opts[:headers]
+ csv.headers
+ else
+ #as in R, if no header we name the headers as V1,V2,V3,V4,..
+ 1.upto(csv.first.length).collect { |i| "V#{i}" }
+ end
+ #we invert row -> column. It means csv[0] is the first column and not row. Similar to R
+ csv.by_col!
+ thash = {}
+ csv_headers.each_with_index do |header,idx|
+ thash[header] = Statsample::Vector.new(csv[idx].drop(ignore_lines))
+ end
+ ds=Statsample::Dataset.new(thash)
+ end
# Returns a Dataset based on a csv file
#
# USE:
View
40 test/test_csv.rb
@@ -38,3 +38,43 @@ def test_write
}
end
end
+
+class StatsampleCSVTestCase2 < MiniTest::Unit::TestCase
+ def setup
+ @ds=Statsample::CSV.read19(File.dirname(__FILE__)+"/fixtures/test_csv.csv")
+ end
+ def test_read
+ assert_equal(6,@ds.cases)
+ assert_equal(%w{id name age city a1}, @ds.fields)
+ id=[1,2,3,4,5,6].to_vector(:scale)
+ name=["Alex","Claude","Peter","Franz","George","Fernand"].to_vector(:nominal)
+ age=[20,23,25,27,5.5,nil].to_vector(:scale)
+ city=["New York","London","London","Paris","Tome",nil].to_vector(:nominal)
+ a1=["a,b","b,c","a",nil,"a,b,c",nil].to_vector(:nominal)
+ ds_exp=Statsample::Dataset.new({'id'=>id,'name'=>name,'age'=>age,'city'=>city,'a1'=>a1}, %w{id name age city a1})
+ ds_exp.fields.each{|f|
+ assert_equal(ds_exp[f],@ds[f])
+ }
+ assert_equal(ds_exp,@ds)
+ end
+ def test_nil
+ assert_equal(nil,@ds['age'][5])
+ end
+ def test_repeated
+ ds=Statsample::CSV.read19(File.dirname(__FILE__)+"/fixtures/repeated_fields.csv")
+ assert_equal(%w{id name_1 age_1 city a1 name_2 age_2},ds.fields)
+ age=[3,4,5,6,nil,8].to_vector(:scale)
+ assert_equal(age,ds['age_2'])
+ end
+ def test_write
+ filename=Tempfile.new("afile")
+ # filename=Dir::tmpdir+"/test_write.csv"
+ Statsample::CSV.write(@ds, filename.path)
+ ds2=Statsample::CSV.read19(filename.path)
+ i=0
+ ds2.each_array{|row|
+ assert_equal(@ds.case_as_array(i),row)
+ i+=1
+ }
+ end
+end
Something went wrong with that request. Please try again.