phenomedb.utilities

class phenomedb.utilities.CustomEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Custom encoder for numpy data types

default(obj)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
phenomedb.utilities.breakdown_annotation_id(colname, harmonise_annotations=False)

Breakdown an annotation id into its constituent parts ie feature::1::LPOS:PPR:CAR(8:10):noUnit

Parameters:

colname (str) – The column name to breakdown

phenomedb.utilities.breakdown_compound_class_id(colname)

Breakdown a compound_class id

compound_class:1828::hmdb:direct_parent:Hypoxanthines:noUnit

phenomedb.utilities.configure_logging(identifier='phenomedb', log_file='phenomedb.log', level=20)

Setup a logger.

Parameters:
  • identifier (str, optional) – an identifier for your messages in the log, defaults to ‘phenomedb’.

  • log_file (str, optional) – file to log to at location specified in config.ini; (will create this dir if necessary), defaults to ‘phenomedb.log’.

  • level (int, optional) – log level, logging.INFO, logging.ERROR, logging.WARNING, defaults to logging.DEBUG.

Returns:

the logger.

Return type:

logging.logger

Flatten an SQLAlchemy model for the search index. Takes a model and returns a paragraph containing the values.

Parameters:

model (phenomedb.models.*) – The model to flatten.

Returns:

The whitespace seperated values.

Return type:

str

phenomedb.utilities.get_date(date_string)

Get data from potential formats

Parameters:

date (str) – date string

Returns:

Date object

Return type:

datetime.datetime

phenomedb.utilities.get_module_and_class_name(mod_class)

Get the module name and class name from the module.class_name string from the typespec.json

Parameters:

mod_class (str) – The module and class name string, ie imports.ImportSampleManifest

Raises:

Exception – mod_class not in the correct format

Returns:

module_name, class_name

Return type:

tuple

phenomedb.utilities.get_npyc_enum_from_value(value)

Get the nPYc enum from the value.

Parameters:

value (nPYc.enumerations.AssayRole or nPYc.enumerations.SampleType or nPYc.enumerations.QuantificationType or nPYc.enumerations.CalibrationMethod) – the value of the enum.

Returns:

The corresponding enum.

Return type:

nPYc.enumerations.AssayRole or nPYc.enumerations.SampleType or nPYc.enumerations.QuantificationType or nPYc.enumerations.CalibrationMethod

phenomedb.utilities.is_number(s)

Check if is number.

Parameters:

s (float, int, or str) – value to check.

Returns:

True or False.

Return type:

boolean

phenomedb.utilities.isfloat(x)

Check is a float.

Parameters:

x (float, int, or str) – value to check.

Returns:

True or False.

Return type:

boolean

phenomedb.utilities.isint(x)

Check if is an integer.

Parameters:

x (float, int, or str) – value to check.

Returns:

True or False.

Return type:

boolean

phenomedb.utilities.round_decimals_down(number: float, decimals: int = 2)

Returns a value rounded down to a specific number of decimal places.

Parameters:
  • number (float) – the number to round

  • decimals (int, optional) – How many decimals to round to defaults to 2

Raises:
  • TypeError – [description]

  • ValueError – [description]

Returns:

The rounded value

Return type:

float

phenomedb.utilities.round_decimals_up(number: float, decimals: int = 2)

Returns a value rounded up to a specific number of decimal places.

Parameters:
  • number (float) – the number to round

  • decimals (int, optional) – How many decimals to round to defaults to 2

Returns:

The rounded value

Return type:

float

phenomedb.utilities.send_tls_email(user_email, subject, message_text)

Send TLS email. Email settings are configuration options.

Parameters:
  • user_email (str) – The email of the recipient

  • subject (str) – The email subject

  • message_text (str) – The email body

phenomedb.utilities.serialise_unserialise(my_dict)

Serialise unserialise

Parameters:

my_dict (dict) – Dictionary to serialise and unserialise

Returns:

my_dict

Return type:

dict

phenomedb.utilities.total_size(o, handlers={}, verbose=False)

Returns the approximate memory footprint an object and all of its contents.

Automatically finds the contents of the following builtin containers and their subclasses: tuple, list, deque, dict, set and frozenset. To search other containers, add handlers to iterate over their contents:

handlers = {SomeContainerClass: iter,

OtherContainerClass: OtherContainerClass.get_elements}