formatting of YTQuantities
Hi, I'm wondering if there's some way to format the way a YTQuantity prints, for example the number of decimal places. Simply using print() leads to many decimal places and includes the units. It's nice to have the units included automatically, but would also be good to have control over the number of decimal places. Actually, from looking at the source I see how the __str__ method for YTArray and YTQuantity objects is defined, which would indicate that no such formatting is possible, as built in -- though I suppose one could use the value and units parts to do the outputting. It might be nice to have a simple method built in like say valunit(x) which would return a tuple of the value and unit so then you could do print('{:.4f} {}'.format(x.valunit())) Jon -- ________________________________________________________ Jonathan D. Slavin Harvard-Smithsonian CfA jslavin@cfa.harvard.edu 60 Garden Street, MS 83 phone: (617) 496-7981 Cambridge, MA 02138-1516 cell: (781) 363-0035 USA ________________________________________________________
Does np.set_printoptions do what you want?
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.set_printo...
I’m on my phone and can’t check if that works with YTQuantity but I think
it should.
On Thu, Aug 23, 2018 at 8:22 AM Slavin, Jonathan
Hi,
I'm wondering if there's some way to format the way a YTQuantity prints, for example the number of decimal places. Simply using print() leads to many decimal places and includes the units. It's nice to have the units included automatically, but would also be good to have control over the number of decimal places.
Actually, from looking at the source I see how the __str__ method for YTArray and YTQuantity objects is defined, which would indicate that no such formatting is possible, as built in -- though I suppose one could use the value and units parts to do the outputting. It might be nice to have a simple method built in like say valunit(x) which would return a tuple of the value and unit so then you could do print('{:.4f} {}'.format(x.valunit()))
Jon
-- ________________________________________________________ Jonathan D. Slavin Harvard-Smithsonian CfA jslavin@cfa.harvard.edu 60 Garden Street, MS 83 phone: (617) 496-7981 Cambridge, MA 02138-1516 cell: (781) 363-0035 USA ________________________________________________________
_______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org
Ah no, looks like it doesn't work, although it does seem to work in unyt.
Unfortunately I don't think we can change printing for YTArray or
YTQuantity before yt 4.0 (which will use unyt under the hood).
However, I *can* do this:
https://github.com/yt-project/yt/pull/1985
Which should satisfy your need, I think.
On Thu, Aug 23, 2018 at 8:24 AM Nathan Goldbaum
Does np.set_printoptions do what you want?
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.set_printo...
I’m on my phone and can’t check if that works with YTQuantity but I think it should.
On Thu, Aug 23, 2018 at 8:22 AM Slavin, Jonathan
wrote: Hi,
I'm wondering if there's some way to format the way a YTQuantity prints, for example the number of decimal places. Simply using print() leads to many decimal places and includes the units. It's nice to have the units included automatically, but would also be good to have control over the number of decimal places.
Actually, from looking at the source I see how the __str__ method for YTArray and YTQuantity objects is defined, which would indicate that no such formatting is possible, as built in -- though I suppose one could use the value and units parts to do the outputting. It might be nice to have a simple method built in like say valunit(x) which would return a tuple of the value and unit so then you could do print('{:.4f} {}'.format(x.valunit()))
Jon
-- ________________________________________________________ Jonathan D. Slavin Harvard-Smithsonian CfA jslavin@cfa.harvard.edu 60 Garden Street, MS 83 phone: (617) 496-7981 Cambridge, MA 02138-1516 cell: (781) 363-0035 USA ________________________________________________________
_______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org
Hi Jon, For me, asdf = 43*yt.units.pc print( '{:.4f}'.format(asdf) ) outputs 43.0000 and print( '{:.4f} {}'.format(asdf, asdf.units) ) outputs 43.0000 pc Is that what you want? It’s not quite clean, but not too bad. Cheers, Alex --------- Alex Hill University of British Columbia and Space Science Institute email: ashill@astro.ubc.ca Based at Dominion Radio Astrophysical Observatory, National Research Council Canada Mailing: PO Box 248, Penticton, BC V2A 6K3, Canada Physical: 717 White Lake Road, Kaleden, BC V0H 1K0, Canada phone: +1 250-497-2356
On Aug 23, 2018, at 06:21, Slavin, Jonathan
wrote: Hi,
I'm wondering if there's some way to format the way a YTQuantity prints, for example the number of decimal places. Simply using print() leads to many decimal places and includes the units. It's nice to have the units included automatically, but would also be good to have control over the number of decimal places.
Actually, from looking at the source I see how the __str__ method for YTArray and YTQuantity objects is defined, which would indicate that no such formatting is possible, as built in -- though I suppose one could use the value and units parts to do the outputting. It might be nice to have a simple method built in like say valunit(x) which would return a tuple of the value and unit so then you could do print('{:.4f} {}'.format(x.valunit()))
Jon
-- ________________________________________________________ Jonathan D. Slavin Harvard-Smithsonian CfA jslavin@cfa.harvard.edu 60 Garden Street, MS 83 phone: (617) 496-7981 Cambridge, MA 02138-1516 cell: (781) 363-0035 USA ________________________________________________________
_______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org
participants (3)
-
Hill, Alex
-
Nathan Goldbaum
-
Slavin, Jonathan