galsci / pysm

PySM 3: Sky emission simulations for Cosmic Microwave Background experiments
https://pysm3.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
36 stars 23 forks source link

Error when I try to include dust model in a simulation #62

Closed chervias closed 4 years ago

chervias commented 4 years ago

I updated to the latest vesion 3.2.2, but I cannot run a model that includes dust. For example, if I run this example

import pysm3 import pysm3.units as u import healpy as hp import numpy as np import warnings warnings.filterwarnings("ignore") sky = pysm3.Sky(nside=128, preset_strings=["d1", "s1"]) map_100GHz = sky.get_emission(100 * u.GHz)

I get this error

Traceback (most recent call last):
  File "Test.py", line 11, in <module>
    map_100GHz = sky.get_emission(100 * u.GHz)
  File "/home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/sky.py", line 151, in get_emission
    output = self.components[0].get_emission(freq, weights=weights)
  File "/home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/astropy/units/decorators.py", line 234, in wrapper
    return_ = wrapped_function(*func_args, **func_kwargs)
  File "/home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/models/dust.py", line 99, in get_emission
    outputs = get_emission_numba(
  File "/home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/numba/core/dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/numba/core/dispatcher.py", line 358, in error_rewrite
    reraise(type(e), e, None)
  File "/home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/numba/core/utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython mode backend)
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:

 >>> getitem(array(float64, 1d, C), UniTuple(uint64 x 2))

There are 22 candidate implementations:
  - Of which 20 did not match due to:
  Overload of function 'getitem': File: <numerous>: Line N/A.
    With argument(s): '(array(float64, 1d, C), UniTuple(uint64 x 2))':
   No match.
  - Of which 2 did not match due to:
  Overload in function 'GetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 162.
    With argument(s): '(array(float64, 1d, C), UniTuple(uint64 x 2))':
   Rejected as the implementation raised a specific error:
     TypeError: cannot index array(float64, 1d, C) with 2 indices: UniTuple(uint64 x 2)
  raised from /home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/numba/core/typing/arraydecl.py:84

During: typing of intrinsic-call at /home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/models/dust.py (137)

File "../../../Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/models/dust.py", line 137:
def get_emission_numba(
    <source elided>
        temp[I] *= (freq / freq_ref_I) ** (mbb_index - 2.0)
        temp[Q:] *= (freq / freq_ref_P) ** (mbb_index - 2.0)
        ^

During: lowering "id=8[LoopNest(index_variable = parfor_index.121, range = (0, $190binary_subscr.52_size0.50, 1)), LoopNest(index_variable = parfor_index.122, range = (0, mbb_index_size0.19, 1))]{306: <ir.Block at /home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/models/dust.py (137)>}Var($parfor_index_tuple_var.123, dust.py:137)" at /home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/models/dust.py (137)
zonca commented 4 years ago

can you update astropy and numba and check if you get still the same error?

zonca commented 4 years ago

also, please run inside IPython or Jupyter Notebook and run the debugger with %debug, then print all the inputs to get_emission_numba (use p variablename)

chervias commented 4 years ago

I updated numba (version 0.51.2) and astropy (version 4.0.1.post1), and I still get the same error. I run in a Jupyter notebook, but I don't really know how to use the debug. I run %debug after the error, and it let me print the values of the variables value, tp and tb, which are the variables in the last function at the end of the error chain, but I cannot print the other variables up the error chain.

zonca commented 4 years ago

Use u and d to navigate down and up the stack. Or Google for using Python debugger pdb

chervias commented 4 years ago

ok thanks, that makes sense. These are all the inputs to get_emission_numba. They seem to be fine.

ipdb>  u

> /home/chervias/Software/anaconda3/envs/healpy/lib/python3.8/site-packages/pysm3/models/dust.py(99)get_emission()
     97         freqs = utils.check_freq_input(freqs)
     98         weights = utils.normalize_weights(freqs, weights)
---> 99         outputs = get_emission_numba(
    100             freqs,
    101             weights,

ipdb>  p freqs

array([100.])

ipdb>  p weights

array([1.])

ipdb>  p self.I_ref.value

array([10.423211, 11.16924 , 12.853205, ..., 10.598779, 11.974686,
       12.612513], dtype=float32)

ipdb>  p self.Q_ref.value

array([ 0.04630192, -0.08084465,  0.25656074, ..., -0.0712268 ,
       -0.20725448, -0.09635498], dtype=float32)

ipdb>  p self.U_ref.value

array([-0.45459473,  0.19183296, -0.338591  , ..., -0.0786793 ,
       -0.17652838,  0.3338661 ], dtype=float32)

ipdb>  p self.freq_ref_I.value

545.0

ipdb>  p self.freq_ref_P.value

353.0

ipdb>  p self.mbb_index.value

array([1.5440001, 1.5506256, 1.5408113, ..., 1.5711942, 1.567093 ,
       1.5721948], dtype=float32)

ipdb>  self.mbb_temperature.value

array([22.504211, 23.154001, 23.610268, ..., 24.235737, 24.127972,
       24.384308], dtype=float32)
zonca commented 4 years ago

ok, I can reproduce the issue, I'll work on a fix

chervias commented 4 years ago

ok thanks!

zonca commented 4 years ago

ok, I believe it is a bug in numba, which I reported at https://github.com/numba/numba/issues/6228.

So you either downgrade numba to 0.49.1 or install pysm from the master branch as soon as I commit the fix.