5
5
# & Institut Laue - Langevin
6
6
# SPDX - License - Identifier: GPL - 3.0 +
7
7
8
- from typing import Optional
9
-
10
8
from sans .common .enums import SANSInstrument , ReductionMode , DetectorType , RangeStepType , FitModeForMerge , DataType , FitType , RebinType
11
9
from sans .common .general_functions import get_bank_for_spectrum_number , get_detector_types_from_instrument
12
- from sans .state .IStateParser import IStateParser
13
10
from sans .state .StateObjects .StateAdjustment import StateAdjustment
14
11
from sans .state .StateObjects .StateCalculateTransmission import get_calculate_transmission
15
12
from sans .state .StateObjects .StateCompatibility import StateCompatibility
18
15
from sans .state .StateObjects .StateMaskDetectors import get_mask_builder , StateMaskDetectors
19
16
from sans .state .StateObjects .StateMoveDetectors import get_move_builder
20
17
from sans .state .StateObjects .StateNormalizeToMonitor import get_normalize_to_monitor_builder
21
- from sans .state .StateObjects .StatePolarization import StatePolarization , StateComponent , StateFilter , StateField
18
+ from sans .state .StateObjects .StatePolarization import StatePolarization
22
19
from sans .state .StateObjects .StateReductionMode import StateReductionMode
23
20
from sans .state .StateObjects .StateSave import StateSave
24
21
from sans .state .StateObjects .StateScale import StateScale
28
25
from sans .user_file .parser_helpers .toml_parser_impl_base import TomlParserImplBase
29
26
from sans .user_file .parser_helpers .wavelength_parser import DuplicateWavelengthStates , WavelengthTomlParser
30
27
from sans .user_file .toml_parsers .toml_v1_schema import TomlSchemaV1Validator
28
+ from sans .user_file .toml_parsers .toml_base_parser import TomlParserBase
31
29
32
30
33
- class TomlV1Parser (IStateParser ):
31
+ class TomlV1Parser (TomlParserBase ):
34
32
def __init__ (self , dict_to_parse , file_information , schema_validator = None ):
35
- self ._validator = schema_validator if schema_validator else TomlSchemaV1Validator (dict_to_parse )
36
- self ._validator .validate ()
37
-
38
- self ._implementation = None
39
- data_info = self .get_state_data (file_information )
40
- self ._implementation = self ._get_impl (dict_to_parse , data_info )
41
- self ._implementation .parse_all ()
33
+ validator = schema_validator if schema_validator else TomlSchemaV1Validator (dict_to_parse )
34
+ super (TomlV1Parser , self ).__init__ (dict_to_parse , file_information , validator )
42
35
43
36
@staticmethod
44
37
def _get_impl (* args ):
45
38
# Wrapper which can replaced with a mock
46
- return _TomlV1ParserImpl (* args )
39
+ return TomlV1ParserImpl (* args )
47
40
48
41
def get_state_data (self , file_information ):
49
42
state_data = super ().get_state_data (file_information )
@@ -76,8 +69,9 @@ def get_state_normalize_to_monitor(self, _):
76
69
def get_state_reduction_mode (self ):
77
70
return self ._implementation .reduction_mode
78
71
79
- def get_state_polarization (self ) -> Optional [StatePolarization ]:
80
- return self ._implementation .polarization
72
+ def get_state_polarization (self ) -> StatePolarization :
73
+ # Not supported by TOML V1, but we return a blank one to keep the parsing results consistent.
74
+ return StatePolarization ()
81
75
82
76
def get_state_save (self ):
83
77
return StateSave ()
@@ -98,9 +92,9 @@ def get_state_wavelength_and_pixel_adjustment(self):
98
92
return self ._implementation .wavelength_and_pixel
99
93
100
94
101
- class _TomlV1ParserImpl (TomlParserImplBase ):
95
+ class TomlV1ParserImpl (TomlParserImplBase ):
102
96
def __init__ (self , input_dict , data_info : StateData ):
103
- super (_TomlV1ParserImpl , self ).__init__ (toml_dict = input_dict )
97
+ super (TomlV1ParserImpl , self ).__init__ (toml_dict = input_dict )
104
98
# Always take the instrument from the TOML file rather than guessing in the new parser
105
99
data_info .instrument = self .instrument
106
100
self ._create_state_objs (data_info = data_info )
@@ -118,7 +112,6 @@ def parse_all(self):
118
112
self ._parse_transmission ()
119
113
self ._parse_transmission_roi ()
120
114
self ._parse_transmission_fitting ()
121
- self ._parse_polarization ()
122
115
123
116
@property
124
117
def instrument (self ):
@@ -141,7 +134,6 @@ def _create_state_objs(self, data_info):
141
134
self .scale = StateScale ()
142
135
self .wavelength = StateWavelength ()
143
136
self .wavelength_and_pixel = get_wavelength_and_pixel_adjustment_builder (data_info = data_info ).build ()
144
- self .polarization = StatePolarization ()
145
137
146
138
# Ensure they are linked up correctly
147
139
self .adjustment .calculate_transmission = self .calculate_transmission
@@ -519,60 +511,6 @@ def _parse_mask(self):
519
511
if "stop" in phi_mask :
520
512
self .mask .phi_max = phi_mask ["stop" ]
521
513
522
- def _parse_polarization (self ):
523
- polarization_dict = self .get_val ("polarization" )
524
- if polarization_dict is None :
525
- return
526
- self .polarization .flipper_configuration = self .get_val ("flipper_configuration" , polarization_dict )
527
- self .polarization .spin_configuration = self .get_val ("spin_configuration" , polarization_dict )
528
- flipper_dicts = self .get_val ("flipper" , polarization_dict )
529
- if flipper_dicts :
530
- for flipper_dict in flipper_dicts .values ():
531
- self .polarization .flippers .append (self ._parse_component (flipper_dict ))
532
- self .polarization .polarizer = self ._parse_filter (self .get_val ("polarizer" , polarization_dict ))
533
- self .polarization .analyzer = self ._parse_filter (self .get_val ("analyzer" , polarization_dict ))
534
- self .polarization .magnetic_field = self ._parse_field (self .get_val ("magnetic_field" , polarization_dict ))
535
- self .polarization .electric_field = self ._parse_field (self .get_val ("electric_field" , polarization_dict ))
536
- self .polarization .validate ()
537
-
538
- def _parse_component (self , component_dict : dict ) -> StateComponent :
539
- component_state = StateComponent ()
540
- if component_dict is None :
541
- return component_state
542
- component_state .idf_component_name = self .get_val ("idf_component_name" , component_dict )
543
- component_state .device_name = self .get_val ("device_name" , component_dict )
544
- component_state .device_type = self .get_val ("device_type" , component_dict )
545
- location_dict = self .get_val ("location" , component_dict )
546
- if location_dict :
547
- component_state .location_x = self .get_val ("x" , location_dict )
548
- component_state .location_y = self .get_val ("y" , location_dict )
549
- component_state .location_z = self .get_val ("z" , location_dict )
550
- component_state .transmission = self .get_val ("transmission" , component_dict )
551
- component_state .efficiency = self .get_val ("efficiency" , component_dict )
552
- return component_state
553
-
554
- def _parse_filter (self , filter_dict : dict ) -> StateFilter :
555
- if filter_dict is None :
556
- return StateFilter ()
557
- filter_state = self ._parse_component (filter_dict )
558
- filter_state .__class__ = StateFilter
559
- filter_state .cell_length = self .get_val ("cell_length" , filter_dict )
560
- filter_state .gas_pressure = self .get_val ("gas_pressure" , filter_dict )
561
- return filter_state
562
-
563
- def _parse_field (self , field_dict : dict ) -> StateField :
564
- field_state = StateField ()
565
- if field_dict is None :
566
- return field_state
567
- field_state .sample_strength_log = self .get_val ("sample_strength_log" , field_dict )
568
- direction_dict = self .get_val ("sample_direction" , field_dict )
569
- if direction_dict :
570
- field_state .sample_direction_a = self .get_val ("a" , direction_dict )
571
- field_state .sample_direction_p = self .get_val ("p" , direction_dict )
572
- field_state .sample_direction_d = self .get_val ("d" , direction_dict )
573
- field_state .sample_direction_log = self .get_val ("sample_direction_log" , field_dict )
574
- return field_state
575
-
576
514
@staticmethod
577
515
def _get_1d_min_max (one_d_binning : str ):
578
516
# TODO: We have to do some special parsing for this type on behalf of the sans codebase
0 commit comments