feat: add CLI options for custom CSV separator and normalize-to-one branch normalization
This commit is contained in:
@@ -731,7 +731,10 @@ class BetaSpectrumAnalyzer:
|
||||
header_lines.append(f"# n_points: {len(self.energies_MeV)}")
|
||||
|
||||
# Add normalization mode
|
||||
header_lines.append("# branch normalization: intensity")
|
||||
if self.config.normalize_to_one:
|
||||
header_lines.append("# branch normalization: to_one (ignoring intensity)")
|
||||
else:
|
||||
header_lines.append("# branch normalization: intensity")
|
||||
|
||||
# Separate branches into active and excluded lists
|
||||
active_branches = []
|
||||
@@ -771,13 +774,31 @@ class BetaSpectrumAnalyzer:
|
||||
# Create data dictionary with concise column headers
|
||||
data: Dict[str, np.ndarray] = {
|
||||
"energy_MeV": self.energies_MeV,
|
||||
"total_spectrum": total,
|
||||
}
|
||||
|
||||
# Only add total_spectrum if not normalizing to one
|
||||
if not self.config.normalize_to_one:
|
||||
data["total_spectrum"] = total
|
||||
|
||||
if csv_verbosity == "full":
|
||||
# Per-branch spectra and components with concise branch naming
|
||||
for i in range(self._n_branches):
|
||||
data[f"branch_{i + 1}_spectrum"] = self._get_branch_spectrum(i)
|
||||
# Get branch spectrum
|
||||
branch_spectrum = self._get_branch_spectrum(i)
|
||||
|
||||
if self.config.normalize_to_one:
|
||||
# Normalize to 1, ignoring intensity
|
||||
sum_spectrum = np.sum(branch_spectrum)
|
||||
if sum_spectrum > 0:
|
||||
normalized_spectrum = branch_spectrum / sum_spectrum
|
||||
else:
|
||||
normalized_spectrum = branch_spectrum
|
||||
else:
|
||||
# Use original spectrum with intensity
|
||||
normalized_spectrum = branch_spectrum * self._unified_branches[i].intensity
|
||||
|
||||
data[f"branch_{i + 1}_spectrum"] = normalized_spectrum
|
||||
|
||||
for comp_name in ["PhaseSpace", "Radiative"]:
|
||||
key = self._get_component_key(i, comp_name)
|
||||
if key in components:
|
||||
@@ -799,14 +820,25 @@ class BetaSpectrumAnalyzer:
|
||||
data[f"branch_{i + 1}"] = np.zeros_like(branch_spectrum)
|
||||
else:
|
||||
# Branch is valid, normalize as usual
|
||||
sum_spectrum = np.sum(branch_spectrum)
|
||||
if sum_spectrum > 0:
|
||||
data[f"branch_{i + 1}"] = branch_spectrum / sum_spectrum
|
||||
if self.config.normalize_to_one:
|
||||
# Normalize to 1, ignoring intensity
|
||||
sum_spectrum = np.sum(branch_spectrum)
|
||||
if sum_spectrum > 0:
|
||||
data[f"branch_{i + 1}"] = branch_spectrum / sum_spectrum
|
||||
else:
|
||||
data[f"branch_{i + 1}"] = branch_spectrum
|
||||
else:
|
||||
data[f"branch_{i + 1}"] = branch_spectrum
|
||||
# Normalize as usual with intensity
|
||||
sum_spectrum = np.sum(branch_spectrum)
|
||||
if sum_spectrum > 0:
|
||||
data[f"branch_{i + 1}"] = branch_spectrum / sum_spectrum * self._unified_branches[i].intensity
|
||||
else:
|
||||
data[f"branch_{i + 1}"] = branch_spectrum
|
||||
|
||||
df = pd.DataFrame(data)
|
||||
df.to_csv(filename, index=False, float_format="%.4e", sep='\t')
|
||||
# Determine separator
|
||||
separator = '\t' if self.config.csv_separator == 'tab' else self.config.csv_separator
|
||||
df.to_csv(filename, index=False, float_format="%.4e", sep=separator)
|
||||
|
||||
with open(filename, "r") as f:
|
||||
content = f.read()
|
||||
|
||||
Reference in New Issue
Block a user