forked from rlowrance/re
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataframe.lua
More file actions
1299 lines (1146 loc) · 40 KB
/
Copy pathDataframe.lua
File metadata and controls
1299 lines (1146 loc) · 40 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Dataframe.lua
-- mimics an R data.frame
require 'asFactor'
require 'head'
require 'ifelse'
require 'keys'
require 'makeVp'
require 'sequenceContains'
require 'splitString'
-- API overview
if false then
-- constructing
df = Dataframe {values = tableExp, levels = tableExp} -- if some factors
df = Dataframe {values = tableExp} -- if no factors
-- this method seems buggy on large files. Use newFromFile2 if possible
df = Dataframe.newFromFile{ -- mimic R's read.table
file = "path/to/file",
header = boolean, -- default true
nRows = integer, -- number of data rows
sep = string, -- default ","
stringsAsFactors = boolean, -- default true
naStrings = seqOfStrings, -- default {"NA", ""}
skip = number -- default 0
}
-- special case: always a header, type of fields named specificially
-- only one NA string in file. Fails if a number column has a non number.
df = Dataframe.newFromFile2{ -- special case, much faster than general
file='path/to/file',
sep=string, -- default ','
naString=string, -- default '' (missing)
nRows=integer, -- default -1 (read all rows)
numberColumns=seqStrings, -- names of number columns, default {}
stringColumns=seqStrings, -- names of string columns, default {}
factorColumns=seqStrings, -- names of factor columns, default {}
skip=number -- default 0
}
df = Dataframe.newFromMerge{ -- merge on one fields
dfX = dfx,
dfY = dfy,
byX = colNameInDfx, -- column in dfx to match
byY = colNameInDfy -- column in dfy to match
}
df = Dataframe.newEmpty() -- has no rows nor columns
df1, df2 = df:splitString(fractionToDf1) -- split rows randomly into 2 pieces
-- a place holder for missing values
NA = Dataframe.NA
-- accessing components
names = df:columnNames() -- seq of all column names
numericNames = df:numberColumnNames() -- just the number columns
factorNames = df:factorColumnNames() -- just the factor columns
stringNames = df:stringColumnNames() -- just the string columns
values = df:column('abc') -- values in one column
-- constructing new Dataframe by selecting subsets
df2 = df:dropColumns{"a", "b"} -- drop specified columns
df2 = df:onlyColumns{"a", "b"} -- keep only specified columns
df2 = df:head(6) -- keep only first 6 rows
df2 = df:onlyRows(seq) -- keep only rows with seq[i] == true
df2 = df:row(n) -- keep only row n
-- mutating
df:addColumn(colName, values) -- add or replace column
df:dropColumn(colName) -- delete existing column
-- printing on stdout
df:print{n=3,maxlevels=10} -- print at most 3 values per column
-- inquiries
df:get("col name", index) -- values at row index of column "col name"
df:level("col name", index) -- level name for factor[col name, i]
df:kind("col name") -- oneOf {"number", "string", "factor", "allNA"}
df:nRows()
df:nCols()
-- reading (mimic R's read.table); constructor
df:writeCsv{file = "path/to/file",
colNames = boolean, -- default true
sep = string, -- default ","
naString = string, -- default ""
quote = boolean -- default false
}
-- convert to 2D tensor replacing Dataframe.NA values with NaN
colNames = {"a", "b"}
tensor, levels = df:asTensor(colNames) -- extract columns "a" and "b"
tensor1D = df:asTensor{'a'}
end -- examples
-- create class object
local Dataframe = torch.class('Dataframe')
--------------------------------------------------------------------------------
-- CONSTANTS
--------------------------------------------------------------------------------
Dataframe.NA = {}
--------------------------------------------------------------------------------
-- METHODS
--------------------------------------------------------------------------------
-- construct a new Dataframe
--
-- ARGS
--
-- argTable.values (required) is a table
-- key = name of column
-- value = a sequence with the same type
-- all the sequences in argTable.values should be the same length
--
-- argTable.levels (optional) is a table
-- key = name of a column that is a factor
-- value = a sequence of strings of names of the factor's levels
--
-- Returns: a new Dataframe object
function Dataframe:__init(argTable)
local arg = {}
arg.values = argTable.values or error('must supply values argument')
arg.levels = argTable.levels or {}
assert(type(arg.values) == 'table', 'values must be a sequence')
assert(type(arg.levels) == 'table', 'levels must be a sequence')
-- each value must be a sequence (check only that each is a table)
for k, v in pairs(arg.values) do
assert(type(v) == "table", 'value ' .. k .. ' is not a table')
end
self.values = arg.values
-- each level must be a sequence (check only that each is a table)
for k, v in pairs(arg.levels) do
assert(type(v) == 'table', 'level ' .. k .. ' is not a table')
end
self.levels = arg.levels
end
-- return seq of names of all columns
function Dataframe:columnNames()
local res = {}
for colName, _ in pairs(self.values) do
res[#res + 1] = colName
end
return res
end
-- return names of specified kind
function Dataframe:_specified(wanted)
local res = {}
for colName, _ in pairs(self.values) do
if self:kind(colName) == wanted then
res[#res + 1] = colName
end
end
return res
end
-- return seq of names for numeric columns
function Dataframe:numberColumnNames()
return self:_specified('number')
end
-- return seq of names for factor columns
function Dataframe:factorColumnNames()
return self:_specified('factor')
end
-- return seq of names for string columns
function Dataframe:stringColumnNames()
return self:_specified('string')
end
-- print compactly on stdout
--
-- ARGS: a table with these elements
--
-- n: optional integer, default 6
-- number of values to print; optional if negative, print them all
-- name: optional string default '', variable name holding the Dataframe
-- maxlevels: integer, default 6
-- number of levels to print for factor columns; if negative, print them all
--
-- RETURNS the Dataframe
function Dataframe:print(t)
local arg = {}
t = t or {}
arg.n = t.n or 6
arg.maxlevels = t.maxlevels or 6
arg.verbose = t.verbose or 0
arg.name = t.name
local vp = makeVp(arg.verbose, 'Dataframe:print')
vp(1, 't', t)
if arg.name then
print(string.format('Dataframe %s with %d rows, %d columns',
arg.name, self:nRows(), self:nCols()))
else
print(string.format('Dataframe with %d rows, %d columns',
self:nRows(), self:nCols()))
end
for colName, values in pairs(self.values) do
-- print values
vp(3, 'colName', colName)
vp(3, 'head(values)', head(values))
local function kind6()
local kind = self:kind(colName)
if #kind == 5 then
return kind .. ' '
else
return kind
end
end
-- capture the first few values and convert them into string s
local s = kind6() .. ' ' .. colName .. ':'
for i, value in ipairs(values) do
vp(3, 'i', i); vp(3, 'value', value)
if arg.n >= 0 and i > arg.n then
s = s .. ' ...'
break
end
local sValue
if value == Dataframe.NA then
sValue = 'NA'
else
sValue = tostring(value)
end
vp(3, 'sValue', sValue)
s = s .. ' ' .. sValue
end
print(s)
local function countNAs(seq)
local count = 0
for _, element in ipairs(seq) do
if element == Dataframe.NA then
count = count + 1
end
end
return count
end
local nNAs = countNAs(values)
if nNAs > 0 then
print(string.format(' with %d NAs', nNAs))
end
-- print levels, if column is a factor
local kind = self:kind(colName)
vp(3, 'kind', kind)
if kind ~= 'factor' then
--print(' ' .. kind)
else
local s = ' levels:'
local levels = self.levels[colName]
for i, level in ipairs(levels) do
if arg.maxlevels >= 0 and i > arg.maxlevels then
s = s .. ' ...'
break
end
s = s .. ' ' .. tostring(level)
end
print(s)
end
--stop()
end
return self
end
function Dataframe:get(name, index)
local values = self.values[name]
assert(values, name .. ' is not a column name')
return values[index]
end
function Dataframe:level(name, index)
assert(self.levels[name], name .. ' is not a factor')
local value = self:get(name, index)
return self.levels[name][value]
end
function Dataframe:nRows()
for _, value in pairs(self.values) do
return # value
end
return 0
end
function Dataframe:nCols()
local n = 0
for _, _ in pairs(self.values) do
n = n + 1
end
return n
end
function Dataframe:kind(name)
local t = self.values[name]
assert(t, 'not the name of a column: ' .. name)
for _, oneValue in ipairs(t) do
if oneValue == Dataframe.NA then
elseif type(oneValue) == "number" then
if self.levels[name] then
return "factor"
else
return "number"
end
elseif type(oneValue) == "string" then
return "string"
end
end
return "allNA"
end
function Dataframe:writeCsv(argTable)
local arg = {}
arg.file = argTable.file or error('missing file argument')
arg.colNames = argTable.colNames or true
arg.sep = argTable.sep or ","
arg.naString = argTable.naString or ""
arg.quote = argTable.quote or false
-- open file
local f, err = io.open(arg.file, "w")
if f == nil then error("unable to open file " .. arg.file) end
-- write header
if arg.colNames then
local firstField = true
for k, _ in pairs(self.values) do
if not firstField then
f:write(arg.sep)
end
firstField = false
f:write(k)
end
f:write("\n")
end
local function writeString(s)
if arg.quote then
f:write(arg.quote .. s .. arg.quote)
else
f:write(s)
end
end
-- write each record
for i = 1, self:nRows() do
local firstField = true
for colName, values in pairs(self.values) do
if not firstField then
f:write(arg.sep)
end
firstField = false
local kind = self:kind(colName)
local value = values[i]
if value == Dataframe.NA then
f:write(arg.naString)
else
if kind == "factor" then
writeString(self:level(colName, value))
elseif kind == "string" then
writeString(value)
else
f:write(value)
end
end
end
f:write("\n")
end
f:close()
end
-- convert to a 2D Tensor and level names
--
-- ARGS
--
-- colnames: seq of strings, the names of the columns to convert
--
-- RETURNS two values
--
-- tensor: a 1D or 2D Tensor, with NA values converted to NaN
--
-- levels: a table
-- key = a string, the name of a column
-- value = a seq, the level names for the column
function Dataframe:asTensor(colNames)
local vp = makeVp(0, 'Dataframe:asTensor')
-- build the tensor returned value
vp(1, 'colNames', colNames)
vp(1, 'self is ' .. self:nRows() .. ' x ' .. self:nCols())
local t = torch.Tensor(self:nRows(), #colNames)
vp(2, 't', t)
local colIndex = 0
for _, colName in ipairs(colNames) do
vp(2, 'colName ', colName)
local values = self.values[colName]
-- make sure column exists
if not values then
error(string.format('column %s is not in the Dataframe', colName))
end
-- assure that the column is numeric
local kind = self:kind(colName)
if kind ~= 'number' and kind ~= 'factor' then
error(string.format('column %s is not numeric', colName))
end
colIndex = colIndex + 1
for rowIndex, value in ipairs(values) do
if value == Dataframe.NA then
t[rowIndex][colIndex] = 0 / 0 -- convert NA to NaN
else
t[rowIndex][colIndex] = value
end
end
end
-- build the levels returned value
local levels = {}
for _, colName in ipairs(colNames) do
levels[colName] = self.levels[colName]
end
-- convert t from 2D to 1D only if 1 column name supplied
if #colNames == 1 then
vp(1, 'only 1 column name')
local newT = torch.Tensor(t:size(1))
for i = 1, newT:size(1) do
newT[i] = t[i]
end
t = newT
end
-- return tensor and levels
vp(1, 'head t', head(t))
vp(1, 'levels', levels)
return t, levels
end
-- return new Dataframe containing all columns but ones specified
function Dataframe:dropColumns(seq)
assert(type(seq) == 'table')
local vp = makeVp(0, 'Dataframe:dropColumns')
vp(1, 'columns to drop', seq)
local retainedColumns = {}
for _, currentColumnName in ipairs(self:columnNames()) do
local toBeDropped = false
for _, droppedColumnName in ipairs(seq) do
if currentColumnName == droppedColumnName then
toBeDropped = true
vp(2, 'found column to be dropped', currentColumnName)
break
end
end
if not toBeDropped then
vp(2, 'retaining column', currentColumnName)
table.insert(retainedColumns, currentColumnName)
end
end
vp(2, 'retained columns', retainedColumns)
return self:onlyColumns(retainedColumns)
end
-- return new Dataframe containing only the specified columns
function Dataframe:onlyColumns(seq)
assert(type(seq) == 'table')
local vp = makeVp(0, 'Dataframe:onlyColumns')
local newValues = {}
local newLevels = {}
for _, colName in ipairs(seq) do
newValues[colName] = self.values[colName]
newLevels[colName] = self.levels[colName]
end
vp(1, 'newValues keys', keys(newValues))
vp(1, 'newLevels', newLevels)
return Dataframe.new{values = newValues, levels=newLevels}
end
-- return new Dataframe containing only the specified rows
function Dataframe:onlyRows(seq)
local vp = makeVp(0)
local newValues = {}
-- copy each key and the selected values
for k, v in pairs(self.values) do
local newSeq = {}
for i, value in ipairs(v) do
local s = seq[i]
assert(type(s) == 'boolean',
'seq[' .. i .. '] is not boolean')
if s then
newSeq[#newSeq + 1] = value
end
end
newValues[k] = newSeq
end
-- do not recode the levels
return Dataframe.new{values = newValues, levels=self.levels}
end
-- return new Dataframe containing only the specified row
function Dataframe:row(n)
local vp = makeVp(0, 'Dataframe:row')
vp(1, 'n', n)
assert(math.floor(n) == n, 'n is not an integer')
assert(n > 0, 'n is not a positive integer')
assert(n <= self:nRows(), 'n exceeds number of rows')
local newValues = {}
for k, v in pairs(self.values) do
local newSeq = {}
newSeq[1] = v[n]
newValues[k] = newSeq
end
vp(1, 'newValues', newValues)
return Dataframe.new{values = newValues, levels = self.levels}
end
-- return new Dataframe containing only first few rows
-- MAYBE: allow n to be negative (to select last few rows)
function Dataframe:head(n)
assert(type(n) == "number", 'n must be a number')
assert(n >= 0, 'n must be non-negative')
-- return up to n values in a sequence
local function head(seq)
local result = {}
for i, v in ipairs(seq) do
if i > n then return result end
result[i] = v
end
return result
end
local newValues = {}
for k, v in pairs(self.values) do
newValues[k] = head(v)
end
local newLevels = {}
for k, v in pairs(self.levels) do
newLevels[k] = v -- return all the levels, not the first n
end
return Dataframe.new{values = newValues, levels=newLevels}
end
-- return values in a specified column
function Dataframe:column(colName)
return self.values[colName] -- may return nil and that's OK
end
-- mutate by adding or changing a column
-- only add number and string columns, not factor columns
function Dataframe:addColumn(colName, values)
if type(colName) ~= 'string' then error('colName must be a string') end
if type(values) ~= 'table' then error('values must be a sequence') end
for _, existingValue in pairs(self.values) do
if #values ~= #existingValue then
error('values must have same length as existing values')
end
break
end
self.values[colName] = values
return self
end
-- mutate by dropping a column
function Dataframe:dropColumn(colName)
if type(colName) ~= 'string' then error('colName must be a string') end
if self.values[colName] == nil then
error('column to be deleted must exist')
end
self.values[colName] = nil
-- don't drop the level, as determining if a column shares levels requires
-- extra work for a case that won't come up often
return self
end
-- df1, df2 = df:split(fractionToDf1)
-- split rows randomly into two new Dataframes
function Dataframe:split(fractionToDf1)
local vp = makeVp(0, 'Dataframe.split')
vp(1, 'self is ' .. self:nRows() .. ' x ' .. self:nCols())
vp(1, 'fractionToDf1', fractionToDf1)
assert(0 <= fractionToDf1 and fractionToDf1 <= 1,
'fractionToDf1 is not in [0,1]')
local inFirst = {}
local inSecond = {}
for i = 1, self:nRows() do
local selected = torch.uniform(0, 1) < fractionToDf1
table.insert(inFirst, selected)
table.insert(inSecond, not selected)
end
vp(2, 'inFirst', inFirst)
vp(2, 'inSecond', inSecond)
local df1 = self:onlyRows(inFirst)
local df2 = self:onlyRows(inSecond)
vp(1, 'df1 is ' .. df1:nRows() .. ' x ' .. df1:nCols())
vp(1, 'df2 is ' .. df2:nRows() .. ' x ' .. df2:nCols())
assert(df1:nRows() + df2:nRows() == self:nRows())
return df1, df2
end
--------------------------------------------------------------------------------
-- AUXILLARY FUNCTION newEmpty (a constructor)
--------------------------------------------------------------------------------
-- construct an empty Dataframe
function Dataframe.newEmpty()
return Dataframe.new{values = {}, levels = {}}
end
--------------------------------------------------------------------------------
-- AUXILLARY FUNCTION newFromFile2 (a constructor)
--------------------------------------------------------------------------------
-- construct Dataframe from content of file, when you know the types of columns
--
-- ARG: a table with these keys and values
--
-- file: string, path to file
--
-- nRows: integer, default -1
-- the maximum number of data rows to read. A negative value indicates that
-- all data rows are to be read.
--
-- sep: string, default ","
-- the field separator character. Each line is read as a string and split
-- using this string. The split determines the fields in the line.
--
-- naString: string, default ""
-- strings with these values are interpretted as missing. Missing values are
-- represented as the value Dataframe.NA. When a Dataframe is converted
-- to a Tensor with method asTensor, the Dataframe.NA values are converted
-- to NaN values.
--
-- skip: integer, default 0
-- Number of lines in the file that are skipped before beginning to process
-- the data.
--
-- factorColumns: seq on {string}
-- names of columns (from header) to be read as factors
-- numberColumns: seq on {string}
-- names of columns (from header) to be read as strings
-- if a number field in the file is not NA or a number, an error is raised
--
-- stringColumns: seq on {string}
-- names of columns (from header) to be read as factors
--
-- verbose: integer, default 0
-- 0 ==> no printing
-- 1 ==> print args and result and keep alive messages
-- 2 ==> print intermediate values
--
-- RETURNS: a Dataframe
function Dataframe.newFromFile2(t)
if t == nil then error('argument (a table) must be supplied') end
local arg = {}
arg.file = t.file or error('must supply a file name')
arg.nRows = t.nRows or -1 -- -1 ==> no limit
arg.sep = t.sep or ','
arg.skip = t.skip or 0
arg.naString = t.naString or ''
arg.factorColumns = t.factorColumns or {}
arg.numberColumns = t.numberColumns or {}
arg.stringColumns = t.stringColumns or {}
arg.verbose = t.verbose or 0
local vp = makeVp(arg.verbose, 'Dataframe.newFromFile2')
vp(1, 'Dataframe.newFromFile2 arg', arg)
local f, err = io.open(arg.file, 'r')
if f == nil then
error('unable to open file ' .. arg.file .. ' message=' .. err)
end
-- skip initial records
while arg.skip > 0 do
local record = f:read()
if record == nil then
error('end of file while skipping initial records')
end
arg.skip = arg.skip - 1
end
-- read and parse header
local header = f:read()
if header == nil then error('did not find a header') end
vp(1, 'header', header)
local headerFieldPosition = {}
for i, headerField in ipairs(splitString(header, arg.sep)) do
headerFieldPosition[headerField] = i
end
vp(1, 'headerFieldPosition', headerFieldPosition)
-- create allColumns and isNumberColumn
-- check that user's column names actually exist
local function assureInHeader(column)
if headerFieldPosition[column] == nil then
error('column ' .. column .. ' is not in the header')
end
end
local allColumns = {}
for _, column in ipairs(arg.factorColumns) do
table.insert(allColumns, column)
assureInHeader(column)
end
local isNumberColumn = {}
for _, column in ipairs(arg.numberColumns) do
table.insert(allColumns, column)
isNumberColumn[column] = true
assureInHeader(column)
end
for _, column in ipairs(arg.stringColumns) do
table.insert(allColumns, column)
assureInHeader(column)
end
vp(2, 'allColumns', allColumns)
local values = {}
for _, column in ipairs(allColumns) do
values[column] = {} -- initialize sequences
end
-- read the records
-- convert naString to Dataframe.NA
-- attempt to convert number columns to numbers
local nRead = 0 -- count number read
while true do
local record = f:read()
if record ~= nil then
nRead = nRead + 1
end
if record == nil or (arg.nRows >= 0 and nRead > arg.nRows) then
break -- at EOF or record limit
end
vp(2, 'record', record)
if arg.verbose >= 1 and nRead % 100000 == 0 then
print('data record ' .. nRead .. ' = ' .. record)
end
-- accumulate values for all specified columns
local recordFields = splitString(record, arg.sep)
for _, column in ipairs(allColumns) do
local i = headerFieldPosition[column]
local value = recordFields[i]
if value == arg.naString then
value = Dataframe.NA
elseif isNumberColumn[column] then
local maybeNumber = tonumber(value)
if maybeNumber == nil then
vp(0, 'data record ' .. nRead ..
' field ' .. column ..
' non-number value', value)
error('data record ' .. nRead ..
' field ' .. column ..
' has non-numeric value')
else
value = maybeNumber
end
end
table.insert(values[column], value)
end
vp(3, 'recordFields', recordFields)
vp(3, 'accumulated string values', values)
end
vp(1, string.format('read %d data records', nRead))
--vp(2, 'accumulated string values', values)
vp(2, 'string values for HEATING.CODE', values['HEATING.CODE'])
collectgarbage()
-- all the columns of interest are stored as string in values[column]
-- convert factor columns (now strings) to factors
-- NOTE that NA values are already represented as Dataframe.NA
vp(1, 'converting strings to factors in factor columns')
local levels = {}
for _, column in ipairs(arg.factorColumns) do
vp(2, 'factor column name', column)
local indicesSeq, levelsSeq = asFactor(values[column],
Dataframe.NA)
vp(2, 'indices', indicesSeq)
vp(2, 'levels', levelsSeq)
values[column] = indicesSeq
levels[column] = levelsSeq
end
vp(2, 'values', values)
vp(2, 'levels', levels)
collectgarbage()
local result = Dataframe.new{values = values, levels = levels}
if arg.verbose >= 1 then
print('Dataframe.newFromFile result')
result:print{n=5, levels=5}
end
return result
end
-------------------------------------------------------------------------------
-- AUXILLARY FUNCTION newFromFile (a constructor)
--------------------------------------------------------------------------------
-- construct a Dataframe containing content of file
--
-- ARG: a table with these keys and values
--
-- file: string, path to file
--
-- header: boolean, default true
-- if true the file is assumed to have a header in the first record.
-- The fields in the header become the names of the columns
--
-- nRows: integer, default -1
-- the maximum number of data rows to read. A negative value indicates that
-- all data rows are to be read.
--
-- sep: string, default ","
-- the field separator character. Each line is read as a string and split
-- using this string. The split determines the fields in the line.
--
-- stringsAsFactors: boolean, default true, required to be true for now.
-- Columns that are not all numbers or missing values or considered
-- columns of strings. If this argument is true, a column of strings is
-- converted into positive integers or NaNs (for missing values). The
-- positive integers are indices into a level table with the same name as
-- the name of the column. For example, if a columns of strings named
-- "direction" contains "east", "west", "east", "north", then
-- get("direction", 1) and get("direction", 3) both have value 1 and
-- level("direction", 1) has value "east".
-- NOTE: in R, the level name strings are sorted. Here they are not.
--
-- naStrings: seq of string, default {"NA", ""}
-- strings with these values are interpretted as missing. Missing values are
-- represented as the value Dataframe.NA. When a Dataframe is converted
-- to a Tensor with method asTensor, the Dataframe.NA values are converted
-- to NaN values.
--
-- skip: integer, default 0
-- Number of lines in the file that are skipped before beginning to process
-- the data.
--
-- verbose: integer, default 0
-- 0 ==> no printing
-- 1 ==> print args and result and keep alive messages
-- 2 ==> print intermediate values
--
-- RETURNS: a Dataframe
function Dataframe.newFromFile(t)
if t == nil then error('argument (a table) not supplied') end
--print('t'); print(t)
if t == nil then
t = {}
end
local arg = {}
arg.file = t.file or error('must supply file argument')
arg.header = t.header or true
arg.nRows = t.nRows or -1 -- -1 ==> no limit
arg.sep = t.sep or ","
if t.stringsAsFactors == nil then
arg.stringsAsFactors = true
else
arg.stringsAsFactors = t.stringsAsFactors
end
arg.naStrings = t.naStrings or {"NA", ""}
arg.skip = t.skip or 0
arg.verbose = t.verbose or 0
local vp = makeVp(arg.verbose)
vp(1, 'Dataframe.newFromFile arguments after defaults set', arg)
-- current implementation implements a subset of possible arguments
if not arg.header then error('for now, must have a header') end
-- open file
local f, error = io.open(arg.file, "r")
if f == nil then error('unable to open file ' .. arg.file) end
-- skip initial records
while arg.skip > 0 do
local record = f:read()
if record == nil then
error('end of file while skipping initial records')
end
arg.skip = arg.skip - 1
end
-- read header
local header = f:read()
if header == nil then
error('file is empty')
end
vp(2, 'header = ' .. header)
local headerFields = splitString(header, arg.sep)
local nFields = #headerFields
vp(2, 'headerFields', headerFields)
if (nFields == 0) then error('no fields in header') end
-- replace empty header fields with 'V n' (as does R's readTable)
local missingHeaderIndex = 0
for i, headerField in ipairs(headerFields) do
if headerField == '' then
missingHeaderIndex = missingHeaderIndex + 1
headerFields[i] = 'V ' .. missingHeaderIndex
end
end
local headerFieldsTable = {}
for _, headerField in ipairs(headerFields) do
headerFieldsTable[headerField] = true
end
-- build initial empty values sequences
local values = {}
for _, headerField in ipairs(headerFields) do
values[headerField] = {}
end
vp(2, 'initial values', values)
local function isNA(value)
for _, naString in ipairs(arg.naStrings) do
if naString == value then
return true
end
end
return false
end
local function isNumber(value)
if tonumber(value) then
return true
else
return false
end
end
-- should we use the colName
local function useColumnName(fieldName)
if headerFieldsTable[fieldName] then
return true
else
return false
end
--return arg.colNames == nil or sequenceContains(arg.colNames, fieldName)
end
local values = {}
local valuesAreAlwaysNumbersOrNA = {}
local function appendToValues(record)
vp(3, 'appending record', record)
local recordFields = splitString(record, arg.sep)
if #recordFields ~= nFields then
print("record = " .. record)
print('recordFields'); print(recordFields)
error(string.format('data record %d has %d fields,' ..
'not %d as the header did',
nRead, #recordFields, nFields))
end
-- append to values sequences
-- keep track of if values are always NA or numbers
for fieldIndex, fieldName in ipairs(headerFields) do
if useColumnName(fieldName) then
local fieldValue = recordFields[fieldIndex]
-- make sure the values[fieldName] sequence exists
if not values[fieldName] then