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