[Avida-SVN] r3146 - in development/support: . math
dk at myxo.css.msu.edu
dk at myxo.css.msu.edu
Fri Feb 6 10:38:12 PST 2009
Author: dk
Date: 2009-02-06 13:38:12 -0500 (Fri, 06 Feb 2009)
New Revision: 3146
Added:
development/support/math/
development/support/math/README
development/support/math/exportfig.m
development/support/math/find_files.m
development/support/math/header_lines.m
development/support/math/load_files.m
development/support/math/newfigure.m
development/support/math/quick_export.m
development/support/math/quick_load.m
development/support/math/stderr.m
Log:
By popular demand, checking in a few matlab scripts to help with the analysis of Avida data files.
Added: development/support/math/README
===================================================================
--- development/support/math/README (rev 0)
+++ development/support/math/README 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,37 @@
+======== Introduction
+
+This directory contains a bunch of matlab scripts that help with analyzing
+Avida data files. Many of these scripts assume unix/os x (sorry).
+
+Most of these scripts have comments to help decipher how they work, try, for example "help quick_load" from Matlab's command window. It would probably be useful to set Matlab's path to point to this directory.
+
+Briefly, here's what each script here does:
+
+exportfig.m: low-level function to export a figure in camera-ready eps format
+find_files.m: recursive search for files
+header_lines.m: retrieve the header lines from an Avida data file
+load_files.m: low-level function to load data files into an array
+newfigure.m: wrapper around figure that makes figures look nice
+quick_load.m: load a series of data files into a structure for analysis
+quick_export.m: a wrapper around exportfig that makes figures look nice
+stderr.m: calculate the standard error of an array
+
+======== How to use quick_load
+
+Quick_load is a Matlab script that I've found useful for dealing with the types of data files that Avida generates (gzip'ed, data from multiple trials, etc.).
+
+In most cases, quick_load can be called with a regular expression that matches the files of interest. For example, the following command loads all files below the current working directory that match the string "bcast_.*deme_compete".
+
+d = quick_load('"bcast_.*deme_compete"');
+
+NOTE THE USE OF MULTIPLE QUOTES!!! One of the nice feature of quick_load is that it doesn't care whether the data files are gzip'ed or not.
+
+The return value from this command looks like this:
+d =
+ filenames: {1x30 cell}
+ fieldnames: {3x1 cell}
+ update: [30x500 double]
+ avgfit: [30x500 double]
+ maxfit: [30x500 double]
+
+If all the header lines of the data file end with [\w+], then quick_load will unpack the array into fieldnames as well.
Added: development/support/math/exportfig.m
===================================================================
--- development/support/math/exportfig.m (rev 0)
+++ development/support/math/exportfig.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,500 @@
+function exportfig(varargin)
+%EXPORTFIG Export a figure to Encapsulated Postscript.
+% EXPORTFIG(H, FILENAME) writes the figure H to FILENAME. H is
+% a figure handle and FILENAME is a string that specifies the
+% name of the output file.
+%
+% EXPORTFIG(...,PARAM1,VAL1,PARAM2,VAL2,...) specifies
+% parameters that control various characteristics of the output
+% file.
+%
+% Format Paramter:
+% 'Format' one of the strings 'eps','eps2','jpeg','png','preview'
+% specifies the output format. Defaults to 'eps'.
+% The output format 'preview' does not generate an output
+% file but instead creates a new figure window with a
+% preview of the exported figure. In this case the
+% FILENAME parameter is ignored.
+%
+% 'Preview' one of the strings 'none', 'tiff'
+% specifies a preview for EPS files. Defaults to 'none'.
+%
+% Size Parameters:
+% 'Width' a positive scalar
+% specifies the width in the figure's PaperUnits
+% 'Height' a positive scalar
+% specifies the height in the figure's PaperUnits
+%
+% Specifying only one dimension sets the other dimension
+% so that the exported aspect ratio is the same as the
+% figure's current aspect ratio.
+% If neither dimension is specified the size defaults to
+% the width and height from the figure's PaperPosition.
+%
+% Rendering Parameters:
+% 'Color' one of the strings 'bw', 'gray', 'cmyk'
+% 'bw' specifies that lines and text are exported in
+% black and all other objects in grayscale
+% 'gray' specifies that all objects are exported in grayscale
+% 'cmyk' specifies that all objects are exported in color
+% using the CMYK color space
+% 'Renderer' one of the strings 'painters', 'zbuffer', 'opengl'
+% specifies the renderer to use
+% 'Resolution' a positive scalar
+% specifies the resolution in dots-per-inch.
+%
+% The default color setting is 'bw'.
+%
+% Font Parameters:
+% 'FontMode' one of the strings 'scaled', 'fixed'
+% 'FontSize' a positive scalar
+% in 'scaled' mode multiplies with the font size of each
+% text object to obtain the exported font size
+% in 'fixed' mode specifies the font size of all text
+% objects in points
+% 'FontEncoding' one of the strings 'latin1', 'adobe'
+% specifies the character encoding of the font
+%
+% If FontMode is 'scaled' but FontSize is not specified then a
+% scaling factor is computed from the ratio of the size of the
+% exported figure to the size of the actual figure. The minimum
+% font size allowed after scaling is 5 points.
+% If FontMode is 'fixed' but FontSize is not specified then the
+% exported font sizes of all text objects is 7 points.
+%
+% The default 'FontMode' setting is 'scaled'.
+%
+% Line Width Parameters:
+% 'LineMode' one of the strings 'scaled', 'fixed'
+% 'LineWidth' a positive scalar
+% the semantics of LineMode and LineWidth are exactly the
+% same as FontMode and FontSize, except that they apply
+% to line widths instead of font sizes. The minumum line
+% width allowed after scaling is 0.5 points.
+% If LineMode is 'fixed' but LineWidth is not specified
+% then the exported line width of all line objects is 1
+% point.
+%
+% Examples:
+% exportfig(gcf,'fig1.eps','height',3);
+% Exports the current figure to the file named 'fig1.eps' with
+% a height of 3 inches (assuming the figure's PaperUnits is
+% inches) and an aspect ratio the same as the figure's aspect
+% ratio on screen.
+%
+% exportfig(gcf, 'fig2.eps', 'FontMode', 'fixed',...
+% 'FontSize', 10, 'color', 'cmyk' );
+% Exports the current figure to 'fig2.eps' in color with all
+% text in 10 point fonts. The size of the exported figure is
+% the figure's PaperPostion width and height.
+
+
+if (nargin < 2)
+ error('Too few input arguments');
+end
+
+% exportfig(H, filename, ...)
+H = varargin{1};
+if ~ishandle(H) | ~strcmp(get(H,'type'), 'figure')
+ error('First argument must be a handle to a figure.');
+end
+filename = varargin{2};
+if ~ischar(filename)
+ error('Second argument must be a string.');
+end
+paramPairs = varargin(3:end);
+
+% Do some validity checking on param-value pairs
+if (rem(length(paramPairs),2) ~= 0)
+ error(['Invalid input syntax. Optional parameters and values' ...
+ ' must be in pairs.']);
+end
+
+format = 'eps';
+preview = 'none';
+width = -1;
+height = -1;
+color = 'bw';
+fontsize = -1;
+fontmode='scaled';
+linewidth = -1;
+linemode=[];
+fontencoding = 'latin1';
+renderer = [];
+resolution = [];
+
+% Process param-value pairs
+args = {};
+for k = 1:2:length(paramPairs)
+ param = lower(paramPairs{k});
+ if (~ischar(param))
+ error('Optional parameter names must be strings');
+ end
+ value = paramPairs{k+1};
+
+ switch (param)
+ case 'format'
+ format = value;
+ if (~strcmp(format,{'eps','eps2','jpeg','png','preview'}))
+ error(['Format must be ''eps'', ''eps2'', ''jpeg'', ''png'' or' ...
+ ' ''preview''.']);
+ end
+ case 'preview'
+ preview = value;
+ if (~strcmp(preview,{'none','tiff'}))
+ error('Preview must be ''none'' or ''tiff''.');
+ end
+ case 'width'
+ width = LocalToNum(value);
+ if(~LocalIsPositiveScalar(width))
+ error('Width must be a numeric scalar > 0');
+ end
+ case 'height'
+ height = LocalToNum(value);
+ if(~LocalIsPositiveScalar(height))
+ error('Height must be a numeric scalar > 0');
+ end
+ case 'color'
+ color = lower(value);
+ if (~strcmp(color,{'bw','gray','cmyk'}))
+ error('Color must be ''bw'', ''gray'' or ''cmyk''.');
+ end
+ case 'fontmode'
+ fontmode = lower(value);
+ if (~strcmp(fontmode,{'scaled','fixed'}))
+ error('FontMode must be ''scaled'' or ''fixed''.');
+ end
+ case 'fontsize'
+ fontsize = LocalToNum(value);
+ if(~LocalIsPositiveScalar(fontsize))
+ error('FontSize must be a numeric scalar > 0');
+ end
+ case 'fontencoding'
+ fontencoding = lower(value);
+ if (~strcmp(fontencoding,{'latin1','adobe'}))
+ error('FontEncoding must be ''latin1'' or ''adobe''.');
+ end
+ case 'linemode'
+ linemode = lower(value);
+ if (~strcmp(linemode,{'scaled','fixed'}))
+ error('LineMode must be ''scaled'' or ''fixed''.');
+ end
+ case 'linewidth'
+ linewidth = LocalToNum(value);
+ if(~LocalIsPositiveScalar(linewidth))
+ error('LineWidth must be a numeric scalar > 0');
+ end
+ case 'renderer'
+ renderer = lower(value);
+ if (~strcmp(renderer,{'painters','zbuffer','opengl'}))
+ error('Renderer must be ''painters'', ''zbuffer'' or ''opengl''.');
+ end
+ case 'resolution'
+ resolution = LocalToNum(value);
+ if ~(isnumeric(value) & (prod(size(value)) == 1) & (value >= 0));
+ error('Resolution must be a numeric scalar >= 0');
+ end
+ otherwise
+ error(['Unrecognized option ' param '.']);
+ end
+end
+
+allLines = findall(H, 'type', 'line');
+allText = findall(H, 'type', 'text');
+allAxes = findall(H, 'type', 'axes');
+allImages = findall(H, 'type', 'image');
+allLights = findall(H, 'type', 'light');
+allPatch = findall(H, 'type', 'patch');
+allSurf = findall(H, 'type', 'surface');
+allRect = findall(H, 'type', 'rectangle');
+allFont = [allText; allAxes];
+allColor = [allLines; allText; allAxes; allLights];
+allMarker = [allLines; allPatch; allSurf];
+allEdge = [allPatch; allSurf];
+allCData = [allImages; allPatch; allSurf];
+
+old.objs = {};
+old.prop = {};
+old.values = {};
+
+% Process format and preview parameter
+showPreview = strcmp(format,'preview');
+if showPreview
+ format = 'png';
+ filename = [tempName '.png'];
+end
+if strncmp(format,'eps',3) & ~strcmp(preview,'none')
+ args = {args{:}, ['-' preview]};
+end
+
+hadError = 0;
+try
+ % Process size parameters
+ paperPos = get(H, 'PaperPosition');
+ old = LocalPushOldData(old, H, 'PaperPosition', paperPos);
+ figureUnits = get(H, 'Units');
+ set(H, 'Units', get(H,'PaperUnits'));
+ figurePos = get(H, 'Position');
+ aspectRatio = figurePos(3)/figurePos(4);
+ set(H, 'Units', figureUnits);
+ if (width == -1) & (height == -1)
+ width = paperPos(3);
+ height = paperPos(4);
+ elseif (width == -1)
+ width = height * aspectRatio;
+ elseif (height == -1)
+ height = width / aspectRatio;
+ end
+ set(H, 'PaperPosition', [0 0 width height]);
+ paperPosMode = get(H, 'PaperPositionMode');
+ old = LocalPushOldData(old, H, 'PaperPositionMode', paperPosMode);
+ set(H, 'PaperPositionMode', 'manual');
+
+ % Process rendering parameters
+ switch (color)
+ case {'bw', 'gray'}
+ if ~strcmp(color,'bw') & strncmp(format,'eps',3)
+ format = [format 'c'];
+ end
+ args = {args{:}, ['-d' format]};
+
+ %compute and set gray colormap
+ oldcmap = get(H,'Colormap');
+ newgrays = 0.30*oldcmap(:,1) + 0.59*oldcmap(:,2) + 0.11*oldcmap(:,3);
+ newcmap = [newgrays newgrays newgrays];
+ old = LocalPushOldData(old, H, 'Colormap', oldcmap);
+ set(H, 'Colormap', newcmap);
+
+ %compute and set ColorSpec and CData properties
+ old = LocalUpdateColors(allColor, 'color', old);
+ old = LocalUpdateColors(allAxes, 'xcolor', old);
+ old = LocalUpdateColors(allAxes, 'ycolor', old);
+ old = LocalUpdateColors(allAxes, 'zcolor', old);
+ old = LocalUpdateColors(allMarker, 'MarkerEdgeColor', old);
+ old = LocalUpdateColors(allMarker, 'MarkerFaceColor', old);
+ old = LocalUpdateColors(allEdge, 'EdgeColor', old);
+ old = LocalUpdateColors(allEdge, 'FaceColor', old);
+ old = LocalUpdateColors(allCData, 'CData', old);
+
+ case 'cmyk'
+ if strncmp(format,'eps',3)
+ format = [format 'c'];
+ args = {args{:}, ['-d' format], '-cmyk'};
+ else
+ args = {args{:}, ['-d' format]};
+ end
+ otherwise
+ error('Invalid Color parameter');
+ end
+ if (~isempty(renderer))
+ args = {args{:}, ['-' renderer]};
+ end
+ if (~isempty(resolution)) | ~strncmp(format,'eps',3)
+ if isempty(resolution)
+ resolution = 0;
+ end
+ args = {args{:}, ['-r' int2str(resolution)]};
+ end
+
+ % Process font parameters
+% if (~isempty(fontmode))
+% oldfonts = LocalGetAsCell(allFont,'FontSize');
+% switch (fontmode)
+% case 'fixed'
+% oldfontunits = LocalGetAsCell(allFont,'FontUnits');
+% old = LocalPushOldData(old, allFont, {'FontUnits'}, oldfontunits);
+% set(allFont,'FontUnits','points');
+% if (fontsize == -1)
+% set(allFont,'FontSize',7);
+% else
+% set(allFont,'FontSize',fontsize);
+% end
+% case 'scaled'
+% if (fontsize == -1)
+% wscale = width/figurePos(3);
+% hscale = height/figurePos(4);
+% scale = min(wscale, hscale);
+% else
+% scale = fontsize;
+% end
+% newfonts = LocalScale(oldfonts,scale,5);
+% set(allFont,{'FontSize'},newfonts);
+% otherwise
+% error('Invalid FontMode parameter');
+% end
+% % make sure we push the size after the units
+% old = LocalPushOldData(old, allFont, {'FontSize'}, oldfonts);
+% end
+ if strcmp(fontencoding,'adobe') & strncmp(format,'eps',3)
+ args = {args{:}, '-adobecset'};
+ end
+
+ % Process linewidth parameters
+ if (~isempty(linemode))
+ oldlines = LocalGetAsCell(allMarker,'LineWidth');
+ old = LocalPushOldData(old, allMarker, {'LineWidth'}, oldlines);
+ switch (linemode)
+ case 'fixed'
+ if (linewidth == -1)
+ set(allMarker,'LineWidth',1);
+ else
+ set(allMarker,'LineWidth',linewidth);
+ end
+ case 'scaled'
+ if (linewidth == -1)
+ wscale = width/figurePos(3);
+ hscale = height/figurePos(4);
+ scale = min(wscale, hscale);
+ else
+ scale = linewidth;
+ end
+ newlines = LocalScale(oldlines, scale, 0.5);
+ set(allMarker,{'LineWidth'},newlines);
+ otherwise
+ error('Invalid LineMode parameter');
+ end
+ end
+
+ % Export
+ print(H, filename, args{:});
+
+catch
+ hadError = 1;
+end
+
+% Restore figure settings
+for n=1:length(old.objs)
+ set(old.objs{n}, old.prop{n}, old.values{n});
+end
+
+if hadError
+ error(deblank(lasterr));
+end
+
+% Show preview if requested
+if showPreview
+ X = imread(filename,'png');
+ delete(filename);
+ f = figure( 'Name', 'Preview', ...
+ 'Menubar', 'none', ...
+ 'NumberTitle', 'off', ...
+ 'Visible', 'off');
+ image(X);
+ axis image;
+ ax = findobj(f, 'type', 'axes');
+ set(ax, 'Units', get(H,'PaperUnits'), ...
+ 'Position', [0 0 width height], ...
+ 'Visible', 'off');
+ set(ax, 'Units', 'pixels');
+ axesPos = get(ax,'Position');
+ figPos = get(f,'Position');
+ rootSize = get(0,'ScreenSize');
+ figPos(3:4) = axesPos(3:4);
+ if figPos(1) + figPos(3) > rootSize(3)
+ figPos(1) = rootSize(3) - figPos(3) - 50;
+ end
+ if figPos(2) + figPos(4) > rootSize(4)
+ figPos(2) = rootSize(4) - figPos(4) - 50;
+ end
+ set(f, 'Position',figPos, ...
+ 'Visible', 'on');
+end
+
+%
+% Local Functions
+%
+
+function outData = LocalPushOldData(inData, objs, prop, values)
+outData.objs = {inData.objs{:}, objs};
+outData.prop = {inData.prop{:}, prop};
+outData.values = {inData.values{:}, values};
+
+function cellArray = LocalGetAsCell(fig,prop);
+cellArray = get(fig,prop);
+if (~isempty(cellArray)) & (~iscell(cellArray))
+ cellArray = {cellArray};
+end
+
+function newArray = LocalScale(inArray, scale, minValue)
+n = length(inArray);
+newArray = cell(n,1);
+for k=1:n
+ newArray{k} = max(minValue,scale*inArray{k}(1));
+end
+
+function newArray = LocalMapToGray(inArray);
+n = length(inArray);
+newArray = cell(n,1);
+for k=1:n
+ color = inArray{k};
+ if (~isempty(color))
+ if ischar(color)
+ switch color(1)
+ case 'y'
+ color = [1 1 0];
+ case 'm'
+ color = [1 0 1];
+ case 'c'
+ color = [0 1 1];
+ case 'r'
+ color = [1 0 0];
+ case 'g'
+ color = [0 1 0];
+ case 'b'
+ color = [0 0 1];
+ case 'w'
+ color = [1 1 1];
+ case 'k'
+ color = [0 0 0];
+ otherwise
+ newArray{k} = color;
+ end
+ end
+ if ~ischar(color)
+ color = 0.30*color(1) + 0.59*color(2) + 0.11*color(3);
+ end
+ end
+ if isempty(color) | ischar(color)
+ newArray{k} = color;
+ else
+ newArray{k} = [color color color];
+ end
+end
+
+function newArray = LocalMapCData(inArray);
+n = length(inArray);
+newArray = cell(n,1);
+for k=1:n
+ color = inArray{k};
+ if (ndims(color) == 3) & isa(color,'double')
+ gray = 0.30*color(:,:,1) + 0.59*color(:,:,2) + 0.11*color(:,:,3);
+ color(:,:,1) = gray;
+ color(:,:,2) = gray;
+ color(:,:,3) = gray;
+ end
+ newArray{k} = color;
+end
+
+function outData = LocalUpdateColors(inArray, prop, inData)
+value = LocalGetAsCell(inArray,prop);
+outData.objs = {inData.objs{:}, inArray};
+outData.prop = {inData.prop{:}, {prop}};
+outData.values = {inData.values{:}, value};
+if (~isempty(value))
+ if strcmp(prop,'CData')
+ value = LocalMapCData(value);
+ else
+ value = LocalMapToGray(value);
+ end
+ set(inArray,{prop},value);
+end
+
+function bool = LocalIsPositiveScalar(value)
+bool = isnumeric(value) & ...
+ prod(size(value)) == 1 & ...
+ value > 0;
+
+function value = LocalToNum(value)
+if ischar(value)
+ value = str2num(value);
+end
Added: development/support/math/find_files.m
===================================================================
--- development/support/math/find_files.m (rev 0)
+++ development/support/math/find_files.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,293 @@
+function filenames = find_files(dir, file_pattern)
+%FIND_FILES Recursively finds matching files.
+%
+% FILENAMES = FIND_FILES(DIR,PATTERN) examines the passed in directory,
+% and recursively descends searching for files whose complete name
+% matches the specified pattern.
+% dir(string): The root directory at which recursive search begins.
+% file_pattern(regex): The pattern used to find matching files.
+% filenames(cell array of strings): The completely specified filenames
+% of files that matched the file_pattern, relative to dir.
+
+% For right now, we're going to cheat. No need to reinvent the wheel.
+[s r] = unix(['find ' dir ' | grep ' file_pattern]);
+assert(s==0, 'Invalid dir or file_pattern.');
+filenames = regexp(r,'([\.\w\d-/]*)','tokens');
+
+% sanity checking:
+cellfun(@(x)assert(exist(char(x),'file')==2), filenames);
+
+
+% % DIRR
+% % Lists all files in the current directory and sub directories
+% % recursively.
+% %
+% % [LIST] = DIRR(PATH)
+% % Returns a structure LIST with the same fieldnames as returned
+% % by LIST = DIR(PATH)
+% % PATH can contain wildcards * and ? after the last \ or / (filename
+% % filter)
+% % The content of each directory in PATH is listed inside its 'isdir'
+% % field with the same format. The 'bytes' field is NOT zero but the
+% % sum of all filesizes inside the directory.
+% %
+% % [LIST,BYTES] = DIRR(PATH)
+% % BYTES is a structure with fields 'total' and 'dir'. 'total' is the total
+% % size of PATH. 'dir' is a recursive substructure that contains the
+% % same fields ('total' and 'dir') for the subdirectories.
+% %
+% % [...] = DIRR(PATH,FILTER)
+% % Lists only files matching the string FILTER (non case sensitive
+% % regular expression).
+% % N.B.: FILTER is optional and must not be equal to a fieldname
+% % ('name' or 'date' ... will never be interpreted as filters)
+% %
+% % [LIST,BYTES,FIELDOUT] = DIRR(PATH,FIELDIN, ...)
+% % FIELDIN is a string specifying a field (of the structure LIST) that
+% % will be listed in a separate cell array of strings in FIELDOUT for
+% % every file with absolute path at the begining of the string.
+% % Multiple fields can be specified.
+% %
+% % [LIST,BYTES,FIELDOUT] = DIRR(PATH,FIELDIN,FILTER, ...)
+% % Only files for which FIELDIN matches FILTER will be returned.
+% % Multiple [FIELDIN, FILTER] couples may be specified.
+% % Recursion can be avoided here by setting 'isdir' filter to '0'.
+% % For bytes, numeric comparison will be performed.
+% %
+% %
+% % EXAMPLES :
+% %
+% % DIRR
+% % Lists all files (including path) in the current directory and it's
+% % subdirectories recursively.
+% %
+% % DIRR('c:\matlab6p5\work\*.m')
+% % Lists all M-files in the c:\matlab6p5\work directory and it's
+% % subdirectories recursively.
+% %
+% % Music = DIRR('G:\Ma musique\&Styles\Reggae\Alpha Blondy')
+% % Returns a structure Music very similar to what DIR returns
+% % but containing the information on the files stored in
+% % subdirectories of 'G:\Ma musique\&Styles\Reggae\Alpha Blondy'.
+% % The structure Music is a bit difficult to explore though.
+% % See next examples.
+% %
+% % [Files,Bytes,Names] = DIRR('c:\matlab6p5\toolbox','\.mex\>','name')
+% % Lists all MEX-files in the c:\matlab6p5\toolbox directory in the cell
+% % array of strings Names (including path).
+% % Note the regexp syntax of the filter string.
+% % Bytes is a structure with fields "total" and "dir". total is the
+% % total size of the directory, dir is a recursive substructure with
+% % the same fields as bytes for the subdirectories.
+% %
+% % [Files,Bytes,Names] = DIRR('c:\toto'...
+% % ,'name','bytes','>50000','isdir','0')
+% % Lists all files larger than 50000 bytes NOT recursively.
+% %
+% % [Files,Bytes,Dates] = DIRR('c:\matlab6p5\work','date','2005')
+% % Lists all dates of files from year 2005. (With path in front of
+% % date in the cell array of strings Dates)
+% %
+% %
+% %
+% % v1.02
+% % Maximilien Chaumon
+% % maximilien.chaumon at chups.jussieu.fr
+% % 2006 06 16
+%
+%
+% verbose = 0;
+% % set to 1 to get folders list in command window
+%
+% if nargin == 0
+% chemin = cd;
+% end
+% if nargout == 0
+% dum = varargin;
+% varargin{1} = 'name';
+% varargin = [varargin(1) dum];
+% end
+%
+% fields = {'name' 'date' 'bytes' 'isdir'};
+%
+% if regexp(chemin,'[\*\?]') % if chemin contains any ? or *
+% filt = regexprep(chemin,'.*[\\/](.*\>)','$1');% get filter
+% filt = regexprep(filt,'\.','\.');% in regexp format
+% filt = regexprep(filt,'\*','.*');
+% filt = regexprep(filt,'\?','.');
+% filt = regexprep(filt,'(.*)','\\<$1');
+% chemin = regexprep(chemin,'(.*)[\\/].*\>','$1');% and chemin
+% end
+%
+% if not(isempty(varargin)) % if additional fields were provided after chemin
+% for i = 1:length(fields)
+% if strcmp(varargin{1},fields{i})% if first varargin matches a fieldname,
+% % assume no filter was provided,
+%
+% if not(exist('filt','var'))% or it was in chemin and was set just before
+% filt = '.*';% set it to wildcard
+% break
+%
+% end
+% end
+% end
+% if not(exist('filt','var'))% else
+% filt = varargin{1};% first varargin is the filter
+% varargin(1) = [];
+% end
+% else% if no additional fields were provided and filter was not in chemin
+% if not(exist('filt','var'))
+% filt = '.*';
+% end
+% end
+% % determine which varargin are fieldnames
+% whicharefields = zeros(1,length(varargin));
+% for i = 1:length(varargin)
+% for j = 1:length(fields)
+% if strcmp(varargin{i},fields{j})
+% whicharefields(i) = 1;
+% break
+% end
+% end
+% end
+% % set f2out and f2outfilt
+% f2out = {}; f2outfilt = {};
+% idx = 0;
+% if not(isempty(varargin))
+% for i = 1:length(varargin)
+% if whicharefields(i)
+% idx = idx + 1;
+% f2out{idx} = varargin{i};
+% f2outfilt{idx} = '';
+% else % if nargin{i} is not a fieldname, assume it's a filter
+% f2outfilt{idx} = varargin{i};
+% end
+% end
+% end
+%
+% %%%%%%%%%%%%%%%%%%%% START
+% if verbose
+% disp(chemin);
+% end
+%
+% list = dir(chemin);
+% if isempty(list)
+% disp([chemin ' not found']);
+% if nargout == 0
+% clear list
+% else
+% for i = 1:nargout - 2
+% varargout{i} = [];
+% end
+% sumbytes = 0;
+% end
+% return
+% end
+% % remove . and ..
+% i_file = 1;
+% while i_file <= length(list)
+% if strcmp(list(i_file).name,'.')|strcmp(list(i_file).name,'..')
+% list(i_file) = [];
+% else
+% i_file = i_file + 1;
+% end
+% end
+%
+% % set sumbytes
+% sumbytes = struct('total',0,'dir',{});
+% sumbytes(1).total = 0;
+% i_dir = 0;
+% % and all output fields
+% for i = 1:size(f2out,2)
+% f2out{2,i} = {};
+% end
+% filenames = {};
+% todel = 0;
+% r = 1;
+% for i_out = 1:size(f2out,2)
+% if strcmp(f2out{1,i_out},'isdir')
+% if strcmp(f2outfilt{i_out},'0') % check if no recursion is wanted
+% r = 0;
+% end
+% end
+% end
+%
+% % for each item in list
+% for i_file = 1:length(list)
+% for i_out = 1:size(f2out,2) % for every output field
+% if not(isempty(f2outfilt{i_out}))% if there is a filter
+% if strcmp(f2out{1,i_out},'bytes') % if field is 'bytes'
+% line = [num2str(list(i_file).(f2out{1,i_out})) f2outfilt{i_out} ';']; % compare with filter numerically
+% if eval(line)% if passes the filter
+% continue % continue to next field
+% else
+% todel(end+1) = i_file; % else set to be deleted
+% end
+% elseif not(strcmp(f2out{1,i_out},'isdir'))% if field is 'name' or 'date'
+% if regexpi(list(i_file).(f2out{1,i_out}),f2outfilt{i_out}) % apply filter
+% continue % continue to next field
+% else
+% todel(end+1) = i_file; % else set to be deleted
+% end
+% end
+% end
+% end
+% % once checked for every field's filter
+% if todel(end) == i_file % if one didn't pass,
+% if not(list(i_file).isdir) % and it's not a directory
+% continue % skip this file and continue
+% end
+% else
+% if regexpi(list(i_file).name,filt) % else, check for general filter on filename
+% sumbytes(1).total = sumbytes(1).total + list(i_file).bytes; % sum bytes of that level
+% for i_out = 1:size(f2out,2)% and assign all output fields with the values of that file
+% f2out{2,i_out}{end+1} = [chemin filesep num2str(list(i_file).(f2out{1,i_out}))];
+% end
+% else
+% todel(end+1) = i_file; % else the file will be removed from the list structure
+% end
+% end
+% if list(i_file).isdir % if it's a directory
+% if not(r)
+% continue
+% end
+% i_dir = i_dir + 1;
+% cheminext = strcat(chemin,filesep,list(i_file).name);
+% % get it's content by recursion
+% % write the line to enter eval
+% line = '[list(i_file).isdir,sumbytes.dir(i_dir)';
+% for i_out = 1:size(f2out,2)% with all the requested fields as temporary variables
+% line = [line ',f2outtemp{' num2str(i_out) '}'];
+% end
+% line = [line '] = dirr(cheminext,filt'];
+% for i_out = 1:size(f2out,2)
+% line = [line ',f2out{1,' num2str(i_out) '}'];
+% if f2outfilt{i_out}
+% line = [line ',f2outfilt{' num2str(i_out) '}'];
+% end
+% end
+% line = [line ');'];
+% eval(line);
+%
+% for i_out = 1:size(f2out,2)
+% f2out{2,i_out} = [f2out{2,i_out} f2outtemp{i_out}]; % catenate temporary variables with f2out
+% end
+% % sum bytes
+% sumbytes(1).total = sumbytes(1).total + sumbytes(1).dir(i_dir).total; % that level + the next one
+% list(i_file).bytes = sumbytes(1).dir(i_dir).total; % and set list(i_file).bytes to that value
+% if list(i_file).bytes & todel(end) == i_file
+% todel(end) = [];
+% end
+% end
+% end
+% todel(1) = [];
+% list(todel) = [];
+%
+%
+% for i_out = 1:size(f2out,2)
+% varargout{i_out} = f2out{2,i_out};
+% end
+% if nargout == 0
+% clear list
+% disp(char(f2out{2,1}));
+% end
Added: development/support/math/header_lines.m
===================================================================
--- development/support/math/header_lines.m (rev 0)
+++ development/support/math/header_lines.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,27 @@
+function lines = header_lines(filenames)
+%HEADER_LINES Retrieve header lines from Avida data files.
+%
+% R = HEADER_LINES(dir, file_pattern) examines the first
+% file returned by the given directory and file pattern,
+% and returns the header lines from that file. The search
+% is performed recursively.
+% dir(string): Absolute or relative path.
+% file_pattern(regex): Pattern used to match files.
+%
+% See also: load_all
+
+assert(size(filenames,2) > 0, 'No files to check for header!');
+
+f=char(filenames{1});
+[result status] = unix(['file ' f]);
+
+if regexp(status, 'gzip')
+ [status result] = unix(['gzcat ' f]);
+ lines = textscan(result, '%s','Delimiter','\n');
+else
+ fid = fopen(char(f));
+ lines = textscan(fid, '%s','Delimiter','\n');
+ fclose(fid);
+end
+
+lines = lines{1}(strmatch('#',lines{1}));
Added: development/support/math/load_files.m
===================================================================
--- development/support/math/load_files.m (rev 0)
+++ development/support/math/load_files.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,53 @@
+function [data,filenames] = load_files(filenames,data_pattern)
+%LOAD_ALL Loads multiple data files into a single matrix.
+%
+% LOAD_ALL(filenames,data_pattern) loads data from the specified files
+% according to the data pattern. Automatically handles gzipped files.
+% filesnames(string): Absolute or relative filenames.
+% data_pattern(format): Format used to scan data (see textscan).
+%
+% The expected format of the data files is:
+% # == comment
+% m (rows==data points) x n (columns==measurements)
+%
+% The format of the output matrix is:
+% m (rows==files) x n (columns==data points) x p (pages==measurements)
+%
+% See also: field_names, textscan
+%[files, bytes, names] = dirr(dir,file_pattern,'name');
+
+for i=1:size(filenames,2), j=char(filenames{i});
+ [result status] = unix(['file ' j]);
+ if regexp(status, 'gzip')
+ [status result] = unix(['gzcat ' j]);
+ x{i} = textscan(result, data_pattern, 'CommentStyle', '#');
+ else
+ fid = fopen(char(j));
+ x{i} = textscan(fid, data_pattern, 'CommentStyle', '#');
+ fclose(fid);
+ end
+end
+
+m=size(x,2); % each row belongs to a different trial.
+n=size(x{1}{1},1); % each column is a different data point.
+p=size(x{1},2); % each page is a different variable.
+
+data=zeros(m,n,p);
+
+% for each datafile (rows)
+row=1;
+for i=1:m
+ % if the datafile has a different number of columns
+ if size(x{i}{1},1) ~= n
+ % remove a row from the returned data
+ data(end,:,:)=[];
+ % and complain:
+ disp(['Warning: killing row ' int2str(i) ' from ' char(filenames{i})]);
+ else
+ % otherwise, copy all the data from that datafile
+ for j=1:p;
+ data(row,:,j)=x{i}{j};
+ end
+ row=row+1;
+ end
+end
\ No newline at end of file
Added: development/support/math/newfigure.m
===================================================================
--- development/support/math/newfigure.m (rev 0)
+++ development/support/math/newfigure.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,10 @@
+function [f a] = newfigure()
+%NEWFIGURE Create a new figure, and return a handle to the figure and
+% its axis.
+f = figure('XVisual',...
+ '0x22 (TrueColor, depth 24, RGB mask 0xff0000 0xff00 0x00ff)',...
+ 'InvertHardcopy','off',...
+ 'Color',[1 1 1]);
+a = axes('Parent',f,'Position',[0.13 0.1952 0.7771 0.7229]);
+box('off');
+hold('all');
\ No newline at end of file
Added: development/support/math/quick_export.m
===================================================================
--- development/support/math/quick_export.m (rev 0)
+++ development/support/math/quick_export.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,9 @@
+function quick_export(filename)
+%QUICK_EXPORT Export the current figure (gcf) to the specified file.
+%
+% QUICK_EXPORT(filename) exports the current figure (gcf) to the
+% specified file in preferred publication-ready format.
+exportfig(gcf, filename,...
+ 'Color','cmyk',...
+ 'Width', 6, 'Height', 3.75,...
+ 'FontMode', 'fixed', 'FontSize', 9);
Added: development/support/math/quick_load.m
===================================================================
--- development/support/math/quick_load.m (rev 0)
+++ development/support/math/quick_load.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,92 @@
+function R = quick_load(file_pattern, dir, data_pattern, fieldnames)
+%QUICK_LOAD Intelligently load the specified Avida data files.
+%
+% R = QUICK_LOAD(DIR,FILE_PATTERN) examines the files returned
+% by the given directory and file pattern, and returns a struct
+% containing filenames, field names, updates, and data from
+% each file returned. The directory is searched recursively.
+% Gzip'ed files are decompressed on-the-fly.
+% dir(string): Absolute or relative path.
+% file_pattern(regex): Pattern used to match files.
+% R(struct): See below.
+%
+% RETURNS: A struct with one of two formats depending on the
+% data files that are being loaded.
+%
+% Format 1 (preferred): This format is used if each of the
+% header lines in the data files contains a naming tag. The
+% naming tag, which must appear at the end of the header, has
+% the following (regex) format: '.*\[(\w+)\]$'. When each
+% header has such a naming tag, the returned struct R has the
+% following format:
+% R.updates(1 x n double array): Updates - Avida time.
+% R.filenames(cell array of chars): Names of the loaded files.
+% R.fieldnames(cell array of chars): Header lines from files.
+% R.<tag>(m x n matrix of doubles): Rows are data from different
+% files, columns are values at different updates. The <tag>
+% specifies the measurement (column from the data files).
+%
+% Example: If the following header lines occur in each Avida
+% data file:
+% # 1: updates
+% # 2: mean value of foo [foo]
+% # 3: max value of bar [bar]
+% Then the following struct will be returned:
+% R.updates = [1...]
+% R.fieldnames = {{'# 1: mean value of foo [foo]'},
+% {'# 2: max value of bar [bar]'}}
+% R.filenames = {filename1, filename2...}
+% R.foo = [[foo from file1]; [foo from file2]...]
+% R.bar = [[bar from file1]; [bar from file2]...]
+%
+% Format 2: This format is used if the header lines in the
+% data files do not have a naming tag.
+% R.filenames(cell array of chars): Names of the loaded files.
+% R.fieldnames(cell array of chars): Header lines from files.
+% R.data(m x n x p matrix of doubles): Rows are data from
+% different files, columns are values at different updates,
+% pages are different measurements (column from the data
+% files).
+%
+% Example: If the following header lines occur in at least one
+% Avida data file:
+% # 1: updates
+% # 2: mean value of foo
+% # 3: max value of bar
+% Then the following struct will be returned:
+% R.fieldnames = {{'# 1: mean value of foo'},
+% {'# 2: max value of bar'}}
+% R.filenames = {filename1, filename2...}
+% R.data(:,:,1) = [[updates from file1]; [updates from file2]...]
+% R.data(:,:,2) = [[foo from file1]; [foo from file2]...]
+% R.data(:,:,3) = [[bar from file1]; [bar from file2]...]
+%
+% See also: header_lines, load_all
+
+if nargin < 2
+ dir = './';
+end
+
+filenames = find_files(dir, file_pattern);
+lines = header_lines(filenames);
+lines = lines(~cellfun('isempty',regexp(lines,'^#\s+\d+:\s')));
+
+if nargin <= 2
+ data_pattern = arrayfun(@(x){'%n'},lines);
+ data_pattern = cat(2,data_pattern{:});
+end
+
+[ldata fnames] = load_files(filenames, data_pattern);
+
+tokens=regexp(lines,'.*\[(\w+)\]$','tokens');
+if isempty(tokens) || any(cellfun('isempty',tokens))
+ R.filenames=fnames;
+ R.fieldnames=lines(1:end);
+ R.data=ldata;
+else
+ R.filenames=fnames;
+ R.fieldnames=lines;
+ for i=1:size(ldata,3);
+ R.(char(tokens{i}{:})) = ldata(:,:,i);
+ end
+end
Added: development/support/math/stderr.m
===================================================================
--- development/support/math/stderr.m (rev 0)
+++ development/support/math/stderr.m 2009-02-06 18:38:12 UTC (rev 3146)
@@ -0,0 +1,5 @@
+function r = stderr(x)
+%STDERR Calculate the standard error of the given array.
+%
+% Standard error calculated as sqrt(variance/size).
+r = sqrt(var(x)/size(x,1));
\ No newline at end of file
More information about the Avida-cvs
mailing list