From: <cde...@us...> - 2010-11-22 16:48:34
|
Revision: 7936 http://octave.svn.sourceforge.net/octave/?rev=7936&view=rev Author: cdemills Date: 2010-11-22 16:48:27 +0000 (Mon, 22 Nov 2010) Log Message: ----------- Better handling of missing files, initialisation from scalar/vector, display of complex numbers Modified Paths: -------------- trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m trunk/octave-forge/extra/dataframe/inst/@dataframe/display.m trunk/octave-forge/extra/dataframe/inst/@dataframe/isfield.m trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m Added Paths: ----------- trunk/octave-forge/extra/dataframe/inst/@dataframe/sum.m Property Changed: ---------------- trunk/octave-forge/extra/dataframe/inst/@dataframe/isfield.m Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m 2010-11-22 12:16:18 UTC (rev 7935) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m 2010-11-22 16:48:27 UTC (rev 7936) @@ -71,6 +71,7 @@ df._ridx = []; df._data = cell(0, 0); df._type = cell(0, 0); + df._src = cell(0, 0); df = class(df, 'dataframe'); return endif @@ -147,111 +148,128 @@ if isa(x, 'char') && size(x, 1) < 2, %# read the data frame from a file try - x = load(tilde_expand(x)); + dummy = tilde_expand(x); + x = load(dummy); + df._src{end+1, 1} = dummy; catch - UTF8_BOM = char([0xEF 0xBB 0xBF]); + %# try our own method + UTF8_BOM = char([0xEF 0xBB 0xBF]); unwind_protect - fid = fopen(tilde_expand(x)); - dummy = fgetl(fid); - if !strcmp(dummy, UTF8_BOM), - frewind(fid); + dummy = tilde_expand(x); + fid = fopen(dummy); + if fid != -1, + dummy = fgetl(fid); + if !strcmp(dummy, UTF8_BOM), + frewind(fid); + endif + in = fscanf(fid, "%c"); %# slurps everything + df._src{end+1, 1} = dummy; + else + in = []; endif - in = fscanf(fid, "%c"); %# slurps everything unwind_protect_cleanup - fclose(fid); + if fid != -1, fclose(fid); endif end_unwind_protect - %# explicit list taken from 'man pcrepattern' -- we enclose all - %# vertical separators in case the underlying regexp engine - %# doesn't have them all. - eol = '(\r\n|\n|\v|\f|\r|\x85)'; - %# cut into lines -- include the EOL to have a one-to-one - %# matching between line numbers. Use a non-greedy match. - lines = regexp(in, ['.*?' eol], 'match'); - dummy = cellfun(@(x) regexp(x, eol), lines); - %# remove the EOL character(s) - lines(1==dummy) = {""}; - %# use a positive lookahead -- eol is not part of the match - lines(dummy > 1) = cellfun(@(x) regexp(x, ['.*?(?=' eol ')'], \ - 'match'), lines(dummy > 1)); - %# a field either starts at a word boundary, either by + - . for - %# a numeric data, either by ' for a string. - %# content = cellfun(@(x) regexp(x, '(\b|[-+\.''])[^,]*(''|\b)', 'match'),\ - %# lines, 'UniformOutput', false); %# extract fields - content = cellfun(@(x) strsplit(x, sep), lines, \ - 'UniformOutput', false); %# extract fields - indl = 1; indj = 1; %# disp('line 151 '); keyboard - if ~isempty(seeked), + if !isempty(in), + %# explicit list taken from 'man pcrepattern' -- we enclose all + %# vertical separators in case the underlying regexp engine + %# doesn't have them all. + eol = '(\r\n|\n|\v|\f|\r|\x85)'; + %# cut into lines -- include the EOL to have a one-to-one + %# matching between line numbers. Use a non-greedy match. + lines = regexp(in, ['.*?' eol], 'match'); + dummy = cellfun(@(x) regexp(x, eol), lines); + %# remove the EOL character(s) + lines(1==dummy) = {""}; + %# use a positive lookahead -- eol is not part of the match + lines(dummy > 1) = cellfun(@(x) regexp(x, ['.*?(?=' eol ')'], \ + 'match'), lines(dummy > 1)); + %# a field either starts at a word boundary, either by + - . for + %# a numeric data, either by ' for a string. + + %# content = cellfun(@(x) regexp(x, '(\b|[-+\.''])[^,]*(''|\b)', 'match'),\ + %# lines, 'UniformOutput', false); %# extract fields + content = cellfun(@(x) strsplit(x, sep), lines, \ + 'UniformOutput', false); %# extract fields + indl = 1; indj = 1; %# disp('line 151 '); keyboard + if ~isempty(seeked), + while indl <= length(lines), + dummy = content{indl}; + if strcmp(dummy{1}, seeked) + break; + endif + indl = indl + 1; + endwhile + %# else + %# dummy = content{indl}; + endif + x = cell(1+length(lines)-indl, size(dummy, 2)); + empty_lines = []; while indl <= length(lines), dummy = content{indl}; - if strcmp(dummy{1}, seeked) - break; + if all(cellfun('size', dummy, 2) == 0), + empty_lines = [empty_lines indj]; + indl = indl + 1; indj = indj + 1; + continue; endif - indl = indl + 1; + %# does it looks like a comment line ? + if regexp(dummy{1}, ['^\s*' char(35)]), + empty_lines = [empty_lines indj]; + indl = indl + 1; indj = indj + 1; + continue; + endif + %# try to convert to float + the_line = cellfun(@(x) sscanf(x, "%f"), dummy, \ + 'UniformOutput', false); + for indk = 1: size(the_line, 2), + if isempty(the_line{indk}) || any(size(the_line{indk}) > 1), + %#if indi > 1 && indk > 1, disp('line 117 '); keyboard; %#endif + if unquot, + try + %# remove quotes and leading space(s) + x(indj, indk) = regexp(dummy{indk}, '[^'' ].*[^'']', 'match'){1}; + catch + %# if the previous test fails, try a simpler one + in = regexp(dummy{indk}, '[^'' ]+', 'match'); + if !isempty(in), + x(indj, indk) = in{1}; + %# else + %# x(indj, indk) = []; + endif + end_try_catch + else + %# no conversion possible, store and remove leading space(s) + x(indj, indk) = regexp(dummy{indk}, '[^ ].*', 'match'); + endif + else + x(indj, indk) = the_line{indk}; + endif + endfor + indl = indl + 1; indj = indj + 1; endwhile - %# else - %# dummy = content{indl}; - endif - x = cell(1+length(lines)-indl, size(dummy, 2)); - empty_lines = []; - while indl <= length(lines), - dummy = content{indl}; - if all(cellfun('size', dummy, 2) == 0), - empty_lines = [empty_lines indj]; - indl = indl + 1; indj = indj + 1; - continue; + if !isempty(empty_lines), + x(empty_lines, :) = []; endif - %# does it looks like a comment line ? - if regexp(dummy{1}, ['^\s*' char(35)]), - empty_lines = [empty_lines indj]; - indl = indl + 1; indj = indj + 1; - continue; + %# detect empty columns + empty_lines = find(0 == sum(cellfun('size', x, 2))); + if !isempty(empty_lines), + x(:, empty_lines) = []; endif - %# try to convert to float - the_line = cellfun(@(x) sscanf(x, "%f"), dummy, \ - 'UniformOutput', false); - for indk = 1: size(the_line, 2), - if isempty(the_line{indk}) || any(size(the_line{indk}) > 1), - %#if indi > 1 && indk > 1, disp('line 117 '); keyboard; %#endif - if unquot, - try - %# remove quotes and leading space(s) - x(indj, indk) = regexp(dummy{indk}, '[^'' ].*[^'']', 'match'){1}; - catch - %# if the previous test fails, try a simpler one - in = regexp(dummy{indk}, '[^'' ]+', 'match'); - if !isempty(in), - x(indj, indk) = in{1}; - %# else - %# x(indj, indk) = []; - endif - end_try_catch - else - %# no conversion possible, store and remove leading space(s) - x(indj, indk) = regexp(dummy{indk}, '[^ ].*', 'match'); - endif - else - x(indj, indk) = the_line{indk}; - endif - endfor - indl = indl + 1; indj = indj + 1; - endwhile - if !isempty(empty_lines), - x(empty_lines, :) = []; + clear UTF8_BOM fid in lines indl the_line content empty_lines endif - %# detect empty columns - empty_lines = find(0 == sum(cellfun('size', x, 2))); - if !isempty(empty_lines), - x(:, empty_lines) = []; - endif - clear UTF8_BOM fid in lines indl the_line content empty_lines end_try_catch endif %# fallback, avoiding a recursive call idx.type = '()'; - indj = df._cnt(2)+(1:size(x, 2)); - + if !isa(x, 'char'), + indj = df._cnt(2)+(1:size(x, 2)); + else + %# at this point, reading some filename failed + error("dataframe: can't open '%s' for reading data", x); + endif; + if iscell(x), if 2 == length(x), %# use the intermediate value as destination column @@ -280,7 +298,7 @@ %# allow overwriting of column names df._over{2}(1, indj) = true; else - if !isempty(indj), + if !isempty(indj), if length(df._name{2}) < indj(1) || isempty(df._name{2}(indj)), [df._name{2}(indj, 1), df._over{2}(1, indj)] ... = df_colnames(inputname(indi), indj); @@ -315,14 +333,24 @@ if any([index(base, "=")]), %# takes the left part as base x = strsplit(base, "="); - x = deblank(x{1}); y = false; - elseif any([index(base, '''')]), - %# base is most probably a filename - x = regexp(base, '[^''].*[^'']', 'match'){1}; y = true; - elseif any([index(base, "(") index(base, ":")]), - x = 'X'; y = true; %# this is a default value, may be changed + x = deblank(x{1}); + if isvarname(x), + y = false; + else + x = 'X'; y = true; + endif else - x = base; y = false; + %# is base most probably a filename ? + x = regexp(base, '''[^''].*[^'']''', 'match'); + if isempty(x), + if isvarname(base), + x = base; y = false; + else + x = 'X'; y = true; %# this is a default value, may be changed + endif + else + x = x{1}; y = true; + endif endif if numel(num) > 1, Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/display.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/display.m 2010-11-22 12:16:18 UTC (rev 7935) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/display.m 2010-11-22 16:48:27 UTC (rev 7936) @@ -83,13 +83,12 @@ %# keep only one horizontal space per line indk = 1; while indk <= size(df._data{indc}, 2), dummy{3, 2+indi} = disp(df._data{indc}(:, indk)); - tmp_str = char(regexp(dummy{3, 2+indi}, ' \S.*', ... + tmp_str = char(regexp(dummy{3, 2+indi}, \ + '[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?(\s??[-+]\s??[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?i)?', \ 'match', 'dotexceptnewline')); - if size(tmp_str, 1) < df._cnt(1), - tmp_str = horzcat... - (vspace, char(regexp(dummy{3, 2+indi}, '\S.*', ... - 'match', 'dotexceptnewline'))); - endif + tmp_str = horzcat... + (vspace, char(regexp(dummy{3, 2+indi}, '\S.*', ... + 'match', 'dotexceptnewline'))); dummy{3, 2+indi} = tmp_str; indi = indi + 1; indk = indk + 1; endwhile Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/isfield.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/isfield.m 2010-11-22 12:16:18 UTC (rev 7935) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/isfield.m 2010-11-22 16:48:27 UTC (rev 7936) @@ -32,7 +32,7 @@ %% Suite 330, Boston, MA 02111-1307, USA. %# - %# $Id: dataframe.m 7931 2010-11-17 17:07:37Z cdemills $ + %# $Id$ %# if !isa(df, 'dataframe'), Property changes on: trunk/octave-forge/extra/dataframe/inst/@dataframe/isfield.m ___________________________________________________________________ Added: svn:keywords + Id Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m 2010-11-22 12:16:18 UTC (rev 7935) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m 2010-11-22 16:48:27 UTC (rev 7936) @@ -189,7 +189,7 @@ else if isvector(RHS), if 0 == df._cnt(1), - nrow = length(RHS); %# try to produce row vectors + nrow = size(RHS, 1); else nrow = df._cnt(1); %# limit to df numbner of rows endif @@ -308,7 +308,7 @@ endif endif endif - + %# perform row resizing if columns are already filled if !isempty(indr) && isnumeric(indr), if max(indr) > df._cnt(1) && size(df._data, 2) == df._cnt(2), @@ -453,8 +453,9 @@ else %# ignore 'column' dimension -- force colum vectors -- use a %# third dim just in case + if isempty(S.subs{1}), S.subs{1} = ':'; endif S.subs(2) = []; - if length(S.subs) < 2, S.subs(2) = 1; endif + if length(S.subs) < 2, S.subs{2} = 1; endif if length(indc) > 1 && length(RHS) > 1, %# set a row from a vector fillfunc = @(x, y) builtin('subsasgn', x, S, RHS(y)); @@ -463,7 +464,11 @@ endif endif for indi = 1:length(indc), - df._data{indc(indi)} = fillfunc(df._data{indc(indi)}, indi); + try + df._data{indc(indi)} = fillfunc(df._data{indc(indi)}, indi); + catch + disp('line 470 '); keyboard + end_try_catch # catch # if ndims(df._data{indc(indi)}) > 2, # %# upstream forgot to give the third dim Added: trunk/octave-forge/extra/dataframe/inst/@dataframe/sum.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/sum.m (rev 0) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/sum.m 2010-11-22 16:48:27 UTC (rev 7936) @@ -0,0 +1,111 @@ +function resu = sum(df, varargin) + + %# -*- texinfo -*- + %# @deftypefn {Function File} {} sum (@var{x}) + %# @deftypefnx {Function File} {} sum (@var{x}, @var{dim}) + %# @deftypefnx {Function File} {} sum (@dots{}, 'native') + %# @deftypefnx {Function File} {} sum (@dots{}, 'double') + %# @deftypefnx {Function File} {} sum (@dots{}, 'extra') + %# Sum of elements along dimension @var{dim}. If @var{dim} is + %# omitted, it defaults to the first non-singleton dimension. + %# + %# If the optional argument 'native' is given, then the sum is performed + %# in the same type as the original argument, rather than in the default + %# double type. For example: + %# + %# @example + %# @group + %# sum ([true, true]) + %# @result{} 2 + %# sum ([true, true], 'native') + %# @result{} true + %# @end group + %# @end example + %# + %# On the contrary, if 'double' is given, the sum is performed in double + %# precision even for single precision inputs. + %# + %# For double precision inputs, 'extra' indicates that a more accurate algorithm + %# than straightforward summation is to be used. For single precision inputs, + %# 'extra' is the same as 'double'. Otherwise, 'extra' has no effect.\n\ + %# @end deftypefn + + %% Copyright (C) 2009-2010 Pascal Dupuis <Pas...@uc...> + %% + %% This file is part of Octave. + %% + %% Octave is free software; you can redistribute it and/or + %% modify it under the terms of the GNU General Public + %% License as published by the Free Software Foundation; + %% either version 2, or (at your option) any later version. + %% + %% Octave is distributed in the hope that it will be useful, + %% but WITHOUT ANY WARRANTY; without even the implied + %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + %% PURPOSE. See the GNU General Public License for more + %% details. + %% + %% You should have received a copy of the GNU General Public + %% License along with Octave; see the file COPYING. If not, + %% write to the Free Software Foundation, 59 Temple Place - + %% Suite 330, Boston, MA 02111-1307, USA. + + %# + %# $Id$ + %# + + + + if !isa(df, 'dataframe'), + resu = []; return; + endif + + dim = []; type_comp = []; + + indi = 1; while indi <= length(varargin) + if isnumeric(varargin{indi}), + if !isempty(dim), + print_usage(); + resu = []; + return + else + dim = varargin{indi}; + endif + else + if !isempty(type_comp), + print_usage(); + resu = []; + return + else + type_comp = varargin{indi}; + endif + endif + endwhile; + + if isempty(dim), dim = 1; endif; + if isempty(type_comp), type_comp = 'double'; endif + + %# pre-assignation + resu = struct(df); + + switch(dim) + case {1}, + resu._ridx = 1; resu._name{1, 1} = []; resu._over{1, 1} = []; + for indi = 1:resu._cnt(2), + resu._data{1, indi} = sum(resu._data{1, indi}, dim, type_comp); + endfor + resu._cnt(1) = 1; + case {2}, + error('Operation not implemented'); + case {3}, + for indi = 1:resu._cnt(2), + resu._data{1, indi} = sum(resu._data{1, indi}, 2, type_comp) + endfor + if length(resu._cnt) > 2, resu._cnt = resu._cnt(1:2); endif + otherwise + error("Invalid dimension %d", dim); + endswitch + + resu = dataframe(resu); + +endfunction Property changes on: trunk/octave-forge/extra/dataframe/inst/@dataframe/sum.m ___________________________________________________________________ Added: svn:keywords + Id This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |