File: Pixel.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 (302 lines) | stat: -rw-r--r-- 8,920 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
package Demeter::Data::Pixel;

=for Copyright
 .
 Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
 All rights reserved.
 .
 This file is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself. See The Perl
 Artistic License.
 .
 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

use Carp;
use autodie qw(open close);

use Moose;
extends 'Demeter::Data';
use MooseX::Aliases;
use Demeter::StrTypes qw( Empty );
if ($Demeter::mode->ui eq 'screen') {
  with 'Demeter::UI::Screen::Pause';
  with 'Demeter::UI::Screen::Progress';
};

use List::MoreUtils qw(minmax);

#has '+name' => (default => 'pixel data',);
has '+datatype' => (default => 'xmu',);
has '+is_special' => (default => 1,);
has '+is_pixel' => (default => 1,);

has 'standard' => (is => 'rw', isa => Empty.'|Demeter::Data',  default => q{},
		   trigger => sub{ my($self, $new) = @_;
				   if ($new) {
				     $self->standardgroup($new->group);
				     $self->_update('background');
				     $new ->_update('background');
				     #$self->offset($new->bkg_e0 - $self->linear*$self->bkg_e0);
				   }
				 });
has 'standardgroup' => (is => 'rw', isa => 'Str',  default => q{});

has 'offset'    => (is => 'rw', isa => 'LaxNum',  default => 0);
has 'linear'    => (is => 'rw', isa => 'LaxNum',  default => 0.4);
has 'quadratic' => (is => 'rw', isa => 'LaxNum',  default => sub{ shift->co->default("dispersive", "quadratic")  || 0});
has 'quartic'   => (is => 'rw', isa => 'LaxNum',  default => 0);

sub BUILD {
  my ($self, @params) = @_;
  $self->mo->push_Pixel($self);
};


after read_data => sub {
  my ($self) = @_;
  $self->name($self->name . ' (pixel)') if ($self->name !~ m{\(pixel\)\z});
  $self->source($self->name);
  return $self;
};

sub _sanity {
  my ($self) = @_;
  if (ref($self->standard) !~ m{Data}) {
    croak("** Data::Pixel: You have not set the standard for fitting your calibration parameters");
  };
  return $self;
};


sub guess {
  my ($self, $quiet) = @_;

  $self->start_spinner("Demeter is setting initial DXAS calibration parameters") if (($self->mo->ui eq 'screen') and (not $quiet));
  $self->standard->bkg_e0_fraction(0.1);
  my $st1 = $self->standard->e0('fraction');
  $self->standard->bkg_e0_fraction(0.9);
  my $st9 = $self->standard->e0('fraction');
  $self->standard->e0('ifeffit');

  $self->_update('background');
  $self->bkg_e0_fraction(0.1);
  my $da1 = $self->e0('fraction');
  $self->bkg_e0_fraction(0.9);
  my $da9 = $self->e0('fraction');
  $self->e0('ifeffit');

  #print "\n$st1  $st9 \n";
  #print "\n$da1  $da9 \n";
  #printf "%.9f\n", ($st9-$st1)/($da9-$da1);
  #printf "%.9f\n",  $st1 - (($st9-$st1)/($da9-$da1)) * $da1;

  $self->linear(($st9-$st1)/($da9-$da1));
  $self->offset($st1 - ($self->linear * $da1));

  $self->stop_spinner if (($self->mo->ui eq 'screen') and (not $quiet));
  return $self;
};

sub pixel {
  my ($self, $quiet) = @_;
  $self->_sanity;
  $self->start_spinner("Demeter is determining DXAS calibration parameters") if (($self->mo->ui eq 'screen') and (not $quiet));

  $self->_update('fft');
  $self->standard->_update('fft');

  $self->dispense('process', 'pixel_setup');
  $self->dispense('process', 'pixel_fit');

  $self->offset($self->fetch_scalar("pixel___a"));
  $self->linear($self->fetch_scalar("pixel___b"));
  $self->quadratic($self->fetch_scalar("pixel___c"));
  #$self->quartic($self->fetch_scalar("pixel___d"));
  #print $self->fetch_scalar('pixel___xmin'), " ",$self->fetch_scalar('pixel___xmax'), $/;
  #print $self->linear, "  ", $self->offset, "  ", $self->quadratic, $/;
  $self->stop_spinner if (($self->mo->ui eq 'screen') and (not $quiet));
  return $self;
};


sub apply {
  my ($self, $convert) = @_;
  $convert ||= $self;
  $convert -> _update('data');
  $convert -> set(offset=>$self->offset, linear=>$self->linear, quadratic=>$self->quadratic);
  my $new  = Demeter::Data->new(name=>$convert->name);
  $new     -> source('DXAS: '.$convert->file);
  $new     -> mo -> standard($convert);
  $new     -> dispense('process', 'pixel_set');
  $new     -> set(update_data=>0, update_columns=>0, update_norm=>1, datatype=>'xmu');
  $new     -> e0;
  $new     -> resolve_defaults;
  $new     -> unset_standard;
  return $new;
};

sub report {
  my ($self) = @_;
  return sprintf("offset = %.3f, linear = %.3f, quadratic = %.3g\n", $self->get(qw(offset linear quadratic)))
};

1;


=head1 NAME

Demeter::Data::Pixel - Handle dispersive XAS data

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

   use Demeter;

   my $stan = Demeter::Data->new(file=>'ESRF_ID24/cus2/cufoil_rt.txt', bkg_nor2=>1000);
   $stan->set_mode(screen=>0);

   my $data = Demeter::Data::Pixel->new(file=>'ESRF_ID24/cus2/cu_08', bkg_nor2=>1000);

   $data->standard($stan);
   $data->guess;
   $data->pixel;

   my $cus2 = Demeter::Data::Pixel->new(file=>$file, name=>basename($file));
   $dispersive_data = $data->apply($cus2);

=head1 DESCRIPTION

The standard way of implementing a dispersive XAS measurement is to
measure a known sample such as a foil in the same dispersive geometry
as the real experiment.  The function for converting from pixel
position to energy is determined by comparing the dispersively
measured sample to a conventionally measurement on the same sample.
This determines as set of conversion parameters that can then be used
on each scan in  the subsequent measurement.

In the synopsis above, the conventional scan on a Cu foil is imported
as a normal Data object.  The foil measured dispersively is then
imported as a Data::Pixel object.  The conventional scan is
established as the standard and the conversion parameters are
determined by fitting a quadratic function relating pixels to energy.

  energy = A + B*pixel + C*pixel^2

In fact, a slightly more complex function is used as the fitting
function in an attempt to to give some weight to the high energy data
where the variations in the data and thus the sensitivity to the fit
is somewhat less than near the edge where the data is changing quickly.

Once the parameters for the conversion are known, they can be applied
to the real data.  Each dispersive scan is imported as a Data::Pixel
object and a normal Data object is generated from that.

=head1 ATTRIBUTES

This is inherited from the Data object, so all of the Data attributes
are inherited.  Additionally, there are these attributes:

=over 4

=item C<standard>

This takes the refernece to the Data object containing the
conventionally measured standard.

=item C<offset>

The value of the offset conversion parameter, C<A> in the formula above.

=item C<linear>

The value of the linear conversion parameter, C<B> in the formula above.

=item C<quadratic>

The value of the quadratic conversion parameter, C<C> in the formula above.

=back

=head1 METHODS

=over 4

=item C<guess>

Make an initial guesss for the conversion parameters.  This is done by
finding e0 values for the conventional and dispersive standard data
(normally a foil or some such) using the fraction-of-an-edge step
method described in L<Demeter::Data::E0>.  The e0 position is found
for fractions of 0.1 and 0.9 in each measurement and offset and linear
terms are determined from those values.  The initial quadratic value
is 0.  This fills the C<offset>, C<linear>, and C<quadratic>
attributes.

=item C<pixel>

Perform the fit between the flattened, normalized conventionald and
dispersive standards to determine values for the conversion
parameters.  This fills the C<offset>, C<linear>, and C<quadratic>
attributes.

=item C<apply>

This method applies the C<offset>, C<linear>, and C<quadratic>
attributes to convert a pixel data groups, returning a normal Data
object.

=item C<report>

This generates a bit of text documenting the values of the conversion
parameters.

=back

=head1 SERIALIZATION AND DESERIALIZATION

Good question ...

=head1 CONFIGURATION AND ENVIRONMENT

See L<Demeter::Config> for a description of the configuration system.
See the lcf configuration group for the relevant parameters.

=head1 DEPENDENCIES

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

=head1 BUGS AND LIMITATIONS

=over 4

=item *

The fitting algorithm is not so robust.

=back

=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