| 1 |
# -*- cperl -*- |
| 2 |
## This file is part of Artemis, copyright (c) 2002-2006 Bruce Ravel |
| 3 |
## |
| 4 |
|
| 5 |
###=================================================================== |
| 6 |
### log viewer subsystem |
| 7 |
###=================================================================== |
| 8 |
|
| 9 |
sub logviewer { |
| 10 |
|
| 11 |
#Echo("You can only look at the log viewer when the data, feff, path or gsd views are showing"), |
| 12 |
# return unless ($current_canvas =~ /(feff|gsd|path|op)/); |
| 13 |
%log_params = (param => 'Statistical parameters', |
| 14 |
absorber => '', |
| 15 |
scatterer => '', |
| 16 |
is_einstein => 0, |
| 17 |
average => 0, |
| 18 |
force => 0, |
| 19 |
zero => 0, |
| 20 |
prefer => $config{logview}{prefer}); |
| 21 |
|
| 22 |
my $logview = $_[0] -> Frame(-relief=>'flat', |
| 23 |
#@window_size, |
| 24 |
-borderwidth=>0, |
| 25 |
-highlightcolor=>$config{colors}{background}); |
| 26 |
|
| 27 |
my $fr = $logview -> Frame(-background => $config{colors}{background2}, |
| 28 |
-borderwidth => 2, |
| 29 |
-relief => 'groove', |
| 30 |
) |
| 31 |
-> pack(-padx=>0, -pady=>0, -fill=>'x'); |
| 32 |
$fr -> Label(-text=>"Examine log files", @title2, -background => $config{colors}{background2},) |
| 33 |
-> pack(-side=>'left', -anchor=>'w', -padx=>6); |
| 34 |
## $widgets{log_latest} = $fr -> Label(-text=>'', |
| 35 |
## -font=>$config{fonts}{bold}, |
| 36 |
## -foreground=>$config{colors}{foreground}, |
| 37 |
## -background => $config{colors}{background2}) |
| 38 |
## -> pack(-side=>'right', -anchor=>'w', -padx=>6); |
| 39 |
$widgets{log_current} = $fr -> Label(-text=>'', |
| 40 |
-font=>$config{fonts}{bold}, |
| 41 |
-foreground=>$config{colors}{foreground}, |
| 42 |
-background => $config{colors}{background2},) |
| 43 |
-> pack(-side=>'right', -anchor=>'w', -padx=>6); |
| 44 |
$fr -> Label(-text=>'Current fit:', |
| 45 |
-font=>$config{fonts}{bold}, |
| 46 |
-foreground=>$config{colors}{button}, |
| 47 |
-background => $config{colors}{background2},) |
| 48 |
-> pack(-side=>'right', -anchor=>'w', -padx=>0); |
| 49 |
$fr -> Label(-text=>'Displaying:', |
| 50 |
-font=>$config{fonts}{bold}, |
| 51 |
-foreground=>$config{colors}{button}); |
| 52 |
## -> pack(-side=>'right', -anchor=>'w', -padx=>0); |
| 53 |
|
| 54 |
my $lfr = $logview -> LabFrame(-label=>'Fits', -labelside=>'acrosstop', |
| 55 |
-width=>14) |
| 56 |
-> pack(-side=>'left', -fill=>'y'); |
| 57 |
|
| 58 |
$widgets{loglistbox} = $lfr -> Scrolled('HList', |
| 59 |
-scrollbars => 'osoe', |
| 60 |
-background => 'white', |
| 61 |
-selectmode => 'extended', |
| 62 |
-selectbackground => $config{colors}{selected}, |
| 63 |
-cursor => $mouse_over_cursor, |
| 64 |
-command => |
| 65 |
sub{ |
| 66 |
#&display_file('', $widgets{loglistbox}->infoData($widgets{loglistbox}->infoSelection)) |
| 67 |
logview_show($config{log}{style}); |
| 68 |
}, |
| 69 |
) |
| 70 |
-> pack(-side=>'top', -expand=>1, -fill=>'both'); |
| 71 |
$widgets{loglistbox}->Subwidget("xscrollbar")->configure(-background=>$config{colors}{background}); |
| 72 |
$widgets{loglistbox}->Subwidget("yscrollbar")->configure(-background=>$config{colors}{background}); |
| 73 |
$widgets{loglistbox}->bind('<ButtonPress-3>',\&logview_post_menu); |
| 74 |
BindMouseWheel($widgets{loglistbox}); |
| 75 |
|
| 76 |
$widgets{log_select} = $lfr -> Button(-text=>'Select all', |
| 77 |
@button2_list,) |
| 78 |
-> pack(-side=>'top', -fill=>'x'); |
| 79 |
$lfr -> Button(-text=>'Clear selection', |
| 80 |
@button2_list, |
| 81 |
-command=>sub{$widgets{loglistbox}->selectionClear(); |
| 82 |
$widgets{loglistbox}->anchorClear();}) |
| 83 |
-> pack(-side=>'top', -fill=>'x'); |
| 84 |
|
| 85 |
|
| 86 |
my $right = $logview -> Frame() |
| 87 |
-> pack(-side=>'right', -fill=>'both', -expand=>1); |
| 88 |
|
| 89 |
$lfr = $right -> LabFrame(-label=>'Choose a parameter', -labelside=>'acrosstop',) |
| 90 |
-> pack(-side=>'top', -fill=>'x'); |
| 91 |
$widgets{log_param_list} = $lfr -> BrowseEntry(-font=>$config{fonts}{med}, |
| 92 |
(($is_windows) ? () : |
| 93 |
(-disabledforeground => $config{colors}{foreground}, |
| 94 |
-state => 'readonly')), |
| 95 |
-width=>25, |
| 96 |
-variable=>\$log_params{param}, |
| 97 |
-browsecmd=>\&logview_write_report, |
| 98 |
) |
| 99 |
-> pack(-padx=>1, -pady=>1, -fill=>'x'); |
| 100 |
$lfr -> Button(-text=>'Get parameters from Guess, Def, Set list', |
| 101 |
@button2_list, |
| 102 |
-command=>sub{&logview_param_list('gds')}) |
| 103 |
-> pack(-padx=>1, -pady=>1, -fill=>'x'); |
| 104 |
|
| 105 |
$widgets{log_write} = $right -> |
| 106 |
Button(-text=>'Parameter report', @button2_list, |
| 107 |
-command=>\&logview_write_report ) |
| 108 |
-> pack(-side=>'top', -fill=>'x', -pady=>2, -padx=>3); |
| 109 |
|
| 110 |
$lfr = $right -> LabFrame(-label=>'Calculations', -labelside=>'acrosstop',) |
| 111 |
-> pack(-side=>'top', -fill=>'x', -pady=>3); |
| 112 |
$widgets{log_av} = |
| 113 |
$lfr -> Checkbutton(-text=>'Compute the average value', |
| 114 |
-selectcolor=>$config{colors}{check}, |
| 115 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 116 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 117 |
-variable=>\$log_params{average}, |
| 118 |
-onvalue=>1, -offvalue=>0, -anchor=>'w', |
| 119 |
-command=>sub{ |
| 120 |
if ($log_params{einstein}) { |
| 121 |
$widgets{log_ei} -> deselect; |
| 122 |
$widgets{log_al} -> configure(-foreground => ($log_params{einstein}) ? |
| 123 |
$config{colors}{activehighlightcolor} : |
| 124 |
$config{colors}{disabledforeground}); |
| 125 |
$widgets{log_sl} -> configure(-foreground => ($log_params{einstein}) ? |
| 126 |
$config{colors}{activehighlightcolor} : |
| 127 |
$config{colors}{disabledforeground}); |
| 128 |
$widgets{log_ae} -> configure(-state => ($log_params{einstein}) ? |
| 129 |
'normal' : 'disabled'); |
| 130 |
$widgets{log_se} -> configure(-state => ($log_params{einstein}) ? |
| 131 |
'normal' : 'disabled'); |
| 132 |
}; |
| 133 |
}) |
| 134 |
-> pack(-padx=>1, -pady=>1, -fill=>'x'); |
| 135 |
$widgets{log_ei} = |
| 136 |
$lfr -> Checkbutton(-text=>'Fit Einstein temp. to sigma^2 values', |
| 137 |
-selectcolor=>$config{colors}{check}, |
| 138 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 139 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 140 |
-variable=>\$log_params{einstein}, |
| 141 |
-onvalue=>1, -offvalue=>0, -anchor=>'w', |
| 142 |
-command=>sub{ |
| 143 |
$widgets{log_av} -> deselect if $log_params{average}; |
| 144 |
$widgets{log_al} -> configure(-foreground => ($log_params{einstein}) ? |
| 145 |
$config{colors}{activehighlightcolor} : |
| 146 |
$config{colors}{disabledforeground}); |
| 147 |
$widgets{log_sl} -> configure(-foreground => ($log_params{einstein}) ? |
| 148 |
$config{colors}{activehighlightcolor} : |
| 149 |
$config{colors}{disabledforeground}); |
| 150 |
$widgets{log_ae} -> configure(-state => ($log_params{einstein}) ? |
| 151 |
'normal' : 'disabled'); |
| 152 |
$widgets{log_se} -> configure(-state => ($log_params{einstein}) ? |
| 153 |
'normal' : 'disabled'); |
| 154 |
}) |
| 155 |
-> pack(-padx=>1, -pady=>1, -fill=>'x'); |
| 156 |
$fr = $lfr -> Frame() |
| 157 |
-> pack(-padx=>1, -pady=>1, -fill=>'x'); |
| 158 |
$widgets{log_al} = $fr -> Label(-text=>'Absorber: ', -foreground=>$config{colors}{disabledforeground}) |
| 159 |
-> pack(-side=>'left', -expand=>1); |
| 160 |
$widgets{log_ae} = $fr -> Entry(-width=>3, -textvariable=>\$log_params{absorber}, -state=>'disabled') |
| 161 |
-> pack(-side=>'left', -expand=>1); |
| 162 |
$widgets{log_sl} = $fr -> Label(-text=>'Scatterer: ', -foreground=>$config{colors}{disabledforeground}) |
| 163 |
-> pack(-side=>'left', -expand=>1); |
| 164 |
$widgets{log_se} = $fr -> Entry(-width=>3, -textvariable=>\$log_params{scatterer}, -state=>'disabled') |
| 165 |
-> pack(-side=>'left', -expand=>1); |
| 166 |
|
| 167 |
$lfr -> Frame(-borderwidth=>2, -relief=>'sunken', -height=>2) |
| 168 |
-> pack(-side=>'top', -pady=>4, -padx=>8, -fill=>'x'); |
| 169 |
|
| 170 |
$widgets{log_rfactor} = $lfr -> Radiobutton(-text => "Prefer R-factor", |
| 171 |
-variable => \$log_params{prefer}, |
| 172 |
-value => 'rfactor', |
| 173 |
-selectcolor => $config{colors}{check}, |
| 174 |
-foreground => $config{colors}{activehighlightcolor}, |
| 175 |
-activeforeground => $config{colors}{activehighlightcolor}, |
| 176 |
-state => 'disabled', |
| 177 |
-command => sub{$log_params{param} = 'Statistical parameters'; |
| 178 |
&logview_write_report; |
| 179 |
&logview_plot}, |
| 180 |
) |
| 181 |
-> pack(-side=>'top', -expand=>1, -anchor=>'w'); |
| 182 |
$widgets{log_chinu} = $lfr -> Radiobutton(-text => "Prefer reduced chi-square", |
| 183 |
-variable => \$log_params{prefer}, |
| 184 |
-value => 'chinu', |
| 185 |
-selectcolor => $config{colors}{check}, |
| 186 |
-foreground => $config{colors}{activehighlightcolor}, |
| 187 |
-activeforeground => $config{colors}{activehighlightcolor}, |
| 188 |
-state => 'disabled', |
| 189 |
-command => sub{$log_params{param} = 'Statistical parameters'; |
| 190 |
&logview_write_report; |
| 191 |
&logview_plot}, |
| 192 |
) |
| 193 |
-> pack(-side=>'top', -expand=>1, -anchor=>'w'); |
| 194 |
|
| 195 |
$lfr -> Frame(-borderwidth=>2, -relief=>'sunken', -height=>2) |
| 196 |
-> pack(-side=>'top', -pady=>4, -padx=>8, -fill=>'x'); |
| 197 |
|
| 198 |
$widgets{log_zero} = $lfr -> Checkbutton(-text => "Show y=0 in plot", |
| 199 |
-variable => \$log_params{zero}, |
| 200 |
-onvalue =>1, |
| 201 |
-offvalue =>0, |
| 202 |
-anchor =>'w', |
| 203 |
-selectcolor => $config{colors}{check}, |
| 204 |
-foreground => $config{colors}{activehighlightcolor}, |
| 205 |
-activeforeground => $config{colors}{activehighlightcolor}, |
| 206 |
-command => sub{&logview_write_report; |
| 207 |
&logview_plot}, |
| 208 |
) |
| 209 |
-> pack(-side=>'top', -expand=>1, -anchor=>'w'); |
| 210 |
|
| 211 |
##$widgets{log_plot} = $right -> |
| 212 |
## Button(-text=>'Plot report', @button2_list, -state=>'disabled', |
| 213 |
## -command=>\&logview_plot ) |
| 214 |
## -> pack(-side=>'top', -fill=>'x', -pady=>2); |
| 215 |
##$fr = $right -> Frame() |
| 216 |
## -> pack(-side=>'top', -fill=>'x', -pady=>2); |
| 217 |
|
| 218 |
$widgets{log_summaries} = $right -> |
| 219 |
Button(-text=>'Quick summaries of selected fits', @button2_list, |
| 220 |
-command=>\&logview_quick_summary ) |
| 221 |
-> pack(-side=>'top', -fill=>'x', -pady=>12, -padx=>3); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
$widgets{help_logview} = |
| 226 |
$right -> Button(-text=>'Document: Log viewer', @button2_list, |
| 227 |
-command=>sub{pod_display("artemis_logview.pod")} ) |
| 228 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>2); |
| 229 |
|
| 230 |
|
| 231 |
return $logview; |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
sub populate_logview { |
| 237 |
## --- fill the HList with fit labels |
| 238 |
opendir F, File::Spec->catfile($project_folder, "fits"); |
| 239 |
my @fits = sort( grep {/fit\d+/ and -d File::Spec->catfile($project_folder, "fits", $_)} readdir(F) ); |
| 240 |
closedir F; |
| 241 |
$widgets{loglistbox} -> delete('all'); |
| 242 |
my @which; |
| 243 |
my $count = 0; |
| 244 |
foreach my $f (@fits) { |
| 245 |
local $| = 1; |
| 246 |
next unless -e File::Spec->catfile($project_folder, "fits", $f, 'label'); |
| 247 |
open LL, File::Spec->catfile($project_folder, "fits", $f, 'label'); |
| 248 |
my $label = <LL>; |
| 249 |
close LL; |
| 250 |
push(@which, $count) if ($f eq $fits[0]); |
| 251 |
push(@which, $count) if ($f eq $fits[-1]); |
| 252 |
my $add = 1; |
| 253 |
foreach my $p (keys %paths) { |
| 254 |
next unless ($paths{$p}->type eq 'fit'); |
| 255 |
next unless $paths{$p}->get('parent'); |
| 256 |
next unless ($paths{$p}->get('folder') eq $f); |
| 257 |
$add = 0 if $list -> info('hidden',$p); |
| 258 |
$add = 1 if ($list->getmode($paths{$p}->get('parent')) eq 'open'); |
| 259 |
last; |
| 260 |
}; |
| 261 |
## don't add if this one is hidden |
| 262 |
if ($add) { |
| 263 |
$widgets{loglistbox} |
| 264 |
-> add($count, |
| 265 |
-itemtype => 'text', |
| 266 |
-text => $label, |
| 267 |
-data => File::Spec->catfile($project_folder, "fits", $f, "log")); |
| 268 |
++$count; |
| 269 |
}; |
| 270 |
}; |
| 271 |
## --- set the callback on the "select all" button |
| 272 |
$widgets{log_select} -> configure(-command=>sub{$widgets{loglistbox}->selectionSet(@which)}); |
| 273 |
## --- fill the list of parameters |
| 274 |
logview_param_list('gds'); |
| 275 |
## --- disable write button if there are no unhidden fits |
| 276 |
$widgets{log_write} -> configure(-state=>($count) ? 'normal' : 'disabled'); |
| 277 |
$widgets{log_summaries} -> configure(-state=>($count) ? 'normal' : 'disabled'); |
| 278 |
## --- disable the plot button |
| 279 |
##$widgets{log_plot} -> configure(-state=>'disabled'); |
| 280 |
$log_params{force} = 0; |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
sub logview_post_menu { |
| 286 |
|
| 287 |
## figure out where the user clicked |
| 288 |
my $w = shift; |
| 289 |
my $Ev = $w->XEvent; |
| 290 |
delete $w->{'shiftanchor'}; |
| 291 |
my $entry = $w->GetNearest($Ev->y, 1); |
| 292 |
return unless (defined($entry) and length($entry)); |
| 293 |
|
| 294 |
## select and anchor the right-clicked parameter |
| 295 |
$w->selectionClear; |
| 296 |
$w->anchorSet($entry); |
| 297 |
$w->selectionSet($entry); |
| 298 |
|
| 299 |
## need to know how many fits there are... |
| 300 |
opendir F, File::Spec->catfile($project_folder, "fits"); |
| 301 |
my @fits = sort( grep {/fit\d+/ and -d File::Spec->catfile($project_folder, "fits", $_)} readdir(F) ); |
| 302 |
closedir F; |
| 303 |
|
| 304 |
## post the message with parameter-appropriate text |
| 305 |
my $which = $w->selectionGet(); |
| 306 |
$which = (ref($which) eq 'ARRAY') ? $$which[0] : $which; |
| 307 |
my $label = $widgets{loglistbox}->entrycget($entry, '-text'); |
| 308 |
my ($X, $Y) = ($Ev->X, $Ev->Y); |
| 309 |
$top -> |
| 310 |
Menu(-tearoff=>0, |
| 311 |
-menuitems=>[[ command=>"Show raw log file for \"$label\"", |
| 312 |
-command=>[\&logview_show, 'raw'] |
| 313 |
], |
| 314 |
[ command=>"Show column view of log file for \"$label\"", |
| 315 |
-command=>[\&logview_show, 'column'] |
| 316 |
], |
| 317 |
[ command=>"Show quick view of log file for \"$label\"", |
| 318 |
-command=>[\&logview_show, 'quick'] |
| 319 |
], |
| 320 |
[ command=>"Show operational view of log file for \"$label\"", |
| 321 |
-command=>[\&logview_show, 'operational'] |
| 322 |
], |
| 323 |
"-", |
| 324 |
[ command=>"Get parameter list from \"$label\"", |
| 325 |
-command=>sub{logview_param_list('file', $widgets{loglistbox}->infoData($widgets{loglistbox}->infoSelection))} |
| 326 |
], |
| 327 |
"-", |
| 328 |
[ command=>"Restore the \"$label\" fit model", |
| 329 |
-command=>sub{logview_restore_model($which)}, |
| 330 |
-state=>($#fits) ? 'normal' : 'disabled'] |
| 331 |
]) |
| 332 |
-> Post($X, $Y); |
| 333 |
$w -> break; |
| 334 |
|
| 335 |
|
| 336 |
}; |
| 337 |
|
| 338 |
sub logview_show { |
| 339 |
my @save = @log_type; |
| 340 |
SW: { |
| 341 |
@log_type = ('Raw log file', 'raw'), last SW if ($_[0] eq 'raw'); |
| 342 |
@log_type = ('Column file', 'column'), last SW if ($_[0] eq 'column'); |
| 343 |
@log_type = ('Quick view', 'quick'), last SW if ($_[0] eq 'quick'); |
| 344 |
@log_type = ('Operational view', 'operational'), last SW if ($_[0] eq 'operational'); |
| 345 |
@log_type = ('Raw log file', 'raw'); |
| 346 |
}; |
| 347 |
$current_file = $widgets{loglistbox}->infoData($widgets{loglistbox}->infoSelection); |
| 348 |
log_file_display('files', $current_file); |
| 349 |
Echo("Showed $log_type[0] for $current_file"); |
| 350 |
@log_type = @save; |
| 351 |
raise_palette('files'); |
| 352 |
}; |
| 353 |
|
| 354 |
|
| 355 |
sub logview_param_list { |
| 356 |
my ($how, $file) = @_; |
| 357 |
my @vars; |
| 358 |
if ($how eq 'gds') { |
| 359 |
## names of all non-skip parameters |
| 360 |
@vars = map { $_->name } (grep {$_->type !~ /s(e|ki)p/} @gds); |
| 361 |
} else { |
| 362 |
return unless (-e $file); |
| 363 |
my $data = Ifeffit::ArtemisLog -> new($file); |
| 364 |
push @vars, sort($data -> list('guess')), sort($data -> list('def')), |
| 365 |
sort($data -> list('set')); |
| 366 |
}; |
| 367 |
unshift @vars, "Statistical parameters"; #, "Data parameters"; |
| 368 |
#'Chi-square', 'Reduced Chi-square', 'R-factor'; |
| 369 |
$widgets{log_param_list} -> delete(0,'end'); |
| 370 |
my $seen = 0; |
| 371 |
foreach my $v (@vars) { |
| 372 |
$widgets{log_param_list} -> insert('end', $v); |
| 373 |
$seen = 1 if (lc($log_params{param}) eq lc($v)); |
| 374 |
}; |
| 375 |
($log_params{param} = 'Statistical parameters') unless $seen; |
| 376 |
Echo("Loaded parameter list from GDS page"), return if ($how eq 'gds'); |
| 377 |
Echo("Loaded parameter list from the log file for ".$widgets{loglistbox}->selectionGet); |
| 378 |
}; |
| 379 |
|
| 380 |
|
| 381 |
sub logview_write_report { |
| 382 |
my $listbox = $widgets{loglistbox}; |
| 383 |
#my $rhash = $_[1]; |
| 384 |
$log_params{is_einstein} = 0; |
| 385 |
my @list; |
| 386 |
my @logs; |
| 387 |
$widgets{log_select}->invoke unless ($listbox->selectionGet); |
| 388 |
foreach my $l ($listbox->selectionGet) { |
| 389 |
push @list, $l; |
| 390 |
push @logs, Ifeffit::ArtemisLog->new($listbox->infoData($l)); |
| 391 |
}; |
| 392 |
Error("You have not selected any log files"), return unless @list; |
| 393 |
## set param name so that Ifeffit::ArtemisLog will recognize it |
| 394 |
my $param = $log_params{param}; |
| 395 |
#($param = 'chisqr') if ($param eq 'Chi-square'); |
| 396 |
#($param = 'chinu') if ($param eq 'Reduced Chi-square'); |
| 397 |
#($param = 'rfact') if ($param eq 'R-factor'); |
| 398 |
(($log_params{average}, $log_params{einstein}) = (0,0)) if ($param eq 'Statistical parameters'); |
| 399 |
(($log_params{average}, $log_params{einstein}) = (0,0)) if ($param eq 'Data parameters'); |
| 400 |
## compute average and standard deviation of best fits values, if requested |
| 401 |
my ($sum, $sdv) = (0, 0); |
| 402 |
if (($log_params{average}) and ($#logs>=1)) { |
| 403 |
map { $sum += ($_ -> get($param))[0] } @logs; |
| 404 |
$sum /= ($#logs+1); |
| 405 |
map { $sdv += ( ($_ -> get($param))[0] - $sum )**2 } @logs; |
| 406 |
$sdv /= $#logs; |
| 407 |
}; |
| 408 |
## do einstein fit if requested |
| 409 |
my ($thetae, $dth, $offset, $doff) = (0,0,0,0); |
| 410 |
if ($log_params{einstein}) { |
| 411 |
($thetae, $dth, $offset, $doff) = |
| 412 |
&logview_do_einstein(\@logs, $param);# pass the data |
| 413 |
}; |
| 414 |
## write report |
| 415 |
my $message = "# $props{'Project title'}\n"; |
| 416 |
$message .= "# report on \"$log_params{param}\"\n"; |
| 417 |
($message .= sprintf("# the average value of $log_params{param} is %.5f +/- %.5f\n", $sum, sqrt($sdv))) |
| 418 |
if ($log_params{average} and ($#logs>=1)); |
| 419 |
## deal with the einstein fit |
| 420 |
$log_params{absorber} ||= " "; |
| 421 |
$log_params{scatterer} ||= " "; |
| 422 |
EINS: { |
| 423 |
($log_params{einstein} and ($thetae>0)) and do { |
| 424 |
$message .= sprintf("# these data fit an Einstein temperature of %8.3f +/- %.3f\n", $thetae, $dth); |
| 425 |
$message .= sprintf("# with an offset of %.6f +/- %.6f\n", $offset, $doff); |
| 426 |
$log_params{is_einstein} = 1; |
| 427 |
last EINS; |
| 428 |
}; |
| 429 |
($log_params{einstein} and ($thetae==-1)) and do { |
| 430 |
$message .= "# The Einstein fit could not be done because\n# Xray::Absorption is not installed\n"; |
| 431 |
last EINS; |
| 432 |
}; |
| 433 |
($log_params{einstein} and ($thetae==-2)) and do { |
| 434 |
$message .= "# The Einstein fit could not be done because\n# \`$log_params{absorber}\' is not an element symbol\n"; |
| 435 |
last EINS; |
| 436 |
}; |
| 437 |
($log_params{einstein} and ($thetae==-3)) and do { |
| 438 |
$message .= "# The Einstein fit could not be done because\n# \`$log_params{scatterer}\' is not an element symbol\n"; |
| 439 |
last EINS; |
| 440 |
}; |
| 441 |
($log_params{einstein} and ($thetae==-4)) and do { |
| 442 |
$message .= "# The Einstein fit could not be done because\n# there are fewer than three data points\n"; |
| 443 |
last EINS; |
| 444 |
}; |
| 445 |
($log_params{einstein} and ($thetae==-5)) and do { |
| 446 |
$message .= "# The Einstein fit could not be done because\n# the data arrays were of unequal length\n"; |
| 447 |
last EINS; |
| 448 |
}; |
| 449 |
($log_params{einstein} and ($thetae==-6)) and do { |
| 450 |
$message .= "# The Einstein fit could not be done because\n# the temperature array does not seem to contain temperature data\n"; |
| 451 |
last EINS; |
| 452 |
}; |
| 453 |
($log_params{einstein} and ($thetae==-7)) and do { |
| 454 |
$message .= "# The Einstein fit could not be done because\n# the sigma^2 array does not seem to contain sigma^2 data\n"; |
| 455 |
last EINS; |
| 456 |
}; |
| 457 |
}; |
| 458 |
|
| 459 |
$message .= "# -----------------------------------------------------------------\n"; |
| 460 |
|
| 461 |
if ($param eq 'Statistical parameters') { |
| 462 |
$message .= "# fit FoM R-factor Reduced_chi-square Chi-square nvar nidp\n"; |
| 463 |
foreach my $i (0..$#list) { |
| 464 |
$message .= sprintf(" %-15s %-6s %6.4f %11.3f %11.3f %3d %3d\n", |
| 465 |
"'".$listbox->itemCget($list[$i], 0, '-text')."'", |
| 466 |
$logs[$i]->get('Figure of merit'), |
| 467 |
$logs[$i]->get('rfact'), |
| 468 |
$logs[$i]->get('chinu'), |
| 469 |
$logs[$i]->get('chisqr'), |
| 470 |
$logs[$i]->get('nvar'), |
| 471 |
$logs[$i]->get('nidp'), |
| 472 |
); |
| 473 |
}; |
| 474 |
## } elsif ($param eq 'Data parameters') { |
| 475 |
## my $n_set = 0; |
| 476 |
## foreach my $d ($data->list('data')) { |
| 477 |
## ++$n_set; |
| 478 |
## $message .= "\n\n Data set $n_set\n\n"; |
| 479 |
## $message .= "# fit kw k-range dk R-range dR\n"; |
| 480 |
## foreach my $i (0..$#list) { |
| 481 |
## $message .= sprintf(" %-15s %-7s [%6.4f:%6.4f] %4.2 [%6.4f:%6.4f] %4.2\n", |
| 482 |
## "'".$listbox->itemCget($list[$i], 0, '-text')."'", |
| 483 |
## $logs[$i]->get($d, 'kw'), |
| 484 |
## $logs[$i]->get($d, 'kmin'), |
| 485 |
## $logs[$i]->get($d, 'kmax'), |
| 486 |
## $logs[$i]->get($d, 'dk'), |
| 487 |
## $logs[$i]->get($d, 'rmin'), |
| 488 |
## $logs[$i]->get($d, 'rmax'), |
| 489 |
## $logs[$i]->get($d, 'dr'), |
| 490 |
## ); |
| 491 |
## }; |
| 492 |
## }; |
| 493 |
} else { |
| 494 |
$message .= "# fit FoM $log_params{param}"; |
| 495 |
$message .= (grep {/^$param$/} $logs[0]->list('guess')) ? " +/- initial\n" : "\n"; |
| 496 |
foreach my $i (0..$#list) { |
| 497 |
my ($first, $pat) = ($list[$i], '%-20s'); |
| 498 |
## need to report error bars for guess values |
| 499 |
my $val = (grep {/^$param$/} $logs[$i]->list('guess')) ? '%15.7f %15.7f %s' : '%15.7f'; |
| 500 |
my @ll = $logs[$i]->get($param); |
| 501 |
#($ll[0] = (split(/=/, $ll[0]))[0]) if (grep {/^$param$/} $logs[$i]->list('after')); |
| 502 |
next if (($ll[0] == -999) or ($ll[0] == -998)); # skip if unused in the fit |
| 503 |
|
| 504 |
## here, one could try to evaluate a math expression if a param |
| 505 |
## is guessed as a mathexp. in stead, I'll just print the mathexp. |
| 506 |
$message .= sprintf(" %-15s %-6s $val\n", |
| 507 |
"'".$listbox->itemCget($list[$i], 0, '-text')."'", |
| 508 |
$logs[$i]->get('Figure of merit'), @ll); |
| 509 |
|
| 510 |
## need to take care about a param guessed as another param (note |
| 511 |
## this will plotz for deeply nested guessing-as-param |
| 512 |
#($ll[2] = ($logs[$i]->get($ll[2]))[2]) unless (not exists($ll[2]) or ($ll[2] =~ /-?\d*\.\d+/)); |
| 513 |
}; |
| 514 |
}; |
| 515 |
&post_message($message, "report"); |
| 516 |
if ($#logs) { |
| 517 |
##$widgets{log_plot} -> configure(-state=>'normal'); |
| 518 |
$widgets{log_rfactor} -> configure(-state=>'disabled'); |
| 519 |
$widgets{log_chinu} -> configure(-state=>'disabled'); |
| 520 |
if ($log_params{param} =~ /Statistical/) { |
| 521 |
$widgets{log_rfactor} -> configure(-state=>'normal'); |
| 522 |
$widgets{log_chinu} -> configure(-state=>'normal'); |
| 523 |
}; |
| 524 |
}; |
| 525 |
my $plot_return = &logview_plot unless ($log_params{param} eq 'Data parameters'); |
| 526 |
$top -> update(); |
| 527 |
Echo("Wrote and plotted report on \"$log_params{param}\"") unless $plot_return; |
| 528 |
}; |
| 529 |
|
| 530 |
## return (theta, d_theta, offset, d_offset) |
| 531 |
## error codes, theta= |
| 532 |
## -1 Xray::Absorption not installed |
| 533 |
## -2 Absorber symbol is not an element |
| 534 |
## -3 Scatterer symbol is not an element |
| 535 |
## -4 Not enough data points |
| 536 |
## -5 Arrays of unequal length (probably unable to harvest temperatures) |
| 537 |
## -6 temperature array does not appear to be valid data |
| 538 |
## -7 sigma^2 array does not appear to be valid data |
| 539 |
sub logview_do_einstein { |
| 540 |
my $rlogs = $_[0]; |
| 541 |
my $param = $_[1]; |
| 542 |
#return (-1,0,0,0) unless $absorption_exists; |
| 543 |
$log_params{absorber} =~ s/\s+//g; |
| 544 |
return (-2,0,0,0) unless Xray::Absorption->in_resource($log_params{absorber}); |
| 545 |
$log_params{scatterer} =~ s/\s+//g; |
| 546 |
return (-3,0,0,0) unless Xray::Absorption->in_resource($log_params{scatterer}); |
| 547 |
my $abs = Xray::Absorption->get_atomic_weight($log_params{absorber}); |
| 548 |
my $sca = Xray::Absorption->get_atomic_weight($log_params{scatterer}); |
| 549 |
my (@t, @ss, @err); |
| 550 |
foreach my $l (@$rlogs) { |
| 551 |
push @t, $l -> get('Figure of merit'); |
| 552 |
push @ss, ($l -> get($param))[0]; |
| 553 |
push @err, ($l -> get($param))[1] || 0; |
| 554 |
}; |
| 555 |
return (-4,0,0,0) unless ($#t >= 2); |
| 556 |
return (-5,0,0,0) unless (($#t == $#ss) and ($#t == $#err)); |
| 557 |
my ($t_bad, $ss_bad) = (0,0); |
| 558 |
map { ++$t_bad if (($_ < 0) or ($_ > $config{logview}{eins_temp_max})) } @t; |
| 559 |
return (-6,0,0,0) if $t_bad; |
| 560 |
map { ++$ss_bad if ($_ > $config{logview}{eins_sigma_max}) } @ss; |
| 561 |
return (-7,0,0,0) if $ss_bad; |
| 562 |
Ifeffit::put_array('eins.1', \@t); |
| 563 |
Ifeffit::put_array('eins.2', \@ss); |
| 564 |
Ifeffit::put_array('eins.3', \@err); |
| 565 |
##$paths{gsd} -> dispose("show \@group eins", $dmode); |
| 566 |
$paths{gsd} -> dispose("eins $abs $sca", $dmode); |
| 567 |
my ($th, $dth, $off, $doff) = (Ifeffit::get_scalar('eins_theta'), |
| 568 |
Ifeffit::get_scalar('delta_eins_theta'), |
| 569 |
Ifeffit::get_scalar('eins_offset'), |
| 570 |
Ifeffit::get_scalar('delta_eins_offset') ); |
| 571 |
$paths{gsd} -> dispose("unguess", $dmode); |
| 572 |
return ($th, $dth, $off, $doff); |
| 573 |
}; |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
sub logview_plot { |
| 578 |
Echo("Cannot plot data parameters"), return if ($log_params{param} eq 'Data parameters'); |
| 579 |
my $param = $log_params{param}; |
| 580 |
if ($log_params{is_einstein}) { |
| 581 |
$paths{gsd} -> dispose("set ___min = floor(eins.2)",1); |
| 582 |
my $ym = Ifeffit::get_scalar("___min"); |
| 583 |
$ym = ($ym < 0) ? 1.1*$ym : 0; |
| 584 |
my $message = "eins.1, eins.2, dy=eins.3, ymin=$ym, xmin=0, title=\"Einstein fit\", "; |
| 585 |
$message .= "xlabel=\"temperature (K)\", ylabel=\"\\gs\\u2\\d (\\A\\u-2\\d)\", "; |
| 586 |
$message .= "key=data, style=points16, color=$config{plot}{c0}"; |
| 587 |
$message .= ", ymin=0" if $log_params{zero}; |
| 588 |
$message = wrap("newplot(", " ", $message) . ")\n"; |
| 589 |
$message .= "plot(eins.xx, eins.yy, style=lines, key=fit, color=$config{plot}{c1})"; |
| 590 |
$paths{gsd} -> dispose($message, $dmode); |
| 591 |
} else { |
| 592 |
## set param name so that Ifeffit::ArtemisLog will recognize it |
| 593 |
## my $param = $log_params{param}; |
| 594 |
## ($param = 'chisqr') if ($param eq 'Chi-square'); |
| 595 |
## ($param = 'chinu') if ($param eq 'Reduced Chi-square'); |
| 596 |
## ($param = 'rfact') if ($param eq 'R-factor'); |
| 597 |
my (@x, @val, @err, @fth); |
| 598 |
foreach my $l (split(/\n/, $notes{messages} -> get(qw(1.0 end)))) { |
| 599 |
next if ($l =~ /^\s*\#/); |
| 600 |
next if ($l =~ /^\s*$/); |
| 601 |
$l = substr($l, index($l, "'")+1); |
| 602 |
$l = substr($l, index($l, "'")+1); |
| 603 |
my @line = split(" ", $l); |
| 604 |
push @x, $line[0]; |
| 605 |
push @val, $line[1]; |
| 606 |
push @err, $line[2] || 0; |
| 607 |
push @fth, $line[3] || 0; |
| 608 |
}; |
| 609 |
Error("Plot aborted. You only selected one fit."), return 1 unless $#x; |
| 610 |
if ($log_params{param} =~ /Statistical/) { |
| 611 |
Ifeffit::put_array("l___og.1", \@x); |
| 612 |
if ($log_params{prefer} eq 'rfactor') { |
| 613 |
$param = "R-factor"; |
| 614 |
Ifeffit::put_array("l___og.2", \@val); |
| 615 |
} elsif ($log_params{prefer} eq 'chinu') { |
| 616 |
$param = "reduced chi-square"; |
| 617 |
Ifeffit::put_array("l___og.2", \@err); |
| 618 |
} else { |
| 619 |
$param = "chi-square"; |
| 620 |
Ifeffit::put_array("l___og.2", \@fth); |
| 621 |
}; |
| 622 |
my $message = "l___og.1, l___og.2, title=\"Report on $param\", "; |
| 623 |
$message .= "xlabel=\"Figure of merit\", ylabel=\"$param\", "; |
| 624 |
$message .= "key=$param, style=points3, markersize=4, color=$config{plot}{c0}"; |
| 625 |
$message .= ", ymin=0" if $log_params{zero}; |
| 626 |
$message = wrap("newplot(", " ", $message) . ")\n"; |
| 627 |
$paths{gsd} -> dispose($message, $dmode); |
| 628 |
} else { |
| 629 |
Ifeffit::put_array("l___og.1", \@x); |
| 630 |
Ifeffit::put_array("l___og.2", \@val); |
| 631 |
Ifeffit::put_array("l___og.3", \@err); |
| 632 |
my $message = "l___og.1, l___og.2, dy=l___og.3, title=\"Report on $param\", "; |
| 633 |
$message .= "xlabel=\"Figure of merit\", ylabel=\"$param\", "; |
| 634 |
$message .= "key=$param, style=points16, color=$config{plot}{c0}"; |
| 635 |
$message .= ", ymin=0" if $log_params{zero}; |
| 636 |
$message = wrap("newplot(", " ", $message) . ")\n"; |
| 637 |
$paths{gsd} -> dispose($message, $dmode); |
| 638 |
}; |
| 639 |
#$paths{gsd} -> dispose("plot(style=line)\n", $dmode); |
| 640 |
}; |
| 641 |
Echo("Plotted $param"); |
| 642 |
return 0; |
| 643 |
}; |
| 644 |
|
| 645 |
|
| 646 |
sub logview_quick_summary { |
| 647 |
$widgets{log_select}->invoke unless ($widgets{loglistbox}->selectionGet); |
| 648 |
$notes{messages} -> delete(qw(1.0 end)); |
| 649 |
foreach my $l ($widgets{loglistbox}->selectionGet) { |
| 650 |
my $data = Ifeffit::ArtemisLog->new($widgets{loglistbox}->infoData($l)); |
| 651 |
my $was_sum = grep {/Fitting was not performed./} ($data->get('warnings')); |
| 652 |
## header |
| 653 |
$notes{messages} -> insert('end', "Project title : " . $data -> get('Project title') . "\n"); |
| 654 |
$notes{messages} -> insert('end', "Comment : " . $data -> get('Comment') . "\n"); |
| 655 |
$notes{messages} -> insert('end', "Figure of merit : " . $data -> get('Figure of merit') . "\n"); |
| 656 |
## statistics |
| 657 |
$notes{messages} -> insert('end', $data->stats) unless $was_sum; |
| 658 |
## guesses |
| 659 |
$notes{messages} -> insert('end', $data->guess); |
| 660 |
## restraints |
| 661 |
$notes{messages} -> insert('end', $data->restraint); |
| 662 |
## separator |
| 663 |
$notes{messages} -> insert('end', $/ . "=*=" x 20 . $/ x 2); |
| 664 |
undef $data; |
| 665 |
}; |
| 666 |
$notes{messages} -> yviewMoveto(0); |
| 667 |
$top -> update; |
| 668 |
raise_palette('messages') unless $_[2]; |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
## show the latest fit if this is the head of a branch |
| 673 |
sub logview_show_fom { |
| 674 |
my $data = $paths{$current}->data; |
| 675 |
my $which = (($paths{$current}->type eq 'fit') and $paths{$current}->get('parent')) ? |
| 676 |
$current : |
| 677 |
$paths{$data.".0"}->get('thisfit'); |
| 678 |
Echo("The figure of merit for \"" . $paths{$which}->descriptor . |
| 679 |
"\" is " . $paths{$which}->get('value')) |
| 680 |
} |
| 681 |
|
| 682 |
sub logview_change_fom { |
| 683 |
logview_change_fit_property("fom"); |
| 684 |
}; |
| 685 |
sub logview_change_comment { |
| 686 |
logview_change_fit_property("comment"); |
| 687 |
}; |
| 688 |
sub logview_change_fit_property { |
| 689 |
|
| 690 |
## before doing this, shift anchor from head of fit branch to latest |
| 691 |
my $data = $paths{$current}->data; |
| 692 |
my $which = (($paths{$current}->type eq 'fit') and $paths{$current}->get('parent')) ? |
| 693 |
$current : |
| 694 |
$paths{$data.".0"}->get('thisfit'); |
| 695 |
$list->anchorSet($which); |
| 696 |
&display_properties; |
| 697 |
$top -> update; |
| 698 |
|
| 699 |
## set some variables based on what's being changed |
| 700 |
my ($long, $lclong, $old) = (q{}, q{}, q{}); |
| 701 |
SW: { |
| 702 |
($_[0] eq 'fom') and do { |
| 703 |
$long = "Figure of merit"; |
| 704 |
$lclong = lc($long); |
| 705 |
$old = $paths{$current}->get('value'); |
| 706 |
last SW; |
| 707 |
}; |
| 708 |
($_[0] eq 'comment') and do { |
| 709 |
$long = "Comment"; |
| 710 |
$lclong = lc($long); |
| 711 |
last SW; |
| 712 |
}; |
| 713 |
}; |
| 714 |
|
| 715 |
my $new = $old; |
| 716 |
my $label = "$long for \"" . $paths{$current}->short_descriptor . "\": "; |
| 717 |
my $dialog = get_string($dmode, $label, \$new); |
| 718 |
$dialog -> waitWindow; # the get_string dialog will be |
| 719 |
# destroyed once the user hits ok, |
| 720 |
# then we can move on... |
| 721 |
Echo("Not changing the $lclong for \"".$paths{$current}->short_descriptor."\""), |
| 722 |
return if ($new eq $old); |
| 723 |
my @parts = split(/\./, $current); |
| 724 |
foreach my $d (&every_data) { |
| 725 |
my $key = "$d.0.$parts[2]"; |
| 726 |
next unless $list->info('exists', $key); |
| 727 |
$paths{$key}->make(value=>$new); |
| 728 |
}; |
| 729 |
|
| 730 |
## need to make the change permanent by altering the log file |
| 731 |
my $folder = $paths{$current}->get('folder'); |
| 732 |
my $log = File::Spec->catfile($project_folder, 'fits', $folder, 'log'); |
| 733 |
do { |
| 734 |
local $/ = undef; |
| 735 |
local $| = 1; |
| 736 |
open L, $log; |
| 737 |
my $contents = <L>; |
| 738 |
close L; |
| 739 |
$contents =~ s/($long\s*:).*\n/$1 $new\n/; |
| 740 |
open L, ">".$log; |
| 741 |
print L $contents; |
| 742 |
close L; |
| 743 |
}; |
| 744 |
|
| 745 |
project_state(0); |
| 746 |
Echo("Changed $lclong for \"" . |
| 747 |
$paths{$current}->descriptor . |
| 748 |
"\" to \'$new\'"); |
| 749 |
}; |
| 750 |
|
| 751 |
## show the latest fit if this is the head of a branch |
| 752 |
sub logview_show_comment { |
| 753 |
my $data = $paths{$current}->data; |
| 754 |
my $which = (($paths{$current}->type eq 'fit') and $paths{$current}->get('parent')) ? |
| 755 |
$current : |
| 756 |
$paths{$data.".0"}->get('thisfit'); |
| 757 |
|
| 758 |
my $log = $_[1] || File::Spec->catfile($project_folder, |
| 759 |
"fits", |
| 760 |
$paths{$which}->get('folder'), |
| 761 |
"log" |
| 762 |
); |
| 763 |
my $logfile = Ifeffit::ArtemisLog -> new($log); |
| 764 |
|
| 765 |
Echo("Comment for \"" |
| 766 |
. $paths{$which}->descriptor |
| 767 |
. "\" : " |
| 768 |
. $logfile->get('Comment') |
| 769 |
) |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
### some subs for managing the fit branches in the Data and Paths List |
| 774 |
|
| 775 |
sub rename_fit { |
| 776 |
|
| 777 |
## before doing this, shift anchor from head of fit branch to latest |
| 778 |
if (($paths{$current}->type eq 'fit') and |
| 779 |
(not $paths{$current}->get('parent')) and |
| 780 |
($paths{$current}->get('thisfit'))) { # this keeps it from |
| 781 |
# plotzing when writing a |
| 782 |
# script without actually |
| 783 |
# running |
| 784 |
display_page($paths{$current}->get('thisfit')); |
| 785 |
}; |
| 786 |
$top -> update; |
| 787 |
|
| 788 |
my $old = $paths{$current}->get('lab'); |
| 789 |
my $new = $old; |
| 790 |
my $label = "Rename \"" . $paths{$current}->short_descriptor . "\" to: "; |
| 791 |
my $dialog = get_string($dmode, $label, \$new, \@rename_buffer); |
| 792 |
$dialog -> waitWindow; # the get_string dialog will be |
| 793 |
# destroyed once the user hits ok, |
| 794 |
# then we can move on... |
| 795 |
Echo("Not changing the name of \"".$paths{$current}->short_descriptor."\""), |
| 796 |
return if ($new eq $old); |
| 797 |
$new =~ s{[\"\']}{}g; |
| 798 |
project_state(0); |
| 799 |
push @rename_buffer, $new; |
| 800 |
my @parts = split(/\./, $current); |
| 801 |
foreach my $d (&every_data) { |
| 802 |
my $key = "$d.0.$parts[2]"; |
| 803 |
next unless $list->info('exists', $key); |
| 804 |
$paths{$key}->make(lab=>$new); |
| 805 |
$list -> itemConfigure($key, 0, -text=>$new); |
| 806 |
}; |
| 807 |
Echo("Renamed for \'$old\' to \'$new\'"); |
| 808 |
## --- touch the label file |
| 809 |
my $file = File::Spec->catfile($project_folder, "fits", $paths{$current}->get('folder'), "label"); |
| 810 |
open L, ">".$file; |
| 811 |
print L $new; |
| 812 |
close L; |
| 813 |
populate_logview if ($current_canvas eq 'logview'); |
| 814 |
}; |
| 815 |
|
| 816 |
|
| 817 |
sub hide_fit { |
| 818 |
|
| 819 |
## before doing this, shift anchor from head of fit branch to latest |
| 820 |
if (($paths{$current}->type eq 'fit') and |
| 821 |
(not $paths{$current}->get('parent')) and |
| 822 |
($paths{$current}->get('thisfit'))) { # this keeps it from |
| 823 |
# plotzing when writing a |
| 824 |
# script without actually |
| 825 |
# running |
| 826 |
&display_page($paths{$current}->get('thisfit')); |
| 827 |
}; |
| 828 |
$top -> update; |
| 829 |
|
| 830 |
## need to hide this fit in other data sets as well. |
| 831 |
my @parts = split(/\./, $current); |
| 832 |
Echo("Hiding ".$paths{$current}->get('lab')); |
| 833 |
foreach my $d (&every_data) { |
| 834 |
my $key = "$d.0.$parts[2]"; |
| 835 |
next unless $list->info('exists', $key); |
| 836 |
$list -> selectionClear($key); |
| 837 |
$list -> hide('entry', $key); |
| 838 |
$list -> itemConfigure($d.".0", 0, -style=>$list_styles{hidden}); |
| 839 |
}; |
| 840 |
&keyboard_up; |
| 841 |
&populate_logview; |
| 842 |
}; |
| 843 |
|
| 844 |
sub hide_selected_fits { |
| 845 |
my $anchor = $list->info('anchor') || 'data0'; |
| 846 |
foreach my $p ($list->info('selection')) { |
| 847 |
next unless (($paths{$p}->type eq 'fit') and $paths{$p}->get('parent')); |
| 848 |
my @parts = split(/\./, $p); |
| 849 |
foreach my $d (&every_data) { |
| 850 |
my $key = "$d.0.$parts[2]"; |
| 851 |
next unless $list->info('exists', $key); |
| 852 |
$list -> selectionClear($key); |
| 853 |
$list -> hide('entry', $key); |
| 854 |
$list -> itemConfigure($d.".0", 0, -style=>$list_styles{hidden}); |
| 855 |
}; |
| 856 |
}; |
| 857 |
if (($paths{$anchor}->type eq 'fit') and $paths{$anchor}->get('parent')) { |
| 858 |
my $pa = $paths{$anchor}->get('parent'); |
| 859 |
&display_page($pa); |
| 860 |
}; |
| 861 |
Echo("Hid selected fits."); |
| 862 |
}; |
| 863 |
|
| 864 |
sub show_fits { |
| 865 |
foreach my $p (keys %paths) { |
| 866 |
next unless (($paths{$p}->type eq 'fit') and $paths{$p}->get('parent')); |
| 867 |
next unless ($list->info('hidden', $p)); |
| 868 |
$list -> show('entry', $p); |
| 869 |
$list -> itemConfigure($paths{$p}->get('parent'), 0, -style=>$list_styles{enabled}); |
| 870 |
}; |
| 871 |
my $anchor = $list->info('anchor') || 'data0'; |
| 872 |
&populate_logview if ($paths{$anchor}->type eq 'fit'); |
| 873 |
Echo("Showing all fits."); |
| 874 |
}; |
| 875 |
|
| 876 |
|
| 877 |
sub discard_fit { |
| 878 |
my $solo = $_[0]; |
| 879 |
my $which = $_[0] || $current; |
| 880 |
|
| 881 |
unless ($solo) { |
| 882 |
## before doing this, shift anchor from head of fit branch to latest |
| 883 |
if (($paths{$which}->type eq 'fit') and |
| 884 |
(not $paths{$which}->get('parent')) and |
| 885 |
($paths{$which}->get('thisfit'))) { # this keeps it from |
| 886 |
# plotzing when writing a |
| 887 |
# script without actually |
| 888 |
# running |
| 889 |
&display_page($paths{$which}->get('thisfit')); |
| 890 |
}; |
| 891 |
$top -> update; |
| 892 |
$which = $current; |
| 893 |
}; |
| 894 |
|
| 895 |
## this could have already been deleted if this is called as part of |
| 896 |
## delete selected or delete all |
| 897 |
return unless exists $paths{$which}; |
| 898 |
##Echo($paths{$which}->descriptor . " is not a fit."), |
| 899 |
return unless (($paths{$which}->type eq 'fit') and $paths{$which}->get('parent')); |
| 900 |
|
| 901 |
## need to get some info while the object still exists ... |
| 902 |
my $this_count = (split(/\./, $which))[2]; |
| 903 |
my $folder = File::Spec->catfile($project_folder, |
| 904 |
"fits", |
| 905 |
$paths{$which}->get('folder')); |
| 906 |
my $above = $list->info('prev', $which); |
| 907 |
my $was = $paths{$which}->descriptor; |
| 908 |
|
| 909 |
foreach my $d (&every_data) { |
| 910 |
my $this = $d . ".0." . $this_count; |
| 911 |
## repoint thisfit in the head fit object if it pointed at this one |
| 912 |
next unless exists $paths{$this}; |
| 913 |
my $parent = $paths{$this}->parent; |
| 914 |
if ($paths{$parent}->get('thisfit') eq $this) { |
| 915 |
my $prev = $list->info('prev', $this); |
| 916 |
if (($paths{$prev}->type eq 'fit') and $paths{$prev}->get('parent')) { |
| 917 |
$paths{$parent}->make(thisfit=>$prev); |
| 918 |
} else { |
| 919 |
$paths{$parent}->make(thisfit=>0); |
| 920 |
}; |
| 921 |
}; |
| 922 |
|
| 923 |
## erase these data from Ifeffit |
| 924 |
my $group = $paths{$this}->get('group'); |
| 925 |
$paths{$this}->dispose("erase \@group $group\n", $dmode); |
| 926 |
|
| 927 |
## undef the object |
| 928 |
delete $paths{$this}; |
| 929 |
|
| 930 |
## remove the DPL entry |
| 931 |
$list->delete('entry', $this); |
| 932 |
}; |
| 933 |
|
| 934 |
## delete the fit folder |
| 935 |
rmtree($folder,0,0) if -d $folder; |
| 936 |
|
| 937 |
## reset fit_count if it was the latest |
| 938 |
if ($fit{count} == $this_count+1) { |
| 939 |
--$fit{count}; |
| 940 |
foreach my $d (&every_data) { |
| 941 |
## what about Sum? |
| 942 |
if (not exists $fit{recent}) { |
| 943 |
$list -> entryconfigure($d.".0", -text=>"Fit"); |
| 944 |
} elsif ($fit{recent} eq 'fit') { |
| 945 |
$list -> entryconfigure($d.".0", -text=>"Fit"); |
| 946 |
} else { |
| 947 |
$list -> entryconfigure($d.".0", -text=>"Sum"); |
| 948 |
}; |
| 949 |
##$list -> entryconfigure($d.".0", -text=>"Fit [$fit{count}]"); |
| 950 |
}; |
| 951 |
}; |
| 952 |
|
| 953 |
return if $solo; |
| 954 |
## and finally, redisplay the newly anchored list entry |
| 955 |
&display_page($above); |
| 956 |
project_state(0); |
| 957 |
Echo("Discarded \"$was\"."); |
| 958 |
}; |
| 959 |
|
| 960 |
|
| 961 |
sub discard_selected_fits { |
| 962 |
my $data = $paths{$current}->data; |
| 963 |
foreach my $f ($list->info('selection')) { |
| 964 |
next unless (($paths{$f}->type eq 'fit') and $paths{$f}->get('parent')); |
| 965 |
discard_fit($f); |
| 966 |
}; |
| 967 |
&display_page($data); |
| 968 |
project_state(0); |
| 969 |
Echo("Discarded selected fits."); |
| 970 |
}; |
| 971 |
|
| 972 |
sub discard_all_fits { |
| 973 |
my $data = $paths{$current}->data; |
| 974 |
my @list = sort(keys %paths); |
| 975 |
foreach my $f (@list) { |
| 976 |
next unless exists $paths{$f}; |
| 977 |
next unless (($paths{$f}->type eq 'fit') and $paths{$f}->get('parent')); |
| 978 |
discard_fit($f); |
| 979 |
}; |
| 980 |
foreach my $d (&every_data) { |
| 981 |
## what about Sum? |
| 982 |
$list -> entryconfigure($d.".0", -text=>"Fit"); |
| 983 |
}; |
| 984 |
#$fit{new} = 1; |
| 985 |
#$fit{count} = 0; |
| 986 |
#$fit{count_full} = 0; |
| 987 |
%fit = (index=>1, count=>0, count_full=>0, new=>1, label=>"", comment=>"", fom=>0); |
| 988 |
&display_page($data); |
| 989 |
project_state(0); |
| 990 |
Echo("Discarded all fits."); |
| 991 |
}; |
| 992 |
|
| 993 |
|
| 994 |
sub save_fit { |
| 995 |
my ($suff, $space) = @_; |
| 996 |
my $kind = "Fit"; |
| 997 |
($kind = "Residual") if ($suff eq 'res'); |
| 998 |
($kind = "Background") if ($suff eq 'bkg'); |
| 999 |
my $sp = $space; |
| 1000 |
($sp = "R") if ($sp =~ /[rR]/); |
| 1001 |
|
| 1002 |
my $data = $paths{$current}->data; |
| 1003 |
## use the latest fit unless this is a fit and not the head of the |
| 1004 |
## fit branch |
| 1005 |
my $to_save = (($paths{$current}->type eq 'fit') and $paths{$current}->get('parent')) ? |
| 1006 |
$current : |
| 1007 |
$paths{$data.".0"}->get('thisfit'); |
| 1008 |
my $init = $paths{$to_save}->descriptor; |
| 1009 |
$init =~ s/[.:@&\/\\ ]+/_/g; |
| 1010 |
|
| 1011 |
## take care that file to save exists... |
| 1012 |
my $data_file = File::Spec->catfile($project_folder, "fits", |
| 1013 |
$paths{$to_save}->get('folder'), |
| 1014 |
join(".",$data,$suff)); |
| 1015 |
Error("There is no $kind file for \"".$paths{$data}->descriptor."\""), return |
| 1016 |
unless (-e $data_file); |
| 1017 |
|
| 1018 |
## get the file name to save to |
| 1019 |
my $save_file = $top -> getSaveFile(-filetypes=>[["$kind files", ".$suff"], |
| 1020 |
['All Files', '*'],], |
| 1021 |
##(not $is_windows) ? |
| 1022 |
## (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () , |
| 1023 |
-initialfile=>$init."_$space.".$suff, |
| 1024 |
-initialdir=> $current_data_dir || cwd, |
| 1025 |
-title => "Artemis: Save $kind"); |
| 1026 |
return unless ($save_file); |
| 1027 |
|
| 1028 |
Echo("Saving $kind in $sp for \"" . $paths{$to_save}->descriptor . "\" ..."); |
| 1029 |
my $first = "# $kind in $sp for \"" . $paths{$to_save}->descriptor . "\"\n"; |
| 1030 |
my $to = File::Spec->catfile($project_folder, "tmp", "outfile"); |
| 1031 |
my $header_file = File::Spec->catfile($project_folder, "fits", $paths{$to_save}->get('folder'), "header.".$data); |
| 1032 |
if (lc($space) eq 'k') { |
| 1033 |
open O, ">".$to; |
| 1034 |
print O $first; |
| 1035 |
do { # cat the header and the data |
| 1036 |
local $| = 1; |
| 1037 |
local $/ = undef; |
| 1038 |
if (-e $header_file) { |
| 1039 |
open H, $header_file; |
| 1040 |
print O <H>; |
| 1041 |
close H; |
| 1042 |
}; |
| 1043 |
open D, $data_file; |
| 1044 |
print O <D>; |
| 1045 |
close D; |
| 1046 |
}; |
| 1047 |
close O; |
| 1048 |
} elsif ($space =~ /[qr]/i) { |
| 1049 |
my $group = $paths{$to_save}->get('group'); |
| 1050 |
$group =~ s/fit/$suff/; |
| 1051 |
my $command = ""; |
| 1052 |
## read in this file |
| 1053 |
my $infile = $paths{$to_save}->get('fitfile'); |
| 1054 |
$infile = substr($infile, 0, -3) . $suff; |
| 1055 |
$command .= "read_data(file=\"$infile\",\n type=chi, group=$group)\n"; |
| 1056 |
## FT this array |
| 1057 |
my $string = "($group.chi, k=$group.k, "; |
| 1058 |
foreach (qw(kmin kmax dk kwindow)) { |
| 1059 |
$string .= "$_=" . $paths{$data}->get($_) . ", "; |
| 1060 |
}; |
| 1061 |
$string .= "kweight=" . $plot_features{kweight} . ", "; |
| 1062 |
$string .= "rmax_out=" . $plot_features{rmax_out} . ", "; |
| 1063 |
if ($paths{$data}->get('pcpath') ne "None") { |
| 1064 |
my $pcp = $paths{$paths{$data}->get('pcpath')}->('fit_index'); |
| 1065 |
$string .= "pc_feff_path=$pcp, "; |
| 1066 |
}; |
| 1067 |
$string =~ s/, $/\)\n/; |
| 1068 |
$string = wrap("fftf", " ", $string); |
| 1069 |
$command .= $string; |
| 1070 |
## do bft for 1 space |
| 1071 |
if (lc($space) eq 'q') { |
| 1072 |
my $string = "(real=$group.chir_re, imag=$group.chir_im, "; |
| 1073 |
foreach (qw(rmin rmax dr rwindow)) { |
| 1074 |
$string .= "$_=" . $paths{$data}->get($_) . ", "; |
| 1075 |
}; |
| 1076 |
$string =~ s/, $/\)\n/; |
| 1077 |
$string = wrap("fftr", " ", $string); |
| 1078 |
$command .= $string; |
| 1079 |
## write it to the tmp/ space |
| 1080 |
$command .= "\nwrite_data(file=$to,\n label=\"q chiq_re chiq_im chiq_mag chiq_pha\",\n $group.q, $group.chiq_re, $group.chiq_im, $group.chiq_mag, $group.chiq_pha)\n"; |
| 1081 |
} else { |
| 1082 |
## write it to the tmp/ space |
| 1083 |
$command .= "\nwrite_data(file=$to,\n label=\"r chir_re chir_im chir_mag chir_pha\",\n $group.r, $group.chir_re, $group.chir_im, $group.chir_mag, $group.chir_pha)\n"; |
| 1084 |
} |
| 1085 |
$paths{$to_save}->dispose($command, $dmode); |
| 1086 |
do { # cat the header and the data |
| 1087 |
local $| = 1; |
| 1088 |
local $/ = undef; |
| 1089 |
open D, $to; |
| 1090 |
my $d = <D>; |
| 1091 |
close D; |
| 1092 |
open O, ">".$to; |
| 1093 |
print O $first; |
| 1094 |
if (-e $header_file) { |
| 1095 |
open H, $header_file; |
| 1096 |
print O <H>; |
| 1097 |
close H; |
| 1098 |
}; |
| 1099 |
print O $d |
| 1100 |
}; |
| 1101 |
close O; |
| 1102 |
}; |
| 1103 |
move($to, $save_file); |
| 1104 |
#unlink $to if (-e $to); |
| 1105 |
|
| 1106 |
## finally, reset current_data_dir |
| 1107 |
my ($name, $pth, $suffix) = fileparse($save_file); |
| 1108 |
$current_data_dir = $pth; |
| 1109 |
Echo("Saved $save_file"); |
| 1110 |
}; |
| 1111 |
|
| 1112 |
|
| 1113 |
sub logview_restore_model { |
| 1114 |
## the argument is either the entry number from the list on the log |
| 1115 |
## viewer page (when called from the log viewer) or the id of the |
| 1116 |
## currently anchored path (when called from the menubar) |
| 1117 |
my $which = (defined $_[0]) ? $_[0] : $paths{$current}; |
| 1118 |
|
| 1119 |
my $this; # this will be used to set the thisfit |
| 1120 |
if (ref($which) =~ /Ifeffit/) { # property of the fit heads |
| 1121 |
$this = $which->get('id'); |
| 1122 |
} else { |
| 1123 |
foreach (keys %paths) { |
| 1124 |
next unless ($paths{$_}->type eq 'fit'); |
| 1125 |
next unless $paths{$_}->get('parent'); |
| 1126 |
my $lv = dirname($widgets{loglistbox} -> info('data', $which)); |
| 1127 |
my $ft = File::Spec->catfile($project_folder, 'fits', $paths{$_}->get('folder')); |
| 1128 |
next unless same_directory($lv, $ft); |
| 1129 |
$this = $_; |
| 1130 |
}; |
| 1131 |
}; |
| 1132 |
my $from = (ref($which) =~ /Ifeffit/) ? |
| 1133 |
File::Spec->catfile($project_folder, 'fits', $which->get('folder'), 'description') : |
| 1134 |
File::Spec->catfile(dirname($widgets{loglistbox} -> info('data', $which)), 'description'); |
| 1135 |
my $to = File::Spec->catfile($project_folder, 'descriptions', 'artemis'); |
| 1136 |
|
| 1137 |
my $label = (ref($which) =~ /Ifeffit/) ? |
| 1138 |
$which->get('lab') : |
| 1139 |
$widgets{loglistbox} -> entrycget($which, '-text'); |
| 1140 |
|
| 1141 |
## need to compare from and to, bail if the requested model is the current |
| 1142 |
Error("\"$label\" is the current fitting model. Restore aborted."), return |
| 1143 |
if compare($from,$to) == 0; |
| 1144 |
|
| 1145 |
$top -> Busy; |
| 1146 |
Echo("Restoring fitting model from \"$label\" ..."); |
| 1147 |
|
| 1148 |
##@-fp-@ my $bnfr = basename($from); |
| 1149 |
##@-fp-@ my $fp_exists = (-e File::Spec->catfile($bnfr, "...fp")); |
| 1150 |
##@-fp-@ if ($fp_exists) { |
| 1151 |
##@-fp-@ my $is_ok = compare_fingerprint(File::Spec->catfile($bnfr, "...fp"), |
| 1152 |
##@-fp-@ File::Spec->catfile($bnfr, "artemis")); |
| 1153 |
##@-fp-@ unless ($is_ok) { |
| 1154 |
##@-fp-@ my $dialog = |
| 1155 |
##@-fp-@ $top -> Dialog(-bitmap => 'warning', |
| 1156 |
##@-fp-@ -text => "The fingerprint of the description file for the selected model has changed. This could indicate that this project file has been tampered with. It may be unsafe to continue reading this project file.", |
| 1157 |
##@-fp-@ -title => 'Artemis: Possibly tainted project file...', |
| 1158 |
##@-fp-@ -buttons => [qw/Continue Abort/], |
| 1159 |
##@-fp-@ -default_button => 'Abort', |
| 1160 |
##@-fp-@ -popover => 'cursor'); |
| 1161 |
##@-fp-@ &posted_Dialog; |
| 1162 |
##@-fp-@ my $response = $dialog->Show(); |
| 1163 |
##@-fp-@ if ($response eq 'Abort') { |
| 1164 |
##@-fp-@ Echo("Restoring fitting model from \"$label\" ... aborted!"); |
| 1165 |
##@-fp-@ $top -> Unbusy; |
| 1166 |
##@-fp-@ return; |
| 1167 |
##@-fp-@ }; |
| 1168 |
##@-fp-@ }; |
| 1169 |
##@-fp-@ }; |
| 1170 |
|
| 1171 |
## copy this fit's description to the description folder |
| 1172 |
Error("Uh oh! Something went wrong backing up the old description file."), |
| 1173 |
return unless copy($to, File::Spec->catfile($project_folder, 'descriptions', 'artemis.bak')); |
| 1174 |
Error("Uh oh! Something went wrong copying the description file for \"$label\"."), |
| 1175 |
return unless copy($from, $to); |
| 1176 |
|
| 1177 |
## save the journal |
| 1178 |
open J, ">".File::Spec->catfile($project_folder, "descriptions", "journal.artemis"); |
| 1179 |
print J $notes{journal}->get(qw(1.0 end)); |
| 1180 |
close J; |
| 1181 |
|
| 1182 |
## discard this fit |
| 1183 |
my $save_project_name = $project_name; |
| 1184 |
delete_project(1); |
| 1185 |
$project_name = $save_project_name; |
| 1186 |
|
| 1187 |
## read in the description now in the description folder |
| 1188 |
open_project($to); |
| 1189 |
foreach (keys %paths) { |
| 1190 |
next unless ($paths{$_}->type eq 'fit'); |
| 1191 |
next if $paths{$_}->get('parent'); |
| 1192 |
$paths{$_} -> make(thisfit=>$this); |
| 1193 |
}; |
| 1194 |
$widgets{log_current} -> configure(-text=>$paths{$this}->get('lab')); |
| 1195 |
project_state(0); |
| 1196 |
|
| 1197 |
$top -> Unbusy; |
| 1198 |
Echo("Restoring fitting model from \"$label\" ... done!"); |
| 1199 |
}; |
| 1200 |
|
| 1201 |
sub display_warnings { |
| 1202 |
my $thisfit = $paths{$current}->get('parent') |
| 1203 |
? $current |
| 1204 |
: $paths{$current}->get('thisfit'); |
| 1205 |
my $warnings_file = File::Spec->catfile($project_folder, "fits", $paths{$thisfit}->get('folder'), 'warnings'); |
| 1206 |
Error("There is no warnings file for this fit"), return |
| 1207 |
unless (-e $warnings_file); |
| 1208 |
display_file('file', $warnings_file) |
| 1209 |
}; |