litebird / litebird_sim

Simulation tools for LiteBIRD
GNU General Public License v3.0
18 stars 13 forks source link

TypingError in destriper.py: cur_obs.destriper_weights is a float instead of array #270

Closed nraffuzz closed 9 months ago

nraffuzz commented 11 months ago

While creating an Obsevation object (and filling it) I tried to apply the built-in destriper, this is the used code:

# fill the TOD
lbs.scan_map_in_observations(obs=obs,maps=input_map,pointings=pointings,input_map_in_galactic=True,component="tod")
# params for 1/f noise: fmin, net, fknee
obs.fmin_hz, obs.net_ukrts, obs.fknee_mhz = 1e-5, 2.2, 30
lbs.noise.add_noise_to_observations(obs=obs,noise_type='one_over_f',random=sim.random,scale=1,component="tod")                        

result = lbs.make_destriped_map(
    nside=nside, 
    obs=obs, 
    pointings=pointings, 
    params=lbs.DestriperParameters(), 
    components=['tod'],
    )

but I get the following error:

TypingError                               Traceback (most recent call last)
/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb Cella 7 line 1
----> [1](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0) result = lbs.make_destriped_map(
      [2](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1)     nside=nside, 
      [3](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2)     obs=obs, 
      [4](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3)     pointings=pointings, 
      [5](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4)     params=lbs.DestriperParameters(), 
      [6](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5)     components=['tod'],
      [7](vscode-notebook-cell://wsl%2Bubuntu/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/madam_vs_lbsdestriper.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6)     )

File [~/anaconda3/envs/lbs_env/lib/python3.9/site-packages/litebird_sim/mapmaking/destriper.py:1524](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/nraffuzzi/mount_marconi/litebird/e2e-simulation/scripts/~/anaconda3/envs/lbs_env/lib/python3.9/site-packages/litebird_sim/mapmaking/destriper.py:1524), in make_destriped_map(nside, obs, pointings, params, components, keep_weights, keep_pixel_idx, keep_pol_angle_rad, callback, callback_kwargs)
   1511 baselines_list = [
   1512     np.zeros((getattr(cur_obs, components[0]).shape[0], len(cur_baseline)))
   1513     for (cur_obs, cur_baseline) in zip(obs_list, baseline_lengths_list)
   1514 ]
   1516 destriped_map = np.empty((3, number_of_pixels))
   1517 (
   1518     baselines_list,
   1519     baseline_errors_list,
   1520     history_of_stopping_factors,
   1521     best_stopping_factor,
   1522     converged,
   1523     bytes_in_temporary_buffers,
-> 1524 ) = _run_destriper(
...
    <source elided>
    for det_idx in range(pixel_idx.shape[0]):
        cur_weight = weights[det_idx]

The TypingError is due to line 375 of destriper.py, where weights[det_idx] is called but weights is a float64 and not an array, and it's inside the _accumulate_nobs_matrix function. I noticed that _accumulate_nobs_matrix function should accept weights: npt.ArrayLike, but the passed object is a float number, at line 460 of destriper.py, where weights=cur_obs.destriper_weights. If I try to print type(obs.destriper_weights) I get float.

ziotom78 commented 11 months ago

Mmm, weights should always be an array; if it's not, it's a bug. That field is initialized by get_map_making_weights, which might fail if obs.net_ukrts is not an array but a scalar.

May you please post here how you create the observations in the code it's failing? We should add an additional check that prevents floats from being passed, but first let's check where the problem originates.

nraffuzz commented 11 months ago

I specifically stated obs.net_ukrts = 2.2, so as a scalar. Indeed, if passed as an array the issue is solved, thanks! Could it be useful to add something like?

if type(obs.net_ukrts) == float or int:
    obs.net_ukrts = obs.net_ukrts * np.ones(obs.n_detectors)
ziotom78 commented 11 months ago

Yes, that would be good, but it's better to write the test in the following way:

if isinstance(obs.net_ukrts, (float, int)):
    …

May you open a PR and add this line?

ziotom78 commented 9 months ago

I believe this can be closed, as PR #272 fixed the issue. Feel free to reopen if you think there is still something that doesn't work.