simudo.util package¶
Submodules¶
simudo.util.attrproxy module¶
simudo.util.blackbody module¶
-
class
simudo.util.blackbody.
Blackbody
(temperature)[source]¶ Bases:
object
-
distance_factor
¶
-
geometric_factor
¶
-
non_overlapping_energy_ranges
(energies, inf=None)[source]¶ Helper method for computing non-overlapping energy ranges.
- Parameters
energies (dict) – Mapping where keys are arbitrary (typically: names of transitions or optical fields), and values are energy range lower bounds.
inf (optional) – Upper limit on energy. By default, 20 eV.
- Returns
ranges – Mapping with the same keys as the
energies
argument, and where the values are tuples(lower, upper)
such that the lower bound is equal toenergies[k]
, and the upper bound is the smallest energy that is abovelower
. If none exists, thenupper
is theinf
argument.- Return type
dict
-
photon_flux_integral_on_earth
(E0, E1)[source]¶ Assuming an observer at normal incidence on Earth, compute the photon flux coming from a black sun in a given energy range.
- Parameters
E0 – Lower bound on photon energy.
E1 – Upper bound on photon energy.
- Returns
photon_flux – Photon flux.
- Return type
quantity
-
property
u
¶
-
simudo.util.cartesian_product module¶
simudo.util.latex module¶
simudo.util.logging module¶
-
class
simudo.util.logging.
NameLevelFilter
(name_levelno_rules, *args, **kwargs)[source]¶ Bases:
logging.Filter
-
class
simudo.util.logging.
TypicalLoggingSetup
(**kwargs)[source]¶ Bases:
simudo.util.setattr_init_mixin.SetattrInitMixin
Class that sets up logging and filtering in a typical way for Simudo.
- Parameters
dolfin (bool, optional) – Configure the dolfin log level as well. Note that this imports
dolfin
, which takes a while. Only use it if you’re okay with that. (default: True)truncate (bool, optional) – Truncate (delete) the log file contents before starting to write to it. (default: False)
-
console_formatter
¶
-
property
debug_filename
¶
-
dolfin
= True¶
-
property
info_filename
¶
-
logfile_formatter
¶
-
stream_console
¶
-
stream_debug
¶
-
stream_info
¶
-
truncate
= True¶
simudo.util.name_dict module¶
simudo.util.os module¶
-
simudo.util.os.
dir_as_prefix
(path)[source]¶ Add the final path separator (“/”) if necessary.
For example:
"abc/def" -> "abc/def/" "abc/def/" -> "abc/def/"
-
simudo.util.os.
outdir_path_helper
(path)[source]¶ Calls
mkdirp()
, then returnsdir_as_prefix()
applied on path.
simudo.util.pint module¶
-
class
simudo.util.pint.
XUnitRegistry
(*args, **kwargs)[source]¶ Bases:
pint.registry.UnitRegistry
-
load_definitions
(file, is_resource=False)[source]¶ Add units and prefixes defined in a definition text file.
- Parameters
file – can be a filename or a line iterable.
is_resource – used to indicate that the file is a resource file and therefore should be loaded from the package. (Default value = False)
-
simudo.util.setattr_init_mixin module¶
simudo.util.string_system module¶
simudo.util.todo module¶
simudo.util.with_default_kwargs module¶
-
simudo.util.with_default_kwargs.
with_default_kwargs
()[source]¶ Python doesn’t like it if we pass the same argument by an explicit kwarg and through **kwargs, e.g.,
f(y=3, **{'x': 4, 'y': 5})
.This decorator transforms a function so that it receives a default dict of kwargs through its first argument, then remaining kwargs normally (latter taking precedence over the former).
Assuming f was decorated with this wrapper, the call above becomes
f({'x': 4, 'y':5}, y=3)
.You can (ab)use this function to be lazy and pass all the local variables to a function as kwargs, and still have the option of overriding some of them, e.g.,
f(locals(), y=3)
.
simudo.util.xcsv module¶
This (standalone) module implements a Pandas CSV reader-writer pair
that allows data types to survive a round-trip (where they wouldn’t
using plain pandas to_csv
). It achieves this by saving some column
metadata to JSON, and by prefixing string values with a “:” character
so that they cannot be confused with NaN values (which are also
allowed in string columns, creating unresolvable ambiguity in the
written data).
See to_csv()
and read_csv()
for more info.
These methods are available as simple functions, so you can do:
>>> to_xcsv(df, "hello.csv")
>>> df2, meta = from_xcsv(df, "hello.csv")
-
class
simudo.util.xcsv.
XCSV
[source]¶ Bases:
object
-
reader_class
¶ alias of
simudo.util.xcsv.XCSVReader
-
classmethod
to_csv
(df, path, json_path=None, to_csv_kwargs={})[source]¶ Basically the same as
pandas.DataFrame.to_csv()
, but with proper escaping for strings to prevent them from being accidentally parsed as numbers or nan, and with column dtypes being written to an accompanying json file.If the csv filename is
"a.csv"
, then the file name containing the metadata will be called"a.csv_meta.json"
.“XCSV” pronounced “excessive”.
Warning: mixed-type (“object”) columns are assumed to be string columns. So make sure those don’t contain anything other than strings or NaN, or your else your data might not survive the roundtrip test.
What’s definitely safe:
Columns with floats/ints and nans.
Columns with strings and nans.
Columns with booleans (no nans allowed!).
-
writer_class
¶ alias of
simudo.util.xcsv.XCSVWriter
-
-
class
simudo.util.xcsv.
XCSVReader
[source]¶ Bases:
simudo.util.xcsv.XCSVBase
-
dtypes_dict
¶
-
meta
¶
-
string_columns
¶
-
string_prefix
¶
-
string_prefix_re
¶
-
-
class
simudo.util.xcsv.
XCSVWriter
[source]¶ Bases:
simudo.util.xcsv.XCSVBase
-
dtypes_dict
¶
-
meta
¶
-
string_columns
¶
-
property
string_prefix
¶
-
-
simudo.util.xcsv.
read_xcsv
(path, json_path=None, read_csv_kwargs={})¶ Opposite of
to_csv()
.
-
simudo.util.xcsv.
to_xcsv
(df, path, json_path=None, to_csv_kwargs={})¶ Basically the same as
pandas.DataFrame.to_csv()
, but with proper escaping for strings to prevent them from being accidentally parsed as numbers or nan, and with column dtypes being written to an accompanying json file.If the csv filename is
"a.csv"
, then the file name containing the metadata will be called"a.csv_meta.json"
.“XCSV” pronounced “excessive”.
Warning: mixed-type (“object”) columns are assumed to be string columns. So make sure those don’t contain anything other than strings or NaN, or your else your data might not survive the roundtrip test.
What’s definitely safe:
Columns with floats/ints and nans.
Columns with strings and nans.
Columns with booleans (no nans allowed!).