simudo.util package

Submodules

simudo.util.attrproxy module

class simudo.util.attrproxy.AttrPrefixProxy(object, prefix)[source]

Bases: object

class simudo.util.attrproxy.DictAttrProxy(mapping)[source]

Bases: object

simudo.util.base32_token module

simudo.util.base32_token.generate_base32_token(length)[source]

simudo.util.blackbody module

class simudo.util.blackbody.Blackbody(temperature)[source]

Bases: object

blackbody_I_E(energy_unit)[source]
blackbody_I_E_solar()[source]

(energy unit, dimensionful prefactor, B_E in energy_unit)

blackbody_I_E_solar_on_earth()[source]
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 to energies[k], and the upper bound is the smallest energy that is above lower. If none exists, then upper is the inf argument.

Return type

dict

photon_flux_integral(eu_prefactor_B, E0, E1)[source]
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

total_intensity_on_earth()[source]
property u

simudo.util.cartesian_product module

simudo.util.cartesian_product.cartesian_product(arrays)[source]

simudo.util.latex module

simudo.util.latex.sci_to_latex1(string, suppress_mantissa_eq_1=True)[source]

Convert a number in scientific notation to reasonable LaTeX code.

Do NOT modify this method! Copy-paste it and change the name. Other code may rely on its exact behavior (bug-for-bug compatibility).

simudo.util.logging module

class simudo.util.logging.NameLevelFilter(name_levelno_rules, *args, **kwargs)[source]

Bases: logging.Filter

filter(record)[source]

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

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
ensure_parent_dir(filename)[source]
property info_filename
logfile_formatter
setup()[source]
setup_dolfin_loglevel()[source]
setup_filters()[source]
setup_handlers()[source]
setup_logging()[source]
stream_console
stream_debug
stream_info
truncate = True

simudo.util.name_dict module

class simudo.util.name_dict.NameDict(iterable=None)[source]

Bases: object

add(obj, existing_raise=True)[source]
copy()[source]
items()[source]
keys()[source]
mapping
obj_eq(obj0, obj1)[source]
obj_to_key(obj)[source]
replace(obj)[source]
update(data, existing_raise=True)[source]
values()[source]

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.fullsplit(path)[source]

fully split path into components

simudo.util.os.mkdirp(path)[source]
simudo.util.os.outdir_path_helper(path)[source]

Calls mkdirp(), then returns dir_as_prefix() applied on path.

simudo.util.os.parse_kv(string, as_list=False)[source]
simudo.util.os.xlistdir(path, both=False)[source]

like listdir, but returns full paths or tuples (basename, fullpath)

simudo.util.pint module

class simudo.util.pint.XUnitRegistry(*args, **kwargs)[source]

Bases: pint.registry.UnitRegistry

custom_file_open(path)[source]
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.pint.make_unit_registry(extra_definitions=())[source]

simudo.util.raise_from module

simudo.util.raise_from.raise_from(new, old)[source]

simudo.util.raise_from_py3 module

simudo.util.raise_from_py3.raise_from(new, old)[source]

simudo.util.setattr_init_mixin module

class simudo.util.setattr_init_mixin.SetattrInitMixin(**kwargs)[source]

Bases: object

simudo.util.string_system module

simudo.util.string_system.format_string_system(string_system_output, replacements=None)[source]
simudo.util.string_system.make_string_system(strings, min_length=6, score=<function default_score>)[source]

simudo.util.todo module

class simudo.util.todo.TODO[source]

Bases: object

Maintain a list of callbacks to be done later. Useful for plotting for example.

>>> from simudo.util.todo import TODO
>>> todo = TODO()
>>> todo(0, print, "a")
>>> todo(2, print, "c")
>>> todo(1, print, "b")
>>> todo.call()
a
b
c
call()[source]

Perform calls in order dictated by their sorting key.

list

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

classmethod read_csv(path, json_path=None, read_csv_kwargs={})[source]

Opposite of to_csv().

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.XCSVBase[source]

Bases: object

is_string_column(df, dtypes_dict, column_name)[source]
json_path
json_path_from_xcsv_path(path)[source]
class simudo.util.xcsv.XCSVReader[source]

Bases: simudo.util.xcsv.XCSVBase

dtypes_dict
load_json_meta()[source]
meta
process_string_column(df, column_name, series)[source]
read_csv()[source]
string_columns
string_prefix
string_prefix_re
class simudo.util.xcsv.XCSVWriter[source]

Bases: simudo.util.xcsv.XCSVBase

compute_meta()[source]
dtypes_dict
meta
string_columns
property string_prefix
to_csv()[source]
write_json_meta()[source]
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!).

simudo.util.xtimeit module

simudo.util.xtimeit.xtimeit(thunk, timer=<built-in function perf_counter>, overall_time=1.0, warmup_iterations=3)[source]

Module contents