wepy.resampling.decisions.decision module

Abstract base class for Decision classes.

See the NoDecision class and others in this module for examples.

To create your own subclass of the Decision class you must customize the following class constants:

  • ENUM

  • FIELDS

  • SHAPES

  • DTYPE

  • RECORD_FIELDS

The ‘ENUM’ field should be a python ‘Enum’ class created by subclassing and customizing ‘Enum’ in the normal pythonic way. The elements of the ‘Enum’ are the actual decision choices, and their numeric value is used for serialization.

The ‘FIELDS’ constant is a specification of the number of fields that a decision record will have. All decision records should contain the ‘decision_id’ field which is the choice of decision. This class implements that and should be used as shown in the examples.

In order that fields be serializable to different formats, we also require that they be a numpy array datatype.

To support this we require the data shapes and data types for each field. Elements of SHAPES and DTYPES should be of a format recognizable by the numpy array constructor.

To this we allow the additional option of specifying SHAPES as the python built-in name Ellipsis (AKA ‘…’). This will specify the shape as a variable length 1-dimensional array.

The RECORD_FIELDS is used as a way to specify fields which are amenable to placement in simplified summary tables, i.e. simple non-compound values.

The only method that needs to be implemented in the Decision is ‘action’.

This function actually implements the algorithm for taking actions on the decisions and instructions and is called from the resampler to perform them on the collection of walkers.

class wepy.resampling.decisions.decision.Decision[source]

Bases: object

Represents and provides methods for a set of decision values.

ENUM = None

The enumeration of the decision types. Maps them to integers.

DEFAULT_DECISION = None

The default decision to choose.

FIELDS = ('decision_id',)

The names of the fields that go into the decision record.

SHAPES = ((1,),)

Field data shapes.

DTYPES = (<class 'int'>,)

Field data types.

RECORD_FIELDS = ('decision_id',)

The fields that could be used in a reduced table-like representation.

ANCESTOR_DECISION_IDS = None

Specify the enum values where their walker state sample value is passed on in the next generation, i.e. after performing the action.

classmethod default_decision()[source]
classmethod field_names()[source]

Names of the decision record fields.

classmethod field_shapes()[source]

Field data shapes.

classmethod field_dtypes()[source]

Field data types.

classmethod fields()[source]

Specs for each field.

Returns

fields – Field specs each spec is of the form (name, shape, dtype).

Return type

list of tuples

classmethod record_field_names()[source]

The fields that could be used in a reduced table-like representation.

classmethod enum_dict_by_name()[source]

Get the decision enumeration as a dict mapping name to integer.

classmethod enum_dict_by_value()[source]

Get the decision enumeration as a dict mapping integer to name.

classmethod enum_by_value(enum_value)[source]

Get the enum name for an enum_value.

Parameters

enum_value (int) –

Returns

enum_name

Return type

enum

classmethod enum_by_name(enum_name)[source]

Get the enum name for an enum_value.

Parameters

enum_name (enum) –

Returns

enum_value

Return type

int

classmethod record(enum_value, **fields)[source]

Generate a record for the enum_value and the other fields.

Parameters

enum_value (int) –

Returns

rec

Return type

dict of str: value

classmethod action(walkers, decisions)[source]

Perform the instructions for a set of resampling records on walkers.

The decisions are a collection of decision records which contain the decision value and the instruction values for a particular walker within its cohort (sample set).

The collection is organized as a list of lists. The outer list corresponds to the steps of resampling within a cycle.

The inner list is a list of decision records for a specific step of resampling, where the index of the decision record is the walker index.

Parameters
  • walkers (list of Walker objects) – The walkers you want to perform the decision instructions on.

  • decisions (list of list of decision records) – The decisions for each resampling step and their instructions to apply to the walkers.

Returns

resampled_walkers – The resampled walkers.

Return type

list of Walker objects

:raises NotImplementedError : abstract method:

classmethod parents(step)[source]

Given a step of resampling records (for a single resampling step) returns the parents of the children of this step.

Parameters

step (list of decision records) – The decision records for a step of resampling for each walker.

Returns

walker_step_parents – For each element, the index of it in the list corresponds to the child index and the value of the element is the index of it’s parent before the decision action.

Return type

list of int

class wepy.resampling.decisions.decision.NothingDecisionEnum(value)[source]

Bases: enum.Enum

Enumeration of the decision values for doing nothing.

NOTHING = 0

Do nothing with the walker.

class wepy.resampling.decisions.decision.NoDecision[source]

Bases: wepy.resampling.decisions.decision.Decision

Decision for a resampling process that does no resampling.

ENUM

alias of wepy.resampling.decisions.decision.NothingDecisionEnum

classmethod default_decision()
classmethod enum_by_name(enum_name)

Get the enum name for an enum_value.

Parameters

enum_name (enum) –

Returns

enum_value

Return type

int

classmethod enum_by_value(enum_value)

Get the enum name for an enum_value.

Parameters

enum_value (int) –

Returns

enum_name

Return type

enum

classmethod enum_dict_by_name()

Get the decision enumeration as a dict mapping name to integer.

classmethod enum_dict_by_value()

Get the decision enumeration as a dict mapping integer to name.

classmethod field_dtypes()

Field data types.

classmethod field_names()

Names of the decision record fields.

classmethod field_shapes()

Field data shapes.

classmethod fields()

Specs for each field.

Returns

fields – Field specs each spec is of the form (name, shape, dtype).

Return type

list of tuples

classmethod parents(step)

Given a step of resampling records (for a single resampling step) returns the parents of the children of this step.

Parameters

step (list of decision records) – The decision records for a step of resampling for each walker.

Returns

walker_step_parents – For each element, the index of it in the list corresponds to the child index and the value of the element is the index of it’s parent before the decision action.

Return type

list of int

classmethod record(enum_value, **fields)

Generate a record for the enum_value and the other fields.

Parameters

enum_value (int) –

Returns

rec

Return type

dict of str: value

classmethod record_field_names()

The fields that could be used in a reduced table-like representation.

DEFAULT_DECISION = 0

The default decision to choose.

FIELDS = ('decision_id', 'target_idxs')

The names of the fields that go into the decision record.

SHAPES = ((1,), Ellipsis)

Field data shapes.

DTYPES = (<class 'int'>, <class 'int'>)

Field data types.

RECORD_FIELDS = ('decision_id', 'target_idxs')

The fields that could be used in a reduced table-like representation.

ANCESTOR_DECISION_IDS = (0,)

Specify the enum values where their walker state sample value is passed on in the next generation, i.e. after performing the action.

classmethod action(walkers, decisions)[source]

Perform the instructions for a set of resampling records on walkers.

The decisions are a collection of decision records which contain the decision value and the instruction values for a particular walker within its cohort (sample set).

The collection is organized as a list of lists. The outer list corresponds to the steps of resampling within a cycle.

The inner list is a list of decision records for a specific step of resampling, where the index of the decision record is the walker index.

Parameters
  • walkers (list of Walker objects) – The walkers you want to perform the decision instructions on.

  • decisions (list of list of decision records) – The decisions for each resampling step and their instructions to apply to the walkers.

Returns

resampled_walkers – The resampled walkers.

Return type

list of Walker objects

:raises NotImplementedError : abstract method: