]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/display.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / display.m
1 function resu = display(df)
2
3   %# function resu = display(df)
4   %# Tries to produce a nicely formatted output of a dataframe.
5
6   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
7   %%
8   %% This file is part of Octave.
9   %%
10   %% Octave is free software; you can redistribute it and/or
11   %% modify it under the terms of the GNU General Public
12   %% License as published by the Free Software Foundation;
13   %% either version 2, or (at your option) any later version.
14   %%
15   %% Octave is distributed in the hope that it will be useful,
16   %% but WITHOUT ANY WARRANTY; without even the implied
17   %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18   %% PURPOSE.  See the GNU General Public License for more
19   %% details.
20   %%
21   %% You should have received a copy of the GNU General Public
22   %% License along with Octave; see the file COPYING.  If not,
23   %% write to the Free Software Foundation, 51 Franklin Street -
24   %% Fifth Floor, Boston, MA 02110-1301, USA.
25   
26   %#
27   %# $Id: display.m 9585 2012-02-05 15:32:46Z cdemills $
28   %#
29
30   %# generate header name
31   dummy = inputname (1);
32   if (isempty(dummy))
33     dummy = "ans";
34   endif
35
36   if (2 == length (df._cnt))
37     head = sprintf ("%s = dataframe with %d rows and %d columns", \
38                     dummy, df._cnt);
39   else
40     head = sprintf ("%s = dataframe with %d rows and %d columns on %d pages", \
41                     dummy, df._cnt);
42   endif
43
44   if (!isempty (df._src))
45     for indi = (1:size (df._src, 1))
46       head = strvcat\
47           (head, [repmat("Src: ", size (df._src{indi, 1}, 1), 1)\
48                   df._src{indi, 1}]);
49     endfor
50   endif
51
52   if (!isempty (df._cmt))
53     for indi = (1:size(df._cmt, 1))
54       head = strvcat\
55           (head, [repmat("Comment: ", size (df._cmt{indi, 1}, 1), 1)\
56                   df._cmt{indi, 1}]);
57     endfor
58   endif
59   
60   if (all (df._cnt > 0))  %# stop for empty df
61     dummy=[]; vspace = repmat (' ', df._cnt(1), 1);
62     indi = 1; %# the real, unfolded index
63     %# loop over columns where the corresponding _data really exists
64     for indc = (1:min (df._cnt(2), size (df._data, 2))) 
65       %# emit column names and type
66       if (1 == length (df._rep{indc}))
67         dummy{1, 2+indi} = deblank (disp (df._name{2}{indc}));
68         dummy{2, 2+indi} = deblank (df._type{indc});
69       else
70         %# append a dot and the third-dimension index to column name
71         tmp_str = [deblank(disp (df._name{2}{indc})) "."];
72         tmp_str = arrayfun (@(x) horzcat (tmp_str, num2str(x)), ...
73                             (1:length (df._rep{indc})), 'UniformOutput', false); 
74         dummy{1, 2+indi} = tmp_str{1};
75         dummy{2, 2+indi} = deblank (df._type{indc});
76         for indk = (2:length (tmp_str))
77           dummy{1, 1+indi+indk} = tmp_str{indk};
78           dummy{2, 1+indi+indk} = dummy{2, 2+indi};
79         endfor
80       endif
81       %# "print" each column
82       switch df._type{indc}
83         case {'char'}
84           indk = 1; while (indk <= size (df._data{indc}, 2))
85             tmp_str = df._data{indc}(:, indk); %#get the whole column
86             indj = cellfun ('isprint', tmp_str, 'UniformOutput', false); 
87             indj = ~cellfun ('all', indj);
88             for indr = (1:length(indj))
89               if (indj(indr)),
90                 if (isna (tmp_str{indr})),
91                   tmp_str{indr} = "NA";
92                 else
93                   if (~ischar (tmp_str{indr}))
94                     tmp_str{indr} = char (tmp_str{indr});
95                   endif
96                   tmp_str{indr} = undo_string_escapes (tmp_str{indr});
97                 endif
98               endif
99             endfor
100             %# keep the whole thing, and add a vertical space
101             dummy{3, 2+indi} = disp (char (tmp_str));
102             dummy{3, 2+indi} = horzcat...
103                 (vspace, char (regexp (dummy{3, 2+indi}, '.*', ...
104                                        'match', 'dotexceptnewline')));
105             indi = indi + 1; indk = indk + 1;
106           endwhile
107         otherwise
108           %# keep only one horizontal space per line
109           unfolded = df._data{indc}(:, df._rep{indc});
110           indk = 1; while (indk <= size (unfolded, 2))
111             dummy{3, 2+indi} = disp (unfolded(:, indk));
112             tmp_str = char (regexp (dummy{3, 2+indi}, \
113                                     '[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?(\s??[-+]\s??[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?i)?', \
114                                     'match', 'dotexceptnewline'));
115             tmp_str = horzcat...
116                 (vspace, char (regexp (dummy{3, 2+indi}, '\S.*', ...
117                                        'match', 'dotexceptnewline')));
118             dummy{3, 2+indi} = tmp_str;
119             indi = indi + 1; indk = indk + 1;
120           endwhile
121       endswitch
122     endfor
123
124     %# put everything together
125     vspace = [' '; ' '; vspace];
126     %# second line content
127     resu = []; 
128     if (!isempty (df._ridx))
129       for (ind1 = 1:size (df._ridx, 2))
130         if ((1 == size(df._ridx, 3)) && \
131               (any (!isna (df._ridx(1:df._cnt(1), ind1)))))
132           dummy{2, 1} = [sprintf("_%d", ind1) ; "Nr"];
133           dummy{3, 1} = disp (df._ridx(1:df._cnt(1), ind1)); 
134           indi = regexp (dummy{3, 1}, '\b.*\b', 'match', 'dotexceptnewline');
135           if (isempty (resu))
136             resu = strjust (char (dummy{2, 1}, indi), 'right');
137           else
138             resu = horzcat(resu, vspace, strjust (char (dummy{2, 1}, indi), \
139                                                   'right'), vspace);
140           endif
141         else 
142           for ind2 = (1:size (df._ridx, 3))
143             if (any (!isna (df._ridx(1:df._cnt(1), ind1, ind2)))),
144               dummy{2, 1} = [sprintf("_%d.%d", ind1, ind2) ; "Nr"];
145               dummy{3, 1} = disp (df._ridx(1:df._cnt(1), ind1, ind2)); 
146               indi = regexp (dummy{3, 1}, '\b.*\b', 'match', 'dotexceptnewline');
147               if (isempty (resu)) 
148                 resu = strjust (char (dummy{2, 1}, indi), 'right');
149               else
150                 resu = horzcat (resu, vspace, strjust (char(dummy{2, 1}, indi), \
151                                                        'right'), vspace);
152               endif
153             endif
154           endfor
155         endif
156       endfor
157     endif
158
159     %# emit row names
160     if (isempty (df._name{1})),
161       dummy{2, 2} = []; dummy{3, 2} = [];
162     else
163       dummy{2, 2} = [" ";" "];
164       dummy{3, 2} = df._name{1};
165     endif
166     
167     %# insert a vertical space
168     if (!isempty(dummy{3, 2}))
169       indi = ~cellfun ('isempty', dummy{3, 2});
170       if (any (indi))
171         try
172           resu = horzcat (resu, vspace, strjust (char(dummy{2, 2}, dummy{3, 2}),\
173                                                  'right'));
174         catch
175           disp ('line 172 '); keyboard
176         end_try_catch
177       endif
178     endif
179     
180     %# emit each colum
181     for indi = (1:size (dummy, 2) - 2)
182       %# was max(df._cnt(2:end)),
183       try
184         %# avoid this column touching the previous one
185         if (any (cellfun ('size', dummy(1:2, 2+indi), 2) >= \
186                  size (dummy{3, 2+indi}, 2)))
187           resu = horzcat (resu, vspace);
188         endif
189         resu = horzcat (resu, strjust (char (dummy{:, 2+indi}), 'right'));
190       catch
191         tmp_str = sprintf ("Emitting %d lines, expecting %d", ...
192                            size (dummy{3, 2+indi}, 1), df._cnt(1));
193         keyboard
194         error (tmp_str);
195       end_try_catch
196     endfor
197   else
198     resu = '';
199   endif
200   
201   resu = char (head, resu); disp (resu)
202
203 endfunction