forked from EntropyOrg/p5-Data-Frame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factor.pm
139 lines (120 loc) · 3.03 KB
/
Factor.pm
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
package PDL::Factor;
use strict;
use warnings;
use Moo;
use PDL::Lite;
use Hash::Ordered;
use Hash::Ordered::Extension;
use Data::Rmap qw(rmap);
use Storable qw(dclone);
use Scalar::Util qw(blessed);
use List::AllUtils qw( first_index );
use Test::Deep::NoTest qw(eq_deeply);
extends 'PDL';
with 'PDL::Role::Enumerable';
# after stringifiable role is added, the string method will exist
eval q{
use overload (
'""' => \&PDL::Factor::string,
'==' => \&PDL::Factor::equal,
'!=' => \&PDL::Factor::not_equal,
);
};
=method new( $data, %opt )
levels => $array_ref
=cut
around new => sub {
my $orig = shift;
my ($class, @args) = @_;
my $data;
# TODO UGLY! create a better interface
#
# new( integer => $enum, levels => $level_arrayref )
# new( $data_arrayref, levels => $level_arrayref )
# etc.
#
# Look at how R does it.
if( @args % 2 != 0 ) {
$data = shift @args; # first arg
}
my %opt = @args;
my $levels = Hash::Ordered->new;
my $enum = $opt{integer} // dclone($data);
if( exists $opt{levels} ) {
# add the levels first if given levels option
for my $l (@{ $opt{levels} } ) {
$levels->set( $l => 1 );
}
# TODO what if the levels passed in are not unique?
# TODO what if the integer enum data outside the range of level indices?
} else {
rmap {
my $v = $_;
$levels->set($v => 1); # add value to hash if it doesn't exist
# Ugly replacement for first_index
my ( $i, $idx ) = ( 0, 0 );
for my $key ( $levels->keys ) {
$idx = $i if $key eq $v;
$i++;
}
$_ = $idx;
} $enum;
}
unshift @args, _data => $enum;
unshift @args, _levels => $levels;
# TODO how do I pass the prefered type to PDL->new()?
my $self = $orig->($class, @args);
$self->{PDL} = $self->{PDL}->long;
$self;
};
sub FOREIGNBUILDARGS {
my ($self, %args) = @_;
( $args{_data} );
}
sub initialize {
bless { PDL => PDL::null() }, shift;
}
around string => sub {
my $orig = shift;
my ($self, %opt) = @_;
my $ret = $orig->(@_);
if( exists $opt{with_levels} ) {
my @level_string = grep { defined } $self->{_levels}->keys();
$ret .= "\n";
$ret .= "Levels: @level_string";
}
$ret;
};
# TODO overload, compare factor level sets
#
#R
# > g <- iris
# > levels(g$Species) <- c( levels(g$Species), "test")
# > iris$Species == g$Species
# : Error in Ops.factor(iris$Species, g$Species) :
# : level sets of factors are different
#
# > g <- iris
# > levels(g$Species) <- levels(g$Species)[c(3, 2, 1)]
# > iris$Species == g$Species
# : # outputs a logical vector where only 'versicolor' indices are TRUE
sub equal {
my ($self, $other, $d) = @_;
# TODO need to look at $d to determine direction
if( blessed($other) && $other->isa('PDL::Factor') ) {
if( eq_deeply($self->_levels, $other->_levels) ) {
return $self->{PDL} == $other->{PDL};
# TODO return a PDL::Logical
} else {
die "level sets of factors are different";
}
} else {
# TODO hacky. need to test this more
my $key_idx = first_index { $_ == $other } $self->_levels->values;
return $self->{PDL} == $key_idx;
}
}
sub not_equal {
return !equal(@_);
}
1;