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
6
7import matplotlib.pylab as plt
8import numpy as np
9import pandas as pd
10from scipy.ndimage import gaussian_filter
11
12
13def plot_my_hist(datum) -> None:
14 plotData = datum[datum["y"] == 0]
15 H, _xedges, _yedges = np.histogram2d(plotData["x"], plotData["z"], bins=[50, 500], weights=plotData["by"])
16 plt.figure(figsize=[20, 10])
17 plt.imshow(H, interpolation="nearest", origin="lower")
18 # plt.colorbar()
19 plt.show()
20
21
23 input_fileName, output, xSpace=73, ySpace=128, zSpace=1214, step=2.5, *, args: argparse.Namespace
24) -> None:
25 # (min, max, max/stepSize + 1) in case of Z: (0, nSteps*2.5 - 2.5, nSteps)
26 field = pd.read_csv(input_fileName, skiprows=1, sep=r"\s+", names=["x", "y", "z", "bx", "by", "bz"])
27
28 field_mask = field.copy()
29 field_mask[["bx", "by", "bz"]] = field_mask[["bx", "by", "bz"]] != 0
30
31 field_new = field.copy()
32
33 if args.sidesOnly:
34 temp_by = np.array(field_new["by"]).reshape([xSpace, ySpace, zSpace])
35 temp_by = gaussian_filter(temp_by, sigma=args.sigma)
36 field_new["by"] = temp_by.reshape(-1)
37 field_new["by"] = field_new["by"] * field_mask["by"]
38 rezult = field_new
39 else:
40 field_new[["bx", "by", "bz"]] = 0
41 index_range = np.random.choice(field_new.index, size=args.nCores)
42 # Draw an independent value per noise core so --peak sets the amplitude;
43 # a single scalar (assigned to every core) was cancelled by the
44 # self-normalisation below, leaving --peak with no effect but its sign.
45 field_new.loc[index_range, "by"] = np.random.uniform(-args.peak, args.peak, size=args.nCores)
46 temp_by = np.array(field_new["by"]).reshape([xSpace, ySpace, zSpace])
47 temp_by = gaussian_filter(temp_by, sigma=args.sigma)
48 field_new["by"] = temp_by.reshape(-1)
49 field_new["by"] = field_new["by"] * field_mask["by"]
50 rezult = field.copy()
51 rezult["by"] = rezult["by"] + rezult["by"] * field_new["by"] * args.fraction
52
53 # plot_my_hist(field_mask)
54 # plot_my_hist(field)
55 # plot_my_hist(field_new)
56 # plot_my_hist(rezult)
57 rezult.to_csv(output, sep="\t", header=False, index=False)
58
59
60if __name__ == "__main__":
61 parser = argparse.ArgumentParser(description="Process field.")
62 parser.add_argument("--input", default=os.path.expandvars("$FAIRSHIP/files/fieldMap.csv"), type=str, action="store")
63 parser.add_argument(
64 "--output", default=os.path.expandvars("$FAIRSHIP/files/noisy_fieldMap.csv"), type=str, action="store"
65 )
66 parser.add_argument("--sidesOnly", default=False, action="store_true")
67 parser.add_argument("--sigma", default=30, type=float, action="store")
68 parser.add_argument("--nCores", default=1000, type=int, action="store")
69 parser.add_argument("--peak", default=500, type=float, action="store")
70 parser.add_argument("--fraction", default=0.4, type=float, action="store")
71 args = parser.parse_args()
72
73 with open(args.input) as f:
74 first_line = f.readline().strip().split()
75
77 args.input,
78 args.output,
79 xSpace=int(first_line[0]),
80 ySpace=int(first_line[1]),
81 zSpace=int(first_line[2]),
82 step=float(first_line[3]),
83 args=args,
84 )
None generate_file(input_fileName, output, xSpace=73, ySpace=128, zSpace=1214, step=2.5, *, argparse.Namespace args)