USRA-STI / gdt-fermi

Gamma-ray Data Tools - Fermi mission components
Apache License 2.0
3 stars 7 forks source link

`Attribute Error` when calling `SpectralFitterPgstat.fit()` #18

Closed rachel-hamburg closed 10 months ago

rachel-hamburg commented 10 months ago

When performing a spectral fit (I've attached a portion of the code here), I get an attribute error:

from gdt.core.spectra.fitting import SpectralFitterPgstat
from gdt.core.spectra.functions import PowerLaw, Comptonized, Band
from gdt.missions.fermi.gbm.response import GbmRsp2
from gdt.missions.fermi.gbm.collection import GbmDetectorCollection

# Load a Response (.rsp) file for each NaI and BGO detector
nai_rsps = [GbmRsp2.open(str(gbm_path)+'/glg_cspec_'+nai+'_bn'+str(bn)+'_v01.rsp2') for nai in nais]
bgo_rsps = [GbmRsp2.open(str(gbm_path)+'/glg_cspec_'+bgo+'_bn'+str(bn)+'_v01.rsp2') for bgo in bgos]

# Create a collection from the list of our files
all_rsps = nai_rsps + bgo_rsps
rsps = GbmDetectorCollection.from_list(all_rsps)

# Initialize spectral fitter with the PHAs, backgrounds, and responses:
specfitter = SpectralFitterPgstat(phas, bkgds.to_list(), rsps.to_list(), method='TNC')

# Instantiate spectral functions
# a power law, cut-off power law, and a Band function
pl  = PowerLaw()
comp = Comptonized()
band  = Band()
models = [pl, comp, band]

for model in models:
    specfitter.fit(model, options={'maxiter': 1000})

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[68], line 5
      2 print ('Fitting with {} model ...'.format(model.name))
      4 # Perform fit 
----> 5 specfitter.fit(model, options={'maxiter': 1000})

File ~/anaconda3/envs/gdt_devel/lib/python3.12/site-packages/gdt/core/spectra/fitting.py:769, in SpectralFitter._fold_model(self, function, params)
    766 stat = np.zeros(self.num_sets)
    767 for i in range(self.num_sets):
    768     # fold model through response and convert to raw model counts
--> 769     model = self._rsp[i].drm.fold_spectrum(function, params, channel_mask=self._chan_masks[i])
    771     # perform likelihood calculation for one dataset
    772     stat[i] = self._eval_stat(i, model)

AttributeError: 'GbmRsp2' object has no attribute 'drm'
AdamGoldstein-USRA commented 10 months ago

You have to extract a DRM from the GbmRsp2 object in order to use it for spectral fitting. There are multiple ways to do this, like selecting the nearest DRM for a given time, interpolating a response, or creating a weighted response. See the gdt-fermi documentation on detector responses.

rachel-hamburg commented 10 months ago

Thank you. I can now complete a spectral fit.