-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageset.h
1513 lines (1340 loc) · 38.7 KB
/
imageset.h
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* imageset.h
*
* Copyright (C) 2017 Diamond Light Source
*
* Author: James Parkhurst
*
* This code is distributed under the BSD license, a copy of which is
* included in the root directory of this package.
*/
#ifndef DXTBX_IMAGESET_H
#define DXTBX_IMAGESET_H
#include <map>
#include <boost/python.hpp>
#include <scitbx/array_family/shared.h>
#include <scitbx/array_family/versa.h>
#include <scitbx/array_family/accessors/c_grid.h>
#include <scitbx/array_family/tiny_types.h>
#include <dxtbx/model/beam.h>
#include <dxtbx/model/detector.h>
#include <dxtbx/model/goniometer.h>
#include <dxtbx/model/scan.h>
#include <dxtbx/format/image.h>
#include <dxtbx/error.h>
#include <dxtbx/masking/goniometer_shadow_masking.h>
namespace dxtbx {
using format::Image;
using format::ImageBuffer;
using format::ImageTile;
using masking::GoniometerShadowMasker;
using model::BeamBase;
using model::Detector;
using model::Goniometer;
using model::Panel;
using model::Scan;
using scitbx::rad_as_deg;
using scitbx::af::int2;
namespace detail {
/**
* Raise exception if we can't dereference pointer
*/
template <typename T>
T safe_dereference(boost::shared_ptr<T> ptr) {
T *item = ptr.get();
DXTBX_ASSERT(item != NULL);
return *item;
}
} // namespace detail
/**
* Hold an external lookup item
*/
template <typename T>
class ExternalLookupItem {
public:
/** Construct the external lookup item */
ExternalLookupItem() {}
/**
* Get the filename
*/
std::string get_filename() const {
return filename_;
}
/**
* Set the filename
* @param filename The filename
*/
void set_filename(const std::string &filename) {
filename_ = filename;
}
/**
* Get the data
*/
Image<T> get_data() const {
return data_;
}
/**
* Set the data
* @param data - The data
*/
void set_data(const Image<T> &data) {
data_ = data;
}
protected:
std::string filename_;
Image<T> data_;
};
/**
* The external lookup group
*/
class ExternalLookup {
public:
/**
* @returns an internal reference to the mask
*/
ExternalLookupItem<bool> &mask() {
return mask_;
}
/**
* @returns an internal reference to the gain
*/
ExternalLookupItem<double> &gain() {
return gain_;
}
/**
* @returns an internal reference to the pedestal
*/
ExternalLookupItem<double> &pedestal() {
return pedestal_;
}
/**
* @returns an internal reference to the dx map
*/
ExternalLookupItem<double> &dx() {
return dx_;
}
/**
* @returns an internal reference to the dy map
*/
ExternalLookupItem<double> &dy() {
return dy_;
}
protected:
ExternalLookupItem<bool> mask_;
ExternalLookupItem<double> gain_;
ExternalLookupItem<double> pedestal_;
ExternalLookupItem<double> dx_;
ExternalLookupItem<double> dy_;
};
/**
* A class to hold data about the imageset
*/
class ImageSetData {
public:
typedef boost::shared_ptr<BeamBase> beam_ptr;
typedef boost::shared_ptr<Detector> detector_ptr;
typedef boost::shared_ptr<Goniometer> goniometer_ptr;
typedef boost::shared_ptr<Scan> scan_ptr;
typedef boost::shared_ptr<GoniometerShadowMasker> masker_ptr;
ImageSetData() {}
/**
* Construct the imageset data object
* @param reader The image reader
* @param masker The image masker
*/
ImageSetData(boost::python::object reader, masker_ptr masker)
: reader_(reader),
masker_(masker),
beams_(boost::python::len(reader)),
detectors_(boost::python::len(reader)),
goniometers_(boost::python::len(reader)),
scans_(boost::python::len(reader)),
reject_(boost::python::len(reader)) {}
/**
* @returns The reader object
*/
boost::python::object reader() {
return reader_;
}
/**
* @returns The masker object
*/
masker_ptr masker() {
return masker_;
}
/**
* @returns Does the imageset have a dynamic mask.
*/
bool has_dynamic_mask() const {
return masker_ != NULL;
}
/**
* Read some image data
* @param index The image index
* @returns The image data
*/
ImageBuffer get_data(std::size_t index) {
// Create the return buffer
ImageBuffer buffer;
// Get the image data object
boost::python::object data = reader_.attr("read")(index);
// Get the class name
std::string name =
boost::python::extract<std::string>(data.attr("__class__").attr("__name__"))();
// Extract the image buffer
if (name == "tuple") {
buffer = get_image_buffer_from_tuple(
boost::python::extract<boost::python::tuple>(data)());
} else {
buffer = get_image_buffer_from_object(data);
}
return buffer;
}
/**
* @returns Is the reader a single file reader
*/
bool has_single_file_reader() const {
return boost::python::extract<bool>(reader_.attr("is_single_file_reader")())();
}
/**
* @returns The image path
*/
std::string get_path(std::size_t index) const {
return boost::python::extract<std::string>(reader_.attr("paths")()[index])();
}
/**
* @returns The master image path
*/
std::string get_master_path() const {
return boost::python::extract<std::string>(reader_.attr("master_path")())();
}
/**
* @returns The image identifier
*/
std::string get_image_identifier(std::size_t index) const {
return boost::python::extract<std::string>(reader_.attr("identifiers")()[index])();
}
/**
* Mark the image for rejection
* @param index The image index
* @param reject True/False reject
*/
void mark_for_rejection(std::size_t index, bool reject) {
DXTBX_ASSERT(index < reject_.size());
reject_[index] = reject;
}
/**
* @param index The image index
* @returns If the image is marked for rejection
*/
bool is_marked_for_rejection(std::size_t index) const {
DXTBX_ASSERT(index < reject_.size());
return reject_[index];
}
/**
* Get the array of booleans for image rejection
*/
scitbx::af::shared<bool> get_reject_list() const {
return reject_;
}
/**
* Set the array of booleans for image rejection
*/
void set_reject_list(const scitbx::af::const_ref<bool> &reject) {
DXTBX_ASSERT(reject_.size() == reject.size());
std::copy(reject.begin(), reject.end(), reject_.begin());
}
/**
* Get a beam model
* @param index The image index
* @returns The beam model
*/
beam_ptr get_beam(std::size_t index) const {
DXTBX_ASSERT(index < beams_.size());
return beams_[index];
}
/**
* Set a beam model
* @param index The image index
* @param The beam model
*/
void set_beam(const beam_ptr &beam, std::size_t index) {
DXTBX_ASSERT(index < beams_.size());
beams_[index] = beam;
}
/**
* Get a detector model
* @param index The image index
* @returns The detector model
*/
detector_ptr get_detector(std::size_t index) const {
DXTBX_ASSERT(index < detectors_.size());
return detectors_[index];
}
/**
* Set a detector model
* @param index The image index
* @param The detector model
*/
void set_detector(const detector_ptr &detector, std::size_t index) {
DXTBX_ASSERT(index < detectors_.size());
detectors_[index] = detector;
}
/**
* Get a goniometer model
* @param index The image index
* @returns The goniometer model
*/
goniometer_ptr get_goniometer(std::size_t index) const {
DXTBX_ASSERT(index < goniometers_.size());
return goniometers_[index];
}
/**
* Set a goniometer model
* @param index The image index
* @param The goniometer model
*/
void set_goniometer(const goniometer_ptr &goniometer, std::size_t index) {
DXTBX_ASSERT(index < goniometers_.size());
goniometers_[index] = goniometer;
}
/**
* Get a scan model
* @param index The image index
* @returns The scan model
*/
scan_ptr get_scan(std::size_t index) const {
DXTBX_ASSERT(index < scans_.size());
return scans_[index];
}
/**
* Set a scan model
* @param index The image index
* @param The scan model
*/
void set_scan(const scan_ptr &scan, std::size_t index) {
DXTBX_ASSERT(index < scans_.size());
scans_[index] = scan;
}
/**
* @returns the number of images
*/
std::size_t size() const {
return boost::python::len(reader_);
}
/**
* @returns The external lookup
*/
ExternalLookup &external_lookup() {
return external_lookup_;
}
/**
* @returns the template
*/
std::string get_template() const {
return template_;
}
/**
* @param x the template
*/
void set_template(std::string x) {
template_ = x;
}
/**
* @returns the vendor
*/
std::string get_vendor() const {
return vendor_;
}
/**
* @param x the vendor
*/
void set_vendor(std::string x) {
vendor_ = x;
}
/**
* @returns the params
*/
std::string get_params() const {
return params_;
}
/**
* @param x the params
*/
void set_params(std::string x) {
params_ = x;
}
/**
* @returns the format
*/
std::string get_format() const {
return format_;
}
/**
* @param x the format
*/
void set_format(std::string x) {
format_ = x;
}
protected:
ImageBuffer get_image_buffer_from_tuple(boost::python::tuple obj) {
// Get the class name
std::string name =
boost::python::extract<std::string>(obj[0].attr("__class__").attr("__name__"))();
// Read the image
ImageBuffer buffer;
if (name == "double") {
buffer = ImageBuffer(get_image_from_tuple<double>(obj));
} else if (name == "float") {
buffer = ImageBuffer(get_image_from_tuple<float>(obj));
} else if (name == "int") {
buffer = ImageBuffer(get_image_from_tuple<int>(obj));
} else {
throw DXTBX_ERROR("Unknown type " + name);
}
return buffer;
}
ImageBuffer get_image_buffer_from_object(boost::python::object obj) {
// Get the class name
std::string name =
boost::python::extract<std::string>(obj.attr("__class__").attr("__name__"))();
// Read the image
ImageBuffer buffer;
if (name == "double") {
buffer = ImageBuffer(Image<double>(get_image_tile_from_object<double>(obj)));
} else if (name == "float") {
buffer = ImageBuffer(Image<float>(get_image_tile_from_object<float>(obj)));
} else if (name == "int") {
buffer = ImageBuffer(Image<int>(get_image_tile_from_object<int>(obj)));
} else {
throw DXTBX_ERROR("Unknown type " + name);
}
return buffer;
}
template <typename T>
Image<T> get_image_from_tuple(boost::python::tuple obj) {
Image<T> image;
for (std::size_t i = 0; i < boost::python::len(obj); ++i) {
image.push_back(get_image_tile_from_object<T>(obj[i]));
}
return image;
}
template <typename T>
ImageTile<T> get_image_tile_from_object(boost::python::object obj) {
typedef typename scitbx::af::flex<T>::type flex_type;
// Extract the data
flex_type a = boost::python::extract<flex_type>(obj)();
// Return the image tile
return ImageTile<T>(scitbx::af::versa<T, scitbx::af::c_grid<2> >(
a.handle(), scitbx::af::c_grid<2>(a.accessor())));
}
boost::python::object reader_;
boost::shared_ptr<GoniometerShadowMasker> masker_;
scitbx::af::shared<beam_ptr> beams_;
scitbx::af::shared<detector_ptr> detectors_;
scitbx::af::shared<goniometer_ptr> goniometers_;
scitbx::af::shared<scan_ptr> scans_;
scitbx::af::shared<bool> reject_;
ExternalLookup external_lookup_;
std::string template_;
std::string vendor_;
std::string params_;
std::string format_;
};
/**
* A class to represent an imageset
*/
class ImageSet {
public:
typedef ImageSetData::beam_ptr beam_ptr;
typedef ImageSetData::detector_ptr detector_ptr;
typedef ImageSetData::goniometer_ptr goniometer_ptr;
typedef ImageSetData::scan_ptr scan_ptr;
/**
* Cache an image
*/
template <class T>
class DataCache {
public:
T image;
int index;
DataCache() : index(-1) {}
};
/**
* Default constructor throws an exception.
* This only here so overloaded functions that
* never return can be compiled without warnings.
*/
ImageSet() {
throw DXTBX_ERROR("ImageSet needs imageset data");
}
/**
* Destructor
*/
virtual ~ImageSet() {}
/**
* Construct the imageset
* @param data The imageset data
*/
ImageSet(const ImageSetData &data) : data_(data), indices_(data.size()) {
// Check number of images
if (data.size() == 0) {
throw DXTBX_ERROR("No images specified in ImageSetData");
}
// Set indices
for (std::size_t i = 0; i < indices_.size(); ++i) {
indices_[i] = i;
}
}
/**
* Construct the imageset
* @param data The imageset data
* @param indices The image indices
*/
ImageSet(const ImageSetData &data, const scitbx::af::const_ref<std::size_t> &indices)
: data_(data), indices_(indices.begin(), indices.end()) {
// Check number of images
if (data.size() == 0) {
throw DXTBX_ERROR("No images specified in ImageSetData");
}
// Set the indices
if (indices.size() == 0) {
indices_.resize(data.size());
for (std::size_t i = 0; i < indices_.size(); ++i) {
indices_[i] = i;
}
} else {
// Check indices
if (scitbx::af::max(indices) >= data.size()) {
throw DXTBX_ERROR("Indices are not consistent with # of images");
}
}
}
/**
* @returns the imageset data
*/
ImageSetData data() const {
return data_;
}
/**
* @returns The image indices
*/
scitbx::af::shared<std::size_t> indices() const {
return indices_;
}
/**
* @returns The number of images
*/
std::size_t size() const {
return indices_.size();
}
/**
* @returns The external lookup
*/
ExternalLookup &external_lookup() {
return data_.external_lookup();
}
/**
* Get the raw image data
* @param index The image index
* @returns The raw image data
*/
ImageBuffer get_raw_data(std::size_t index) {
DXTBX_ASSERT(index < indices_.size());
if (data_cache_.index == index) {
return data_cache_.image;
}
ImageBuffer image = data_.get_data(indices_[index]);
data_cache_.index = index;
data_cache_.image = image;
return image;
}
/**
* Get the corrected data array (raw - pedestal) / gain
* @param index The image index
* @returns The corrected data array
*/
Image<double> get_corrected_data(std::size_t index) {
typedef scitbx::af::versa<double, scitbx::af::c_grid<2> > array_type;
typedef scitbx::af::const_ref<double, scitbx::af::c_grid<2> > const_ref_type;
// Get the multi-tile data, gain and pedestal
DXTBX_ASSERT(index < indices_.size());
Image<double> data = get_raw_data_as_double(index);
Image<double> gain = get_gain(index);
Image<double> dark = get_pedestal(index);
DXTBX_ASSERT(gain.n_tiles() == 0 || data.n_tiles() == gain.n_tiles());
DXTBX_ASSERT(dark.n_tiles() == 0 || data.n_tiles() == dark.n_tiles());
// Loop through tiles
Image<double> result;
for (std::size_t i = 0; i < data.n_tiles(); ++i) {
// Get the data
const_ref_type r = data.tile(i).data().const_ref();
// Get the gain and dark
const_ref_type g = gain.n_tiles() > 0
? gain.tile(i).data().const_ref()
: const_ref_type(NULL, scitbx::af::c_grid<2>(0, 0));
const_ref_type p = dark.n_tiles() > 0
? dark.tile(i).data().const_ref()
: const_ref_type(NULL, scitbx::af::c_grid<2>(0, 0));
// Check gain and dark sizes
DXTBX_ASSERT(g.size() == 0 || r.accessor().all_eq(g.accessor()));
DXTBX_ASSERT(p.size() == 0 || r.accessor().all_eq(p.accessor()));
if (p.size() == 0 && g.size() == 0) {
// Nothing to apply, save the copy
result.push_back(ImageTile<double>(data.tile(i).data()));
} else {
// Create the result array
array_type c(r.accessor(),
scitbx::af::init_functor_null<array_type::value_type>());
// Copy the data values
std::uninitialized_copy(r.begin(), r.end(), c.begin());
// Apply dark
if (p.size() > 0) {
for (std::size_t j = 0; j < r.size(); ++j) {
c[j] = c[j] - p[j];
}
}
// Apply gain
if (g.size() > 0) {
for (std::size_t j = 0; j < r.size(); ++j) {
DXTBX_ASSERT(g[j] > 0);
c[j] = c[j] / g[j];
}
}
// Add the image tile
result.push_back(ImageTile<double>(c));
}
}
// Return the result
return result;
}
/**
* Get the detector gain map. Either take this from the external gain map or
* try to construct from the detector gain.
* @param index The image index
* @returns The gain
*/
Image<double> get_gain(std::size_t index) {
// If the external lookup is empty
DXTBX_ASSERT(index < indices_.size());
if (external_lookup().gain().get_data().empty()) {
// Get the detector
Detector detector = detail::safe_dereference(get_detector_for_image(index));
// Compute the gain for each panel
bool use_detector_gain = true;
bool need_gain_map = false;
std::vector<double> gain(detector.size(), 0);
for (std::size_t i = 0; i < detector.size(); ++i) {
gain[i] = detector[i].get_gain();
if (gain[i] <= 0) {
use_detector_gain = false;
break;
} else if (std::abs(gain[i] - 1.0) > 1e-7) {
need_gain_map = true;
}
}
// If using the gain from the panel, construct a gain map
if (use_detector_gain && need_gain_map) {
Image<double> result;
for (std::size_t i = 0; i < detector.size(); ++i) {
std::size_t xsize = detector[i].get_image_size()[0];
std::size_t ysize = detector[i].get_image_size()[1];
scitbx::af::c_grid<2> grid(ysize, xsize);
scitbx::af::versa<double, scitbx::af::c_grid<2> > data(grid, gain[i]);
result.push_back(ImageTile<double>(data));
}
return result;
}
}
return external_lookup().gain().get_data();
}
/**
* Get the pedestal
* @param index The image index
* @returns The pedestal image
*/
Image<double> get_pedestal(std::size_t index) {
//
// If the external lookup is empty
DXTBX_ASSERT(index < indices_.size());
if (external_lookup().pedestal().get_data().empty()) {
// Get the detector
Detector detector = detail::safe_dereference(get_detector_for_image(index));
// Compute the pedestal for each panel
bool use_detector_pedestal = false;
std::vector<double> pedestal(detector.size(), 0);
for (std::size_t i = 0; i < detector.size(); ++i) {
pedestal[i] = detector[i].get_pedestal();
if (std::abs(pedestal[i]) > 1e-7) {
use_detector_pedestal = true;
}
}
// If using the pedestal from the panel, construct a pedestal map
if (use_detector_pedestal) {
Image<double> result;
for (std::size_t i = 0; i < detector.size(); ++i) {
std::size_t xsize = detector[i].get_image_size()[0];
std::size_t ysize = detector[i].get_image_size()[1];
scitbx::af::c_grid<2> grid(ysize, xsize);
scitbx::af::versa<double, scitbx::af::c_grid<2> > data(grid, pedestal[i]);
result.push_back(ImageTile<double>(data));
}
return result;
}
}
return external_lookup().pedestal().get_data();
}
/**
* @returns Does the imageset have a dynamic mask.
*/
bool has_dynamic_mask() const {
return data_.has_dynamic_mask();
}
/**
* Get an empty mask
* @param index The image index
* @returns The mask
*/
Image<bool> get_empty_mask() const {
Detector detector = detail::safe_dereference(get_detector_for_image(0));
Image<bool> mask;
for (std::size_t i = 0; i < detector.size(); ++i) {
std::size_t xsize = detector[i].get_image_size()[0];
std::size_t ysize = detector[i].get_image_size()[1];
mask.push_back(ImageTile<bool>(scitbx::af::versa<bool, scitbx::af::c_grid<2> >(
scitbx::af::c_grid<2>(ysize, xsize), true)));
}
return mask;
}
/**
* Get the untrusted rectangle mask
* @param mask The mask to write into
* @returns The mask
*/
Image<bool> get_untrusted_rectangle_mask(Image<bool> mask) const {
Detector detector = detail::safe_dereference(get_detector_for_image(0));
DXTBX_ASSERT(mask.n_tiles() == detector.size());
for (std::size_t i = 0; i < detector.size(); ++i) {
detector[i].apply_untrusted_rectangle_mask(mask.tile(i).data().ref());
}
return mask;
}
/**
* Apply the external mask
* @param mask The input mask
* @returns The external mask
*/
Image<bool> get_external_mask(Image<bool> mask) {
Image<bool> external_mask = external_lookup().mask().get_data();
if (!external_mask.empty()) {
DXTBX_ASSERT(external_mask.n_tiles() == mask.n_tiles());
for (std::size_t i = 0; i < mask.n_tiles(); ++i) {
scitbx::af::ref<bool, scitbx::af::c_grid<2> > m1 = mask.tile(i).data().ref();
scitbx::af::const_ref<bool, scitbx::af::c_grid<2> > m2 =
external_mask.tile(i).data().const_ref();
DXTBX_ASSERT(m1.accessor().all_eq(m2.accessor()));
for (std::size_t j = 0; j < m1.size(); ++j) {
m1[j] = m1[j] && m2[j];
}
}
}
return mask;
}
/**
* Get the static mask common to all images
* @param mask The input mask
* @returns The mask
*/
Image<bool> get_static_mask(Image<bool> mask) {
return get_untrusted_rectangle_mask(
get_external_mask(mask.empty() ? get_empty_mask() : mask));
}
/**
* Get the static mask common to all images
* @returns The mask
*/
Image<bool> get_static_mask() {
return get_static_mask(Image<bool>());
}
/**
* Get the trusted range mask for the index
* @param mask The mask to write into
* @param index The image index
* @returns The mask
*/
Image<bool> get_trusted_range_mask(Image<bool> mask, std::size_t index) {
Detector detector = detail::safe_dereference(get_detector_for_image(index));
Image<double> data = get_raw_data_as_double(index);
DXTBX_ASSERT(mask.n_tiles() == data.n_tiles());
DXTBX_ASSERT(data.n_tiles() == detector.size());
for (std::size_t i = 0; i < detector.size(); ++i) {
detector[i].apply_trusted_range_mask(data.tile(i).data().const_ref(),
mask.tile(i).data().ref());
}
return mask;
}
/**
* Get the dynamic mask for the requested image
* @param index The image index
* @returns The image mask
*/
virtual Image<bool> get_dynamic_mask(std::size_t index) {
return get_trusted_range_mask(get_static_mask(), index);
}
/**
* Compute the mask
* @param index The image index
* @returns The image mask
*/
Image<bool> get_mask(std::size_t index) {
return get_dynamic_mask(index);
}
/**
* @param index The image index
* @returns the beam at index
*/
virtual beam_ptr get_beam_for_image(std::size_t index = 0) const {
DXTBX_ASSERT(index < indices_.size());
return data_.get_beam(indices_[index]);
}
/**
* @param index The image index
* @returns the detector at index
*/
virtual detector_ptr get_detector_for_image(std::size_t index = 0) const {
DXTBX_ASSERT(index < indices_.size());
return data_.get_detector(indices_[index]);
}
/**
* @param index The image index
* @returns the goniometer at index
*/
virtual goniometer_ptr get_goniometer_for_image(std::size_t index = 0) const {
DXTBX_ASSERT(index < indices_.size());
return data_.get_goniometer(indices_[index]);
}
/**
* @param index The image index
* @returns the scan at index
*/
virtual scan_ptr get_scan_for_image(std::size_t index = 0) const {
DXTBX_ASSERT(index < indices_.size());
return data_.get_scan(indices_[index]);
}
/**
* Set the beam model
* @param index The image index
* @param beam The beam model
*/
virtual void set_beam_for_image(const beam_ptr &beam, std::size_t index = 0) {
DXTBX_ASSERT(index < indices_.size());
data_.set_beam(beam, indices_[index]);
}
/**
* Set the detector model
* @param index The image index
* @param detector The detector model
*/
virtual void set_detector_for_image(const detector_ptr &detector,
std::size_t index = 0) {
DXTBX_ASSERT(index < indices_.size());
data_.set_detector(detector, indices_[index]);
}
/**
* Set the goniometer model
* @param index The image index
* @param goniometer The goniometer model
*/
virtual void set_goniometer_for_image(const goniometer_ptr &goniometer,
std::size_t index = 0) {
DXTBX_ASSERT(index < indices_.size());
data_.set_goniometer(goniometer, indices_[index]);
}
/**
* Set the scan model
* @param index The image index
* @param scan The scan model
*/
virtual void set_scan_for_image(const scan_ptr &scan, std::size_t index = 0) {
DXTBX_ASSERT(scan == NULL || scan->get_num_images() == 1);
DXTBX_ASSERT(index < indices_.size());
data_.set_scan(scan, indices_[index]);
}
/**
* Get the image path
* @param index The image index
* @returns The image path
*/
std::string get_path(std::size_t index) const {
DXTBX_ASSERT(index < indices_.size());
if (data_.has_single_file_reader()) {
return data_.get_master_path();
}
return data_.get_path(indices_[index]);
}
/**
* Get the image identifier
* @param index The image index
* @returns The image identifier
*/
std::string get_image_identifier(std::size_t index) const {
DXTBX_ASSERT(index < indices_.size());
return data_.get_image_identifier(indices_[index]);
}