feat(grid-fit): implement resume logic

This commit is contained in:
2026-01-29 12:01:01 +03:00
parent c0bd75ff6c
commit 7dad98469c

View File

@@ -159,9 +159,46 @@ private class GridArgs : CliktCommand() {
val chi2Matrix = mutableMapOf<Pair<Double, Double>, Double?>()
val tsvPath = Path(
dataPath.parent.toString(),
dataPath.nameWithoutExtension + postfix + ".chi2_grid.tsv"
)
val tsvFile = File(tsvPath.toString())
if (tsvFile.exists()) {
println("Found existing chi2 grid: ${tsvFile.name}")
val lines = tsvFile.readLines()
if (lines.size > 1 && lines[0].startsWith("m\\u2")) {
val header = lines[0].split("\t")
val fileU2 = header.drop(1).mapNotNull { it.toDoubleOrNull() }
lines.drop(1).forEach { line ->
val parts = line.split("\t")
if (parts.size >= 2) {
val mStr = parts[0]
val m = mStr.toDoubleOrNull() ?: return@forEach
parts.drop(1).forEachIndexed { idx, value ->
if (idx < fileU2.size) {
val u2 = fileU2[idx]
val chi2 = when {
value.isBlank() || value == "NaN" -> null
else -> value.toDoubleOrNull()
}
chi2Matrix[Pair(m, u2)] = chi2
}
}
}
}
println("Loaded ${chi2Matrix.size} points from existing file")
}
}
mGrid.forEach { m ->
u2Grid.forEach { u2 ->
chi2Matrix[Pair(m, u2)] = null
val key = Pair(m, u2)
if (key !in chi2Matrix) {
chi2Matrix[key] = null
}
}
}
@@ -172,9 +209,17 @@ private class GridArgs : CliktCommand() {
for (m in mGrid) {
for (u2Val in u2Grid) {
current++
val pair = Pair(m, u2Val)
val taskId = "${m.toInt()}_${String.format("%.0e", u2Val)}"
if (chi2Matrix[pair] != null) {
current++
println("$current/$total [$taskId] Already computed (chi² = ${chi2Matrix[pair]}), skipping")
continue
}
current++
val fitParams = fitParamsBase.toMutableMap()
fitParams[NumassBeta.msterile2] = (m * 1000.0).pow(2)
fitParams[NumassBeta.u2] = u2Val
@@ -190,7 +235,7 @@ private class GridArgs : CliktCommand() {
spectrumModel, data, fitVars, fitParams, dumpLogger
)
chi2Matrix[Pair(m, u2Val)] = fit.chiSquaredOrNull
chi2Matrix[pair] = fit.chiSquaredOrNull
if (!noReports) {
val mStr = String.format("%04.1f", m)
@@ -211,9 +256,12 @@ private class GridArgs : CliktCommand() {
logMessages
).makeFile(path = reportPath, show = false)
}
saveChi2GridToTsv(spectrum, postfix, mGrid, u2Grid, chi2Matrix)
}
}
println("Grid completed. Final chi² table saved to ${tsvPath.fileName}")
}
}
}