File: Difference.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (424 lines) | stat: -rw-r--r-- 16,508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package Demeter::UI::Athena::Difference;

use strict;
use warnings;

use Wx qw( :everything );
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON EVT_TEXT EVT_TEXT_ENTER EVT_CHOICE EVT_CHECKBOX);
use Wx::Perl::TextValidator;

use Demeter::Diff;
use Demeter::UI::Wx::SpecialCharacters qw(:all);

use File::Basename;
use Scalar::Util qw(looks_like_number);

use vars qw($label);
$label = "Difference spectra";	# used in the Choicebox and in status bar messages to identify this tool

my $tcsize   = [60,-1];
my $icon     = File::Spec->catfile(dirname($INC{"Demeter/UI/Athena.pm"}), 'Athena', , 'icons', "bullseye.png");
my $bullseye = Wx::Bitmap->new($icon, wxBITMAP_TYPE_PNG);
my @forms = (qw(xmu norm der nder sec nsec)); #  chi

sub new {
  my ($class, $parent, $app) = @_;
  my $this = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxMAXIMIZE_BOX );

  my $box = Wx::BoxSizer->new( wxVERTICAL);
  $this->{sizer}  = $box;
  $this->{Diff} = Demeter::Diff->new;
  $this->{updatemarked} = 1;
  my $gbs = Wx::GridBagSizer->new( 5, 5 );

  my $label = Wx::StaticText->new($this, -1, "Standard");
  $gbs->Add($label, Wx::GBPosition->new(0,0));
  $label = Wx::StaticText->new($this, -1, "Multiplier");
  $gbs->Add($label, Wx::GBPosition->new(1,0));
  $label = Wx::StaticText->new($this, -1, "Data");
  $gbs->Add($label, Wx::GBPosition->new(2,0));
  $label = Wx::StaticText->new($this, -1, "Form");
  $gbs->Add($label, Wx::GBPosition->new(3,0));
  $label = Wx::StaticText->new($this, -1, "Name template");
  $gbs->Add($label, Wx::GBPosition->new(4,0));

  $this->{standard}   = Demeter::UI::Athena::GroupList -> new($this, $app, 1);
  $this->{multiplier} = Wx::TextCtrl->new($this, -1, '1', wxDefaultPosition, [150,-1]);
  $this->{data}       = Wx::StaticText->new($this, -1, q{Group});
  $this->{form}       = Wx::Choice->new($this, -1, wxDefaultPosition, wxDefaultSize,
					["$MU(E)", "normalized $MU(E)", "derivative $MU(E)",
					 "deriv. of normalized $MU(E)", "2nd derivative $MU(E)",
					 "2nd deriv. of normalized $MU(E)"]); # , "$CHI(k)"
  $this->{template}   = Wx::TextCtrl->new($this, -1, 'diff %d - %s', wxDefaultPosition, [150,-1]);
  $gbs->Add($this->{standard},   Wx::GBPosition->new(0,1));
  $gbs->Add($this->{multiplier}, Wx::GBPosition->new(1,1));
  $gbs->Add($this->{data},       Wx::GBPosition->new(2,1));
  $gbs->Add($this->{form},       Wx::GBPosition->new(3,1));
  $gbs->Add($this->{template},   Wx::GBPosition->new(4,1));
  $this->{form}->SetSelection(1);
  EVT_CHOICE($this, $this->{form}, \&OnChoice);
  EVT_TEXT($this, $this->{template}, sub{$this->{updatemarked} = 1});
  $this->{template}->SetFont( Wx::Font->new( Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)->GetPointSize,
					     wxTELETYPE, wxNORMAL, wxNORMAL, 0, "" )) ;
  $::app->mouseover($this->{template}, 'TOKENS: %d=data name; %s=standard name; %f=form; %m=multiplier; %n=xmin; %x=xmax; %a=area');

  $this->{invert}  = Wx::CheckBox->new($this, -1, 'Invert difference spectrum');
  $this->{plotspectra} = Wx::CheckBox->new($this, -1, 'Plot data and standard with difference');
  $this->{make_nor} = Wx::CheckBox->new($this, -1, 'Allow difference group to be renormalized');
  $this->{do_integrate} = Wx::CheckBox->new($this, -1, 'Do integration');
  $gbs->Add($this->{invert},  Wx::GBPosition->new(5,0), Wx::GBSpan->new(1,2));
  $gbs->Add($this->{plotspectra}, Wx::GBPosition->new(6,0), Wx::GBSpan->new(1,2));
  $gbs->Add($this->{make_nor}, Wx::GBPosition->new(7,0), Wx::GBSpan->new(1,2));
  $gbs->Add($this->{do_integrate}, Wx::GBPosition->new(8,0), Wx::GBSpan->new(1,2));
  $this->{invert}->SetValue(0);
  $this->{plotspectra}->SetValue(1);
  $this->{make_nor}->SetValue(0);
  $this->{do_integrate}->SetValue(1);
  EVT_CHECKBOX($this, $this->{invert},       sub{$this->{updatemarked} = 1});
  EVT_CHECKBOX($this, $this->{make_nor},     sub{$this->{updatemarked} = 1});
  EVT_CHECKBOX($this, $this->{do_integrate}, \&ToggleIntegration);

  $box -> Add($gbs, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10);

  my $hbox = Wx::BoxSizer->new( wxHORIZONTAL );

  $this->{xmin_label} = Wx::StaticText->new($this, -1, 'Integration range');
  $hbox->Add($this->{xmin_label}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
  $this->{xmin} = Wx::TextCtrl->new($this, -1, $this->{Diff}->xmin, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
  $hbox->Add($this->{xmin}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
  $this->{xmin_pluck}   = Wx::BitmapButton -> new($this, -1, $bullseye);
  $hbox->Add($this->{xmin_pluck}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 1);
  $this->{to} = Wx::StaticText->new($this, -1, 'to');
  $hbox->Add($this->{to}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
  $this->{xmax} = Wx::TextCtrl->new($this, -1, $this->{Diff}->xmax, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
  $hbox->Add($this->{xmax}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
  $this->{xmax_pluck}   = Wx::BitmapButton -> new($this, -1, $bullseye);
  $hbox->Add($this->{xmax_pluck}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 1);

  $this->{multiplier} -> SetValidator( Wx::Perl::TextValidator->new( qr([0-9.]) ) );
  $this->{xmin} -> SetValidator( Wx::Perl::TextValidator->new( qr([-0-9.]) ) );
  $this->{xmax} -> SetValidator( Wx::Perl::TextValidator->new( qr([-0-9.]) ) );
  EVT_BUTTON($this, $this->{xmin_pluck}, sub{Pluck(@_, $app, 'xmin')});
  EVT_BUTTON($this, $this->{xmax_pluck}, sub{Pluck(@_, $app, 'xmax')});
  EVT_TEXT($this, $this->{xmin},         sub{$this->{updatemarked} = 1});
  EVT_TEXT($this, $this->{xmax},         sub{$this->{updatemarked} = 1});
  EVT_TEXT_ENTER($this, $this->{xmin},   sub{$this->plot});
  EVT_TEXT_ENTER($this, $this->{xmax},   sub{$this->plot});


  $box -> Add($hbox, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  $hbox = Wx::BoxSizer->new( wxHORIZONTAL );
  $this->{area_label} = Wx::StaticText->new($this, -1, 'Integrated area');
  $hbox->Add($this->{area_label}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
  $this->{area} = Wx::TextCtrl->new($this, -1, 0, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
  $hbox->Add($this->{area}, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
  $box -> Add($hbox, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  $this->{plot}        = Wx::Button->new($this, -1, 'Plot difference spectrum', wxDefaultPosition, $tcsize);
  $this->{plotk}       = Wx::Button->new($this, -1, 'Plot difference spectrum in k', wxDefaultPosition, $tcsize);
  $this->{make}        = Wx::Button->new($this, -1, 'Make difference group',    wxDefaultPosition, $tcsize);
  $this->{marked}      = Wx::Button->new($this, -1, 'Plot difference spectra for all marked groups', wxDefaultPosition, $tcsize);
  $this->{markedareas} = Wx::Button->new($this, -1, 'Plot integrated areas from all marked groups', wxDefaultPosition, $tcsize);
  $this->{markedmake}  = Wx::Button->new($this, -1, 'Make difference groups from all marked groups', wxDefaultPosition, $tcsize);
  $box -> Add($this->{$_}, 0, wxGROW|wxALL, 2) foreach (qw(plot plotk make));
  $box -> Add(1,15,0);
  $box -> Add($this->{$_}, 0, wxGROW|wxALL, 2) foreach (qw(marked markedareas markedmake));
  $this->{$_}->Enable(0) foreach (qw(make marked markedareas markedmake));
  EVT_BUTTON($this, $this->{plot},        sub{$this->plot('E')});
  EVT_BUTTON($this, $this->{plotk},       sub{$this->plot('k')});
  EVT_BUTTON($this, $this->{make},        sub{$this->make});
  EVT_BUTTON($this, $this->{marked},      sub{$this->marked_spectra});
  EVT_BUTTON($this, $this->{markedareas}, sub{$this->marked_areas});
  EVT_BUTTON($this, $this->{markedmake},  sub{$this->marked_make});


  $box->Add(1,1,1);		# spacer

  $this->{document} = Wx::Button->new($this, -1, 'Document section: difference spectra');
  $box -> Add($this->{document}, 0, wxGROW|wxALL, 2);
  EVT_BUTTON($this, $this->{document}, sub{  $app->document("analysis.diff")});

  $this->SetSizerAndFit($box);
  return $this;
};

## deprecated?
sub pull_values {
  my ($this, $data) = @_;
  1;
};

## this subroutine fills the controls when an item is selected from the Group list
sub push_values {
  my ($this, $data) = @_;
  $this->{data}  -> SetLabel($data->name);
  my $count = 0;
  foreach my $i (0 .. $::app->{main}->{list}->GetCount - 1) {
    my $data = $::app->{main}->{list}->GetIndexedData($i);
    ++$count if $data->datatype ne 'chi';
  };
  $this->Enable(1);
  if ($count < 2) {
    $this->Enable(0);
    return;
  };
  if ($data->datatype eq 'chi') {
    $this->Enable(0);
    return;
  };
  my $was = $this->{standard}->GetStringSelection;
  $this->{standard}->fill($::app, 1, 1);
  if ((not $was) or ($was eq 'None')) {
    $this->{standard}->SetSelection(0);
  } else {
    $this->{standard}->SetStringSelection($was);
    $this->{standard}->SetSelection(0) if not scalar $this->{standard}->GetSelection;
  };
  #($was eq 'None') ? $this->{standard}->SetSelection(0) : $this->{standard}->SetStringSelection($was);
  #$this->{standard}->SetSelection(0) if not defined($this->{standard}->GetClientData(scalar $this->{standard}->GetSelection));
  $data->po->start_plot;
  $data->po->set(emin=>$this->{Diff}->xmin-10, emax=>$this->{Diff}->xmax+20);
  $data->po->set(e_mu=>1, e_markers=>0, e_bkg=>0, e_pre=>0, e_post=>0, e_norm=>1, e_der=>0, e_sec=>0, e_i0=>0, e_signal=>0, e_smooth=>0);
  $this->{make}->Enable(0);
  my $save = $this->{plotspectra}->GetValue;
  $this->{plotspectra}->SetValue(1);
  $this->plot if not $::app->{plotting};
  $this->{plotspectra}->SetValue($save);
};

## this subroutine sets the enabled/frozen state of the controls
sub mode {
  my ($this, $data, $enabled, $frozen) = @_;
  1;
};

sub ToggleIntegration {
  my ($this, $event) = @_;
  my $onoff = $this->{do_integrate}->GetValue;
  foreach my $w (qw(xmin_label xmin xmin_pluck to xmax xmax_pluck area area_label markedareas)) {
    $this->{$w}->Enable($onoff);
  };
  $this->{Diff}->do_integrate($onoff);
  $this->{updatemarked} = 1;
};

sub setup {
  my ($this, $data, $diff) = @_;
  $diff ||= $this->{Diff};
  foreach my $att (qw(xmin xmax invert plotspectra multiplier)) {
    next if (($att =~ m{\A(?:xm|mu)}) and (not looks_like_number($this->{$att}->GetValue)));
    $diff->$att($this->{$att}->GetValue);
  };
  my $form = $forms[$this->{form}->GetSelection];
  $diff->data($data);
  $diff->space($form);
  $diff->standard($this->{standard}->GetClientData(scalar $this->{standard}->GetSelection));
  $diff->name_template($this->{template}->GetValue);
  $diff->diff;
  Demeter->po->set(emin=>$diff->xmin-20, emax=>$diff->xmax+30, space=>'E');
  Demeter->po->set(e_mu=>1, e_markers=>1, e_bkg=>0, e_pre=>0, e_post=>0, e_i0=>0, e_signal=>0, e_smooth=>0);
  if ($this->{form}->GetSelection == 0) {			# mu(E)
    Demeter->po->set(e_norm=>0, e_der=>0, e_sec=>0,);
  } elsif ($this->{form}->GetSelection == 1) {			# norm(E)
    Demeter->po->set(e_norm=>1, e_der=>0, e_sec=>0,);
  } elsif ($this->{form}->GetSelection == 2) {			# deriv(E)
    Demeter->po->set(e_norm=>0, e_der=>1, e_sec=>0,);
  } elsif ($this->{form}->GetSelection == 3) {			# deriv(norm(E))
    Demeter->po->set(e_norm=>1, e_der=>1, e_sec=>0,);
  } elsif ($this->{form}->GetSelection == 4) {			# sec(E)
    Demeter->po->set(e_norm=>0, e_der=>0, e_sec=>1,);
  } elsif ($this->{form}->GetSelection == 5) {			# sec(norm(E))
    Demeter->po->set(e_norm=>1, e_der=>0, e_sec=>1,);
  };
};

sub plot {
  my ($this, $space) = @_;
  $this->setup($::app->current_data);
  $this->{area}->SetValue(sprintf("%.5f",$this->{Diff}->area));

  Demeter->po->start_plot;
  $this->{Diff}->is_nor(not $this->{make_nor}->GetValue);
  $this->{Diff}->plot($space);
  $::app->{lastplot} = [$space, 'single'];
  $this->{$_}->Enable(1) foreach (qw(make marked markedareas markedmake));
  $::app->heap_check(0);
};

sub OnChoice {
  my ($this, $event) = @_;
  $this->plot;
  $this->{make_nor}->SetValue($this->{form}->GetSelection == 0);
};

sub make {
  my ($this, $diff, $at_end) = @_;
  $diff   ||= $this->{Diff};
  $at_end ||= 0;

  $diff->name_template($this->{template}->GetValue);
  $diff->is_nor(not $this->{make_nor}->GetValue);
  $diff->datatype('xanes');
  $diff->datatype('xmu') if ($this->{form}->GetSelection == 0);
  my $data = $diff->make_group;
  my $index = $::app->current_index;
  if ($at_end) {
    $::app->{main}->{list}->AddData($data->name, $data);
  } elsif ($index == $::app->{main}->{list}->GetCount-1) {
    $::app->{main}->{list}->AddData($data->name, $data);
  } else {
    $::app->{main}->{list}->InsertData($data->name, $index+1, $data);
  };
  $::app->{main}->status("Made new difference group, ".$data->name);
  $::app->modified(1);
  $::app->heap_check(0);
  $data->update_norm(1);
};

sub Pluck {
  my ($this, $event, $app, $which) = @_;
  my $on_screen = lc($app->{lastplot}->[0]);
  if ($on_screen ne 'e') {
    $app->{main}->status("Cannot pluck for energy from a $on_screen plot.");
    return;
  };
  my ($ok, $x, $y) = $app->cursor;
  $app->{main}->status("Failed to pluck cursor value."), return if not $ok;
  my $data = $app->current_data;
  my $plucked = sprintf("%.3f", $x - $data->bkg_e0);
  $this->{$which}->SetValue($plucked);
  $this->plot;
  $app->{main}->status("Plucked $plucked for $which");
};

sub marked {
  my ($this) = @_;
  my $save = $this->{Diff};

  my @all = ();
  foreach my $i (0 .. $::app->{main}->{list}->GetCount-1) {
    next if (not $::app->{main}->{list}->IsChecked($i));
    my $data = $::app->{main}->{list}->GetIndexedData($i);
    ## make sure that the Standard is not in the list of marked groups...
    next if ($this->{standard}->GetClientData(scalar $this->{standard}->GetSelection)->group eq $data->group);
    $::app->{main}->status("Computing difference from ".$data->name);
    my $new = Demeter::Diff->new;
    $new->invert($save->invert);
    #$this->{Diff} = $new;
    $this->setup($data, $new);
    push @all, $new;
  };
  #$this->{Diff} = $save;
  $this->{updatemarked} = 0;
  $this->{cachemarked} = \@all;
  return @all;
};

sub marked_spectra {
  my ($this) = @_;
  my $busy = Wx::BusyCursor->new();
  my @all = ($this->{updatemarked}) ? $this->marked : @{$this->{cachemarked}};
  Demeter->po->start_plot;
  Demeter->po->title("Sequence of difference spectra");
  foreach my $d (@all) {
    $d->plotspectra(0);
    $d->plotindicators(0);
    $d->name($d->data->name);
    $d->plot;
  };
  $::app->{main}->status("Plotted sequence of difference spectra");
  undef $busy;
};
sub marked_areas {
  my ($this) = @_;
  my $busy = Wx::BusyCursor->new();
  my @all = ($this->{updatemarked}) ? $this->marked : @{$this->{cachemarked}};
  Demeter->po->start_plot;
  Demeter->po->title("Sequence of difference spectra");
  my $temp = Demeter->po->tempfile;
  open(my $O, '>', $temp);
  foreach my $i (0 .. $#all) {
    printf $O "%d  %.9f\n", $i+1, $all[$i]->area;
  };
  close $O;
  Demeter->chart('plot', 'plot_file', {file=>$temp, xmin=>1, xmax=>$#all+1, param=>'integrated area',
				       showy=>0, xlabel=>'data group', linetype=>'linespoints', title=>'areas'});

  $::app->{main}->status("Plotted sequence of difference spectra");
  undef $busy;
};

sub marked_make {
  my ($this) = @_;
  my $busy = Wx::BusyCursor->new();
  my @all = ($this->{updatemarked}) ? $this->marked : @{$this->{cachemarked}};
  foreach my $d (@all) {
    $this->make($d, 1);
  };
  $::app->{main}->status("Made difference groups from all marked groups");
  undef $busy;
};

1;


=head1 NAME

Demeter::UI::Athena::Difference - A difference spectrum tool for Athena

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This module provides a tool for computing difference spectra from
normalized mu(E) data, including the calculation of integrated area
under a portion of the difference spectrum.

=head1 CONFIGURATION


=head1 DEPENDENCIES

Demeter's dependencies are in the F<Build.PL> file.

=head1 BUGS AND LIMITATIONS

=over 4

=item *

plucking

=item *

Marked groups functionality

=back

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel, L<http://bruceravel.github.io/home>

L<http://bruceravel.github.io/demeter/>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut