save a plot in png, eps, and fig formats

| categories: matlab, octave, workflow | View Comments

When you save a figure, what you really want to save is - several formats, a fig file, all relevant data that is needed to reconstruct the fig, and a README that tells you what is there. That's the purpose of my savefigs :

 1: % purpose : save a figure in png,eps, and fig formats
 2: % syntax : savefigs(filename,readme_text,data_str)
 3: % filename - file name without any suffix (savefigs does not check this, so
 4: % if you mistakenly set filename="stam.fig", the output files will 
 5: % be stam.fig.fig, stam.fig.eps, stam.fig.png)
 6: % readme_text - a string that describes the figure, and the data.
 7: % data_str - a data structure that contains all needed info in
 8: % order to reconstruct the figure
 9: % 
10: % see also: print, hgsave
11: 
12: 
13: % Copyright 2013 Avi Gozolchiani (http://tiny.cc/avigoz)
14: % This program is free software: you can redistribute it and/or modify
15: % it under the terms of the GNU General Public License as published by
16: % the Free Software Foundation, either version 3 of the License, or
17: % (at your option) any later version.
18: %
19: % This program is distributed in the hope that it will be useful,
20: % but WITHOUT ANY WARRANTY; without even the implied warranty of
21: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22: % GNU General Public License for more details.
23: %
24: % You should have received a copy of the GNU General Public License
25: % along with this program.  If not, see <http://www.gnu.org/licenses/>.
26: 
27: % $Log$
28: function savefigs(filename,readme_text,data_str)
29: isoctave=(exist('OCTAVE_VERSION','builtin')~=0);
30: if(isoctave)
31:     prints=struct('suff',{'png','eps'},...
32:                   'func',{@(x)print('-dpng',x),@(x)print('-depsc2',x)});    
33: else
34:     prints=struct('suff',{'png','eps','fig'},...
35:                   'func',{@(x)print('-dpng',x),@(x)print('-depsc2',x), ...
36:                         @hgsave});
37: end
38: n_printfuncs=length(prints);
39: for i_printfunc=1:n_printfuncs % fig,png, and eps files
40:     prints(i_printfunc).func([filename,'.',prints(i_printfunc).suff]);
41: end % for i_printfunc=1:n_printfuncs
42: % document what is it
43: fid=fopen([filename,'_README','.txt'],'wt');
44: fprintf(fid,'%s',readme_text);
45: fclose(fid);
46: % save the vector/matrix for future crunching
47: save([filename,'_data','.mat'],'data_str');

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

Screen - unique logs for each run

| categories: workflow, linux | View Comments

Screen is a little wrap around linux shell that enables detaching and logging out while the session you created is still running. It could be used for lots of different purposes, and running an intensive computation on a remote computer is an obvious example.

You would normally do :

1: screen -md -L -S session_name your_program

-md = detach immediately after running, and return to the current terminal session -L = create a log file -S = create meaningful name for your session

To check the stat of your sessions you will use:

1: screen -ls

To have different log files with unique names for different sessions, you need to create a ~/.screenrc file, with the following single line

logfile screenlog-%Y%m%d-%c:%s

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

linux copy to clipboard

| categories: alias, workflow, osx, linux | View Comments

Is it true that you have to give names to things to really appreciate and understand them? It's really one of these questions that are just too gross for answering a straight yes or no.

When you think of this question, math comes to mind. I have little doubt that you must internalize dozens of definitions and the relations between them before you master differential geometry, or group theory. But do you need language to understand subtraction? The answer is - NO . Babies do it intuitively. So there is some border beyond which things become too abstract, and we've got to give them names. But isn't my answer a tautology? Isn't "abstract" just the name of this phenomena of having to name something in order to understand it? Sure enough, if we were not so used to giving names to everything, we would have found lots of things "abstract". Helen Keller writes :

"my teacher placed my hand under the spout. As the cool stream gushed over one hand she spelled into the other the word water, first slowly, then rapidly. I stood still, my whole attention fixed upon the motions of her fingers. Suddenly I felt a misty consciousness as of something forgotten - a thrill of returning thought; and somehow the mystery of language was revealed to me. I knew then that "w-a-t-e-r" meant the wonderful cool something that was flowing over my hand"

Most of us don't think of the notion of water as abstract, but it's just a matter of experience.

SO… It didn't occur to me that I need to copy linux outputs to the clipboard, until I found out about xclip (keep your comments about the long intro to yourself, by the way…). Now that I know about it, I also care about cases where I want the trailing '\n', and cases where I don't.

Here are my aliases for linux:

1: alias xc='xclip -selection clipboard'
2: alias xcn='perl -ne "chomp();print" |xclip -selection clipboard'

And here they are for mac osx :

1: alias xc="pbcopy"
2: alias xcn="tr -d '\n' | pbcopy"

The two approaches for removing the newlines work equivalently on both systems.

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

create a document from your figures

| categories: latex, workflow | View Comments

A part of the scientific workflow is creating images and categorizing them into directories. In our little parties, we scientist show these images to each other and brag about our ability to create more. It is therefore very useful to have bundles of these in pdf or html files (depending on the kind of party).

Here's how to create a pdf (using LaTeX) :

1: #!/usr/bin/perl -nw 
2: ## syntax : ls fig_patterns | latexfigs.pl > latexfile
3: chomp();
4: print "\\begin\{figure\}\n\\centering\n\\includegraphics\[scale=1.2,angle=0\]\{$_\}\n";
5: s/_/\\_/g;
6: print "\\caption\{$_\}\n\\end\{figure\}\n\\clearpage\n";

and Here's how to create a html :

1: #!/usr/bin/perl -nw 
2: ## syntax : ls fig_patterns | htmlfigs.pl > htmlfile
3: chomp();
4: print "<IMG src=\"$_\" width=650><BR>\n";
5: print "$_<BR><BR>\n";

After some time, you may want to make a section in your book/paper from each directory.

here's the LaTeX version :

1: #!/usr/bin/perl -w 
2: ## syntax : anchor_latex.pl "tag" "text" >> file.latex
3: $tag=shift or die "syntax error: anchor_latex.pl \"tag\" \"text\">>file.latex\n";
4: $text=shift or die "syntax error: anchor_latex.pl \"tag\" \"text\">>file.latex\n";
5: print "\\section{$text}\\label{sec:$tag}\n";

and here's the html :

1: #!/usr/bin/perl -w 
2: ## syntax : anchor_html.pl "tag" "text" >> file.html
3: $tag=shift or die "syntax error: anchor_html.pl \"tag\" \"text\">>file.html\n";
4: $text=shift or die "syntax error: anchor_html.pl \"tag\" \"text\">>file.html\n";
5: print "<a id=\"$tag\"><h2>$text</h2></a>\n";

you can include a template for a latexfile in your home directory :

 1: \documentclass[A4paper]{article}
 2: \usepackage{graphicx}
 3: \usepackage{cite}
 4: \usepackage{placeins} % floatbarrier definition
 5: \usepackage[caption=false]{subfig}
 6: \usepackage{fullpage}
 7: \newcommand{\unit}[1]{\ensuremath{\, \mathrm{#1}}}
 8: \begin{document}
 9: TEXT
10: \end{document}

and substitute your created latex code into the TEXT part, using perl again :

 1: #!/usr/bin/perl -w 
 2: # syntax : merge_latex_tmpl.pl tmpfile > merged_file.tex
 3: $tmplfilename=$ENV{'LATEXTMPL'};
 4: $filename=shift // die "syntax error";
 5: open TMPL,"<$tmplfilename" // die "could not find the template file";
 6: open FILE,"<$filename" // die "could not find the file $filename";
 7: $uniq_content = join("", <FILE>); 
 8: while(<TMPL>){
 9:     if(/TEXT/){
10:         print $uniq_content;
11:     }else{
12:         print;
13:     }
14: }

where LATEXTMPL is an environment variable, telling your script the location of your template. I like templates, and I clutter quite a bit as hidden files in my home directory. Do you do it differently ?

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments