AbstractFuzzyIntersect

class hybrid_learning.fuzzy_logic.predicates.custom_ops.AbstractFuzzyIntersect(*in_keys, logical_and=None, logical_or=None, mask_dims=(- 2, - 1), keep_dims=False, **kwargs)[source]

Bases: TorchOperation, ABC

Abstract class to define fuzzy intersection (over union) operations. The core method provided in this class is torch_intersect_proportion().

Public Data Attributes:

ARITY

The arity of the operation.

settings

Settings to reproduce the instance.

setting_defaults

Defaults used for settings.

Inherited from : py: class:Merge

SYMB

The string symbol of this class (override for sub-classes).

ARITY

The arity of the operation.

IS_COMMUTATIVE

Whether instances are equivalent to ones with permuted in_keys.

is_variadic

Whether the instance is variadic.

settings

Settings to reproduce the instance.

setting_defaults

Defaults used for settings.

pretty_op_symb

Name of the operation symbol suitable for filenames etc.

children

The input keys which are child operations.

all_children

All children operations in the flattened computational tree, sorted depth first.

consts

The constant string keys in the input keys.

operation_keys

The list of keys used for this parent operation in original order (constants and children output keys).

all_in_keys

All string input keys both of self and of all child operations.

all_out_keys

Output keys of self and all child operations.

Inherited from : py: class:DictTransform

settings

Settings to reproduce the instance.

Inherited from : py: class:Transform

IDENTITY_CLASS

The identity class or classes for composition / addition.

settings

Settings to reproduce the instance.

Public Methods:

torch_intersect(*masks)

torch_union(*masks)

torch_intersect_proportion(*masks[, iou, ...])

Calculate to what degree mask_a is covered by mask_b.

Inherited from : py: class:TorchOperation

operation(annotation_vals)

Calculate the predicate output.

Inherited from : py: class:Merge

to_infix_notation([sort_key, ...])

Return an infix str encoding equal for differently sorted operations.

to_str(**infix_notation_kwargs)

Alias for to_infix_notation().

to_pretty_str(**infix_notation_kwargs)

Same as to_str() but using pretty operation names suitable for filenames etc.

to_repr([settings, defaults, sort_key, ...])

Return str representation which can be used to reproduce and compare the instance.

treerecurse_replace_keys(**replace_map)

Return a new formula with all occurences of variables in replace_map replaced and else identical settings.

treerecurse(fun)

Apply the given function recursively to this and all children instances.

apply_to(annotations[, keep_keys])

Apply this operation to the annotations dict.

variadic_apply_to(annotations)

Return the result of operation on the values/items of a mapping or sequence of arbitrary length.

operation(annotation_vals)

Calculate the predicate output.

Inherited from : py: class:DictTransform

apply_to(annotations[, keep_keys])

Apply this operation to the annotations dict.

Inherited from : py: class:Transform

apply_to(annotations[, keep_keys])

Apply this operation to the annotations dict.

Special Methods:

__init__(*in_keys[, logical_and, ...])

Init.

Inherited from : py: class:Merge

__init__(*in_keys[, logical_and, ...])

Init.

__str__()

Return str(self).

__repr__()

Call to_repr() without sorting.

__eq__(other)

Two merge operations are considered equal, if their normalized representations coincide.

__copy__()

Return a deep copy of self using settings.

__call__(annotations[, keep_keys])

Call method modifying a given dictionary.

Inherited from : py: class:DictTransform

__call__(annotations[, keep_keys])

Call method modifying a given dictionary.

Inherited from : py: class:Transform

__repr__()

Call to_repr() without sorting.

__eq__(other)

Two merge operations are considered equal, if their normalized representations coincide.

__copy__()

Return a deep copy of self using settings.

__add__(other)

Return a flat composition of self with other.

__radd__(other)

Return a flat composition of other and self.

__call__(annotations[, keep_keys])

Call method modifying a given dictionary.


Parameters
__init__(*in_keys, logical_and=None, logical_or=None, mask_dims=(- 2, - 1), keep_dims=False, **kwargs)[source]

Init.

Hand over input keys either as str or as a Merge operation of str.

Parameters
  • in_keys – sequence of either Merge operation instances or strings with placeholders for the input keys

  • out_key – key for the output of this operation; used to init out_key

  • overwrite – on call, whether to overwrite the value at out_key in the given dict if the key already exists; raise if key exists and overwrite is true; saved in overwrite.

  • replace_none – if not None, the value to replace any None values with; see replace_none

  • symb – override the SYMB for this instance

  • keep_keys – intermediate output keys to add to call output; see keep_keys

  • cache_duplicates – whether outputs of children with identical keys should be cached and reused; see cache_duplicates

  • _variadic – the preferred way to specify this argument is variadic_(); see there for details

  • logical_and (Optional[Merge]) –

  • logical_or (Optional[Merge]) –

  • mask_dims (Union[int, Sequence[int]]) –

  • keep_dims (bool) –

torch_intersect(*masks)[source]
Parameters

masks (Tensor) –

Return type

FloatTensor

torch_intersect_proportion(*masks, iou=True, mask_dims=None)[source]

Calculate to what degree mask_a is covered by mask_b.

Returns

Depending on iou, this is the following for masks A=mask_a, B=mask_b, and fuzzy set membership function \(\in\):

  • iou=True: intersection over union between mask_a and mask_b as \(\min(1, \frac{\sum_c c\in A \wedge c\in B}{(\sum_c c \in A \vee c \in B)})\)

  • iou=False: what proportion of mask_a area intersects with mask_b as \(\min(1, \frac{\sum_c c\in A \wedge c\in B}{\sum_a a \in A})\) (here, \(a\vee b\) is calculated as \(1 - ((1-a) \wedge (1-b))\))

Parameters
Return type

FloatTensor

torch_union(*masks)[source]
Parameters

masks (Tensor) –

Return type

FloatTensor

ARITY: int = 2

The arity of the operation. -1 means unlimited number of arguments possible.

keep_dims: bool

Whether to keep dim 1 entries in the mask_dims dimensions.

logical_and: Optional[Merge]

The logical AND operation to use for calculating mask intersection (assumed to be commutative). If None, product logic AND is used.

logical_or: Optional[Merge]

The logical OR operation to use for calculating mask union (assumed to be commutative). If None, it calculates as 1-self.logical_and([1-mask_a, 1-mask_b]).

mask_dims: Tuple[int, ...]

The dimensions along which the area of a mask is defined (after broadcasting).

property setting_defaults: Dict[str, Any]

Defaults used for settings.

property settings: Dict[str, Any]

Settings to reproduce the instance. (Mind that in_keys must be expanded! For direct reproduction use copy.)