ATLAS Offline Software
Loading...
Searching...
No Matches
module_selector_from_json Namespace Reference

Functions

 load_json (file_path="Geometry.json")
 find (bec=None, layer_disk=None, phi=None, eta=None, side=None, asdec=False, input_data="Geometry.json", output_file="Geometry_find.json")
 merge (file_1="Geometry_1.json", file_2="Geometry_2.json", output_file="Geometry_merge.json")
 select_random (frac=0, input_data="Geometry.json", output_file="Geometry_rand.json", seed=None)
 generate_uncorr (fractions=[], input_data="Geometry.json", prefix="Geometry_rand", output_dir=None, seed=None)
 generate_corr (fractions=[], input_data="geometry.json", prefix="Geometry_rand", output_dir=None, seed=None)
 difference (file_1="Geometry_1.json", file_2="Geometry_2.json", output_file="Geometry_diff.json")
 frac_convertor (frac=None, file_1="Geometry.json", file_2="Selected_geometry.json", file_3="Not_selected_yet.json")

Variables

 current_dir = os.path.dirname(os.path.abspath(__file__))
 data = find(bec = 0, layer_disk = 0, phi = None, eta = None, side = None, asdec = False, input_data = "PixelGeometry.json", output_file = "IBL_modules.json")

Function Documentation

◆ difference()

module_selector_from_json.difference ( file_1 = "Geometry_1.json",
file_2 = "Geometry_2.json",
output_file = "Geometry_diff.json" )
Function to perform the differnence between two json files produces with different configuration

Definition at line 194 of file module_selector_from_json.py.

194def difference(file_1 = "Geometry_1.json", file_2 = "Geometry_2.json", output_file = "Geometry_diff.json"):
195
196 '''
197 Function to perform the differnence between two json files produces with different configuration
198 '''
199
200 data1=load_json(file_1)
201 data2=load_json(file_2)
202 IDs = []
203
204 IDs = [ID for ID in list(data1.keys()) if ID not in list(data2.keys())]
205
206 with open(output_file, "w") as f:
207 selected_data = {ID : data1[ID] for ID in IDs}
208 json.dump(selected_data, f, indent=4)
209
210 return IDs
211

◆ find()

module_selector_from_json.find ( bec = None,
layer_disk = None,
phi = None,
eta = None,
side = None,
asdec = False,
input_data = "Geometry.json",
output_file = "Geometry_find.json" )
this can look for specific set of modules based on parameters: bec(barrel or end cap), layer_disk, phi, eta and side.

Definition at line 28 of file module_selector_from_json.py.

28def find(bec = None, layer_disk = None, phi = None, eta = None, side = None, asdec = False, input_data = "Geometry.json", output_file = "Geometry_find.json"):
29
30 '''
31 this can look for specific set of modules based on parameters: bec(barrel or end cap), layer_disk, phi, eta and side.
32 '''
33
34 data = load_json(input_data)
35 IDs = []
36
37 for ID, info in data.items():
38 if bec is not None and int(info["BEC"]) != bec:
39 continue
40
41 if layer_disk is not None and int(info["LayerDisk"]) != layer_disk:
42 continue
43
44 if phi is not None and int(info["PhiModule"]) not in [i for i in phi]:
45 continue
46
47 if eta is not None and int(info["EtaModule"]) > eta:
48 continue
49
50 if side is not None and int(info["Side"]) != side:
51 continue
52
53 if asdec:
54 info["Decimal_ID"] = str(int(ID, 16))
55
56 IDs.append(ID)
57
58 with open(output_file, "w") as f:
59 selected_data = {ID: data[ID] for ID in IDs}
60 json.dump(selected_data, f, indent=4)
61
62 return IDs
63
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138

◆ frac_convertor()

module_selector_from_json.frac_convertor ( frac = None,
file_1 = "Geometry.json",
file_2 = "Selected_geometry.json",
file_3 = "Not_selected_yet.json" )
Function used in the generate_corr() function to compute the new fraction of module to select.

Since the input_data file in select_random() contains less and less modules as one selects them, the fraction of module to be selected has to be recompute to match the desired one.

Exemple : 
    In file A, there are 100 modules. One wants to create two correlated files : 
        - file B : 10% of modules ( size = 10 modules )
        - file C : 50% of modules ( size = 50 modules = 10 modules from file A + 40 new modules )

        For file C, one can not use 0.5 as a fraction because it would select 45 modules and file C would contains 55 modules instead of 50, as desired:
        
            # of selected modules = len(file A - file B) * frac = (100 - 10) * 0.5 = 45
            So, 45 + 10 = 55 !

        The new fraction should be:

            new_frac = [ (number of wanted modules in final file) - (number of already selected modules) ] / (number of not yet selected modules) 
                     = [ (len(file A) * frac) - len(file B) ] / (len(file A - file B))
                     = [ 50 - 10 ] / 90
                     =  4/9 ~ 0.4445

        So the file C contains :

            # of selected modules = len(file A - file B) * new_frac = (100 - 10) * 4/9 = 40
            So, 40 + 10 = 50 !

Definition at line 212 of file module_selector_from_json.py.

212def frac_convertor(frac = None, file_1 = "Geometry.json", file_2 = "Selected_geometry.json", file_3 = "Not_selected_yet.json"):
213
214 '''
215 Function used in the generate_corr() function to compute the new fraction of module to select.
216
217 Since the input_data file in select_random() contains less and less modules as one selects them, the fraction of module to be selected has to be recompute to match the desired one.
218
219 Exemple :
220 In file A, there are 100 modules. One wants to create two correlated files :
221 - file B : 10% of modules ( size = 10 modules )
222 - file C : 50% of modules ( size = 50 modules = 10 modules from file A + 40 new modules )
223
224 For file C, one can not use 0.5 as a fraction because it would select 45 modules and file C would contains 55 modules instead of 50, as desired:
225
226 # of selected modules = len(file A - file B) * frac = (100 - 10) * 0.5 = 45
227 So, 45 + 10 = 55 !
228
229 The new fraction should be:
230
231 new_frac = [ (number of wanted modules in final file) - (number of already selected modules) ] / (number of not yet selected modules)
232 = [ (len(file A) * frac) - len(file B) ] / (len(file A - file B))
233 = [ 50 - 10 ] / 90
234 = 4/9 ~ 0.4445
235
236 So the file C contains :
237
238 # of selected modules = len(file A - file B) * new_frac = (100 - 10) * 4/9 = 40
239 So, 40 + 10 = 50 !
240 '''
241
242 data1=load_json(file_1)
243 data2=load_json(file_2)
244 data3=load_json(file_3)
245
246 nIDs1=len(data1)
247 nIDs2=len(data2)
248 nIDs3=len(data3)
249
250 nIDs=int(frac*nIDs1) - nIDs2
251 new_frac = nIDs / nIDs3 if nIDs3 > 0 else 1
252
253 return new_frac
254

◆ generate_corr()

module_selector_from_json.generate_corr ( fractions = [],
input_data = "geometry.json",
prefix = "Geometry_rand",
output_dir = None,
seed = None )
Generate correlated files using a random selection for each fractions, but modules that have been selected are kept in the next files and removed from the input_data file.

Exemple : 
    Running :
        generate_corr(fraction=[1,5], input_data = "PixelGeometry.json", prefix = "PixelGeometry" output_dir = "path/to/dir")
    will create 3 files : 
        - PixelGeometry_1pct_corr.json : Contains 1% of modules from PixelGeometry.json file
        - PixelGeometry_5pct_corr.json : Contains all selected modules in the 1% file + additional Pixel modules to get to a total of 5% module w.r.t to the input file, PixelGeometry.json
        - PixelGeometry_not_selected.json : Contains all modules that have not been randomly selected.

Definition at line 143 of file module_selector_from_json.py.

143def generate_corr(fractions = [], input_data = "geometry.json", prefix = "Geometry_rand", output_dir = None, seed = None):
144
145 '''
146 Generate correlated files using a random selection for each fractions, but modules that have been selected are kept in the next files and removed from the input_data file.
147
148 Exemple :
149 Running :
150 generate_corr(fraction=[1,5], input_data = "PixelGeometry.json", prefix = "PixelGeometry" output_dir = "path/to/dir")
151 will create 3 files :
152 - PixelGeometry_1pct_corr.json : Contains 1% of modules from PixelGeometry.json file
153 - PixelGeometry_5pct_corr.json : Contains all selected modules in the 1% file + additional Pixel modules to get to a total of 5% module w.r.t to the input file, PixelGeometry.json
154 - PixelGeometry_not_selected.json : Contains all modules that have not been randomly selected.
155
156 '''
157
158 if output_dir is None:
159 raise ValueError("No output_dir was provided.")
160
161 not_selected_yet_file=f"{output_dir}/{prefix}_not_selected.json"
162
163 IDs = []
164 output_files = []
165
166 for i, frac in enumerate(sorted(fractions)):
167 pct = int(frac*100)
168 output_file = f"{output_dir}/{prefix}_{pct}pct_corr.json"
169 output_file_temp = f"{output_dir}/{prefix}_{pct}pct_temp.json"
170
171 if i == 0:
172 data = select_random(frac, input_data, output_file, seed)
173 data_not_masked = difference(input_data, output_file, not_selected_yet_file)
174 output_files.append(output_file)
175
176 else:
177 new_frac = frac_convertor(frac, input_data, output_files[i-1], not_selected_yet_file)
178
179 select_random(new_frac, not_selected_yet_file, output_file_temp, seed)
180 data_not_masked = difference(not_selected_yet_file, output_file_temp, not_selected_yet_file)
181
182 data = merge(output_files[i-1], output_file_temp, output_file)
183 output_files.append(output_file)
184
185 os.remove(output_file_temp)
186
187 IDs.append(data)
188
189 if i == len(fractions) - 1:
190 IDs.append(data_not_masked)
191
192 return IDs
193
Definition merge.py:1

◆ generate_uncorr()

module_selector_from_json.generate_uncorr ( fractions = [],
input_data = "Geometry.json",
prefix = "Geometry_rand",
output_dir = None,
seed = None )
Function to generate uncorrelated files. Each file contains randomly selected modules according to the values in fractions.

Exemple : 
    Running : 
        generate_uncorr(fraction=[1,5], input_data = "PixelGeometry.json", prefix = "PixelGeometry" output_dir = "path/to/dir")
    will create 2 files:
        - PixelGeometry_1pct_corr.json : Contains 1% of modules from PixelGeometry.json file.
        - PixelGeometry_5pct_corr.json : Contains 5% of modules from PixelGeometry.json file (Some module can be in the first file but not the second one and vis versa.)

Definition at line 118 of file module_selector_from_json.py.

118def generate_uncorr(fractions = [], input_data = "Geometry.json", prefix = "Geometry_rand", output_dir = None, seed = None):
119
120 '''
121 Function to generate uncorrelated files. Each file contains randomly selected modules according to the values in fractions.
122
123 Exemple :
124 Running :
125 generate_uncorr(fraction=[1,5], input_data = "PixelGeometry.json", prefix = "PixelGeometry" output_dir = "path/to/dir")
126 will create 2 files:
127 - PixelGeometry_1pct_corr.json : Contains 1% of modules from PixelGeometry.json file.
128 - PixelGeometry_5pct_corr.json : Contains 5% of modules from PixelGeometry.json file (Some module can be in the first file but not the second one and vis versa.)
129 '''
130
131 if output_dir is None:
132 raise ValueError("No output_dir was provided.")
133
134 IDs = []
135 for frac in sorted(fractions):
136 output_file = f"{output_dir}/{prefix}_{int(frac*100)}pct_uncorr.json"
137 data = select_random(frac, input_data, output_file, seed)
138
139 IDs.append(data)
140
141 return IDs
142

◆ load_json()

module_selector_from_json.load_json ( file_path = "Geometry.json")
Function to load properly module informations from an input json file.

Definition at line 11 of file module_selector_from_json.py.

11def load_json(file_path = "Geometry.json"):
12
13 '''
14 Function to load properly module informations from an input json file.
15 '''
16
17 if file_path.endswith(".json"):
18 try:
19 with open(file_path, "r") as file:
20 return json.load(file)
21 except Exception as e:
22 print(f"Can not read file at {file_path}: {e}.")
23 return {}
24 else:
25 print("Unexpected input file or format.")
26 return {}
27
void print(char *figname, TCanvas *c1)

◆ merge()

module_selector_from_json.merge ( file_1 = "Geometry_1.json",
file_2 = "Geometry_2.json",
output_file = "Geometry_merge.json" )
Function to merge any two json files produced with different configuration.

Definition at line 64 of file module_selector_from_json.py.

64def merge(file_1 = "Geometry_1.json", file_2 = "Geometry_2.json", output_file = "Geometry_merge.json"):
65
66 '''
67 Function to merge any two json files produced with different configuration.
68 '''
69
70 data1 = load_json(file_1)
71 data2 = load_json(file_2)
72 IDs = []
73
74 IDs.extend(data1.keys())
75 IDs.extend(data2.keys())
76
77 with open(output_file, "w") as f3:
78 selected_data = {}
79 for ID in IDs:
80 if ID in data1:
81 selected_data[ID] = data1[ID]
82 elif ID in data2:
83 selected_data[ID] = data2[ID]
84 json.dump(selected_data, f3, indent=4)
85
86 return IDs
87

◆ select_random()

module_selector_from_json.select_random ( frac = 0,
input_data = "Geometry.json",
output_file = "Geometry_rand.json",
seed = None )
Function to select a given fraction of modules randomly from all the modules using Geometry.json as input. The seed can be change.

Definition at line 88 of file module_selector_from_json.py.

88def select_random(frac = 0, input_data = "Geometry.json", output_file = "Geometry_rand.json", seed = None):
89
90 '''
91 Function to select a given fraction of modules randomly from all the modules using Geometry.json as input. The seed can be change.
92 '''
93
94 data = load_json(input_data)
95 IDs = []
96
97 num = int(round(frac * len(data), 0))
98
99 if seed:
100 random.seed(seed)
101
102 # Use of random.sample() instead of random.choice() to avoid selecting the same module multiple times.
103 IDs = random.sample(list(data.keys()), k=num)
104
105 for ID, info in data.items():
106 info["Decimal_ID"] = str(int(ID, 16))
107
108 with open(output_file, "w") as f:
109 # In a dictionary, keys must be unique.
110 # Therefore, if the same module is selected multiple times,
111 # the key will only appear once.
112 # As a result, the number of modules selected may not match the desired count.
113 selected_data = {ID: data[ID] for ID in IDs}
114 json.dump(selected_data, f, indent=4)
115
116 return IDs
117

Variable Documentation

◆ current_dir

module_selector_from_json.current_dir = os.path.dirname(os.path.abspath(__file__))

Definition at line 257 of file module_selector_from_json.py.

◆ data

module_selector_from_json.data = find(bec = 0, layer_disk = 0, phi = None, eta = None, side = None, asdec = False, input_data = "PixelGeometry.json", output_file = "IBL_modules.json")

Definition at line 263 of file module_selector_from_json.py.