SND@LHC Software
Loading...
Searching...
No Matches
add_noise_to_field.py
Go to the documentation of this file.
1import pandas as pd
2import numpy as np
3import matplotlib.pylab as plt
4from scipy.ndimage import gaussian_filter
5import random
6import argparse
7import os
8
9def plot_my_hist(datum):
10 plotData = datum[datum['y'] == 0]
11 H, xedges, yedges = np.histogram2d(plotData['x'], plotData['z'], bins=[50, 500], weights=plotData['by'])
12 plt.figure(figsize=[20, 10])
13 plt.imshow(H, interpolation='nearest', origin='low')
14 # plt.colorbar()
15 plt.show()
16
17def generate_file(input_fileName, output, xSpace=73, ySpace=128, zSpace=1214, step=2.5, args=None):
18 # (min, max, max/stepSize + 1) in case of Z: (0, nSteps*2.5 - 2.5, nSteps)
19 field = pd.read_csv(input_fileName, skiprows=1, sep ='\s+', names=['x', 'y', 'z', 'bx', 'by', 'bz'])
20
21 field_mask = field.copy()
22 field_mask[['bx', 'by', 'bz']] = field_mask[['bx', 'by', 'bz']] != 0
23
24 field_new = field.copy()
25
26 if args.sidesOnly:
27 temp_by = np.array(field_new['by']).reshape([xSpace, ySpace, zSpace])
28 temp_by = gaussian_filter(temp_by, sigma=args.sigma)
29 field_new['by'] = temp_by.reshape(-1)
30 field_new['by'] = field_new['by'] * field_mask['by']
31 rezult = field_new
32 else:
33 field_new[['bx', 'by', 'bz']] = 0
34 index_range = np.random.choice(field_new.index, size=args.nCores)
35 field_new.loc[index_range, 'by'] = random.uniform(-args.peak, args.peak)
36 temp_by = np.array(field_new['by']).reshape([xSpace, ySpace, zSpace])
37 temp_by = gaussian_filter(temp_by, sigma=args.sigma)
38 field_new['by'] = temp_by.reshape(-1)
39 field_new['by'] = field_new['by'] / (field_new['by'].abs().max()) # *field_mask['by']
40 field_new['by'] = field_new['by'] * field_mask['by']
41 rezult = field.copy()
42 rezult['by'] = rezult['by'] + rezult['by'] * field_new['by'] * args.fraction
43
44 # plot_my_hist(field_mask)
45 # plot_my_hist(field)
46 # plot_my_hist(field_new)
47 # plot_my_hist(rezult)
48 rezult.to_csv(output, sep='\t', header=None, index=None)
49
50
51if __name__ == "__main__":
52 parser = argparse.ArgumentParser(description='Process field.')
53 parser.add_argument('--input', default=os.path.expandvars("$FAIRSHIP/files/fieldMap.csv"), type=str, action='store')
54 parser.add_argument('--output', default=os.path.expandvars("$FAIRSHIP/files/noisy_fieldMap.csv"), type=str, action='store')
55 parser.add_argument('--sidesOnly', default=False, action='store_true')
56 parser.add_argument('--sigma', default=30, type=float, action='store')
57 parser.add_argument('--nCores', default=1000, type=int, action='store')
58 parser.add_argument('--peak', default=500, type=float, action='store')
59 parser.add_argument('--fraction', default=0.4, type=float, action='store')
60 args = parser.parse_args()
61
62 with open(args.input) as f:
63 first_line = f.readline().strip().split()
64
65 generate_file(args.input, args.output,
66 xSpace=int(first_line[0]),
67 ySpace=int(first_line[1]),
68 zSpace=int(first_line[2]),
69 step=float(first_line[3]), args=args)
generate_file(input_fileName, output, xSpace=73, ySpace=128, zSpace=1214, step=2.5, args=None)