Debian Codegraph @

lib/Ifeffit/ArtemisLog.pm

raw
1 #! /usr/bin/perl -w
2 package Ifeffit::ArtemisLog;
3 ######################################################################
4 ## Ifeffit::ArtemisLog: Object oriented Artemis log file parsing
5 ##
6 ## Artemis is copyright (c) 2001-2010 Bruce Ravel
7 ## [email protected]
8 ## http://feff.phys.washington.edu/~ravel/
9 ##
10 ## Ifeffit is copyright (c) 1992-2010 Matt Newville
11 ## [email protected]
12 ## http://cars.uchicago.edu/~newville/ifeffit/
13 ##
14 ## The latest version of Artemis can always be found at
15 ## http://feff.phys.washington.edu/~ravel/software/exafs
16 ##
17 ## -------------------------------------------------------------------
18 ## All rights reserved. This program is free software; you can
19 ## redistribute it and/or modify it provided that the above notice
20 ## of copyright, these terms of use, and the disclaimer of
21 ## warranty below appear in the source code and documentation, and
22 ## that none of the names of The Naval Research Laboratory, The
23 ## University of Chicago, University of Washington, or the authors
24 ## appear in advertising or endorsement of works derived from this
25 ## software without specific prior written permission from all
26 ## parties.
27 ##
28 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30 ## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 ## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32 ## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33 ## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 ## OTHER DEALINGS IN THIS SOFTWARE.
36 ## -------------------------------------------------------------------
37 ######################################################################
38
39 use strict;
40 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
41
42 require Exporter;
43
44 #@ISA = qw(Exporter AutoLoader);
45 @ISA = qw(Exporter);
46 # Items to export into callers namespace by default. Note: do not export
47 # names by default without a very good reason. Use EXPORT_OK instead.
48 # Do not simply export all your public functions/methods/constants.
49 @EXPORT_OK = qw();
50
51 $VERSION = "0.8.014";
52
53 use Carp;
54 use File::Basename;
55 use File::Spec;
56
57 sub new {
58 my $classname = shift;
59 my $self = {};
60
61 $self->{"info"} = {}; # version, creation_date, os
62 $self->{"stats"} = {}; # chisqr, chinu, cormin, rfactor, nidp, nvar
63 $self->{"bestfit"} = {}; # best fits of guess params
64 $self->{"error"} = {}; # errors in guess params
65 $self->{"initial"} = {}; # initial values of guess params
66 $self->{"bkg"} = {}; # backgroun parameters
67 $self->{"bkgerr"} = {}; # errors in background parameters
68 $self->{"correlation"} = {}; # correlations between guess params
69 $self->{"def"} = {}; # final values for def params
70 $self->{"restraint"} = {}; # final values for restraints
71 $self->{"restraint_expr"} = {}; # math expression for restraints
72 $self->{"set"} = {}; # values for set params
73 $self->{"after"} = {}; # values for after-fit params
74 $self->{"after_expr"} = {}; # math expressions for after-fit params
75 #$self->{"delset"} = {}; # propagated errors in set params
76 $self->{"chifiles"} = []; # input chi file names
77 $self->{"warnings"} = []; # warnings found in the log file
78 #$self->{"datasets"} = {}; # attribute values for data sets
79 #$self->{"pathparam"} = {}; # final path param values
80 #$self->{"delpath"} = {}; # path param propagated errors
81 $self->{"order"} = {};
82
83 $self->{order}->{guess} = [];
84 $self->{order}->{set} = [];
85 $self->{order}->{def} = [];
86 $self->{order}->{restraint} = [];
87 $self->{order}->{after} = [];
88 $self->{order}->{correlation} = [];
89 $self->{order}->{bkg} = [];
90
91 bless($self, $classname);
92 ($_[0]) and $self -> read(@_);
93 return $self;
94 };
95
96 sub read {
97 my $self = shift;
98 my $logfile = $_[0];
99 croak "Artemis logfile $logfile is not a readable file" unless (-e $logfile);
100
101 ## ----------------------------------------------------------------
102 ## match operational params and data set specific statistics in the
103 ## data set section
104 my %opparams = (file => 'file',
105 'k-range' => 'krange',
106 dk => 'dk',
107 'k-window' => 'kwin',
108 'k-weight' => 'kw',
109 'R-range' => 'rrange',
110 dR => 'dr',
111 'R-window' => 'rwin',
112 'fitting space' => 'fitspace',
113 'background function' => 'bkg',
114 'spline parameters' => 'nbkg',
115 'phase correction' => 'pcpath',
116 'Chi-square' => 'chisqr',
117 'R-factor for this data set' => 'rfact');
118 my $opregex = "(" . join("|", (sort (keys %opparams))) . ")";
119 ## ----------------------------------------------------------------
120 ## match path parameters
121 my @pathparams = (qw(feff id label s02 reff dr r 3rd ei degen e0
122 ss2 4th dphase));
123 my $ppregexp = '(3rd|4th|d(egen|phase|r)|e[0i]|feff|id|label|s02|r(|eff)|ss2)';
124 ## ----------------------------------------------------------------
125
126 #-#-#--------------------------------------------------------------
127 # begin reading log file
128
129 my $section = 'header';
130 my $data_set = 0;
131 my $current_set = "";
132 my $path = "";
133 my $number_of_data_sets = 0;
134 open(LOG, $logfile) or croak "could not open $logfile as a logfile";
135
136 while (<LOG>) {
137 $_ =~ s{\r}{};
138 chomp;
139 next if (/^\s*$/);
140 WHERE: {
141
142 ## --- a warning section ------------------------
143 (/^\s*!!/) and do {
144 ## ill-defined guesses are marked by >>
145 push @{$self->{warnings}},
146 "The guess parameter $1 did not affect the fit"
147 if (/>>\s*(\w+)/);
148 push @{$self->{warnings}}, "Fitting was not performed."
149 if (/FITTING/);
150 last WHERE;
151 };
152
153 ## --- project information section --------------
154 ($section eq 'header') and do {
155 if (/^\s*=+$/) {
156 $section = 'general';
157 last WHERE;
158 };
159 my @line = split(/\s+:\s+/, $_);
160 $self->{info}{$line[0]} = $line[1] || " ";
161 last WHERE;
162 };
163
164 ## --- fit statistics section --------------
165 ($section eq 'general') and do {
166 ($section = 'guess'), last WHERE if (/^Guess/);
167 GEN: {
168 ($self->{stats}{nidp} = $1), next GEN if (/^Independent points\s+=\s+(\d*\.\d+)/);
169 ($self->{stats}{nvar} = sprintf("%d", $1)), next GEN if (/^Number of variables\s+=\s+(\d*\.\d+)/);
170 ($self->{stats}{chisqr} = $1), next GEN if (/^Chi-square\s+=\s+(\d*\.\d+)/);
171 ($self->{stats}{chinu} = $1), next GEN if (/^Reduced Chi-square\s+=\s+(\d*\.\d+)/);
172 ($self->{stats}{rfact} = $1), next GEN if (/^R-factor\s+=\s+(\d*\.\d+)/);
173 ($self->{stats}{epsk} = $1), next GEN if (/^Measurement uncertainty \(k\)\s+=\s+(\d*\.\d+)/);
174 ($self->{stats}{epsr} = $1), next GEN if (/^Measurement uncertainty \(R\)\s+=\s+(\d*\.\d+)/);
175 ($self->{stats}{ndata} = sprintf("%d", $1)), next GEN if (/^Number of data sets\s+=\s+(\d*\.\d+)/);
176 }
177 last WHERE;
178 };
179
180 ## --- guess parameters section --------------
181 ($section eq 'guess') and do {
182 if (/^Def/) { # recognize next section
183 $self->{stats}{pathindex} = "";
184 if (/\(using \"(.*)\"/) { # snarf ifeffit's current path
185 $self->{stats}{pathindex} = $1;
186 };
187 ($section = 'def');
188 last WHERE;
189 };
190 ($section = 'restraint'), last WHERE if (/^Restraints/);
191 ($section = 'bkg'), last WHERE if (/^Background/);
192 ($section = 'set'), last WHERE if (/^Set/);
193 ($section = 'after'), last WHERE if (/^After/);
194 ($section = 'corr'), last WHERE if (/^Correlations/);
195 if (/^=+ Data set >>(.+)<</) {
196 ($section = 'dataset');
197 ++$data_set;
198 $current_set = $1;
199 push @{$self->{"chifiles"}}, $current_set;
200 $self->{$current_set}->{titles} = [];
201 $self->{$current_set}->{paths} = [];
202 last WHERE;
203 };
204 if (/^\s+(\w+)\s+=\s+(-?\d*\.\d+)\s+\+\/\-\s+(\d*\.\d+)\s+\(guessed as ([\-\*\+\/a-zA-Z_0-9. ()]+)\)/) {
205 $self->{bestfit}{$1} = $2;
206 $self->{error}{$1} = $3;
207 $self->{initial}{$1} = $4;
208 push @{ $self->{order}->{guess} }, $1;
209 } elsif (/^\s+(\w+)\s+=\s+(-?\d*\.\d+)\s+\+\/\-\s+(\d*\.\d+)\s+\((-?\d*(\.\d+|))\)/) {
210 $self->{bestfit}{$1} = $2;
211 $self->{error}{$1} = $3;
212 $self->{initial}{$1} = $4;
213 push @{ $self->{order}->{guess} }, $1;
214 };
215 last WHERE;
216 };
217
218 ## --- the background parameters section --------------
219 ($section eq 'bkg') and do {
220 if (/^Def/) { # recognize next section
221 $self->{stats}{pathindex} = "";
222 if (/\(using \"(.*)\"/) { # snarf ifeffit's current path
223 $self->{stats}{pathindex} = $1;
224 };
225 ## fill up correlations hash with zeros, that way all
226 ## possible correlations are defined.
227 foreach my $x (sort (keys %{$self->{bestfit}})) {
228 foreach my $y (sort (keys %{$self->{bestfit}})) {
229 last if ($x eq $y);
230 my $key = join(":", $x, $y);
231 $self->{corr}->{$key} = 0;
232 };
233 };
234 ($section = 'def');
235 last WHERE;
236 };
237 ($section = 'restraint'), last WHERE if (/^Restraints/);
238 ($section = 'set'), last WHERE if (/^Set/);
239 ($section = 'after'), last WHERE if (/^After/);
240 ($section = 'corr'), last WHERE if (/^Correlations/);
241 if (/^\s+(\w+)\s+=\s+(-?\d*\.\d+)\s+\+\/\-\s+(\d*\.\d+)/) {
242 $self->{bkg}{$1} = $2;
243 $self->{bkgerr}{$1} = $3;
244 push @{ $self->{order}->{bkg} }, $1;
245 };
246 last WHERE;
247 };
248
249 ## --- the def parameters section --------------
250 ($section eq 'def') and do {
251 ($section = 'restraint'), last WHERE if (/^Restraints/);
252 ($section = 'set'), last WHERE if (/^Set/);
253 ($section = 'after'), last WHERE if (/^After/);
254 ($section = 'bkg'), last WHERE if (/^Background/);
255 ($section = 'corr'), last WHERE if (/^Correlations/);
256 if (/^=+ Data set >>(.+)<</) {
257 ($section = 'dataset');
258 ++$data_set;
259 $current_set = $1;
260 push @{$self->{"chifiles"}}, $current_set;
261 $self->{$current_set}->{titles} = [];
262 $self->{$current_set}->{paths} = [];
263 last WHERE;
264 };
265 ($self->{def}{$1} = $2) if (/^\s+(\w+)\s+=\s+(-?\d*(\.\d+|))/);
266 push @{ $self->{order}->{def} }, $1;
267 last WHERE;
268 };
269
270 ## --- the restraint section -------------------
271 ($section eq 'restraint') and do {
272 ($section = 'set'), last WHERE if (/^Set/);
273 ($section = 'after'), last WHERE if (/^After/);
274 ($section = 'bkg'), last WHERE if (/^Background/);
275 ($section = 'corr'), last WHERE if (/^Correlations/);
276 if (/^=+ Data set >>(.+)<</) {
277 ($section = 'dataset');
278 ++$data_set;
279 $current_set = $1;
280 push @{$self->{"chifiles"}}, $current_set;
281 $self->{$current_set}->{titles} = [];
282 $self->{$current_set}->{paths} = [];
283 last WHERE;
284 };
285 if (/^\s+(\w+)\s+=\s+(-?\d*\.\d+)\s+(?:\:=)?\s+(.+)/) {
286 $self->{restraint}{$1} = $2;
287 $self->{restraint_expr}{$1} = $3;
288 };
289 push @{ $self->{order}->{restraint} }, $1;
290 last WHERE;
291 };
292
293 ## --- set parameters section --------------
294 ($section eq 'set') and do {
295 ($section = 'after'), last WHERE if (/^After/);
296 ($section = 'bkg'), last WHERE if (/^Background/);
297 ($section = 'corr'), last WHERE if (/^Correlations/);
298 if (/^=+ Data set >>(.+)<</) {
299 ($section = 'dataset');
300 ++$data_set;
301 $current_set = $1;
302 push @{$self->{"chifiles"}}, $current_set;
303 $self->{$current_set}->{titles} = [];
304 $self->{$current_set}->{paths} = [];
305 last WHERE;
306 };
307 if (/^\s+(\w+)\s+=\s+(-?\d*(\.\d+|))/) {
308 ($self->{set}{$1} = $2);
309 push @{ $self->{order}->{set} }, $1;
310 ##print "?????>", $1, " ", $self->{order}->{set}, " ", @{ $self->{order}->{set} }, $/;
311 };
312 last WHERE;
313 };
314
315 ## --- after-fit parameters section --------------
316 ($section eq 'after') and do {
317 ($section = 'bkg'), last WHERE if (/^Background/);
318 ($section = 'corr'), last WHERE if (/^Correlations/);
319 if (/^=+ Data set >>(.+)<</) {
320 ($section = 'dataset');
321 ++$data_set;
322 $current_set = $1;
323 push @{$self->{"chifiles"}}, $current_set;
324 $self->{$current_set}->{titles} = [];
325 $self->{$current_set}->{paths} = [];
326 last WHERE;
327 };
328 (($self->{after}{$1}, $self->{after_expr}{$1}) = ($2, $3)) if (/^\s+(\w+)\s*:\s*(-?\d*\.\d+)\s*=\s*(.+)/);
329 push @{ $self->{order}->{after} }, $1;
330 last WHERE;
331 };
332
333 ## --- correlations section + cormin --------------
334 ($section eq 'corr') and do {
335 if (/^All other[^0-9]*(\d\.\d+)/) { # recognize next section
336 $self->{stats}{cormin} = $1;
337 $section = 'dataset';
338 last WHERE;
339 };
340 if (/^\s+(\w+) and (\w+)\s+-->\s+(-?\d*\.\d+)/) {
341 my $key = join(":", $1, $2);
342 ($key = join(":", $2, $1)) unless exists $self->{corr}->{$key};
343 my $val = $3;
344 $self->{corr}{$key} = $val;
345 push @{ $self->{order}->{correlation} }, $key;
346 };
347 last WHERE;
348 };
349
350 ## --- data set info section --------------
351 ($section eq 'dataset') and do {
352 ## grab the label for the current data set, use this as a hash
353 ## key in the data structure
354 if (/^=+ Data set >>(.+)<</) {
355 ++$data_set;
356 $current_set = $1;
357 push @{$self->{"chifiles"}}, $current_set;
358 $self->{$current_set}->{titles} = [];
359 $self->{$current_set}->{paths} = [];
360 last WHERE;
361 };
362 if (/^\s+=+/) { # recognize next section
363 $section = 'paths';
364 last WHERE;
365 };
366 DATASET: {
367 ## grab operational params and data set specific statistics
368 ($_ =~ /^\s+$opregex/) and do {
369 my $key = $opparams{$1};
370 if ($key eq 'pcpath') {
371 $self->{$current_set}->{$key} = (split(/\s+=\s+/, $_))[1];
372 } else {
373 $self->{$current_set}->{$key} = (split(/(:|\s+=)\s+/, $_))[2];
374 };
375 last DATASET;
376 };
377 ## grab title lines associated with this data set
378 ($_ =~ /^\s+title lines/) and do {
379 $self->{$current_set}->{titles} = [];
380 my $line = <LOG>;
381 while ($line !~ /^\s*$/) {
382 chomp $line;
383 $line =~ s/^\s+//;
384 push @{$self->{$current_set}->{titles}}, $line;
385 $line = <LOG>;
386 };
387 };
388 };
389 last WHERE;
390 };
391
392 ## --- paths section for "current" data set --------------
393 ($section eq 'paths') and do {
394 if (/=+ Data set >>(.+)<</) { # recognize and intialize the next data set
395 ++$data_set;
396 $current_set = $1;
397 push @{$self->{"chifiles"}}, $current_set;
398 $self->{$current_set}->{titles} = [];
399 $self->{$current_set}->{paths} = [];
400 $section = 'dataset';
401 last WHERE;
402 };
403 if ($_ !~ /^\s+$ppregexp/) { # the line identifying this path
404 ($path = $_) =~ s/^\s+//;
405 $path =~ s/\s*\.\.$//;
406 push @{$self->{$current_set}->{paths}}, $path;
407 $path = $current_set . ": " . $path;
408 $self->{$path} = {};
409 };
410 last WHERE if ($! =~ /^\s+reff\+dr/); # skip the reff+dr line
411 if ($_ =~ /^\s+feff/) { # treat feff and id as strings
412 $self->{$path}{feff} = (split(/\s+=\s+/, $_))[1];
413 last WHERE;
414 } elsif ($_ =~ /^\s+(id|label)/) {
415 $self->{$path}{id} = (split(/\s+=\s+/, $_))[1] || "";
416 last WHERE;
417 };
418 ## snarf the rest of the path params as numbers
419 while (/$ppregexp\s+=\s+(-?\d*\.\d+)/g) {
420 ##print join(" ", $path, $1, $5), $/;
421 my $key = $1;
422 #($key = 'ns02') if ($key =~ /s02/);
423 $self->{$path}{$key} = $4;
424 };
425 last WHERE;
426 };
427 };
428
429 };
430 close LOG;
431
432 ## fill up correlations hash with zeros, that way all
433 ## possible correlations are defined.
434 foreach my $x (sort (keys %{$self->{bestfit}})) {
435 foreach my $y (sort (keys %{$self->{bestfit}})) {
436 last if ($x eq $y);
437 my $key = join(":", $x, $y);
438 $self->{corr}->{$key} ||= 0;
439 };
440 foreach my $y (sort (keys %{$self->{bkg}})) {
441 my $key = join(":", $x, $y);
442 $self->{corr}->{$key} ||= 0;
443 };
444 };
445 };
446
447
448 sub list {
449 my $self = shift;
450 return (@{$self->{chifiles}}) if (lc($_[0]) eq 'data');
451
452 return @{ $self->{order}->{guess} } if (lc($_[0]) eq 'guess');
453 return @{ $self->{order}->{bkg} } if (lc($_[0]) eq 'bkg');
454 return @{ $self->{order}->{def} } if (lc($_[0]) eq 'def');
455 return @{ $self->{order}->{restraint} } if (lc($_[0]) =~ /restrain/);
456 return @{ $self->{order}->{set} } if (lc($_[0]) eq 'set');
457 return @{ $self->{order}->{after} } if (lc($_[0]) eq 'after');
458 return @{ $self->{order}->{correlation} } if (lc($_[0]) eq 'corr');
459
460 ## return (keys %{$self->{bestfit}}) if (lc($_[0]) eq 'guess');
461 ## return (keys %{$self->{bkg}}) if (lc($_[0]) eq 'bkg');
462 ## return (keys %{$self->{def}}) if (lc($_[0]) eq 'def');
463 ## return (keys %{$self->{restraint}}) if (lc($_[0]) eq 'restraint');
464 ## return (keys %{$self->{set}}) if (lc($_[0]) eq 'set');
465 ## return (keys %{$self->{after}}) if (lc($_[0]) eq 'after');
466 ## return (keys %{$self->{corr}}) if (lc($_[0]) eq 'corr');
467
468 return ('Project title', 'Comment', 'Prepared by', 'Contact', 'Started',
469 'This fit at', 'Environment', 'Fit label', 'Data sets',
470 'Figure of merit') if (lc($_[0]) eq 'info');
471 return (qw(nidp nvar chisqr chinu rfact epsk epsr cormin ndata
472 pathindex cormin)) if (lc($_[0]) eq 'stats');
473 return (qw(file titles paths krange dk kwin kw rrange dr rwin fitspace
474 bkg nbkg pcpath chisqr rfact)) if (lc($_[0]) eq 'dataparams');
475 return (qw(feff id label r reff dr degen s02 e0 ss2 3rd 4th ei dphase))
476 if (lc($_[0]) eq 'pathparams');
477 };
478
479 sub get {
480 my $self = shift;
481 my $item = $_[0];
482 my $escaped = _escape_meta($item);
483
484 my @info = ('Project title', 'Comment', 'Prepared by', 'Contact', 'Started',
485 'This fit at', 'Environment', 'Fit label', 'Data sets', 'Figure of merit');
486 my @stats = (qw(nidp nvar chisqr chinu rfact epsk epsr cormin ndata
487 pathindex cormin));
488
489 QUERY: {
490
491 ## warnings
492 return @{$self->{warnings}} if ($item eq 'warnings');
493
494 ## project information
495 return $self->{info}->{$item} if (grep {/^$escaped$/} @info);
496
497 ## fit statistics
498 return $self->{stats}->{$item}||0 if (grep {/^$escaped$/} @stats);
499
500 ## guess variables
501 return ($self->{bestfit}->{$item},
502 $self->{error} ->{$item},
503 $self->{initial}->{$item}) if (grep {/^$escaped$/} $self->list('guess'));
504
505 ## background variables
506 return ($self->{bkg}->{$item},
507 $self->{bkgerr} ->{$item}) if (grep {/^$escaped$/} $self->list('bkg'));
508
509 ## def variables
510 return $self->{def}->{$item} if (grep {/^$escaped$/} $self->list('def'));
511
512 ## restraints
513 return $self->{restraint}->{$item} if (grep {/^$escaped$/} $self->list('restraint'));
514
515 ## set variables
516 return $self->{set}->{$item} if (grep {/^$escaped$/} $self->list('set'));
517
518 ## after-fit variables
519 return $self->{after}->{$item} if (grep {/^$escaped$/} $self->list('after'));
520
521 ## correlations
522 if (lc($item) =~ /^corr/) {
523 my $key = join(":", $_[1], $_[2]);
524 ($key = join(":", $_[2], $_[1])) unless (exists $self->{corr}->{$key});
525 return 0 unless (exists $self->{corr}->{$key});
526 return $self->{corr}->{$key};
527 };
528
529 ## opparams and pathparams for a data set
530 if (grep {/^$escaped$/} $self->list('data')) {
531 my $tmp = _escape_meta($_[1]);
532 ##print ">>$_[1]<< >>$tmp<<\n";
533 if (grep {/^$tmp$/} @{$self->{$item}->{paths}}) {
534 my $key = join(": ", $item, $_[1]);
535 return 0 unless exists($self->{$key}->{$_[2]});
536 return $self->{$key}->{$_[2]} if (grep {/^$_[2]$/} $self->list('pathparams'));
537 };
538 return @{$self->{$item}->{titles}} if ($_[1] eq 'titles');
539 return @{$self->{$item}->{paths}} if ($_[1] eq 'paths');
540 return $self->{$item}->{$_[1]} if (grep {/^$_[1]$/} $self->list('dataparams'));
541 return -998;
542 };
543
544
545
546 return -999;
547 };
548 };
549
550
551 sub _escape_meta {
552 my $tmp = $_[0];
553 $tmp =~ s/([\[\]^\$\\\(\)\+\*\-\?])/\\$1/g;
554 #$tmp =~ s/\(/\\\(/g;
555 return $tmp;
556 };
557
558 sub get_parameter {
559 my $self = shift;
560 my $item = $_[0];
561
562 ## guess variables
563 return ($self->{bestfit}->{$item},
564 $self->{error} ->{$item},
565 $self->{initial}->{$item}) if (grep {/^$item$/} $self->list('guess'));
566
567 ## def variables
568 return $self->{def}->{$item} if (grep {/^$item$/} $self->list('def'));
569
570 ## set variables
571 return $self->{set}->{$item} if (grep {/^$item$/} $self->list('set'));
572
573 ## after variables
574 return ($self->{after}->{$item}, $self->{after_expr}->{$item}) if (grep {/^$item$/} $self->list('after'));
575
576 # restraint
577 return ($self->{restraint}->{$item}, $self->{restraint_expr}->{$item})
578 if (grep {/^$item$/} $self->list('restraint'));
579
580 $self -> get(@_);
581 };
582
583
584
585 ## display methods
586
587 sub header {
588 my $self = $_[0];
589 my $string = "";
590 foreach my $i ($self -> list('info')) {
591 my $this = $i;
592 ($this = "This fit at") if ($this eq 'Last fit');
593 my $val = $self->get($i);
594 $string .= sprintf("%-15s : ", $this);
595 $string .= "\n", next if (not defined($val));
596 $string .= "\n", next if ($val =~ /^\<.*\>$/);
597 $string .= "\n", next if ($val =~ /^\s*$/);
598 $string .= "$val\n";
599 };
600 $string .= "\n" . "=" x 60 . "\n\n";
601 return $string;
602 };
603
604 sub stats {
605 my $self = $_[0];
606 my $string = "";
607 $string .= "\nFitting statistics\n";
608 my %names = (nidp => "Number of independent points",
609 nvar => "Number of variables",
610 chisqr => "Chi-square",
611 chinu => "Reduced chi-square",
612 rfact => "R-factor",
613 epsk => "Measurement uncertainty (k)",
614 epsr => "Measurement uncertainty (R)");
615 foreach my $i ($self -> list('stats')) {
616 next if (($i eq 'cormin') or ($i eq 'ndata') or ($i eq 'pathindex'));
617 $string .= sprintf(" %-28s : %s\n", $names{$i}, $self -> get($i));
618 };
619 return $string;
620 };
621
622 sub guess {
623 my $self = $_[0];
624 my $string = "";
625 $string .= "\nGuess parameters\n";
626 foreach my $i ($self -> list('guess')) {
627 my @ret = $self->get_parameter($i);
628 ##$notes{results} -> insert('end', join(" ", $i, @ret, $/));
629 if (grep {/$i/} @Ifeffit::Tools::buffer) {
630 ## ... and was guessed as a math expr.
631 if ($ret[2] !~ /^\s*-?(\d+\.?\d*|\.\d+)\s*$/) {
632 $string .= sprintf(" %-15s = %12.7f : no effect on the fit (guessed as %s)\n",
633 $i, @ret[0,2]);
634 ## ... and was guessed as a number
635 } else {
636 $string .= sprintf(" %-15s = %12.7f : no effect on the fit (%.4g)\n",
637 $i, @ret[0,2]);
638 };
639 ## this variable was guessed as a math expr.
640 } elsif ($ret[2] !~ /^\s*-?(\d+\.?\d*|\.\d+)\s*$/) {
641 $string .= sprintf(" %-15s = %12.7f +/- %12.7f (guessed as %s)\n",
642 $i, @ret);
643 } else { ## this variable was guessed as a number
644 $string .= sprintf(" %-15s = %12.7f +/- %12.7f (%.4f)\n",
645 $i, @ret);
646 };
647 };
648 return $string;
649 };
650
651 sub def {
652 my $self = $_[0];
653 my $which = $_[1];
654 my $string = "";
655 $string .= "\nDef parameters";
656 $string .= " (using \"$which\")" if ($which);
657 $string .= "\n";
658 foreach my $i ($self -> list('def')) {
659 $string .= sprintf(" %-15s = %12.7f\n", $i, $self->get_parameter($i));
660 };
661 return $string;
662 };
663
664 sub restraint {
665 my $self = $_[0];
666 my $which = $_[1];
667 my $string = "";
668 foreach my $i ($self -> list('restraint')) {
669 $string .= sprintf(" %-15s = %12.7f := %s \n", $i, $self->get_parameter($i));
670 };
671 return ($string) ? "\nRestraints\n" . $string : "";
672 };
673
674 sub set {
675 my $self = $_[0];
676 my $string = "";
677 $string .= "\nSet parameters\n";
678 foreach my $i ($self -> list('set')) {
679 $string .= sprintf(" %-15s = %s\n", $i, $self->get_parameter($i));
680 };
681 return $string;
682 };
683
684 sub after {
685 my $self = $_[0];
686 my $string = "";
687 foreach my $i ($self -> list('after')) {
688 $string .= sprintf(" %-15s : %12.7f = %s\n", $i, $self->get_parameter($i));
689 };
690 return ($string) ? "\nAfter-fit parameters\n" . $string : "";
691 };
692
693 sub correlations {
694 my $self = $_[0];
695 my $cormin = $self->get('cormin');
696 my $string = "\nCorrelations between variables:\n";
697 foreach my $c ($self->list("corr")) {
698 my @vars = split(/:/, $c);
699 my $corr = $self->get('correlation', @vars);
700 next if (abs($corr) < $cormin);
701 $string .= sprintf(" %10s and %-10s --> %7.4f\n", @vars, $corr);
702 };
703 $string .= "All other correlations are below $cormin\n\n";
704 return $string;
705 };
706
707 sub dataparams {
708 my $self = $_[0];
709 my $data = $_[1];
710 my $write_titles = $_[2];
711 my $string = "\n ===== Data set >>" . $data . "<< " . "=" x 40 . "\n\n";
712 my %opparams = reverse (file => 'file',
713 'k-range' => 'krange',
714 dk => 'dk',
715 'k-window' => 'kwin',
716 'k-weight' => 'kw',
717 'R-range' => 'rrange',
718 dR => 'dr',
719 'R-window' => 'rwin',
720 'fitting space' => 'fitspace',
721 'background function' => 'bkg',
722 'spline parameters' => 'nbkg',
723 'phase correction' => 'pcpath',
724 'Chi-square' => 'chisqr',
725 'R-factor for this data set' => 'rfact');
726 if (defined($self->{$data}->{rfact})) {
727 $string .= sprintf(" %-19s = %s\n", 'file', $self->get($data, 'file'));
728 } else {
729 $string .= sprintf(" %-19s = %s\n", 'file', "");
730 };
731 if ($write_titles) {
732 $string .= " titles:\n";
733 $string .= join("\n", @{$self->get($data, 'titles')});
734 $string .= "\n";
735 };
736 foreach my $k (qw(krange dk kwin kw rrange dr rwin fitspace bkg pcpath)) {
737 $string .= sprintf(" %-19s = %s\n", $opparams{$k}, $self->get($data, $k));
738 $string .= sprintf(" %-19s = %s\n", $opparams{nbkg}, $self->get($data, 'nbkg'))
739 if (($k eq 'bkg') and $self->get($data, 'nbkg'));
740 };
741 $string .= sprintf("\n R-factor for this data set = %s\n", $self->get($data, 'rfact'))
742 if defined($self->{$data}->{rfact});
743 $string .= "\n";
744 return $string;
745 };
746
747
748
749 1;
750
751
752 __END__
753
754 =head1 NAME
755
756 B<Ifeffit::ArtemisLog> - read Artemis log files
757
758 =head1 SYNOPSIS
759
760 use Ifeffit::ArtemisLog;
761 my $data = Ifeffit::ArtemisLog -> new();
762 $data -> read("myfit.log");
763 print "Reduced chi-square = ", $data -> get("chinu");
764
765 =head1 DESCRIPTION
766
767
768 B<Ifeffit::ArtemisLog> is an object oriented representation of log
769 files written by Artemis, which contain the results of a fit to EXAFS
770 data. Using B<Ifeffit::ArtemisLog> you can easily extract information
771 from these log files and easily write specially tailored reports on
772 your fitting results. See L<EXAMPLES> below for a simple script to
773 show the temperature dependence of a variable in a set of fits.
774
775
776 =head1 METHODS
777
778 =over 4
779
780 =item new()
781
782 The C<new> method is used to create the ArtemisLog object. Optionally
783 you can give the name of the log file as an argument to the C<new> method.
784
785 my $data = Ifeffit::ArtemisLog -> new('myfit.log');
786
787 =item read()
788
789 The C<read> method reads the log file, parses its contents, and saves
790 its information in a data structure. This method is called when
791 C<new> is given a log file as its argument.
792
793 $data -> read('myfit.log');
794
795 =item list()
796
797 The list method returns lists of general information about the data
798 structure and about the contents of the log file. C<list> can be
799 called in a number of ways.
800
801 Return a list of the labels of all the data sets used in the fit:
802 $data -> list('data');
803
804 Return a list of all guess variables used in the fit:
805 $data -> list('guess');
806
807 Return a list of all def variables used in the fit:
808 $data -> list('def');
809
810 Return a list of all set variables used in the fit:
811 $data -> list('set');
812
813 Return a list of all after-fit variables:
814 $data -> list('after');
815
816 Return a list of identifiers for the project information:
817 $data -> list('info');
818
819 Return a list of identifiers for the fitting statistical:
820 $data -> list('stats');
821
822 Return a list of identifiers for the fitting parameters for each data
823 set:
824 $data -> list('dataparams');
825
826 Return a list of identifiers for the path parameters:
827 $data -> list('pathparams');
828
829 =item get()
830
831 This method returns values of the various parameters read from the log
832 file. The C<get> method takes one, two, or three arguments depending
833 on what information you are looking for. If you ask to C<get>
834 something that Ifeffit::ArtemisLog cannot interpret, an error code
835 will be returned. -999 is the code for a string that could not be
836 interpreted. -998 is the code for an operational or path parameter
837 that could not be interpreted.
838
839 =over 4
840
841 =item def or set parameters
842
843 To get the value of a def or set parameter, use the one-argument form:
844 my $value = $data -> get('def_or_set_param');
845
846 =item after-fit parameters
847
848 To get the evaluation of an after-fit parameter, use the one-argument form:
849 my $value = $data -> get('after_param');
850
851 =item guess parameters
852
853 To get the best-fit value, error bar, and initial guess on a guess
854 parameter, use the one-argument form:
855 my ($best_fit, $error, $initial) = $data -> get('guess_param');
856
857 =item correlations
858
859 To get the correlation between two guess parameters, use the
860 three-argument form:
861 my $corr = $data -> get('correlation', 'var1', 'var2');
862
863 Any correlations below the cutoff set by the cormin parameter will be
864 reported as 0.
865
866 =item project information
867
868 To get project information, use the one-argument form:
869 my $title = $data -> get('Project title');
870
871 The available project information parameters are identified by
872 'Project title', 'Comment', 'Prepared by', 'Contact', 'Started', 'This fit
873 at', and 'Environment'. This list of identifiers is returned using
874 the C<list('info')> method.
875
876 =item fitting statistics
877
878 To get fitting statistics, use the one-argument form:
879 my $chi_square = $data -> get('chisqr')
880
881 The identifiers for the various fitting statistic parameters are
882 returned using the C<list('stats')> method. These identifiers are
883
884 nidp nvar chisqr chinu rfact epsk epsr cormin ndata
885 pathindex cormin
886
887 If the log file is from a summation (i.e. no fit was performed) 0 will
888 be returned for parameters such as rfact that were not computed.
889
890 =item data set parameters
891
892 To get the values of the fitting parameters for a data set as well as
893 data set specific statistics, use the two-argument form:
894 my $k_weight = $data -> get($data_set, 'kw');
895
896 The titles or the list of paths associated with a data set are
897 returned by the C<get> method as lists:
898 my @titles = $data -> get($data_set, 'titles');
899 my @paths = $data -> get($data_set, 'paths');
900
901 The identifiers for the the various data set parameters are returned
902 by the C<list('dataparams')> method. They are:
903
904 file titles paths krange dk kwin kw rrange dr rwin fitspace
905 bkg pcpath chisqr rfact
906
907 =item path parameters
908
909 To get the values of path parameters for individual paths, use the
910 three-argument form:
911 my $delta_R = $data -> get($data_set, $path, 'dr');
912
913 The identifiers for the path parameters are returned by the
914 C<list('pathparams')> method. They are:
915
916 feff id reff degen s02 e0 dr ss2 3rd 4th ei dphase
917
918 The first two arguments to C<get> in this case identify the data set
919 and the path. The identifiers for the data sets and for the
920 individual paths depend on information obtained from the log file.
921 The identifiers for the data sets can be obtained by
922
923 my @data_sets = $data -> list('data');
924
925 The identifiers for the paths can be obtained by
926
927 my @paths = $data -> get($data_set, 'paths');
928
929 These identifiers are related to the strings used as entries in the
930 Data and Paths list in Artemis. The identifiers obtained as described
931 above will be presented in the same order as they appear in the Data
932 and Paths list.
933
934 =back
935
936 =item get_parameter()
937
938 It is possible for the C<get> method to return the wrong kind of
939 parameter in certain situations. One such situation would be if you
940 have a guess, def, set, or after parameter called by the name of
941 fitting statistic parameter, for example, a guess parameter called
942 "chisqr". That is not prohibited in Ifeffit, but it will lead to
943 confusion in Ifeffit::ArtemisLog. To resolve this confusion, there is
944 the C<get_parameter> method, which operates by the same syntax as the
945 C<get> method, but which only serves to return guess, def, set, or
946 after parameters. Doing C<$data -> get_parameter('chisqr')> would, in
947 the case of the example, return the value of the "chisqr" parameter,
948 while C<get('chisqr')> woould return the value of chi-square for the
949 fit.
950
951 =back
952
953
954
955 =head1 EXAMPLES
956
957 Here is a simple script illustrating the use of
958 B<Ifeffit::ArtemisLog>. Suppose you have a set of temperature
959 dependent data on porridge at three temperatures: too cold, just
960 right, and too hot. This script will read the log files from the fits
961 to these three data sets and print a report on the temperature
962 dependence of a sigma^2 parameter called "sig2".
963
964 #!/usr/bin/perl -w
965
966 use Ifeffit::ArtemisLog;
967 $toocold = Ifeffit::ArtemisLog -> new("toocold.log");
968 $justright = Ifeffit::ArtemisLog -> new("justright.log");
969 $toohot = Ifeffit::ArtemisLog -> new("toohot.log");
970
971 print "# temperature dependence of sig2 in porridge data", $\ ;
972 print "# --------------------------------------------", $\ ;
973 print "# data_set sig2 error (initial)\n";
974 printf " too_cold %8.5f %8.5f %8.5f\n", $toocold -> get("sig2");
975 printf " justright %8.5f %8.5f %8.5f\n", $justright -> get("sig2");
976 printf " too_hot %8.5f %8.5f %8.5f\n", $toohot -> get("sig2");
977
978
979 =head1 AUTHOR
980
981 Bruce Ravel, [email protected]
982 http://feff.phys.washington.edu/~ravel/software/exafs/
983
984
985 =cut