icecube/ASTERIA

Error in supernova_mixing

Opened this issue · 2 comments

In cells In[5], In[6], In[7], and In[11] of the notebook supernova-mixing, a unit conversion error appears. Running at git hash ec1024d.

The code of In[5] is:

fig1, ax = plt.subplots(1,1, figsize=(7,5))
t = np.linspace(-0.1, 1, 201) * u.s

nu_list = []
i = 0
for flavor in Flavor:
    flux = ccsn.get_flux(t, flavor)
    nu_list.append(flux)

nu_e = [a + b for a, b in zip(nu_list[2], nu_list[3])]  
nu_x = [(a + b + c)/2 for a, b, c in zip(nu_list[0], nu_list[2], nu_list[3])]
nu_e_bar = [a*c2t12 + (b + c)*s2t12 for a, b, c in zip(nu_list[1], nu_list[2], nu_list[3])]
nu_x_bar = [((1.0-c2t12)*a + (1.0+c2t12)*(b + c))/2 for a, b, c in zip(nu_list[1], nu_list[2], nu_list[3])]
nu_new = [nu_e, nu_e_bar, nu_x, nu_x_bar]

for ls, i, flavor in zip(["-", "--", "-.", ":"], range(len(nu_new)), Flavor):
    new_flux = nu_new[i]
    ax.plot(t, new_flux, ls, lw=2, alpha=0.7, label=flavor.to_tex()), 

# plt.yscale('log')
# plt.ylim(3e51, 5e53)

ax.set(xlabel='time - $t_{bounce}$ [s]',
       ylabel='flux')
ax.legend()
fig1.tight_layout()

The error is:

---------------------------------------------------------------------------
UnitConversionError                       Traceback (most recent call last)
/opt/anaconda3/lib/python3.8/site-packages/astropy/units/quantity.py in to_value(self, unit, equivalencies)
    739             try:
--> 740                 scale = self.unit._to(unit)
    741             except Exception:

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/core.py in _to(self, other)
    949 
--> 950         raise UnitConversionError(
    951             f"'{self!r}' is not a scaled version of '{other!r}'")

UnitConversionError: 'Unit("1 / s")' is not a scaled version of 'Unit(dimensionless)'

During handling of the above exception, another exception occurred:

UnitConversionError                       Traceback (most recent call last)
/opt/anaconda3/lib/python3.8/site-packages/astropy/units/quantity.py in __float__(self)
   1085         try:
-> 1086             return float(self.to_value(dimensionless_unscaled))
   1087         except (UnitsError, TypeError):

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/quantity.py in to_value(self, unit, equivalencies)
    742                 # Short-cut failed; try default (maybe equivalencies help).
--> 743                 value = self._to_value(unit, equivalencies)
    744             else:

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/quantity.py in _to_value(self, unit, equivalencies)
    661             equivalencies = self._equivalencies
--> 662         return self.unit.to(unit, self.view(np.ndarray),
    663                             equivalencies=equivalencies)

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/core.py in to(self, other, value, equivalencies)
    986         else:
--> 987             return self._get_converter(other, equivalencies=equivalencies)(value)
    988 

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/core.py in _get_converter(self, other, equivalencies)
    917 
--> 918             raise exc
    919 

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/core.py in _get_converter(self, other, equivalencies)
    902         try:
--> 903             return self._apply_equivalencies(
    904                 self, other, self._normalize_equivalencies(equivalencies))

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/core.py in _apply_equivalencies(self, unit, other, equivalencies)
    885 
--> 886         raise UnitConversionError(
    887             "{} and {} are not convertible".format(

UnitConversionError: '1 / s' (frequency) and '' (dimensionless) are not convertible

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-f40ec5842608> in <module>
     16 for ls, i, flavor in zip(["-", "--", "-.", ":"], range(len(nu_new)), Flavor):
     17     new_flux = nu_new[i]
---> 18     ax.plot(t, new_flux, ls, lw=2, alpha=0.7, label=flavor.to_tex()),
     19 
     20 # plt.yscale('log')

/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1741         """
   1742         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1744         for line in lines:
   1745             self.add_line(line)

/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py in __call__(self, data, *args, **kwargs)
    271                 this += args[0],
    272                 args = args[1:]
--> 273             yield from self._plot_args(this, kwargs)
    274 
    275     def get_next_color(self):

/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    387         if len(tup) == 2:
    388             x = _check_1d(tup[0])
--> 389             y = _check_1d(tup[-1])
    390         else:
    391             x, y = index_of(tup[-1])

/opt/anaconda3/lib/python3.8/site-packages/matplotlib/cbook/__init__.py in _check_1d(x)
   1293     """Convert scalars to 1d arrays; pass-through arrays as is."""
   1294     if not hasattr(x, 'shape') or len(x.shape) < 1:
-> 1295         return np.atleast_1d(x)
   1296     else:
   1297         try:

<__array_function__ internals> in atleast_1d(*args, **kwargs)

/opt/anaconda3/lib/python3.8/site-packages/numpy/core/shape_base.py in atleast_1d(*arys)
     64     res = []
     65     for ary in arys:
---> 66         ary = asanyarray(ary)
     67         if ary.ndim == 0:
     68             result = ary.reshape(1)

/opt/anaconda3/lib/python3.8/site-packages/numpy/core/_asarray.py in asanyarray(a, dtype, order, like)
    169         return _asanyarray_with_like(a, dtype=dtype, order=order, like=like)
    170 
--> 171     return array(a, dtype, copy=False, order=order, subok=True)
    172 
    173 

/opt/anaconda3/lib/python3.8/site-packages/astropy/units/quantity.py in __float__(self)
   1086             return float(self.to_value(dimensionless_unscaled))
   1087         except (UnitsError, TypeError):
-> 1088             raise TypeError('only dimensionless scalar quantities can be '
   1089                             'converted to Python scalars')
   1090 

TypeError: only dimensionless scalar quantities can be converted to Python scalars

Another error appears in In[10], which seems can be traced back to In[7].

The code of In[10] is:

fig1, ax = plt.subplots(1, 1, figsize=(15,5), sharex = True, sharey = True)
t = np.linspace(-0.1, 4, 201) * u.s

s2t12_val = np.random.normal(loc = s2t12, scale = s2t12_er, size = 1000)
c2t12_val = 1 - s2t12_val

nu_e2_er = [a*s2t12_val + (b + c)*c2t12_val for a, b, c in zip(nu_list2[0], nu_list2[2], nu_list2[3])] 
std_e, mean_e, sig1_lo_e, sig1_hi_e, sig2_lo_e, sig2_hi_e, sig3_lo_e, sig3_hi_e = errors(nu_e2_er)

ax.fill_between(t, sig1_lo_e, sig1_hi_e, color='g', alpha=0.3, label = r'$1\sigma_{e}$')
ax.fill_between(t, sig2_lo_e, sig2_hi_e, color = 'g', alpha = 0.2, label = '$2\sigma_e$')
ax.fill_between(t, sig3_lo_e, sig3_hi_e, color='g', alpha = 0.1, label = '$3\sigma_e$')
ax.plot(t, mean_e, 'darkgreen', label=r'Mean $\nu_e$')
  
nu_x2_er = [((1.0-s2t12_val)*a + (1.0+s2t12_val)*(b+c))/2 for a, b, c in zip(nu_list2[0], nu_list2[2], nu_list2[3])] 
std_x, mean_x, sig1_lo_x, sig1_hi_x, sig2_lo_x, sig2_hi_x, sig3_lo_x, sig3_hi_x = errors(nu_x2_er)
ax.fill_between(t, sig1_lo_x, sig1_hi_x, color='orange', alpha=0.3, label=r'$1\sigma_x$')
ax.fill_between(t, sig2_lo_x, sig2_hi_x, color = 'orange', alpha = 0.2, label = r'$2\sigma_x$')
ax.fill_between(t, sig3_lo_x, sig3_hi_x, color='orange', alpha = 0.1, label = r'$3\sigma_x$')
ax.plot(t, mean_x, 'r', label=r'Mean $\nu_x$')


ax.set(xlabel='time - $t_{bounce}$ [s]',
        xlim = (0.25, 0.4),
        ylabel='flux',#, yscale ='log',
        ylim=(1.5e55, 3.4e55),
      title = r'$\theta_{12}$ dependent flavor oscillations: Inverted Mixing')
ax.legend()

fig.tight_layout()

The error is:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-c62ff99f4e7e> in <module>
      5 c2t12_val = 1 - s2t12_val
      6 
----> 7 nu_e2_er = [a*s2t12_val + (b + c)*c2t12_val for a, b, c in zip(nu_list2[0], nu_list2[2], nu_list2[3])]
      8 std_e, mean_e, sig1_lo_e, sig1_hi_e, sig2_lo_e, sig2_hi_e, sig3_lo_e, sig3_hi_e = errors(nu_e2_er)
      9 

NameError: name 'nu_list2' is not defined

Is this using the ASTERIA source module or is the error occurring after the switch to the SNEWPY models?

It's hard to say what the exact source of the error is without digging in, it appears to be a mix of issues. This was run using the old ASTERIA source module. I'm planning to test the doc notebooks against the new version before I merge with main.