Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in __str__ method of tdm after calculating source data fields #215

Open
juanma-cano-vila opened this issue Feb 12, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@juanma-cano-vila
Copy link
Collaborator

After one inicializates and instace of TrialDataManager(), the print function works as expected.

tdm = TrialDataManager()
print(tdm)
TrialDataManager:
    Base data fields:
        None
    Source data fields:
        None
    Pre-event-selection static data fields:
        None
    Static data fields:
        None
    Global fitparam data fields:
        None

After adding the data fields, it still works before initializing them:

tdm.add_source_data_field(
    name='src_array',
    func=pointlikesource_to_data_field_array)
tdm.add_data_field(
    name='psi',
    func=get_tdm_field_func_psi(),
    dt='dec',
    is_srcevt_data=True)

print(tdm)
    Base data fields:
        None
    Source data fields:
        DataField: src_array: {dtype: None, vmin:  nan, vmax:  nan}
    Pre-event-selection static data fields:
        None
    Static data fields:
        DataField: psi: {dtype: None, vmin:  nan, vmax:  nan}
    Global fitparam data fields:
        None

Nevertheless, after calculating the source data fields, it crashes:

tdm.calculate_source_data_fields(shg_mgr=shg_mgr,
                     pmm=pmm,
                     )
print(tdm)
{
	"name": "UFuncTypeError",
	"message": "ufunc 'minimum' did not contain a loop with signature matching types (dtype([('ra', '<f8'), ('dec', '<f8'), ('weight', '<f8')]), dtype([('ra', '<f8'), ('dec', '<f8'), ('weight', '<f8')])) -> None",
	"stack": "---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
Cell In[20], line 1
----> 1 print(tdm)

File ~/LRZ Sync+Share/master_thesis/Projects/stack_analysis_masq_BLLacs/skyllh.stack_public/skyllh/core/trialdata.py:578, in TrialDataManager.__str__(self)
    574 s += '\
'
    576 s1 = 'Source data fields:\
'
    577 s2 = '\
'.join(
--> 578     [
    579         str(df)
    580         for (_, df) in self._source_data_fields_dict.items()
    581     ]
    582 )
    583 if s2 == '':
    584     s2 = 'None'

File ~/LRZ Sync+Share/master_thesis/Projects/stack_analysis_masq_BLLacs/skyllh.stack_public/skyllh/core/trialdata.py:579, in <listcomp>(.0)
    574 s += '\
'
    576 s1 = 'Source data fields:\
'
    577 s2 = '\
'.join(
    578     [
--> 579         str(df)
    580         for (_, df) in self._source_data_fields_dict.items()
    581     ]
    582 )
    583 if s2 == '':
    584     s2 = 'None'

File ~/LRZ Sync+Share/master_thesis/Projects/stack_analysis_masq_BLLacs/skyllh.stack_public/skyllh/core/trialdata.py:195, in DataField.__str__(self)
    193 if self._values is not None:
    194     dtype = str(self._values.dtype)
--> 195     vmin = np.min(self._values)
    196     vmax = np.max(self._values)
    198 s = f'{classname(self)}: {self.name}: '

File ~/envs/SkyLLH/lib/python3.10/site-packages/numpy/core/fromnumeric.py:2953, in min(a, axis, out, keepdims, initial, where)
   2836 @array_function_dispatch(_min_dispatcher)
   2837 def min(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   2838         where=np._NoValue):
   2839     \"\"\"
   2840     Return the minimum of an array or minimum along an axis.
   2841 
   (...)
   2951     6
   2952     \"\"\"
-> 2953     return _wrapreduction(a, np.minimum, 'min', axis, None, out,
   2954                           keepdims=keepdims, initial=initial, where=where)

File ~/envs/SkyLLH/lib/python3.10/site-packages/numpy/core/fromnumeric.py:88, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     85         else:
     86             return reduction(axis=axis, out=out, **passkwargs)
---> 88 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

UFuncTypeError: ufunc 'minimum' did not contain a loop with signature matching types (dtype([('ra', '<f8'), ('dec', '<f8'), ('weight', '<f8')]), dtype([('ra', '<f8'), ('dec', '<f8'), ('weight', '<f8')])) -> None"
}

The reason seems to be that the str method for DataField objects computes the minimun and maximum value. But for the Source Data Fields we don't have one parameter with a range of values, but rather two parameters with a single value, namely the declination and right ascension. Therefore, when trying to compute the minima on this special case, it crashes.

A possible solution could be to handle this case of source data fields separatedly, but I don't know if the same bug could arise for the other data fields, and therefore a more profound change is better.

@juanma-cano-vila juanma-cano-vila added the bug Something isn't working label Feb 12, 2024
@tomaskontrimas tomaskontrimas changed the title Error in __srt__ method of dm after calculating source data fields Error in __srt__ method of tdm after calculating source data fields Feb 12, 2024
@tomaskontrimas tomaskontrimas changed the title Error in __srt__ method of tdm after calculating source data fields Error in __str__ method of tdm after calculating source data fields Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant