SNEWS2 / snewpy

A Python package for working with supernova neutrinos
https://snewpy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
24 stars 17 forks source link

Models that define `_metadata_from_filename` helper function cannot be used in `snewpy.snowglobes.generate_fluence` #336

Closed JostMigenda closed 1 month ago

JostMigenda commented 1 month ago

[Received this bug report via email from a user.]

The following example (simplified version of SNOwGLoBES_usage.ipynb, but with a different model):

from snewpy import snowglobes, model_path

distance = 10  # Supernova distance in kpc
detector = "wc100kt30prct"  # Name of SNOwGLoBES detector model to use
modeltype = 'Nakazato_2013'  # Model type from snewpy.models
model = 'nakazato-shen-z0.004-t_rev100ms-s20.0.fits'  # Name of model
transformation = 'AdiabaticMSW_NMO'  # Desired flavor transformation

model_path = model_path + "/" + modeltype + "/" + model
outfile = modeltype + "_" + model + "_" + transformation

tarredfile = snowglobes.generate_fluence(model_path, modeltype, transformation, distance, outfile)

fails with the following error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 16
     13 model_path = model_path + "/" + modeltype + "/" + model
     14 outfile = modeltype + "_" + model + "_" + transformation
---> 16 tarredfile = snowglobes.generate_fluence(model_path, modeltype, transformation, distance, outfile)

File ~/opt/miniconda3/envs/snewpy/lib/python3.12/site-packages/snewpy/snowglobes.py:140, in generate_fluence(model_path, model_type, transformation_type, d, output_filename, tstart, tend, snmodel_dict)
    137 flavor_transformation = flavor_transformation_dict[transformation_type]
    139 model_dir, model_file = os.path.split(os.path.abspath(model_path))
--> 140 snmodel = model_class(model_path, **snmodel_dict)
    142 #set the timings up
    143 #default if inputs are None: full time window of the model
    144 times = None

File ~/opt/miniconda3/envs/snewpy/lib/python3.12/site-packages/snewpy/models/registry_model.py:100, in deprecated.<locals>._wrapper.<locals>._f(*args, **kwargs)
     98     if name in params.arguments:
     99         warn(message.format(name=name), category=UserWarning, stacklevel=2)
--> 100 return func(*args,**kwargs)

File ~/opt/miniconda3/envs/snewpy/lib/python3.12/site-packages/snewpy/models/base.py:24, in _wrap_init.<locals>._wrapper(self, *arg, **kwargs)
     22 @wraps(init)
     23 def _wrapper(self, *arg, **kwargs):
---> 24     init(self, *arg, **kwargs)
     25     check(self)

File ~/opt/miniconda3/envs/snewpy/lib/python3.12/site-packages/snewpy/models/registry_model.py:476, in legacy_filename_initialization.<locals>.c1.__init__(self, filename, *args, **kwargs)
    474         self.metadata = {}
    475     if hasattr(self,'_metadata_from_filename'):
--> 476         self.metadata.update(*self._metadata_from_filename(filename))
    477     self._loader_class.__init__(self, filename=os.path.abspath(filename), metadata=self.metadata)
    478 else:

TypeError: update expected at most 1 argument, got 4

The issue is in the line self.metadata.update(*self._metadata_from_filename(filename)). Metadata is a dictionary, and dict.update expects either a single argument of type dict, or a set of keyword arguments. However, *self._metadata_from_filename(filename) returns an iterable of dict.keys(); we could either drop the * to pass in the raw dict or use ** to pass the dict as a series of kwargs.

This appears to affect all models that define _metadata_from_filename. I’ll prepare a PR with a fix.

Riya-Gaba commented 1 month ago

Thank you @JostMigenda for your prompt response and for taking the necessary actions regarding the reported issue with SNEWPY. I'm glad to see that a pull request has been submitted to address it.

JostMigenda commented 1 month ago

@Riya-Gaba Thanks for reporting this! I've just made a v1.5.1 release including this bugfix; please update and enjoy snewpy!