Skip to content

main

rionid.__main__

build_parser()

Build the parser used by the python -m rionid interface.

Source code in src/rionid/__main__.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def build_parser():
    """Build the parser used by the ``python -m rionid`` interface."""
    parser = argparse.ArgumentParser(description="RionID: Ring-stored ion Identification")
    modes = parser.add_mutually_exclusive_group(required=True)

    # Main Arguments
    parser.add_argument("datafile", type=str, nargs="+", help="Name of the input file with data.")
    parser.add_argument(
        "-ap",
        "--alphap",
        type=float,
        required=True,
        help="Momentum compaction factor of the ring.",
    )
    parser.add_argument(
        "-r",
        "--refion",
        type=str,
        required=True,
        help="Reference ion (format: AAXXCC+, e.g., 72Ge35+).",
    )
    parser.add_argument(
        "-psim",
        "--filep",
        type=str,
        required=True,
        help="Path to particle list file (LISE++ output).",
    )
    parser.add_argument(
        "-hrm", "--harmonics", type=float, default=[1.0], nargs="+", help="Harmonics to simulate."
    )

    # Secondary Arguments
    parser.add_argument(
        "-n", "--nions", type=int, help="Number of ions to display, sorted by yield."
    )

    # Arguments for Each Mode (Exclusive)
    modes.add_argument("-b", "--brho", type=float, help="Brho value [Tm] (Isochronous mode).")
    modes.add_argument(
        "-ke", "--kenergy", type=float, help="Kinetic energy [MeV/u] (Isochronous mode)."
    )
    modes.add_argument("-gam", "--gamma", type=float, help="Lorentz factor gamma.")
    modes.add_argument(
        "-f", "--fref", type=float, help="Revolution frequency [Hz] (Standard mode)."
    )

    # Visualization & Output
    parser.add_argument(
        "-d", "--ndivs", type=int, default=4, help="Number of divisions (Deprecated)."
    )
    parser.add_argument(
        "-am", "--amplitude", type=int, default=0, help="Display options (0=const, else=scaled)."
    )
    parser.add_argument(
        "-l",
        "--log",
        dest="logLevel",
        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
        default="INFO",
        help="Set logging level.",
    )
    parser.add_argument("-s", "--show", help="Show display.", action="store_true")
    parser.add_argument("-w", "--ods", help="Write output to ODS file.", action="store_true")
    parser.add_argument(
        "-o", "--outdir", type=str, nargs="?", default=os.getcwd(), help="Output directory."
    )
    parser.add_argument(
        "-c",
        "--correct",
        nargs="*",
        type=float,
        help="Polynomial correction parameters (a0, a1, a2).",
    )

    parser.add_argument(
        "--circumference",
        type=float,
        default=ESR_CIRCUMFERENCE_M,
        metavar="METRES",
        help=f"Ring circumference in metres (default: ESR, {ESR_CIRCUMFERENCE_M}).",
    )
    return parser

display_nions(nions, yield_data, nuclei_names, simulated_data_dict, ref_ion, harmonics)

Filters the top N ions by yield.

Source code in src/rionid/__main__.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def display_nions(nions, yield_data, nuclei_names, simulated_data_dict, ref_ion, harmonics):
    """Filters the top N ions by yield."""
    sorted_indices = argsort(yield_data)[::-1][:nions]
    ref_index = where(nuclei_names == ref_ion)[0]

    # Ensure reference ion is always included
    if len(ref_index) > 0 and ref_index[0] not in sorted_indices:
        sorted_indices = append(sorted_indices, ref_index)

    nuclei_names = nuclei_names[sorted_indices]

    for harmonic in harmonics:
        name = f"{harmonic}"
        if name in simulated_data_dict:
            simulated_data_dict[name] = simulated_data_dict[name][sorted_indices]

main()

Main entry point for the command-line interface (CLI).

Parses arguments, initializes logging, and dispatches the analysis controller for one or more data files.

Source code in src/rionid/__main__.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def main():
    """
    Main entry point for the command-line interface (CLI).

    Parses arguments, initializes logging, and dispatches the analysis controller
    for one or more data files.
    """
    scriptname = "RionID"
    parser = build_parser()

    args = parser.parse_args()

    # Argument Validation
    if args.circumference <= 0:
        parser.error("--circumference must be greater than zero.")

    # Logging Setup
    if args.logLevel:
        log.basicConfig(level=log.getLevelName(args.logLevel))

    # Handle alphap conversion
    if args.alphap and args.alphap > 1:
        args.alphap = 1 / args.alphap**2

    print(f"Running {scriptname}...")
    log.info(f"Processing {args.datafile} for reference {args.refion}.")

    # Handle file lists vs single files
    files_to_process = []
    if "txt" in args.datafile[0] and len(args.datafile) == 1:
        files_to_process = read_masterfile(args.datafile[0])
    else:
        files_to_process = args.datafile

    # Initialize Qt Application ONCE (Crucial for loops)
    app = None
    if args.show:
        app = QApplication.instance()
        if not app:
            app = QApplication(sys.argv)

    # Run Controller for each file
    for file in files_to_process:
        run_controller(
            data_file=file,
            particles_to_simulate=args.filep,
            alphap=args.alphap,
            ref_ion=args.refion,
            harmonics=args.harmonics,
            circumference=args.circumference,
            brho=args.brho,
            fref=args.fref,
            ke=args.kenergy,
            gam=args.gamma,
            correct=args.correct,
            ods=args.ods,
            nions=args.nions,
            show=args.show,
            app=app,
        )

    if args.show and app:
        sys.exit(app.exec_())

read_masterfile(master_filename)

Reads list of filenames from a text file.

Source code in src/rionid/__main__.py
284
285
286
def read_masterfile(master_filename):
    """Reads list of filenames from a text file."""
    return [file.strip() for file in open(master_filename).readlines() if file.strip()]

run_controller(data_file, particles_to_simulate, alphap, ref_ion, harmonics, circumference=ESR_CIRCUMFERENCE_M, brho=None, fref=None, ke=None, gam=None, correct=None, ods=False, nions=None, show=True, app=None)

Unified controller: Calculates physics and launches the PyQt GUI.

Parameters:

Name Type Description Default
data_file str

Path to experimental data.

required
particles_to_simulate str

Path to LISE++ file.

required
alphap float

Momentum compaction factor.

required
ref_ion str

Reference ion string.

required
harmonics list

List of harmonics to simulate.

required
circumference float

Ring circumference in metres. Defaults to the ESR value of 108.36 m.

ESR_CIRCUMFERENCE_M
brho float

Magnetic rigidity.

None
fref float

Reference frequency.

None
ke float

Kinetic energy.

None
gam float

Gamma factor.

None
correct list

Polynomial correction coefficients.

None
ods bool

Whether to export to ODS.

False
nions int

Number of top ions to display.

None
show bool

Whether to show the GUI.

True
app QApplication

The Qt application instance.

None
Source code in src/rionid/__main__.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
def run_controller(
    data_file,
    particles_to_simulate,
    alphap,
    ref_ion,
    harmonics,
    circumference=ESR_CIRCUMFERENCE_M,
    brho=None,
    fref=None,
    ke=None,
    gam=None,
    correct=None,
    ods=False,
    nions=None,
    show=True,
    app=None,
):
    """
    Unified controller: Calculates physics and launches the PyQt GUI.

    Parameters
    ----------
    data_file : str
        Path to experimental data.
    particles_to_simulate : str
        Path to LISE++ file.
    alphap : float
        Momentum compaction factor.
    ref_ion : str
        Reference ion string.
    harmonics : list
        List of harmonics to simulate.
    circumference : float, optional
        Ring circumference in metres. Defaults to the ESR value of 108.36 m.
    brho : float, optional
        Magnetic rigidity.
    fref : float, optional
        Reference frequency.
    ke : float, optional
        Kinetic energy.
    gam : float, optional
        Gamma factor.
    correct : list, optional
        Polynomial correction coefficients.
    ods : bool, optional
        Whether to export to ODS.
    nions : int, optional
        Number of top ions to display.
    show : bool, optional
        Whether to show the GUI.
    app : QApplication, optional
        The Qt application instance.
    """
    # 1. Calculations (Model)
    mydata = ImportData(ref_ion, alphap, filename=data_file, circumference=circumference)
    log.debug(f"Experimental data shape: {shape(mydata.experimental_data)}")

    mydata._set_particles_to_simulate_from_file(particles_to_simulate)
    mydata._calculate_moqs()

    # Calculate Reference Frequency
    mydata._calculate_srrf(fref=fref, brho=brho, ke=ke, gam=gam, correct=correct)
    log.debug(f"Reference Frequency: {mydata.ref_frequency}")

    # Simulate Data
    if not isinstance(harmonics, list):
        harmonics = [harmonics]

    mydata._simulated_data(harmonics=harmonics, brho=brho, mode="frequency" if fref else "brho")

    # 2. Filter Ions (Optional)
    if nions:
        display_nions(
            nions,
            mydata.yield_data,
            mydata.nuclei_names,
            mydata.simulated_data_dict,
            ref_ion,
            harmonics,
        )

    # 3. Logging & ODS Export
    sort_index = argsort(mydata.srrf)
    if ods:
        write_arrays_to_ods(
            "Data_simulated_RionID",
            "Data",
            ["Name", "freq", "yield"],
            (mydata.nuclei_names)[sort_index],
            (mydata.srrf)[sort_index] * mydata.ref_frequency,
            (mydata.yield_data)[sort_index],
        )

    # 4. Visualization (View)
    if show and app:
        sa = CreatePyGUI(mydata.experimental_data, mydata.simulated_data_dict)
        sa.show()

    return mydata