Skip to content

Commit

Permalink
Bug fix (number 2 of #310)
Browse files Browse the repository at this point in the history
Fix bug happening when no destriping is perfomed, but report is requested
  • Loading branch information
ggalloni committed Mar 27, 2024
1 parent 16f6309 commit 0432b2e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 50 deletions.
87 changes: 37 additions & 50 deletions litebird_sim/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,9 @@ def make_destriped_map_splits(
)

if append_to_report:
self._build_and_append_destriped_split_report(ts, ds, result)
self._build_and_append_destriped_report(
"report_destriper_splits.md", ts, ds, result
)

dest_file = f"DET{ds}_TIME{ts}_destriper_results.fits"
base_file = (
Expand Down Expand Up @@ -1632,51 +1634,15 @@ def make_destriped_map_splits(
)

if append_to_report:
self._build_and_append_destriped_split_report(
ts, ds, destriped_maps[f"{ds}_{ts}"]
self._build_and_append_destriped_report(
"report_destriper_splits.md",
ts,
ds,
destriped_maps[f"{ds}_{ts}"],
)

return destriped_maps

def _build_and_append_destriped_split_report(
self,
detector_split: str,
time_split: str,
results: DestriperResult,
):
fig, ax = plt.subplots()
ax.set_xlabel("Iteration number")
ax.set_ylabel("Residual [K]")
ax.set_title("CG convergence of the destriper")
ax.semilogy(
np.arange(len(results.history_of_stopping_factors)),
results.history_of_stopping_factors,
"ko-",
)

template_file_path = get_template_file_path("report_destriper_splits.md")
with template_file_path.open("rt") as inpf:
markdown_template = "".join(inpf.readlines())

cg_plot_filename = f"destriper-DET{detector_split}-TIME{time_split}-cg-convergence-{uuid4()}.png"

self.append_to_report(
detector_split=detector_split,
time_split=time_split,
markdown_text=markdown_template,
results=results,
history_of_stopping_factors=[
float(x) for x in results.history_of_stopping_factors
],
bytes_in_cholesky_matrices=results.nobs_matrix_cholesky.nbytes,
cg_plot_filename=cg_plot_filename,
figures=[
# Using uuid4() we can have more than one section
# about “destriping” in the report
(fig, cg_plot_filename),
],
)

def make_destriped_map(
self,
nside: int,
Expand Down Expand Up @@ -1719,6 +1685,24 @@ def make_destriped_map(
)

if append_to_report:
self._build_and_append_destriped_report(
"report_destriper.md", detector_split, time_split, results
)

return results

def _build_and_append_destriped_report(
self,
template_file: str,
detector_split: str,
time_split: str,
results: DestriperResult,
):
template_file_path = get_template_file_path(template_file)
with template_file_path.open("rt") as inpf:
markdown_template = "".join(inpf.readlines())

if results.params.samples_per_baseline is not None:
fig, ax = plt.subplots()
ax.set_xlabel("Iteration number")
ax.set_ylabel("Residual [K]")
Expand All @@ -1728,14 +1712,11 @@ def make_destriped_map(
results.history_of_stopping_factors,
"ko-",
)

template_file_path = get_template_file_path("report_destriper.md")
with template_file_path.open("rt") as inpf:
markdown_template = "".join(inpf.readlines())

cg_plot_filename = f"destriper-cg-convergence-{uuid4()}.png"
cg_plot_filename = f"destriper-DET{detector_split}-TIME{time_split}-cg-convergence-{uuid4()}.png"

self.append_to_report(
detector_split=detector_split,
time_split=time_split,
markdown_text=markdown_template,
results=results,
history_of_stopping_factors=[
Expand All @@ -1749,8 +1730,14 @@ def make_destriped_map(
(fig, cg_plot_filename),
],
)

return results
else:
self.append_to_report(
detector_split=detector_split,
time_split=time_split,
markdown_text=markdown_template,
results=results,
bytes_in_cholesky_matrices=results.nobs_matrix_cholesky.nbytes,
)

def write_observations(
self,
Expand Down
4 changes: 4 additions & 0 deletions templates/report_destriper.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ The destriper was executed with the following parameters:

- `NSIDE = {{ results.nside }}`
- Coordinate system for the output map: `{{ results.params.output_coordinate_system }}`
- Detector split: `{{ detector_split }}`
- Time split: `{{ time_split }}`
- Samples per baseline: `{{ results.params.samples_per_baseline }}`
- Maximum number of iterations: `{{ results.params.iter_max }}`
- Threshold to stop the CG iteration: `{{ results.params.threshold }}`
- CG preconditioner: {% if results.params.use_preconditioner %}**on**{% else %}off{% endif %}
{% if not results.baselines %}- No destriping (only binning){% endif %}

{% if results.baselines %}
### Convergence

- The convergence was {% if results.converged %}achieved{% else %}**not** achieved{% endif %}
Expand All @@ -28,3 +31,4 @@ The destriper was executed with the following parameters:
|----------------------------------------------------|------------------|
{% for cur_value in history_of_stopping_factors %}| {{loop.index}} | {{ "%.2e" | format(cur_value) }} |
{% endfor %}
{% endif %}
2 changes: 2 additions & 0 deletions templates/report_destriper_splits.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The destriper was executed with the following parameters:
- CG preconditioner: {% if results.params.use_preconditioner %}**on**{% else %}off{% endif %}
{% if not results.baselines %}- No destriping (only binning){% endif %}

{% if results.baselines %}
### Convergence

- The convergence was {% if results.converged %}achieved{% else %}**not** achieved{% endif %}
Expand All @@ -28,3 +29,4 @@ The destriper was executed with the following parameters:
|----------------------------------------------------|------------------|
{% for cur_value in history_of_stopping_factors %}| {{loop.index}} | {{ "%.2e" | format(cur_value) }} |
{% endfor %}
{% endif %}

0 comments on commit 0432b2e

Please sign in to comment.