andreatramacere / jetset

JetSeT a framework for self-consistent modeling and fitting of astrophysical relativistic jets
BSD 3-Clause "New" or "Revised" License
30 stars 15 forks source link

Cannot load data in ObsData object #16

Closed cosimoNigro closed 5 years ago

cosimoNigro commented 5 years ago

I cannot load a dataset into the ObsData object, not even the default datasets in jetset.test_data_helper. Here a snippet

from jetset.data_loader import ObsData
from astropy.table import Table
from jetset.test_data_helper import  test_SEDs

mySED = test_SEDs[1]                                                        
sed_data = ObsData(data_file=mySED) 

gives me the error

Traceback (most recent call last):
  File "test_loader.py", line 6, in <module>
    sed_data = ObsData(data_file=mySED)
  File "/home/cosimo/software/miniconda3/lib/python3.7/site-packages/jetset-1.0.2-py3.7-linux-x86_64.egg/jetset/data_loader.py", line 265, in __init__
    raise RuntimeError('table is not an astropy Table')
RuntimeError: table is not an astropy Table

looking into the code https://github.com/andreatramacere/jetset/blob/master/jetset/data_loader.py#L223 ObsData has a data_table argument in the __init__ than then is checked wheter it is a Table instance https://github.com/andreatramacere/jetset/blob/master/jetset/data_loader.py#L262

But even generating a dummy astropy table

table_string = """# %ECSV 0.9
# ---
# datatype:
# - {name: x, unit: Hz, datatype: float64}
# - {name: y, unit: erg / (cm2 s), datatype: float64}
# - {name: dy, unit: erg / (cm2 s), datatype: float64}
# schema: astropy-2.0
x y dy
6.204e-05 6.555e-13 1.74e-13
0.000153032 1.4282e-12 4.255e-13
0.00039292 2.052e-12 1.14e-13
0.000355696 2.7004e-12 7.31e-13
0.000947144 4.0304e-12 9.389e-13
0.570768 1.00878e-11 3.7122e-12
0.756888 7.7958e-12 4.392e-12"""

t = Table.read(table_string, format='ascii')
print(t)  

sed_data = ObsData(data_table=t, data_scale="lin-lin", z=1) 

I cannot load it in ObsData

Traceback (most recent call last):
  File "test_loader.py", line 28, in <module>
    sed_data = ObsData(data_table=t, data_scale="lin-lin", z=1) 
  File "/home/cosimo/software/miniconda3/lib/python3.7/site-packages/jetset-1.0.2-py3.7-linux-x86_64.egg/jetset/data_loader.py", line 284, in __init__
    self._build_data(dupl_filter=dupl_filter)
  File "/home/cosimo/software/miniconda3/lib/python3.7/site-packages/jetset-1.0.2-py3.7-linux-x86_64.egg/jetset/data_loader.py", line 447, in _build_data
    self._set_data_frame_and_scale()
  File "/home/cosimo/software/miniconda3/lib/python3.7/site-packages/jetset-1.0.2-py3.7-linux-x86_64.egg/jetset/data_loader.py", line 510, in _set_data_frame_and_scale
    self.data['nu_data_log']+= np.log10(nu_conv_factor)
UnboundLocalError: local variable 'nu_conv_factor' referenced before assignment

Any idea on how to solve it and why not even default dataset work?

andreatramacere commented 5 years ago

Hi, the astropy table has to be loaded using the Data Class, and then passed to ObsData:

from jetset.data_loader import ObsData
from astropy.table import Table
from jetset.test_data_helper import  test_SEDs
from jetset.data_loader import Data

data_table=Data(data_table=test_SEDs[1])
sed_data=ObsData(data_table=data_table)

You can find all the examples here: https://jetset.readthedocs.io/en/latest/user_guide/load_data/Jet_example_load_data.html

Regarding the table generated in the snippet, you need to give at least the 'data_scale' and the 'restframe' values. You can do this in three different ways

1) adding the metadata to the table text

table_string = """# %ECSV 0.9
# ---
# datatype:
# - {name: x, unit: Hz, datatype: float64}
# - {name: y, unit: erg / (cm2 s), datatype: float64}
# - {name: dy, unit: erg / (cm2 s), datatype: float64}
# meta: !!omap
# - {z: 0.0336 }
# - {restframe: obs}
# - {data_scale: lin-lin}
# schema: astropy-2.0
x y dy
6.204e-05 6.555e-13 1.74e-13
0.000153032 1.4282e-12 4.255e-13
0.00039292 2.052e-12 1.14e-13
0.000355696 2.7004e-12 7.31e-13
0.000947144 4.0304e-12 9.389e-13
0.570768 1.00878e-11 3.7122e-12
0.756888 7.7958e-12 4.392e-12"""

t = Table.read(table_string, format='ascii')
data_table=Data(data_table=t)
sed_data=ObsData(data_table=data_table)

2) adding it to the metadata of the Data object

data_table=Data(data_table=t)
data_table.metadata['data_scale']='lin-lin'
data_table.metadata['restframe']='obs'
sed_data=ObsData(data_table=data_table)

3) passing 'data_scale' and the 'restframe' to the constructor of ObsData

data_table=Data(data_table=t)
sed_data=ObsData(data_table=Data(data_table=t),data_scale='lin-lin',restframe='obs')

Probably all these information are not written clearly in the docs, I will try to improve the docs, or probably to make the data loader smoother. Thanks a lot for the feedback

cosimoNigro commented 5 years ago

Thanks for the clarification @andreatramacere. I am sorry, it was my fault, I was looking at the wrong docs http://isdc.unige.ch/~tramacer/jetset_doc/build/user_guide/model_fit/fit_example.html Closing it!

andreatramacere commented 5 years ago

no fault, the feedback is always useful! Can you tell me where was referenced the wrong doc? I have to update the link in case Thanks!