Class: Daru::IO::Importers::Plaintext

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/importers/plaintext.rb

Overview

Plaintext Importer Class, that extends read_plaintext method to Daru::DataFrame

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from, guess_parse

Methods inherited from Base

#optional_gem

Constructor Details

#initializePlaintext

Checks for required gem dependencies of Plaintext Importer



12
# File 'lib/daru/io/importers/plaintext.rb', line 12

def initialize; end

Class Method Details

.read(path) ⇒ Daru::IO::Importers::Plaintext

Reads data from a plaintext (.dat) file

Examples:

Reading from plaintext file

instance = Daru::IO::Importers::Plaintext.read("bank2.dat")

Parameters:

  • path (String)

    Path to plaintext file, where the dataframe is to be imported from.

Returns:



25
26
27
28
29
30
31
32
# File 'lib/daru/io/importers/plaintext.rb', line 25

def read(path)
  @file_data = File.read(path).split("\n").map do |line|
    row = process_row(line.strip.split(/\s+/),[''])
    next if row == ["\x1A"]
    row
  end
  self
end

Instance Method Details

#call(fields) ⇒ Daru::DataFrame

Imports Daru::DataFrame from a Plaintext Importer instance

Examples:

Initializing with fields

df = instance.call([:v1, :v2, :v3, :v4, :v5, :v6])

#=> #<Daru::DataFrame(200x6)>
#       v1    v2    v3    v4    v5    v6
#  0 214.8 131.0 131.1   9.0   9.7 141.0
#  1 214.6 129.7 129.7   8.1   9.5 141.7
#  2 214.8 129.7 129.7   8.7   9.6 142.2
#  3 214.8 129.7 129.6   7.5  10.4 142.0
#  4 215.0 129.6 129.7  10.4   7.7 141.8
#  5 215.7 130.8 130.5   9.0  10.1 141.4
#  6 215.5 129.5 129.7   7.9   9.6 141.6
#  7 214.5 129.6 129.2   7.2  10.7 141.7
#  8 214.9 129.4 129.7   8.2  11.0 141.9
#  9 215.2 130.4 130.3   9.2  10.0 140.7
# 10 215.3 130.4 130.3   7.9  11.7 141.8
# 11 215.1 129.5 129.6   7.7  10.5 142.2
# 12 215.2 130.8 129.6   7.9  10.8 141.4
# 13 214.7 129.7 129.7   7.7  10.9 141.7
# 14 215.1 129.9 129.7   7.7  10.8 141.8
#...   ...   ...   ...   ...   ...   ...

Parameters:

  • fields (Array)

    An array of vectors.

Returns:



61
62
63
# File 'lib/daru/io/importers/plaintext.rb', line 61

def call(fields)
  Daru::DataFrame.rows(@file_data, order: fields)
end

#read(path) ⇒ Daru::IO::Importers::Plaintext

Reads data from a plaintext (.dat) file

Examples:

Reading from plaintext file

instance = Daru::IO::Importers::Plaintext.read("bank2.dat")

Parameters:

  • path (String)

    Path to plaintext file, where the dataframe is to be imported from.

Returns:



25
26
27
28
29
30
31
32
# File 'lib/daru/io/importers/plaintext.rb', line 25

def read(path)
  @file_data = File.read(path).split("\n").map do |line|
    row = process_row(line.strip.split(/\s+/),[''])
    next if row == ["\x1A"]
    row
  end
  self
end