threeML / astromodels

Spatial and spectral models for astrophysics
BSD 3-Clause "New" or "Revised" License
44 stars 45 forks source link

model.display() weirdness #144

Closed henrikef closed 3 years ago

henrikef commented 3 years ago

Minor issue, but model.display() by default displays small flux values as 0 instead of switching to scientific notation. I thought I could fix that in the general display options for data frames, but that leads to trailing zeroes in the exponent being cut off (see below). Any ideas if this is a bug in pandas or astromodels?

Example:

from threeML import *
set_threeML_style()

lat_catalog = FermiLATSourceCatalog()
ra, dec, table = lat_catalog.search_around_source("Crab", radius=3.0)
model = lat_catalog.get_model()
model.free_point_sources_within_radius(3.0, normalization_only=True)

model.display()

pd.options.display.float_format = '{:.6g}'.format
model.display()

print(model.Crab_synch.spectrum.main.shape.K.value)
print(model.PSR_J0534p2200.spectrum.main.shape.K.max_value)

Relevant parts of the output:

First display (note that the values are all 0):

Free parameters (5):
--------------------

                                     value min_value max_value            unit
PSR_J0534p2200...K                     0.0       0.0       0.0  keV-1 s-1 cm-2
Crab_synch.spectrum.main.Powerlaw.K    0.0       0.0       0.0  keV-1 s-1 cm-2
Crab_IC.spectrum.main.Log_parabola.K   0.0       0.0       0.0  keV-1 s-1 cm-2
NVSS_J052622p224801...K                0.0       0.0       0.0  keV-1 s-1 cm-2
_4FGL_J0544d4p2238...K                 0.0       0.0       0.0  keV-1 s-1 cm-2

Second display (note that the two values ending in e1 should be e10):

Free parameters (5):
--------------------

                                            value    min_value    max_value  \
PSR_J0534p2200...K                   1.415332e-13 1.415332e-16  1.415332e-1   
Crab_synch.spectrum.main.Powerlaw.K   2.345400e-1 2.345400e-13 2.345400e-07   
Crab_IC.spectrum.main.Log_parabola.K 5.500000e-16 5.500000e-19 5.500000e-13   
NVSS_J052622p224801...K              1.135400e-17  1.135400e-2 1.135400e-14   
_4FGL_J0544d4p2238...K               2.965000e-17  2.965000e-2 2.965000e-14   

                                                unit  
PSR_J0534p2200...K                    keV-1 s-1 cm-2  
Crab_synch.spectrum.main.Powerlaw.K   keV-1 s-1 cm-2  
Crab_IC.spectrum.main.Log_parabola.K  keV-1 s-1 cm-2  
NVSS_J052622p224801...K               keV-1 s-1 cm-2  
_4FGL_J0544d4p2238...K                keV-1 s-1 cm-2  

Printing the two values in question directly:

2.3453999999999974e-10
1.4153316909221638e-10
omodei commented 3 years ago

I have tracked down the problem to be a pandas issue: Example:

import pandas as pd
pd.options.display.float_format = '{:.6g}'.format

my_dictionary = {'Pippo': {'value':1.4e-10,'toto':1e-20,'gigi':2}}
df = pd.DataFrame.from_dict(my_dictionary)
print (df)

my_dictionary = {'Pippo': {'value':1.4e-10,'toto':1e-20,'gigi':'2'}}
df = pd.DataFrame.from_dict(my_dictionary)
print (df)

results in

Pippo
gigi        2
toto    1e-20
value 1.4e-10
       Pippo
gigi       2
toto    1e-2
value 1.4e-1

The first print (where all the values in the dictionary are numbers, including gigi that is equal to the number 2) returns the correct values, while the second print, where now gigi is a string '2', prints out the wrong format.

henrikef commented 3 years ago

https://github.com/pandas-dev/pandas/issues

They only have 3529 open issues right now 😭

github-actions[bot] commented 3 years ago

This issue has become stale. Is there an update? We will close in 15 days