]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/dataframe
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / dataframe
1 %# -*- mode: Octave -*-
2 %!shared a, b, x, xl, y, yl, Y, z
3 %! x=dataframe(randn(3, 3), 'rownames', (7:-1:5).');
4 %! x(1:3, 1) = 3;
5 %! x(1:3, 1) = (4:6).';
6 %!assert(x.array(2, 1), 5);
7 %! x(1, 1:3) = 3;         
8 %! x(1, 1:3) = (4:6).';
9 %!assert(x.array(1, 2), 5);
10 %!assert(isempty(x.rowidx), false);
11 %! x.types(2) ='single';
12 %!assert(class(x.array(1, 2)), 'single')
13 %! x=dataframe('data_test.csv');
14 %!assert(isna(x.array(9, 4)))
15 %! # remove rownames
16 %! x.rownames = [];
17 %!assert(size(x.rownames), [0 0])
18 %! # remove a column through '.' access
19 %! y = x; y.DataName = [];
20 %!assert(size(y(:, '_IBIAS_')), [10 1])
21 %!assert(size(y), [10 6]);
22 %! y = x{};
23 %!assert(size(y), [10 7]);
24 %! y = x{[2 5], [2 7]};
25 %!assert(y, {-5.8, "E"; -5.2, "C"});
26 %! y = x{}([2 5], [2 7]);
27 %!assert(y, {-5.8, "E"; -5.2, "C"});
28 %! y = x{1:2, 1:2}(4);
29 %!assert(y, {-5.8});    
30 %! # remove a column through (:, name) access
31 %! y = x; y(:, "DataName") = [];
32 %!assert(size(y), [10 6]);
33 %! # create an empty dataframe
34 %! y = dataframe([]);
35 %!assert(isempty(y), true);
36 %! y = x.df(:, 2:6);
37 %! Y = 2*pi*double(y.Freq).*y.C+y.GOUT;
38 %! z = dataframe(y,{{'Y'; Y}});
39 %!assert(size(z), [10 6]);
40 %!assert(abs(z(1, "Y") - Y(1)).array, 0);
41 %! # direct matrix setting through struct access
42 %! y.Freq=[(1:10).' (10:-1:1).'];
43 %! # verify the "end" operator on the third dim
44 %!assert(y.array(2, 2, end), 9);
45 %! # direct setting through 3D matrix
46 %! y(:, ["C"; "G"], 1:2) = repmat(y(:, ["C"; "G"]), [1 1 2]);
47 %! y(4:5, 4:5) = NaN;
48 %!test
49 %! if any(size(x) != [10 7]),
50 %!   error('x: wrong input size')
51 %! endif
52 %! if any(size(y) != [10 5 2]),
53 %!   error('y: wrong input size')
54 %! endif
55 %!#assert(numel(x), 1);
56 %! # test simple slices
57 %!assert(x.VBIAS(1:6), (-6:.2:-5).');
58 %!assert(x.array(6:10, 2), (-5:.2:-4.2).');
59 %!assert(x.array(6, "OK_"), 'B');
60 %!assert(x.array(2, logical([0 0 1 1])), x.array(2, 3:4))
61 %!assert(size(y.array(:, :, :)), [10 5 2]);
62 %!assert(size(y.array(:, :)), [10 10]);
63 %!assert(size(y.array(:, 2, 2)), [10 1]);
64 %!assert(size(y.array(:, 2)), [10 1]);
65 %!assert(y.C(4:5), [NaN NaN]);
66 %!error error("Accessing past limits")
67 %! x(1, 8)
68 %! x(11, 1)
69 %! x(1, logical(ones(1, 7)))
70 %! x.types{"FReq*"}
71 %! x(1, :)
72 %!test
73 %! #!! removed -- output format may only be specified before selection
74 %! #select one column         
75 %! #assert(x(1:3, 1).cell(:), x.cell(1:3)(:))   
76 %! #assert(x(33:35).cell.', x(33:35).cell(:))
77 %! #select two columns        
78 %!assert(x.cell(1:10, 2:3)(:), x.cell(11:30)(:))        
79 %!error error("Complex accesses");
80 %! x(:);
81 %! x.dataframe(:);
82 %! x.dataframe.cell
83 %!test
84 %! # test modifying column type
85 %! x.types("Freq") = 'uint32'; x.types(2) = 'single';
86 %! # downclassing must occur !
87 %!assert(class(x.array(1, ["Freq"; "C"])), 'uint32')
88 %! # upclassing must occur !
89 %!assert(class(x.as.double(1, ["Freq"; "C"])), 'double')
90 %!error error("Incorrect internal field sub-referencing")
91 %! x.types{"Freq"}       
92 %!error error("mixing different types") 
93 %! x([12:18 22:28 32:38]);
94 %!error error("non-square access")
95 %! x([22:28 32:37]).dataframe;
96 %! x.cell([1:19]);
97 %!error error("Single-dimension name access")
98 %! x("Freq");
99 %!test
100 %! # complex access
101 %! x(x(:, "OK_") == '?', ["C"; "G"]) = NaN;
102 %!assert(x.array(4, 5:6), [NaN NaN])
103 %! # extract values
104 %! y = x.dataframe(x(:, "OK_") =='A', {"Freq", "VB*", "C", "G"});
105 %! #comparison using cell output class, because assert use (:)
106 %!assert(y.cell(:, 2:3), x.cell([1 7], ["VB*"; "C"]))
107 %!assert(x.array((33:35).'), x.array(3:5, 4))
108 %! #test further dereferencing
109 %!assert(x.array(:, "C")(2:4), x.array(2:4, "C"))
110 %! # complex modifications through cell access
111 %! z = dataframe(x, {"VB*", {"Polarity" ,"Sense"; ones(12,2), zeros(10,2)}});
112 %!assert(size(z), [12 9 2]);
113 %!assert(z.Sense(11:12, :), NA*ones(2, 2));
114 %!assert(size(z._over{2}, 2) - size(x._over{2}, 2), 2);
115 %! x = dataframe(randn(3, 3)); y = x.array;
116 %! xl = x > 0; yl = y > 0;
117 %! a = zeros(size(yl)); b = a;
118 %! a(xl) = 1; b(yl) = 1;
119 %!assert(a, b);
120 %! [a, b] = sort(y(:)); y = reshape(b, 3, 3); x = dataframe(y);
121 %! a = zeros(size(yl)); b = a;
122 %! a(x) = 10:-1:2; b(y) = 10:-1:2;
123 %!assert(a, b);
124 %! x = dataframe(randn(3, 3)); y = randn(3, 3); z = dataframe(y);
125 %!assert((x+y(1)).array, x.array+y(1))
126 %!assert((y(1)+x).array, y(1)+x.array)
127 %!assert((x+y).array, x.array+y)
128 %!assert((y+x).array, y+x.array)
129 %!assert((x+z).array, x.array+z.array)
130 %!assert((bsxfun(@plus, x, z(1,:))).array, bsxfun(@plus, x.array, z.array(1,:)))
131 %!assert((bsxfun(@plus, x, z(:,1))).array, bsxfun(@plus, x.array, z.array(:,1)))
132 %!assert((bsxfun(@minus,z(1,:),x)).array, bsxfun(@minus,z.array(1,:),x.array))
133 %!assert((bsxfun(@minus,z(:,1),x)).array, bsxfun(@minus,z.array(:,1),x.array))
134 %!assert((x > 0).array, x.array > 0)
135 %!assert((0 > x).array, 0 > x.array)
136 %!assert((x > y).array, x.array > y);
137 %!assert((y > x).array, y > x.array);
138 %!assert((x > z).array, x.array > z.array)
139 %!assert((x*y(1)).array, x.array*y(1))
140 %!assert((y(1)*x).array, y(1)*x.array)
141 %!assert((x.*y).array, x.array.*y)
142 %!assert((y.*x).array, y.*x.array)
143 %!assert((z.*x).array, z.array.*x.array)
144 %!assert((x*y).array, x.array*y)
145 %!assert((y*x).array, y*x.array)
146 %!assert((x*z).array, x.array*z.array)
147 %!assert((x/y(1)).array, x.array/y(1))
148 %!assert((x./y).array, x.array./y)
149 %!assert((y./x).array, y./x.array)
150 %!assert((z./x).array, z.array./x.array)
151 %!assert((x/y).array, x.array/y)
152 %!assert((y/x).array, y/x.array)
153 %!assert((x/z).array, x.array/z.array)
154 % # left division is a bit more complicated
155 %!assert((x(1, 1)\y).array, x.array(1, 1)\y)
156 %!assert((x(:, 1)\y).array, x.array(:, 1)\y, sqrt(eps))
157 %!assert((x\y).array, x.array\y)
158 %!assert((y\x).array, y\x.array)
159 %!assert((x\z).array, x.array\z.array)
160 % x=dataframe(randn(4, 3, 2)); y=randn(4, 3, 2); z=dataframe(y);
161 %!assert((abs(sum(center(x)) < sqrt(eps)).array))
162 %!assert((x+y).array, x.array+y)
163 %!assert((y+x).array, y+x.array)
164 %!assert((x+z).array, x.array+z.array);
165 %!assert((bsxfun(@plus,x,z(1,:,:))).array, bsxfun(@plus,x.array,z.array(1,:,:)));
166 %!assert((bsxfun(@plus,x,z(:,1,:))).array, bsxfun(@plus,x.array,z.array(:,1,:)));
167 %!assert((bsxfun(@plus,z(1,:,:),x)).array, bsxfun(@plus,z.array(1,:,:),x.array));
168 %!assert((bsxfun(@plus,z(:,1,:),x)).array, bsxfun(@plus,z.array(:,1,:),x.array));
169
170 %! [a, b] = sort(x(:)); x=dataframe(reshape((1:9)(b), [3 3])); 
171 %! y = reshape((1:9)(b), [3 3]); z = dataframe(y);
172 %!assert(x(x(:)), y(x(:)));
173 %!assert(x(y(:)), y(y(:)));
174 %! z= x(x);
175 %!assert(z.array, y(x));
176 %! z = x(y);
177 %!assert(z.array, y(y)); 
178
179 %!demo
180 %! x=dataframe('octave_frame/data_test.csv')
181 %! disp("Access as a struct: x.VBIAS(1:6)")
182 %! x.VBIAS(1:6)
183 %! pause; disp("Access as a matrix: x(6, 'OK')")
184 %! x(6, "OK?")
185 %! pause; disp("Removing the row names:  x.rownames = []");
186 %! x.rownames = []
187 %! pause; disp("Modifying column type: x.types['Freq']='uint32'");
188 %! x.types("Freq")='uint32'
189 %! pause; disp("Partial extract");
190 %! disp("y = x(x(:, 'OK.') == 'A'|x(:, ""OK?"") == 'B', {'Freq', 'VB*', 'C', 'G'}")
191 %! y = x(x(:, 'OK.') == 'A'|x(:, "OK?") == 'B', {'Freq', 'VB*', 'C', 'G'})
192 %! disp("y.rownames = char({'low', 'med', 'med', 'high'})");
193 %! y.rownames = char({'low', 'med', 'med', 'high'})
194 %! pause; disp("Partial modification of one column")
195 %! disp("y.Freq('med')=[290e3; 310e3]")
196 %! y.Freq('med') = [290e3; 310e3]
197 %! pause; disp('Complex access');
198 %! disp("y.C('med')([2 1])");
199 %! y.C('med')([2 1])
200 %! pause; disp('Print stats about a dataframe: summary(y)');
201 %! summary(y)
202
203 %!demo
204 %! disp('Modifying a dataframe from a cell array')
205 %!  RHS={ 'don''t care', 'idx', 'Vb', 'freq', 'Ib', 'C', 'status', 'comment'
206 %!        'yes',     uint16(5), single(3.2), 10000, 1e-11, 6e-13, 'bla', '@'
207 %!        'no',     uint16(16), 4, 12000, 2e-11, 4e-13, 7, 'X'};
208 %! disp("Resetting a dataframe: x=dataframe([])");
209 %! x = dataframe([]);
210 %! x(:, :) = RHS
211 %! disp("Overwriting the second line")
212 %! RHS{1, 2} = "idg"; RHS{3, 1}= "No!";
213 %! disp("'x(2, :) = RHS(1:2, :)' will produce two warnings")
214 %! disp("Notice that only the second line content will change");
215 %! disp("x(2, :) = RHS(1:2, :)")
216 %! x(2, :) = RHS(1:2, :)
217 %! pause; disp('same effect, but skipping first column');
218 %! disp("x(1, :) = RHS([1 3], 2:end)");
219 %! x(1, []) = RHS([1 3], 2:end)
220 %!demo
221 %! disp("same game, but using row indexes.")
222 %! disp("Notice the first field name is empty")
223 %! RHS= { '', 'idx', 'Vb', 'freq', 'Ib', 'C', 'status', 'comment'
224 %!        5, uint32(16),   5.3, 11000, 3e-12, 5e-12, "may", "8th"}; 
225 %! disp("x= dataframe(RHS)")
226 %! x = dataframe(RHS)
227 %! pause; disp("The same effect is achieved by assigning to an empty dataframe")
228 %! x = dataframe([]);
229 %! x(:, :) = RHS