ATLAS Offline Software
Functions | Variables
module_selector_from_json Namespace Reference

Functions

def load_json (file_path="Geometry.json")
 
def find (bec=None, layer_disk=None, phi=None, eta=None, side=None, asdec=False, input_data="Geometry.json", output_file="Geometry_find.json")
 
def merge (file_1="Geometry_1.json", file_2="Geometry_2.json", output_file="Geometry_merge.json")
 
def select_random (frac=0, input_data="Geometry.json", output_file="Geometry_rand.json", seed=None)
 
def generate_uncorr (fractions=[], input_data="Geometry.json", prefix="Geometry_rand", output_dir=None, seed=None)
 
def generate_corr (fractions=[], input_data="geometry.json", prefix="Geometry_rand", output_dir=None, seed=None)
 
def difference (file_1="Geometry_1.json", file_2="Geometry_2.json", output_file="Geometry_diff.json")
 
def 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__))
 
def 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()

def 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 195 of file module_selector_from_json.py.

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

◆ find()

def 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.

28 def 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 

◆ frac_convertor()

def 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 213 of file module_selector_from_json.py.

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

◆ generate_corr()

def 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.

143 def 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(f"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  data_temp = 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  del data_temp
187 
188  IDs.append(data)
189 
190  if i == len(fractions) - 1:
191  IDs.append(data_not_masked)
192 
193  return IDs
194 

◆ generate_uncorr()

def 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.

118 def 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(f"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()

def 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.

11 def 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 

◆ merge()

def 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.

64 def 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()

def 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.

88 def 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 258 of file module_selector_from_json.py.

◆ data

def 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 264 of file module_selector_from_json.py.

DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
module_selector_from_json.generate_uncorr
def generate_uncorr(fractions=[], input_data="Geometry.json", prefix="Geometry_rand", output_dir=None, seed=None)
Definition: module_selector_from_json.py:118
module_selector_from_json.generate_corr
def generate_corr(fractions=[], input_data="geometry.json", prefix="Geometry_rand", output_dir=None, seed=None)
Definition: module_selector_from_json.py:143
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
module_selector_from_json.select_random
def select_random(frac=0, input_data="Geometry.json", output_file="Geometry_rand.json", seed=None)
Definition: module_selector_from_json.py:88
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
module_selector_from_json.load_json
def load_json(file_path="Geometry.json")
Definition: module_selector_from_json.py:11
module_selector_from_json.difference
def difference(file_1="Geometry_1.json", file_2="Geometry_2.json", output_file="Geometry_diff.json")
Definition: module_selector_from_json.py:195
module_selector_from_json.frac_convertor
def frac_convertor(frac=None, file_1="Geometry.json", file_2="Selected_geometry.json", file_3="Not_selected_yet.json")
Definition: module_selector_from_json.py:213
module_selector_from_json.merge
def merge(file_1="Geometry_1.json", file_2="Geometry_2.json", output_file="Geometry_merge.json")
Definition: module_selector_from_json.py:64
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
str
Definition: BTagTrackIpAccessor.cxx:11
merge
Definition: merge.py:1