Skip to content

Commit

Permalink
Vectors implemented and README update
Browse files Browse the repository at this point in the history
Implemented basic vector
Created files

Wrote tests for vector initialize

Implemented basic vector

enumerables

Vectors done

readme update

readme update
  • Loading branch information
v0dro committed Oct 6, 2014
1 parent 9a0c1de commit b1c0866
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in daru.gemspec
gemspec
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
PATH
remote: .
specs:
daru (0.0.1)

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
json (1.8.1)
nmatrix (0.1.0.rc5)
rdoc (~> 4.0, >= 4.0.1)
rdoc (4.1.2)
json (~> 1.4)
rspec (3.1.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-core (3.1.5)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.2)
rspec-support (~> 3.1.0)
rspec-support (3.1.1)

PLATFORMS
ruby

DEPENDENCIES
bundler
daru!
nmatrix (~> 0.1.0.rc5)
rspec (~> 3.0)
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@ daru
====

Data Analysis in RUby

## Introduction

daru (Data Analysis in RUby) is a library for storage, analysis and manipulation of data. It aims to be the preferred data analysis library for Ruby.

Development of daru was started to address the fragmentation of Dataframe-like classes which were created in many ruby gems as per their own needs.

This creates a hurdle in using these gems together to solve a problem. For example, calculating something in [statsample](https://github.com/clbustos/statsample) and plotting the results in [Nyaplot](https://github.com/domitry/nyaplot).

daru is heavily inspired by `Statsample::Dataset`, `Nyaplot::DataFrame` and the super-awesome pandas, a very mature solution in Python.

## Data Structures

daru employs several data structures for storing and manipulating data:
* Vector - A basic 1-D vector.
* DataFrame - A 2-D matrix-like structure which is internally composed of named `Vector` classes.

daru data structures can be constructed by using several Ruby classes. These include `Array`, `Hash`, `Matrix`, [NMatrix](https://github.com/SciRuby/nmatrix) and [MDArray](https://github.com/rbotafogo/mdarray). daru brings a uniform API for handling and manipulating data represented in any of the above Ruby classes.
29 changes: 29 additions & 0 deletions daru.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf-8
$:.unshift File.expand_path("../lib", __FILE__)

This comment has been minimized.

Copy link
@abinashmeher999

abinashmeher999 Jun 19, 2015

@v0dro What does this line do?

This comment has been minimized.


require 'version.rb'

DESCRIPTION = <<MSG
Daru (Data Analysis in RUby) is a library for storage, analysis and manipulation
of data.
MSG

Gem::Specification.new do |spec|
spec.name = 'daru'
spec.version = Daru::VERSION
spec.authors = ['Sameer Deshmukh']
spec.email = ['sameer.deshmukh93@gmail.com']
spec.summary = %q{Data Analysis in RUby}
spec.description = DESCRIPTION
spec.homepage = "http://github.com/v0dro/daru"
spec.license = 'BSD-2'

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'nmatrix', '~> 0.1.0.rc5'
end
5 changes: 5 additions & 0 deletions lib/daru.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'securerandom'
require 'matrix'

require 'daru/vector.rb'
require 'daru/dataframe.rb'
7 changes: 7 additions & 0 deletions lib/daru/dataframe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Daru
class DataFrame
def initialize source, name=SecureRandom.uuid

end
end
end
56 changes: 56 additions & 0 deletions lib/daru/vector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Daru
class Vector
include Enumerable

def each(&block)
@vector.each(&block)
end

attr_reader :name

attr_reader :size

def initialize source, name=nil

if source.is_a?(Hash)
initialize source.values[0], source.keys[0]
else
@name = name || SecureRandom.uuid

@vector =
case source
when Range, Matrix
source.to_a.flatten
else
source
end

@size = @vector.size
end
end

def [](index)
@vector[index]
end

def to_json
@vector.to_a.to_json
end

def to_a
@vector.to_a
end

def to_html threshold=15
html = '<table><tr><th>' + @name.to_s + '</th></tr>>'

@vector.to_a.each_with_index do |el,i|
next if threshold < i and i < @arr.length-1
content = i == threshold ? '...' : el.to_s
html.concat('<tr><td>' + content + '</td></tr>')
end

html += '</table>'
end
end
end
3 changes: 3 additions & 0 deletions lib/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Daru
VERSION = "0.0.1"
end
1 change: 1 addition & 0 deletions spec/jruby/dataframe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Tests if interpreter is JRuby
19 changes: 19 additions & 0 deletions spec/jruby/vector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Tests if interpreter is JRuby

# describe Daru::Vector do
# context ".initialize" do
# it "creates a vector object with an MDArray" do
# vector = Daru::Vector.new(MDArray.new([5], [1,2,3,4,5]), :uhura)

# expect(vector[1]) .to eq(2)
# expect(vector.name).to eq(:uhura)
# end

# it "creates a vector object with a Hash with different values" do
# vector = Daru::Vector.new { sulu: MDArray.new([5], [1,2,3,4,5])}

# expect(vector[1]) .to eq(2)
# expect(vector.name).to eq(:sulu)
# end
# end
# end
5 changes: 5 additions & 0 deletions spec/mri/dataframe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper.rb'

describe Daru::DataFrame do

end
89 changes: 89 additions & 0 deletions spec/mri/vector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'spec_helper.rb'

describe Daru::Vector do
context "#initialize" do
it "creates a vector object with an Array" do
vector = Daru::Vector.new [1,2,3,4,5], :mowgli

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:mowgli)
end

it "creates a vector object with a Range" do
vector = Daru::Vector.new 1..5, :bakasur

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:bakasur)
end

it "creates a vector object with an NMatrix" do
vector = Daru::Vector.new(NMatrix.new([5], [1,2,3,4,5],
dtype: :int32), :scotty)

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:scotty)
end

it "creates a vector object with a Matrix" do
vector = Daru::Vector.new Matrix[[1,2,3,4,5]], :ravan

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:ravan)
end

it "creates a vector object with a Hash with different values" do
vector = Daru::Vector.new({orion: [1,2,3,4,5]})

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:orion)

vector = Daru::Vector.new({ kirk: 1..5 })

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:kirk)

vector = Daru::Vector.new({ spock: NMatrix.new([5], [1,2,3,4,5],
dtype: :int32) })

expect(vector[1]) .to eq(2)
expect(vector.name).to eq(:spock)
end

it "auto assigns a name if not specified" do
earth = Daru::Vector.new 1..5
organion = Daru::Vector.new 1..5

expect(earth.name == organion.name).to eq(false)
end
end

context "tests for methods" do
before do
@anakin = Daru::Vector.new NMatrix.new([5], [1,2,3,4,5]), :anakin
@luke = Daru::Vector.new NMatrix.new([3], [3,4,5,6]) , :luke
end

it "checks for an each block" do
sum = 0

@anakin.each{ |e| sum += e}
expect(sum).to eq(15)
end

it "checks for inequality of vectors" do
expect(@anakin == @luke).to be(false)
end

it "calculates maximum value" do
expect(@anakin.max).to eq(5)
end

it "calculates minimmum value" do
expect(@anakin.min).to eq(1)
end

it "delegates to the internal array storage" do
expect(@anakin.size).to eq(@anakin.to_a.size)
end
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rspec'
require 'nmatrix'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'daru'

0 comments on commit b1c0866

Please sign in to comment.