| 1 |
# -*- cperl -*- |
| 2 |
## This file is part of Artemis, copyright (c) 2002-2006 Bruce Ravel |
| 3 |
## |
| 4 |
## THE PALETTES SUBSECTION |
| 5 |
|
| 6 |
sub display_file { |
| 7 |
my ($type, $fname) = @_; |
| 8 |
Echo("No feff calculation!"), return if (($type eq 'path') and ($n_feff == 0)); |
| 9 |
SWITCH: { |
| 10 |
last SWITCH if ($type =~ /^\s*$/); |
| 11 |
$fname = $paths{$current}->get($fname), last SWITCH if ($type eq 'feff') ; |
| 12 |
$fname = File::Spec->catfile($paths{$current}->get('path'), |
| 13 |
$paths{$current}->get('feff')), |
| 14 |
last SWITCH if (($type eq 'path') and |
| 15 |
($fname eq 'this')); |
| 16 |
$fname = File::Spec->catfile($paths{$current}->get('path'), $fname), |
| 17 |
last SWITCH if ($type eq 'path'); |
| 18 |
## dereference the group that a fit or bkg is the same as |
| 19 |
$fname = $paths{$current}->get('file'), |
| 20 |
last SWITCH if (($type eq 'data') and |
| 21 |
($fname eq 'this') and |
| 22 |
$paths{$current}->get('sameas')); |
| 23 |
## dereference the group that a path or feff refers to |
| 24 |
$fname = $paths{$current}->get('file'), |
| 25 |
last SWITCH if (($type eq 'data') and |
| 26 |
($fname eq 'this') and |
| 27 |
($current =~ /feff\d+/)); |
| 28 |
$fname = $paths{$current}->get('file'), last SWITCH if (($type eq 'data') and |
| 29 |
($fname eq 'this')); |
| 30 |
Echo("Want to display $fname"), return unless ($type eq 'file'); |
| 31 |
}; |
| 32 |
## paths file, deal with feff6 vs. feff8!! |
| 33 |
($fname = substr($fname, 0, -9)."path00.dat") if (($fname =~ /paths\.dat$/) and (not -e $fname)); |
| 34 |
Echo("You have not read any data yet"), return unless (defined $fname); |
| 35 |
Echo("The file you asked for could not be found"), return unless (-e $fname); |
| 36 |
Echo("Displaying $fname"); |
| 37 |
$current_file = $fname; |
| 38 |
$notes{files} -> delete(qw(1.0 end)); |
| 39 |
|
| 40 |
my $was_mac = $paths{data0} -> |
| 41 |
fix_mac($fname, $stash_dir, lc($config{general}{mac_eol}), $top); |
| 42 |
return, Echo("\"$fname\" had Macintosh EOL characters and was skipped.") if ($was_mac eq "-1"); |
| 43 |
if ($was_mac !~ m{^(?:0|-?1)}) { |
| 44 |
Echo("\"$fname\" had Macintosh EOL characters and was fixed."); |
| 45 |
$fname = $was_mac; |
| 46 |
}; |
| 47 |
|
| 48 |
Echo("Could not find \"$fname\""), return unless (-e $fname); |
| 49 |
open F, "$fname" or die "Could not open $fname for viewing.\n"; |
| 50 |
while (<F>) { |
| 51 |
$_ =~ s{\r}{}g if not $is_windows; |
| 52 |
$notes{files} -> insert('end', $_); |
| 53 |
}; |
| 54 |
close F; |
| 55 |
$notes{files} -> yviewMoveto(0); |
| 56 |
$top -> update; |
| 57 |
raise_palette('files'); |
| 58 |
}; |
| 59 |
|
| 60 |
sub show_things { |
| 61 |
if ($_[0] eq 'paths') { |
| 62 |
my $error = ""; |
| 63 |
$error .= &verify_parens; |
| 64 |
if ($error) { |
| 65 |
post_message($error, "Error Messages"); |
| 66 |
Error("cannot show paths due to errors in parameters and math expressions"); |
| 67 |
return; |
| 68 |
}; |
| 69 |
}; |
| 70 |
$paths{data0}->dispose("show \@$_[0]", $dmode); |
| 71 |
$top -> update(); |
| 72 |
raise_palette('ifeffit'); |
| 73 |
}; |
| 74 |
|
| 75 |
sub show_defs { |
| 76 |
my $which = &which_set_path; |
| 77 |
if ($which) { |
| 78 |
$paths{data0}->dispose("\n## Evaluating def parameters using " . $paths{$which}->descriptor(), $dmode); |
| 79 |
$paths{data0}->dispose("set path_index=" . $paths{$which}->get('fit_index'), $dmode); |
| 80 |
}; |
| 81 |
my %seen; |
| 82 |
my @defs; |
| 83 |
foreach my $p (@gds) { |
| 84 |
push @defs, $p->name if ($p->type eq 'def'); |
| 85 |
}; |
| 86 |
$paths{data0}->dispose("show " . join(" ", @defs), $dmode); |
| 87 |
$top -> update(); |
| 88 |
raise_palette('ifeffit'); |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
## $pal : key for %notes hash |
| 94 |
## $init : value for -initialfile |
| 95 |
## $title : value for -title |
| 96 |
## $t : anon array of values for $types list |
| 97 |
## $prepend : text string to prepend to contents of $notes{$pal} |
| 98 |
## $append : text string to append to contents of $notes{$pal} |
| 99 |
sub save_from_palette { |
| 100 |
my ($pal, $init, $title, $t, $prepend, $append) = @_; |
| 101 |
##local $Tk::FBox::a; |
| 102 |
##local $Tk::FBox::b; |
| 103 |
my $path = $current_data_dir || cwd; |
| 104 |
##$path = File::Spec->catfile($project_folder, "log_files") if ($pal eq 'results'); |
| 105 |
##$path = File::Spec->catfile($project_folder, "log_files") if ($pal =~ /report/); |
| 106 |
##$path = File::Spec->catfile($project_folder, "log_files") if ($init =~ /report/); |
| 107 |
my $types = ($t) ? [$t, ['All Files', '*']] : [['All Files', '*'], ['Input Files', '*.inp']]; |
| 108 |
($init =~ s/[\\:\/\*\?\'<>\|]/_/g);# if ($is_windows); |
| 109 |
my $file = $top -> getSaveFile(-filetypes=>$types, |
| 110 |
##(not $is_windows) ? |
| 111 |
## (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () , |
| 112 |
-initialfile=>$init||$generic_name, |
| 113 |
-initialdir=>$path, |
| 114 |
-title => $title); |
| 115 |
return unless ($file); |
| 116 |
my ($name, $pth, $suffix) = fileparse($file); |
| 117 |
#if ($pal eq 'results') { |
| 118 |
# project_state(0) if sub_directory($pth, $project_folder); |
| 119 |
#} else { |
| 120 |
$current_data_dir = $pth; |
| 121 |
#}; |
| 122 |
open F, ">".$file or do { |
| 123 |
Error("You cannot write to \"$file\"."); return |
| 124 |
}; |
| 125 |
print F $prepend, $notes{$pal}->get(qw(1.0 end)), $append; |
| 126 |
close F; |
| 127 |
|
| 128 |
$generic_name = "artemis.stuff"; |
| 129 |
Echo("Saved contents of $pal palette to $file"); |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
## post a text message to the "messages" palette |
| 136 |
## args: 0:text of message 1:id string 2:hide palette |
| 137 |
sub post_message { |
| 138 |
$notes{messages} -> delete(qw(1.0 end)); |
| 139 |
$notes{messages} -> insert('end', $_[0]||""); |
| 140 |
$notes{messages} -> yviewMoveto(0); |
| 141 |
##$current_file = $_[1] || ""; |
| 142 |
$top -> update; |
| 143 |
raise_palette('messages') unless $_[2]; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
sub raise_palette { |
| 148 |
##($update->state() eq "normal") ? |
| 149 |
$update->deiconify; |
| 150 |
$update->raise; |
| 151 |
$notebook->raise($_[0]); |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
sub write_results_header { |
| 156 |
my ($fh, $r_fit) = @_; |
| 157 |
foreach ('Project title', 'Comment', 'Prepared by', 'Contact', 'Started', 'Last fit', 'Environment') { |
| 158 |
my $this = $_; |
| 159 |
($this = "This fit at") if ($this eq 'Last fit'); |
| 160 |
print $fh sprintf("%-15s : ", $this); |
| 161 |
print($fh "\n"), next if (not defined($props{$_})); |
| 162 |
print($fh "\n"), next if ($props{$_} =~ /^\<.*\>$/); |
| 163 |
print($fh "\n"), next if ($props{$_} =~ /^\s*$/); |
| 164 |
print $fh "$props{$_}\n"; |
| 165 |
}; |
| 166 |
my $string = q{}; |
| 167 |
foreach my $d (&all_data) { |
| 168 |
$string .= '"' . $paths{$d}->get('lab') . '", '; |
| 169 |
}; |
| 170 |
chop $string; chop $string; |
| 171 |
print $fh sprintf("%-15s : %s\n", "Data sets", $string); |
| 172 |
print $fh sprintf("%-15s : %s\n", "Fit label", $$r_fit{label}); |
| 173 |
print $fh sprintf("%-15s : %s\n", "Figure of merit", $$r_fit{fom}); |
| 174 |
print $fh "\n" . "=" x 60 . "\n\n"; |
| 175 |
}; |
| 176 |
|
| 177 |
sub write_results { |
| 178 |
my ($fh, $how, $how_many) = @_; ## how=1 means fit, how=2 means summation |
| 179 |
## compile a list of parameters in a sensible order |
| 180 |
my %seen; # see The Perl Cookbook, 4.6, p. 102, |
| 181 |
# need to weed out repeated params |
| 182 |
my @all = grep { $_->{name} } @gds; |
| 183 |
my (@params, @defs, @sets, @restraints, @afters); |
| 184 |
foreach my $p (@gds) { |
| 185 |
push @params, lc($p->name) if ($p->type eq 'guess'); |
| 186 |
push @defs, lc($p->name) if ($p->type eq 'def'); |
| 187 |
push @sets, lc($p->name) if ($p->type eq 'set'); |
| 188 |
push @restraints, lc($p->name) if ($p->type eq 'restrain'); |
| 189 |
push @afters, lc($p->name) if ($p->type eq 'after'); |
| 190 |
}; |
| 191 |
$paths{data0}->dispose("show \@variables", 1); |
| 192 |
my ($lines, $response) = (Ifeffit::get_scalar('&echo_lines'), ""); |
| 193 |
my @bkg; |
| 194 |
foreach my $i (1 .. $lines) { |
| 195 |
my $this = (split(" ", Ifeffit::get_echo()))[0]; |
| 196 |
push @bkg, $this if ($this =~ /^bkg\d\d_\d\d$/); |
| 197 |
}; |
| 198 |
|
| 199 |
my @things = ('n_idp n_varys chi_square chi_reduced r_factor epsilon_k epsilon_r', |
| 200 |
@params, @defs, @bkg); |
| 201 |
my %things = ("n_idp" => "Independent points ", |
| 202 |
"n_varys" => "Number of variables ", |
| 203 |
"chi_square" => "Chi-square ", |
| 204 |
"chi_reduced" => "Reduced Chi-square ", |
| 205 |
"r_factor" => "R-factor ", |
| 206 |
"epsilon_k" => "Measurement uncertainty (k) ", |
| 207 |
"epsilon_r" => "Measurement uncertainty (R) ", |
| 208 |
"data_total" => "Number of data sets ", |
| 209 |
); |
| 210 |
if ($how == 1) { |
| 211 |
$paths{data0}->dispose("show n_idp n_varys chi_square chi_reduced r_factor epsilon_k epsilon_r data_total\n", 1); |
| 212 |
($lines, $response) = (Ifeffit::get_scalar('&echo_lines'), ""); |
| 213 |
map {$response .= Ifeffit::get_echo()."\n"} (1 .. $lines); |
| 214 |
foreach my $k (keys %things) { |
| 215 |
$response =~ s/($k)\s+/$things{$k}/eg; |
| 216 |
}; |
| 217 |
} else { |
| 218 |
my $this = $paths{$paths{$current}->data}->descriptor(); |
| 219 |
$response .= sprintf("%-70s\n", "!! FITTING WAS NOT PERFORMED."); |
| 220 |
$response .= sprintf("%-70s", "!! Summation performed for data set \"$this\" using $how_many paths."); |
| 221 |
}; |
| 222 |
print $fh $response."\n"; |
| 223 |
|
| 224 |
$response = ""; |
| 225 |
if (@Ifeffit::Tools::buffer) { |
| 226 |
print $fh "!! \n"; |
| 227 |
print $fh "!! WARNING. The following variables had no effect on the fit: \n"; |
| 228 |
map {push @bad_params, $_; printf $fh "!! >> %-58s\n", $_} @Ifeffit::Tools::buffer; |
| 229 |
print $fh "!! \n"; |
| 230 |
print $fh "!! Uncertainties could not be estimated. \n"; |
| 231 |
print $fh "!! \n"; |
| 232 |
}; |
| 233 |
print $fh "\nGuess parameters +/- uncertainties (initial guess):\n"; |
| 234 |
foreach my $p (@gds) { |
| 235 |
next unless ($p->{type} eq 'guess'); |
| 236 |
my $string; |
| 237 |
## this variable had no effect on the fit ... |
| 238 |
if (grep {/$p/} @Ifeffit::Tools::buffer) { |
| 239 |
## ... and was guessed as a math expr. |
| 240 |
if ($p->{mathexp} !~ /^\s*-?(\d+\.?\d*|\.\d+)\s*$/) { |
| 241 |
$string = sprintf(" %-15s = %12.7f : no effect on the fit (guessed as %s)\n", |
| 242 |
$p->name, |
| 243 |
$p->bestfit, |
| 244 |
$p->mathexp); |
| 245 |
## ... and was guessed as a number |
| 246 |
} else { |
| 247 |
$string = sprintf(" %-15s = %12.7f : no effect on the fit (%.4g)\n", |
| 248 |
$p->name, |
| 249 |
$p->bestfit, |
| 250 |
$p->mathexp); |
| 251 |
}; |
| 252 |
## this variable was guessed as a math expr. |
| 253 |
} elsif ($p->{mathexp} !~ /^\s*-?(\d+\.?\d*|\.\d+)\s*$/) { |
| 254 |
$string = sprintf(" %-15s = %12.7f +/- %12.7f (guessed as %s)\n", |
| 255 |
$p->name, |
| 256 |
$p->bestfit, |
| 257 |
$p->error, |
| 258 |
$p->mathexp); |
| 259 |
} else { ## this variable was guessed as a number |
| 260 |
$string = sprintf(" %-15s = %12.7f +/- %12.7f (%.4f)\n", |
| 261 |
$p->name, |
| 262 |
$p->bestfit, |
| 263 |
$p->error, |
| 264 |
$p->mathexp); |
| 265 |
}; |
| 266 |
print $fh $string; |
| 267 |
}; |
| 268 |
if (@defs) { |
| 269 |
print $fh "\nDef parameters"; |
| 270 |
my $which = &which_set_path; |
| 271 |
print $fh " (using \"" . $paths{$which}->descriptor() . "\")" |
| 272 |
if ($which); |
| 273 |
print $fh ":\n"; |
| 274 |
}; |
| 275 |
foreach (@gds) { |
| 276 |
next unless ($_->{type} eq 'def'); |
| 277 |
my $string = sprintf(" %-15s = %12.7f\n", |
| 278 |
$_->{name}, |
| 279 |
$_->{bestfit}); |
| 280 |
print $fh $string; |
| 281 |
}; |
| 282 |
@restraints and print $fh "\nRestraints:\n"; |
| 283 |
foreach (@gds) { |
| 284 |
next unless ($_->{type} eq 'restrain'); |
| 285 |
my $string = sprintf(" %-15s = %12.7f := %s \n", |
| 286 |
$_->name, |
| 287 |
$_->bestfit, |
| 288 |
$_->mathexp); |
| 289 |
print $fh $string; |
| 290 |
}; |
| 291 |
@sets and print $fh "\nSet parameters:\n"; |
| 292 |
foreach (@gds) { |
| 293 |
next unless ($_->{type} eq 'set'); |
| 294 |
my $string = sprintf(" %-15s = %s\n", |
| 295 |
$_->name, |
| 296 |
$_->mathexp); |
| 297 |
print $fh $string; |
| 298 |
}; |
| 299 |
@afters and print $fh "\nAfter-fit parameters:\n"; |
| 300 |
foreach (@gds) { |
| 301 |
next unless ($_->{type} eq 'after'); |
| 302 |
my $string = sprintf(" %-15s : %12.7f = %s\n", |
| 303 |
$_->name, |
| 304 |
$_->bestfit, |
| 305 |
$_->mathexp); |
| 306 |
print $fh $string; |
| 307 |
}; |
| 308 |
@bkg and print $fh "\nBackground parameters +/- uncertainties:\n"; |
| 309 |
foreach (@bkg) { |
| 310 |
my $string = sprintf " %-15s = %12.7f +/- %12.7f\n", $_, |
| 311 |
Ifeffit::get_scalar($_), Ifeffit::get_scalar("delta_$_"); |
| 312 |
print $fh $string; |
| 313 |
}; |
| 314 |
my $first = &first_data; |
| 315 |
}; |
| 316 |
|
| 317 |
|
| 318 |
sub write_opparams { |
| 319 |
my ($fh, $d) = @_; |
| 320 |
my @lines = (); |
| 321 |
push @lines, sprintf("file: %s", $paths{$d}->get('file')); |
| 322 |
push @lines, "title lines:"; |
| 323 |
foreach my $t (split(/\n/, $paths{$d}->get('titles'))) { push @lines, " ".$t; }; |
| 324 |
push @lines, ""; |
| 325 |
push @lines, sprintf("k-range = %.3f - %.3f", $paths{$d}->get('kmin'), $paths{$d}->get('kmax')); |
| 326 |
push @lines, sprintf("dk = %.3f", $paths{$d}->get('dk')); |
| 327 |
push @lines, sprintf("k-window = %s", $paths{$d}->get('kwindow')); |
| 328 |
my @kw = (); |
| 329 |
push @kw, 1 if $paths{$d}->get('k1'); |
| 330 |
push @kw, 2 if $paths{$d}->get('k2'); |
| 331 |
push @kw, 3 if $paths{$d}->get('k3'); |
| 332 |
push @kw, 1 unless @kw; |
| 333 |
push @kw, $paths{$d}->{karb} if $paths{$d}->get('karb'); |
| 334 |
push @lines, sprintf("k-weight = %s", join(",", @kw)); |
| 335 |
push @lines, sprintf("R-range = %.3f - %.3f", $paths{$d}->get('rmin'), $paths{$d}->get('rmax')); |
| 336 |
push @lines, sprintf("dR = %.3f", $paths{$d}->get('dr')); |
| 337 |
push @lines, sprintf("R-window = %s", $paths{$d}->get('rwindow')); |
| 338 |
my $bkg = "none"; |
| 339 |
($bkg = "fitted spline") if ($paths{$d}->get('do_bkg') eq 'yes'); |
| 340 |
($bkg = "previous fit spline") if $paths{$d}->get('use_bkg'); |
| 341 |
push @lines, sprintf("fitting space = %s", $paths{$d}->get('fit_space')); |
| 342 |
push @lines, sprintf("background function = %s", $bkg); |
| 343 |
my $n_bkg_params = 0; |
| 344 |
if ($paths{$d}->get('do_bkg') eq 'yes') { |
| 345 |
$n_bkg_params = ($paths{$d}->get('kmax') - $paths{$d}->get('kmin') + $paths{$d}->get('dk')) |
| 346 |
* $paths{$d}->get('rmin') |
| 347 |
* 2 / PI; |
| 348 |
$n_bkg_params = round($n_bkg_params) + 1; |
| 349 |
push @lines, sprintf("spline parameters = %d", $n_bkg_params); |
| 350 |
}; |
| 351 |
if (lc($paths{$d}->get('pcpath')) eq 'none') { |
| 352 |
push @lines, "phase correction = none"; |
| 353 |
} else { |
| 354 |
push @lines, sprintf("phase correction = %s", $paths{$paths{$d}->get('pcpath')}->descriptor()); |
| 355 |
}; |
| 356 |
push @lines, "fit the difference spectrum" if $paths{$d}->get('fit_diff'); |
| 357 |
push @lines, "\n"; |
| 358 |
## compute chi-square and R-factor for this data set and for all its k-weightings |
| 359 |
my (@datasum, @diffsum, $ndata) = ((), (), 0); |
| 360 |
if (lc($paths{$d}->get('fit_space')) eq 'k') { |
| 361 |
my @k = Ifeffit::get_array($paths{$d}->get('group').".k"); |
| 362 |
my @chi = Ifeffit::get_array($paths{$d}->get('group').".chi"); |
| 363 |
my @fit = Ifeffit::get_array($paths{$d}->get('group')."_fit.chi"); |
| 364 |
my $i = 0; |
| 365 |
foreach my $w ($paths{$d}->group_weights) { |
| 366 |
$datasum[$i] = 0; |
| 367 |
$ndata = 0; |
| 368 |
foreach (0 .. $#k) { |
| 369 |
next if ($k[$_] < $paths{$d}->get('kmin')); |
| 370 |
last if ($k[$_] > $paths{$d}->get('kmax')); |
| 371 |
$datasum[$i] += ($chi[$_]*$k[$_]**$w)**2; |
| 372 |
$diffsum[$i] += ($chi[$_]*$k[$_]**$w - $fit[$_]*$k[$_]**$w)**2; |
| 373 |
++$ndata; |
| 374 |
}; |
| 375 |
++$i; |
| 376 |
}; |
| 377 |
} elsif (lc($paths{$d}->get('fit_space')) eq 'r') { |
| 378 |
my $i = 0; |
| 379 |
foreach my $w ($paths{$d}->group_weights) { |
| 380 |
## bring fit up to date in R space for this k-weight |
| 381 |
$paths{$d}->dispose($paths{$d}->write_fft($w, $config{data}{rmax_out}), $dmode); |
| 382 |
$paths{$d.".0"}->dispose($paths{$d.".0"}->write_fft($w, $config{data}{rmax_out}), $dmode); |
| 383 |
my @r = Ifeffit::get_array($paths{$d}->get('group').".r"); |
| 384 |
my @chi = Ifeffit::get_array($paths{$d}->get('group').".chir_re"); |
| 385 |
my @chi2 = Ifeffit::get_array($paths{$d}->get('group').".chir_im"); |
| 386 |
my @fit = Ifeffit::get_array($paths{$d}->get('group')."_fit.chir_re"); |
| 387 |
my @fit2 = Ifeffit::get_array($paths{$d}->get('group')."_fit.chir_im"); |
| 388 |
my $rmin = $paths{$d}->get('rmin'); |
| 389 |
($rmin = 0) if (lc($paths{$d}->get('do_bkg')) eq 'yes'); |
| 390 |
$datasum[$i] = 0; |
| 391 |
$ndata = 0; |
| 392 |
foreach (0 .. $#r) { |
| 393 |
next if ($r[$_] < $rmin); |
| 394 |
last if ($r[$_] > $paths{$d}->get('rmax')); |
| 395 |
$datasum[$i] += $chi[$_]**2 + $chi2[$_]**2; |
| 396 |
$diffsum[$i] += ($chi[$_]-$fit[$_])**2 + ($chi2[$_]-$fit2[$_])**2; |
| 397 |
++$ndata; |
| 398 |
}; |
| 399 |
++$i; |
| 400 |
}; |
| 401 |
} elsif (lc($paths{$d}->get('fit_space')) eq 'q') { |
| 402 |
my $i = 0; |
| 403 |
foreach my $w ($paths{$d}->group_weights) { |
| 404 |
## bring this group up to date in q space for this k-weight |
| 405 |
$paths{$d}->dispose($paths{$d}->write_fft($w, $config{data}{rmax_out}), $dmode); |
| 406 |
$paths{$d}->dispose($paths{$d}->write_bft(), $dmode); |
| 407 |
$paths{$d.".0"}->dispose($paths{$d.".0"}->write_fft($w, $config{data}{rmax_out}), $dmode); |
| 408 |
$paths{$d.".0"}->dispose($paths{$d.".0"}->write_bft(), $dmode); |
| 409 |
my @q = Ifeffit::get_array($paths{$d}->get('group').".q"); |
| 410 |
my @chi = Ifeffit::get_array($paths{$d}->get('group').".chiq_re"); |
| 411 |
my @chi2 = Ifeffit::get_array($paths{$d}->get('group').".chiq_im"); |
| 412 |
my @fit = Ifeffit::get_array($paths{$d}->get('group')."_fit.chiq_re"); |
| 413 |
my @fit2 = Ifeffit::get_array($paths{$d}->get('group')."_fit.chiq_im"); |
| 414 |
foreach (0 .. $#q) { |
| 415 |
next if ($q[$_] < $paths{$d}->get('kmin')); |
| 416 |
last if ($q[$_] > $paths{$d}->get('kmax')); |
| 417 |
$datasum[$i] += $chi[$_]**2 + $chi2[$_]**2; |
| 418 |
$diffsum[$i] += ($chi[$_]-$fit[$_])**2 + ($chi2[$_]-$fit2[$_])**2; |
| 419 |
++$ndata; |
| 420 |
}; |
| 421 |
++$i; |
| 422 |
}; |
| 423 |
}; |
| 424 |
if (-e $paths{$d}->get('file')) { |
| 425 |
my $rfactor = 0; |
| 426 |
$rfactor += $diffsum[$_]/$datasum[$_] foreach (0 .. $#diffsum); |
| 427 |
$rfactor /= ($#diffsum+1); |
| 428 |
my @noise = $paths{$d}->chi_noise; |
| 429 |
my $epsilon = (lc($paths{$d}->get('fit_space')) eq 'r') ? $noise[1] : $noise[0]; |
| 430 |
my $nidp = Ifeffit::get_scalar("n_idp"); |
| 431 |
#print join(" ", $rfactor, @noise, $epsilon, $nidp, $ndata, $/); |
| 432 |
my $chisqr = 0; |
| 433 |
$chisqr += ($nidp*$diffsum[$_]) / ($ndata*$epsilon**2) foreach (0 .. $#diffsum); |
| 434 |
#push @lines, "These are not yet computed quite right in all situations..."; |
| 435 |
#push @lines, sprintf("Chi-square for this data set = %.5f", $chisqr); |
| 436 |
push @lines, sprintf("R-factor for this data set = %.5f", $rfactor); |
| 437 |
if ($#diffsum) { |
| 438 |
my $i = 0; |
| 439 |
foreach my $w ($paths{$d}->group_weights) { |
| 440 |
push @lines, sprintf("R-factor with k-weight=$w for this data set = %.5f", $diffsum[$i]/$datasum[$i]); |
| 441 |
++$i; |
| 442 |
}; |
| 443 |
}; |
| 444 |
}; |
| 445 |
|
| 446 |
foreach my $l (@lines) { |
| 447 |
print $fh " $l\n"; |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
}; |
| 452 |
|
| 453 |
|
| 454 |
## in the case of a log file for a summation, only write out those |
| 455 |
## paths included in the sum |
| 456 |
sub write_paths { |
| 457 |
my ($fh, $d, $how, $how_many, $rhash) = @_; |
| 458 |
my @included = @{$paths{$d}->get('included')}; |
| 459 |
my @inc_mapping = @{$paths{$d}->get('inc_mapping')}; |
| 460 |
|
| 461 |
my $warnings = ""; |
| 462 |
my %pathtext; |
| 463 |
my %pp = (reff=>0, dr=>0); |
| 464 |
my $pp_re = "(3rd|4th|d(egen|phase|r)|e[0i]|n.s02|r(|eff)|s(02|s2))"; |
| 465 |
foreach my $i (@included) { |
| 466 |
next if (($how == 2) and |
| 467 |
($how_many =~ /sel/) and |
| 468 |
(not exists($$rhash{$inc_mapping[$i]})) ); |
| 469 |
$pp{index} = $i; |
| 470 |
$pp{descriptor} = $paths{$inc_mapping[$i]}->descriptor(); |
| 471 |
$paths{data0} -> dispose("show \@path $i", 1); |
| 472 |
my ($lines, $response) = (Ifeffit::get_scalar('&echo_lines')||0, ""); |
| 473 |
my ($this, $text) = ("",""); |
| 474 |
if ($lines) { |
| 475 |
foreach my $l (1 .. $lines) { |
| 476 |
$response = Ifeffit::get_echo()."\n"; |
| 477 |
next unless $response; |
| 478 |
next if ($response =~ /\*\*\* correl:/); |
| 479 |
if ($response =~ /^PATH/) { |
| 480 |
my $p = $inc_mapping[$i]; |
| 481 |
$text .= "\n ^^^" . $paths{$p}->descriptor(); |
| 482 |
$text .= "\n"; |
| 483 |
##$text .= ($paths{$p}->get('group')) ? " (ifeffit group = ".$paths{$p}->get('group').")\n" : "\n"; |
| 484 |
} elsif ($response =~ /^\s*feff/){ |
| 485 |
$this = (split(/\s+=\s+/, $response))[1]; |
| 486 |
PPMATCH1: { |
| 487 |
($pp{$1} = $5), last PPMATCH1 # OK value, beginning of line |
| 488 |
if ($response =~ /^\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 489 |
($pp{$1} = "tilt"), last PPMATCH1 # bad value, beginning of line |
| 490 |
if ($response =~ /^\s*$pp_re\s+=\*+/); |
| 491 |
($pp{$1} = $5), last PPMATCH1 # OK value, end of line |
| 492 |
if ($response =~ /,\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 493 |
($pp{$1} = "tilt"), last PPMATCH1 # bad value, end of line |
| 494 |
if ($response =~ /,\s*$pp_re\s+=\*+/); |
| 495 |
}; |
| 496 |
$text .= " ".$response; |
| 497 |
} else { |
| 498 |
PPMATCH3: { |
| 499 |
($pp{$1} = $5), last PPMATCH3 # OK value, beginning of line |
| 500 |
if ($response =~ /^\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 501 |
($pp{$1} = "tilt"), last PPMATCH3 # bad value, beginning of line |
| 502 |
if ($response =~ /^\s*$pp_re\s+=\*+/); |
| 503 |
}; |
| 504 |
$text .= $response; |
| 505 |
}; |
| 506 |
}; |
| 507 |
}; |
| 508 |
$pathtext{$i} = [$this,$text]; |
| 509 |
$warnings .= &check_path(\%pp); |
| 510 |
store_ppvalues($inc_mapping[$i], \%pp); |
| 511 |
}; |
| 512 |
|
| 513 |
foreach my $t (@included) { |
| 514 |
next if (($how == 2) and |
| 515 |
($how_many =~ /sel/) and |
| 516 |
(not exists($$rhash{$inc_mapping[$t]})) ); |
| 517 |
foreach my $l (split(/\n/, $pathtext{$t}[1])) { |
| 518 |
if ($l =~ /^\s*\^\^\^/i) { |
| 519 |
$l =~ s/^\s*\^\^\^//; |
| 520 |
print $fh " "; |
| 521 |
print $fh $l." ..\n"; |
| 522 |
} elsif ($l =~ /^\s*feff/) { |
| 523 |
print $fh $l."\n"; |
| 524 |
} elsif ($l =~ /s02\s*=/) { |
| 525 |
$l =~ s/n\*s/s/; |
| 526 |
$l =~ s/=/ =/ if $paths{data0}->vstr == 1.02005; |
| 527 |
print $fh " ".$l."\n"; |
| 528 |
} else { |
| 529 |
print $fh " ".$l."\n"; |
| 530 |
}; |
| 531 |
}; |
| 532 |
}; |
| 533 |
return $warnings; |
| 534 |
} |
| 535 |
|
| 536 |
sub write_paths_pre_1_2_5 { |
| 537 |
my ($fh, $d, $how, $how_many, $rhash) = @_; |
| 538 |
my @included = @{$paths{$d}->get('included')}; |
| 539 |
my @inc_mapping = @{$paths{$d}->get('inc_mapping')}; |
| 540 |
|
| 541 |
my $warnings = ""; |
| 542 |
my %pathtext; |
| 543 |
my %pp = (reff=>0, dr=>0); |
| 544 |
my $pp_re = "(3rd|4th|d(egen|phase|r)|e[0i]|reff|s(02|s2))"; |
| 545 |
foreach my $i (@included) { |
| 546 |
next if (($how == 2) and |
| 547 |
($how_many =~ /sel/) and |
| 548 |
(not exists($$rhash{$inc_mapping[$i]})) ); |
| 549 |
$pp{index} = $i; |
| 550 |
$pp{descriptor} = $paths{$inc_mapping[$i]}->descriptor(); |
| 551 |
$paths{data0} -> dispose("show \@path $i", 1); |
| 552 |
my ($lines, $response) = (Ifeffit::get_scalar('&echo_lines')||0, ""); |
| 553 |
my ($this, $text) = ("",""); |
| 554 |
if ($lines) { |
| 555 |
foreach my $l (1 .. $lines) { |
| 556 |
$response = Ifeffit::get_echo()."\n"; |
| 557 |
next unless $response; |
| 558 |
next if ($response =~ /\*\*\* correl:/); |
| 559 |
if ($response =~ /^PATH/) { |
| 560 |
my $p = $inc_mapping[$i]; |
| 561 |
$text .= "\n " . $paths{$p}->descriptor(); |
| 562 |
$text .= "\n"; |
| 563 |
##$text .= ($paths{$p}->{group}) ? " (ifeffit group = ".$paths{$p}->get('group').")\n" : "\n"; |
| 564 |
} elsif ($response =~ /^\s*feff/){ |
| 565 |
$this = (split(/\s+=\s+/, $response))[1]; |
| 566 |
$response =~ s/=/ =/g; |
| 567 |
PPMATCH1: { |
| 568 |
($pp{$1} = $4), last PPMATCH1 # OK value, beginning of line |
| 569 |
if ($response =~ /^\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 570 |
($pp{$1} = "tilt"), last PPMATCH1 # bad value, beginning of line |
| 571 |
if ($response =~ /^\s*$pp_re\s+=\*+/); |
| 572 |
($pp{$1} = $4), last PPMATCH1 # OK value, end of line |
| 573 |
if ($response =~ /,\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 574 |
($pp{$1} = "tilt"), last PPMATCH1 # bad value, end of line |
| 575 |
if ($response =~ /,\s*$pp_re\s+=\*+/); |
| 576 |
}; |
| 577 |
$text .= " ".$response; |
| 578 |
} else { |
| 579 |
$response =~ s/=/ =/g; # align = signs with reff+dr line |
| 580 |
$text .= " ".$response; |
| 581 |
PPMATCH3: { |
| 582 |
($pp{$1} = $4), last PPMATCH3 # OK value, beginning of line |
| 583 |
if ($response =~ /^\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 584 |
($pp{$1} = "tilt"), last PPMATCH3 # bad value, beginning of line |
| 585 |
if ($response =~ /^\s*$pp_re\s+=\*+/); |
| 586 |
}; |
| 587 |
PPMATCH4: { |
| 588 |
($pp{$1} = $4), last PPMATCH4 # OK value, end of line |
| 589 |
if ($response =~ /,\s*$pp_re\s+=\s+(-?\d+\.\d+)/); |
| 590 |
($pp{$1} = "tilt"), last PPMATCH4 # bad value, end of line |
| 591 |
if ($response =~ /,\s*$pp_re\s+=\*+/); |
| 592 |
}; |
| 593 |
## I know that reff is reported before dr... |
| 594 |
if ($response =~ /^\s*dr\s+=\s+/) { # report net R |
| 595 |
$text .= sprintf(" reff+dr = %9.6f\n", $pp{reff}+$pp{dr}); |
| 596 |
}; |
| 597 |
}; |
| 598 |
}; |
| 599 |
}; |
| 600 |
$pathtext{$i} = [$this,$text]; |
| 601 |
$warnings .= &check_path(\%pp); |
| 602 |
}; |
| 603 |
|
| 604 |
foreach my $t (@included) { |
| 605 |
next if (($how == 2) and |
| 606 |
($how_many =~ /sel/) and |
| 607 |
(not exists($$rhash{$inc_mapping[$t]})) ); |
| 608 |
foreach my $l (split(/\n/, $pathtext{$t}[1])) { |
| 609 |
if ($l =~ /^FEFF/) { |
| 610 |
print $fh $l."\n", 'pathid'; |
| 611 |
} else { |
| 612 |
print $fh $l."\n"; |
| 613 |
}; |
| 614 |
}; |
| 615 |
}; |
| 616 |
return $warnings; |
| 617 |
}; |
| 618 |
|
| 619 |
|
| 620 |
sub check_path { |
| 621 |
my $pp = $_[0]; |
| 622 |
my ($epsi, $warnings) = (0.0001, ""); |
| 623 |
## check for bad values |
| 624 |
my %names = ('n*s02'=>'n*S02', ss2=>'sigma^2', dr=>'delta_R', e0=>'e0', ei=>'ei', |
| 625 |
dphase=>'dphase', '3rd'=>'3rd cumulant', '4th'=>'4th cumulant'); |
| 626 |
foreach my $k (qw(n*s02 ss2 dr e0 ei dphase 3rd 4th)) { |
| 627 |
next unless exists $$pp{$k}; |
| 628 |
if ($$pp{$k} eq 'tilt') { |
| 629 |
$warnings .= "The $names{$k} of \"$$pp{descriptor}\" (path $$pp{index}) is not a number.\n\n"; |
| 630 |
$$pp{$k} = $epsi; |
| 631 |
}; |
| 632 |
}; |
| 633 |
SWITCH: { |
| 634 |
my $s02 = ($paths{data0}->vstr == 1.02005) ? 'n*s02' : 's02'; |
| 635 |
($config{warnings}{s02_neg} and |
| 636 |
($$pp{$s02} < 0)) and do { |
| 637 |
$warnings .= "The S02 of \"$$pp{descriptor}\" (path $$pp{index}) is negative.\n\n"; |
| 638 |
last SWITCH; |
| 639 |
}; |
| 640 |
($config{warnings}{s02_max} and |
| 641 |
($$pp{$s02} > $config{warnings}{s02_max})) and do { |
| 642 |
$warnings .= "The S02 of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 643 |
last SWITCH; |
| 644 |
}; |
| 645 |
|
| 646 |
($config{warnings}{ss2_neg} and |
| 647 |
($$pp{ss2} < 0)) and do { |
| 648 |
$warnings .= "The sigma^2 of \"$$pp{descriptor}\" (path $$pp{index}) is negative.\n\n"; |
| 649 |
last SWITCH; |
| 650 |
}; |
| 651 |
($config{warnings}{ss2_max} and |
| 652 |
($$pp{ss2} > $config{warnings}{ss2_max})) and do { |
| 653 |
$warnings .= "The sigma^2 of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 654 |
last SWITCH; |
| 655 |
}; |
| 656 |
($config{warnings}{dr_max} and |
| 657 |
(abs($$pp{dr}) > $config{warnings}{dr_max})) and do { |
| 658 |
$warnings .= "The delta_R of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 659 |
last SWITCH; |
| 660 |
}; |
| 661 |
($config{warnings}{e0_max} and |
| 662 |
(abs($$pp{e0}) > $config{warnings}{e0_max})) and do { |
| 663 |
$warnings .= "The e0 of \"$$pp{descriptor}\" (path $$pp{index}) is greater than $config{warnings}{e0_max} eV.\n\n"; |
| 664 |
last SWITCH; |
| 665 |
}; |
| 666 |
($config{warnings}{ei_max} and |
| 667 |
(abs($$pp{ei}) > $config{warnings}{ei_max})) and do { |
| 668 |
$warnings .= "The ei of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 669 |
last SWITCH; |
| 670 |
}; |
| 671 |
($config{warnings}{dphase_max} and |
| 672 |
(abs($$pp{dphase}) > $config{warnings}{dphase_max})) and do { |
| 673 |
$warnings .= "The dphase of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 674 |
last SWITCH; |
| 675 |
}; |
| 676 |
($config{warnings}{'3rd_max'} and |
| 677 |
(abs($$pp{'3rd'}) > $config{warnings}{'3rd_max'})) and do { |
| 678 |
$warnings .= "The 3rd cumulant of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 679 |
last SWITCH; |
| 680 |
}; |
| 681 |
($config{warnings}{'4th_max'} and |
| 682 |
(abs($$pp{'4th'}) > $config{warnings}{'4th_max'})) and do { |
| 683 |
$warnings .= "The 4th cumulant of \"$$pp{descriptor}\" (path $$pp{index}) is suspiciously large.\n\n"; |
| 684 |
last SWITCH; |
| 685 |
}; |
| 686 |
}; |
| 687 |
return $warnings; |
| 688 |
}; |
| 689 |
|
| 690 |
|
| 691 |
sub store_ppvalues { |
| 692 |
my ($this, $rpp) = @_; |
| 693 |
my %names = ('n*s02'=>'s02', ss2=>'sigma^2', dr=>'delr', e0=>'e0', ei=>'ei', |
| 694 |
dphase=>'dphase', '3rd'=>'3rd', '4th'=>'4th'); |
| 695 |
foreach my $key (keys %$rpp) { |
| 696 |
next unless exists($names{$key}); |
| 697 |
my $p = $names{$key}; |
| 698 |
#print join("|", $key, $$rpp{$key}), $/; |
| 699 |
$paths{$this} -> make("value_$p" => $$rpp{$key}); |
| 700 |
}; |
| 701 |
}; |
| 702 |
|
| 703 |
|
| 704 |
sub show_correlations { |
| 705 |
my $fh = $_[0]; |
| 706 |
my @order; |
| 707 |
foreach (@gds) { |
| 708 |
push @order, $_->name if ($_->type eq 'guess'); |
| 709 |
}; |
| 710 |
my $nd = 0; |
| 711 |
my %bkg_map; |
| 712 |
foreach my $d (keys %paths) { # get all bkg params from all data sets |
| 713 |
next unless (ref($paths{$d}) =~ /Ifeffit/); |
| 714 |
next unless ($paths{$d}->type eq 'data'); |
| 715 |
$nd = $paths{$d}->get('data_index'); |
| 716 |
if ($paths{$d}->get('do_bkg') eq 'yes') { |
| 717 |
my $i = 1; |
| 718 |
my $bkg = sprintf("bkg%2.2d_%2.2d", $nd, $i); |
| 719 |
while (Ifeffit::get_scalar($bkg)) { |
| 720 |
push @order, $bkg; |
| 721 |
++$i; |
| 722 |
$bkg = sprintf("bkg%2.2d_%2.2d", $nd, $i); |
| 723 |
}; |
| 724 |
}; |
| 725 |
my $this = sprintf("bkg%2.2d_XX", $nd); |
| 726 |
$bkg_map{$this} = $paths{$d}->descriptor(); |
| 727 |
}; |
| 728 |
my @correls; |
| 729 |
my $cormin = $paths{&first_data}->{cormin}; |
| 730 |
$paths{data0} -> dispose("\n## get correlations\n", $dmode); |
| 731 |
$paths{data0} -> dispose('correl(x=@all, y=@all, save, min=0)', $dmode); |
| 732 |
|
| 733 |
foreach my $i (0 .. $#order) { |
| 734 |
INNER: foreach my $j ($i .. $#order) { |
| 735 |
next INNER if ($i == $j); |
| 736 |
## default is to skip correlations between background parameters |
| 737 |
## belonging to the same data set |
| 738 |
next INNER if ((lc($config{data}{bkg_corr}) eq 'no') and |
| 739 |
($order[$i] =~ /bkg(\d\d)_\d\d/) and |
| 740 |
($order[$j] =~ /bkg$1_\d\d/)); |
| 741 |
my $cor = join("_", "correl", $order[$j], $order[$i]); |
| 742 |
my $val = Ifeffit::get_scalar($cor); |
| 743 |
next INNER unless (abs($val) > $cormin); |
| 744 |
push @correls, [$order[$i], $order[$j], $val]; |
| 745 |
}; |
| 746 |
}; |
| 747 |
$paths{data0} -> dispose("\n", $dmode); |
| 748 |
@correls = sort { abs(@$b[2]) <=> abs(@$a[2]) } @correls; |
| 749 |
print $fh "\n\nCorrelations between variables:\n"; |
| 750 |
foreach (@correls) { |
| 751 |
my $str = sprintf(" %10s and %-10s --> %7.4f\n", @$_); |
| 752 |
print $fh $str; |
| 753 |
}; |
| 754 |
print $fh "All other correlations are below $cormin\n\n"; |
| 755 |
my $did_bkg = grep {$paths{$_}->get('do_bkg') eq 'yes'} (&all_data); |
| 756 |
return unless $did_bkg; |
| 757 |
foreach my $k (sort (keys %bkg_map)) { |
| 758 |
print $fh sprintf("Background parameters \"%s\" belong to data set %s\n", |
| 759 |
$k, $bkg_map{$k}); |
| 760 |
}; |
| 761 |
}; |
| 762 |
|
| 763 |
## END OF THE PALETTES SUBSECTION |
| 764 |
|