pyphi.measures.distribution.DistanceResult#
- class pyphi.measures.distribution.DistanceResult(value, **kwargs)[source]#
Bases:
floatA
floatthat carries auxiliary data about its computation.A distance or φ value with arbitrary metadata attached, so the provenance of a result (the measure used, the direction, the winning partition, and so on) remains inspectable after the number has been produced.
Comparison and arithmetic follow exact
floatsemantics. Metadata is preserved throughmin()/max()and copying, but is dropped by plain arithmetic, which returns an ordinary float. Tolerant comparison of φ values at decision points — where two values equal to withinconfig.numerics.precisionshould be treated as equal — is performed explicitly throughpyphi.numericsandpyphi.resolve_ties.resolve_ties(), not by the comparison operators of this type.- Parameters:
value – The numeric value.
**kwargs – Arbitrary keyword arguments stored as metadata attributes.
:ivar All
floatattributes are available, plus any metadata passed as: :ivar keyword arguments.:Notes
To build a NumPy float64 array from a collection of
DistanceResultobjects, useDistanceResult.values_array(results): it extracts the float values and drops the metadata. The metadata stays available on the individual results.Examples
Basic usage with metadata:
>>> from pyphi.measures.distribution import DistanceResult >>> result = DistanceResult(0.5, method='EMD', direction='CAUSE') >>> float(result) # Extract numeric value 0.5 >>> result.method # Access metadata 'EMD' >>> result.direction 'CAUSE'
Mathematical operations preserve the numeric value:
>>> result + 0.3 0.8 >>> result * 2 1.0 >>> result > 0.3 True
Type preservation in min/max with metadata:
>>> results = [ ... DistanceResult(0.5, method='EMD'), ... DistanceResult(0.3, method='L1'), ... DistanceResult(0.7, method='GID') ... ] >>> min_result = min(results) >>> float(min_result) 0.3 >>> min_result.method # Metadata from the minimum value is preserved 'L1'
NumPy array creation (explicit float extraction):
>>> results = [DistanceResult(0.5), DistanceResult(0.3), DistanceResult(0.7)] >>> arr = DistanceResult.values_array(results) >>> arr.dtype dtype('float64') >>> arr array([0.5, 0.3, 0.7])
Serialization through
pyphi.serializepreserves the auxiliary metadata:>>> from pyphi import serialize >>> result = DistanceResult(0.5, method='EMD', direction='CAUSE') >>> restored = serialize.loads(serialize.dumps(result)) >>> restored.method 'EMD'
Typical scientific workflow:
>>> # Compute multiple phi values >>> phi_values = [ ... DistanceResult(0.5, method='EMD', system='ABC'), ... DistanceResult(0.3, method='L1', system='ABC'), ... DistanceResult(0.7, method='GID', system='DEF') ... ] >>> # Find maximum >>> max_phi = max(phi_values) >>> print(f"Max φ = {max_phi:.3f} using {max_phi.method}") Max φ = 0.700 using GID >>> # Statistical analysis >>> # Explicitly extract floats for statistics (metadata dropped): >>> phi_array = DistanceResult.values_array(phi_values) >>> np.mean(phi_array) 0.5
- classmethod values_array(results, dtype=None)[source]#
Return the float values of an iterable of
DistanceResults as a NumPy array.Auxiliary metadata (
method,state, etc.) is intentionally dropped — callers that need it should iterate the input directly. Use this method when you want explicit control over the metadata-loss boundary, rather than relying on implicitnp.array(results)coercion, which silently drops metadata.- Parameters:
results (Iterable[DistanceResult]) – Iterable of DistanceResult objects.
dtype (Any) – Optional NumPy dtype for the array (default: float64).
- Returns:
np.ndarray – 1-D array of the float values.
- Return type:
Examples
>>> results = [DistanceResult(0.5, method='EMD'), ... DistanceResult(0.3, method='L1')] >>> arr = DistanceResult.values_array(results) >>> arr.dtype dtype('float64') >>> arr array([0.5, 0.3])