core package

Submodules

core.bikegeometry module

BikeGeometry

Main object of the datavalidation package. It holds information about a bike geometry, including its parameters and certain config options. It also provides logic to work with bike geometries and to convert from/to JSON representation.

Author: Javier Chiyah, Heriot-Watt University, 2019

class datavalidation.core.bikegeometry.BikeGeometry(json_dict: dict)[source]

Bases: object

A BikeGeometry can be initialised with a JSON-like dict with a list of parameters.

If the dict given contains more elements than those needed by the BikeGeometry itself, it will save and output them when the BikeGeometry is converted back to a dict. For instance, giving a field “id” would return the same field with the values at the end.

Example valid dict:

{
        "parameter_list": [
                {
                        "p": "reach",
                        "v": "371",
                        "id": "**any-value**"
                },
                {
                        "p": "stack",
                        "v": "533",
                        "id": "**any-value**"
                }
        }
}

There is a @classmethod available if you need to create a BikeGeometry from a dict of parameters instead.

Parameters:json_dict – dict with the bike geometry information
classmethod from_parameter_dict(json_dict: dict)[source]

Creates a new BikeGeometry from a dictionary of parameters. This is used mostly in testing. Example dict:

{
        "parameter1": "value1",
        "parameter2": "value2"
}
Parameters:json_dict – dict of parameters
Returns:BikeGeometry
get_confidence_score()[source]

Gets the confidence score of this BikeGeometry. It is None if no validation has been performed on this BikeGeometry.

The confidence score is how confident we are that the parameters of this BikeGeometry fit together. The score is calculated only when this function is called, without updating parameters confidences when these change. Therefore, it is safe for parallel execution, but it is best not to call this function too often as it may take some time to calculate.

Returns:float from 0 to 1, or None if no validation has been performed
get_missing_parameter_list() → list[source]

Gets a list of all the parameters that are missing (e.g. None value, “” value, etc). It only returns those parameters that can be calculated based on constants.VALIDATABLE_PARAMETERS_LIST

Returns:list of parameter names (list of str)
get_parameter(parameter_name: str)[source]

Gets a GeometryParameter of this BikeGeometry. It returns None if the parameter has not been initialised or does not exists.

Example usage:

>> bike.get_parameter("slug")
GeometryParameter()
Parameters:parameter_name – name of the parameter
Returns:GeometryParameter or None
get_parameter_list(filter_empty: bool = True) → list[source]

Gets a list with all the parameters from the BikeGeometry. It filters empty/invalid parameters by default.

Parameters:filter_empty – whether to filter empty parameters (those that are None or their value is empty), True by default
Returns:list of GeometryParameter
get_parameter_value(parameter_name: str)[source]

Gets the value of a GeometryParameter of this BikeGeometry. It returns None if the parameter is missing, does not exists or if its value is None.

Example usage:

>> bike.get_parameter_value("slug")
"bike_slug_name"
# this is also equivalent
>> bike.get_parameter("slug").value
"bike_slug_name"
Parameters:parameter_name – name of the parameter
Returns:the value of the parameter, which can be str, int, float (or None if it does not exists)
set_parameter(parameter: datavalidation.core.geometryparameter.GeometryParameter)[source]

Sets a parameter of the BikeGeometry. This is used to set the parameters of the BikeGeometry when they were missing from the initialisation dict. Note that it substitutes the value, overriding whatever was before!

Parameters:parameter – GeometryParameter
Returns:None
to_dict(string_values=True) → dict[source]

Returns the value as a dict type so it can be serialised in a JSON response.

Parameters:string_values – whether to return the values in string format or not. Default is True, so everything is returned as string
Returns:dict object similar to that given in the __init__ function

core.config module

config

Module with several utility functions to configure certain parts of the package and read the config file.

The config file is located under the package’s root, in datavalidation.config.

Author: Javier Chiyah, Heriot-Watt University, 2019

datavalidation.core.config.read_config_file(filepath: str = 'config.json') → dict[source]

Reads the config file and returns a dict with the package config. The config file is by default under the root of the package “datavalidation.config.json”.

Parameters:filepath – path to the config file, default is the root of the package
Returns:config dict
datavalidation.core.config.set_up_logging(filepath: str = 'config.json', use_test_config: bool = False)[source]

Sets up the app-wide logger using the settings retrieved from the configuration file.

Note that the console option was not implemented in the end.

Parameters:
  • filepath – path to a custom config file, default is the package config.json file
  • use_test_config – True to use the test configuration of the logger, default is False
Returns:

None

core.constants module

constants

Module containing different package-wide constants

Author: Javier Chiyah, Heriot-Watt University, 2019

core.geometryparameter module

GeometryParameter

Object that holds information about a parameter of a BikeGeometry. It contains several utility functions to retrieve the parameter value, its confidence, calculated value, etc.

It also provides logic to convert from/to JSON representation.

Author: Javier Chiyah, Heriot-Watt University, 2019

class datavalidation.core.geometryparameter.GeometryParameter(name: str, value, extra_values: dict = None)[source]

Bases: object

A GeometryParameter can be initialised with its name and value. The value can be None if it is used as a placeholder for a future value.

Example initialisation:

>> GeometryParamter("head_angle", 73)
GeometryParameter() object
# or
>> GeometryParamter("head_angle", "73") # here "73" is casted to float without having to normalise
GeometryParameter() object
# or
>> GeometryParamter("head_angle", None)
GeometryParameter() object

Normalising or validating the parameter changes the object in place and expand the available functionality with additional class member variables such as calculated_value.

Parameters:
  • name – name of the parameter
  • value – value of the parameter, either as string or the correct value
  • extra_values – if there is anything else to store in the parameter, but not used (e.g. “id” field)
calculated_value

Gets the calculated value of the parameter if one exists. It will return the value in the correct type (e.g. integers will return as int).

Returns:[int|float|str] value or None
confidence

Gets the confidence score of the GeometryParameter.

Returns:float confidence score
classmethod from_dict(json_dict: dict)[source]

Creates a new GeometryParameter from a dictionary.

Example dict:

{
        "p": "parameter_name",
        "v": "parameter_value",
        "id": "optional additional field",
        ...
}
Parameters:json_dict – dict of the parameter (aka JSON)
Returns:GeometryParameter
is_number() → bool[source]

Checks if the GeometryParameter is a number type (float or int, but not a string or unknown). It is used to check if the GeometryParameter needs to be normalised or validated.

Returns:bool, True if the GeometryParameter is a float or an int
name

Gets the name of the parameter.

Returns:string name
normalised_value

Gets the normalised value of the parameter, without taking the calculated value into account. It will return the value in the correct type (e.g. integers will return as int).

If the GeometryParameter has not been normalised, it will return the original parameter instead.

Returns:[int|float|str] value
original_value

Gets the original value of the GeometryParameter as a string, regardless of its true type.

Returns:string value
set_calculated_value(new_value, change_confidence: bool = True)[source]

Sets the calculated value of the GeometryParameter.

It will raise an exception if the new value is not a valid type.

Parameters:
  • new_value – calculated value
  • change_confidence – if True, it will change confidence, default is False
Returns:

None

Raises:
  • ValueError – raised if the new value cannot be casted into the correct type
  • TypeError – raised if the new value is None or the empty string
set_confidence(confidence: float, force: bool = False)[source]

Sets the confidence score of the GeometryParameter. Note that it will average the current confidence value with the one given to smooth differences in precision of the equations. It also rounds the confidence to be within the range of 0 to 1 (inclusive)

Parameters:
  • confidence – new confidence value
  • force – if True, it will force the confidence change. Otherwise, it chooses the highest (default is False)
Returns:

None

set_normalised_value(new_value)[source]

Sets the value of the GeometryParameter after normalising its value.

It will raise an exception if the new value is not a valid type.

It gets the new value (e.g., a str) and casts it to the correct type (e.g., float) to make sure it does not violate any constraints of normalised parameters. It can also accept a list, which means a range of values (e.g., [190, 170]).

Parameters:

new_value – normalised value

Returns:

None

Raises:
  • ValueError – raised if the new value cannot be casted into the correct type
  • TypeError – raised if the new value is None or the empty string
to_dict(string_values: bool = True) → dict[source]

Returns the GeometryParameter as a dictionary ready to be serialised into a JSON response. It packs all the information currently contained in the GeometryParameter.

Parameters:string_values – whether to return the values in string format or not, default is True
Returns:dict
type

Gets the type of the GeometryParameter.

Returns:type
value

Gets the most up to date value of the parameter (it can be the normalised value, the calculated value or the original value if none of those is available). It will return the value in the correct type (e.g. integers will return as int) and it will not produce exceptions if there is something wrong with the GeometryParameter value.

In order, it provides: - the calculated value if such exists - the normalised value if the parameter has been normalised - the original value in the correct type (if it can cast it, e.g. “17” can be easily casted to float) - the original value as a string if all above fails

You should check other functions if you require one of these values in particular instead.

Returns:[int|float|str] value