🔖 Task: 🧠 HIVE MIND COLLECTIVE INTELLIGENCE SYSTEM ══════════════════...

This commit is contained in:
rUv committed 2025-08-07 16:26:46 +00:00
1 parent bc02888945
commit 823c8bc2dd
8 files changed
+5531 -86

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
{
"startTime": 1754583516549,
"startTime": 1754584003469,
"totalTasks": 1,
"successfulTasks": 1,
"failedTasks": 0,
File diff suppressed because it is too large. Load diff
+3 -3
View File
@@ -1,10 +1,10 @@
[
{
"id": "cmd-hive-mind-1754583516647",
"id": "cmd-hive-mind-1754584003573",
"type": "hive-mind",
"success": true,
"duration": 40.82693599999999,
"timestamp": 1754583516688,
"duration": 42.26903999999996,
"timestamp": 1754584003616,
"metadata": {}
}
]
+1
View File
@@ -0,0 +1 @@
{"branch": "checkpoint/pre-edit-20250807-162549", "file": "/workspaces/claude-code-flow/test_uncertainty_conversion.py", "timestamp": "2025-08-07T16:25:49Z", "type": "pre-edit", "original_branch": "swe-bench"}
+1
View File
@@ -0,0 +1 @@
{"tag": "checkpoint-20250807-162551", "file": "/workspaces/claude-code-flow/test_uncertainty_conversion.py", "timestamp": "2025-08-07T16:25:51Z", "type": "post-edit", "branch": "swe-bench", "diff_summary": " .claude-flow/metrics/performance.json | 2 +- .claude-flow/metrics/system-metrics.json | 5356 +++++++++++++++++++++++++++++- .claude-flow/metrics/task-metrics.json | 8 +- .claude/checkpoints/1754583844.json | 1 + .claude/checkpoints/1754583846.json | 1 + .claude/checkpoints/1754583881.json | 1 + .claude/checkpoints/1754583884.json | 1 + .claude/checkpoints/1754583904.json | 1 + astropy_fix | 0 test_uncertainty_conversion.py | 178 + 10 files changed, 5464 insertions(+), 85 deletions(-) "}
+69
View File
@@ -0,0 +1,69 @@
# Solution Summary: Add Helpers to Convert Between Uncertainty Types
## Issue Description
GitHub Issue astropy__astropy-12057 requested adding helper functions to convert between different types of uncertainties in the astropy.nddata module. While the underlying conversion mechanism (`represent_as`) already existed, it was not easily discoverable or convenient to use.
## Problem Analysis
1. The `NDUncertainty.represent_as()` method existed but required passing a class reference
2. No convenience methods like `to_variance()`, `to_stddev()` were available
3. No standalone function for conversion with string-based type selection
4. The feature was not well-documented or discoverable
## Solution Implemented
### 1. Added Convenience Methods to NDUncertainty Base Class
- `to_stddev()` - Convert any uncertainty to StdDevUncertainty
- `to_variance()` - Convert any uncertainty to VarianceUncertainty
- `to_inverse_variance()` - Convert any uncertainty to InverseVariance
These methods internally use the existing `represent_as()` method but provide a more intuitive API.
### 2. Added Standalone Conversion Function
- `convert_uncertainty(uncertainty, target_type)` - Convert between uncertainty types
- Accepts string identifiers ('std', 'variance', 'ivar') or class references
- Provides clear error messages for invalid inputs
### 3. Updated Exports
- Added `convert_uncertainty` to the `__all__` list in nduncertainty.py
- This makes it available via `from astropy.nddata import convert_uncertainty`
## Key Features
- **Backward Compatible**: All existing functionality remains unchanged
- **Easy to Use**: Simple method calls like `uncertainty.to_variance()`
- **Flexible**: Supports both method calls and standalone function
- **String Support**: Can use strings like 'variance' instead of class references
- **Unit Aware**: Properly handles unit conversions (e.g., m → m² for variance)
- **Well Documented**: Includes docstrings with examples
## Example Usage
```python
from astropy.nddata import StdDevUncertainty, convert_uncertainty
import numpy as np
# Create a standard deviation uncertainty
std = StdDevUncertainty(np.array([2, 3, 4]))
# Convert using convenience methods
var = std.to_variance() # Returns VarianceUncertainty([4, 9, 16])
ivar = std.to_inverse_variance() # Returns InverseVariance([0.25, 0.111, 0.0625])
# Convert using standalone function with strings
var = convert_uncertainty(std, 'variance')
ivar = convert_uncertainty(std, 'ivar')
# Round-trip conversions work correctly
std2 = var.to_stddev() # Returns StdDevUncertainty([2, 3, 4])
```
## Testing
The solution includes comprehensive test cases covering:
- Conversion between all three uncertainty types
- Round-trip conversions
- Unit handling
- Error cases for unsupported conversions
- String-based type selection
- Invalid input handling
## Impact
This enhancement makes uncertainty conversion in astropy much more accessible to users, especially those working with external libraries that expect specific uncertainty formats (e.g., variances). The solution maintains full backward compatibility while significantly improving the user experience.
Submodule astropy_fix updated: a0fd11e5cc...a565504658.
+154
View File
@@ -0,0 +1,154 @@
diff --git a/astropy/nddata/nduncertainty.py b/astropy/nddata/nduncertainty.py
index fc7c37eb1d..9954ddd4c8 100644
--- a/astropy/nddata/nduncertainty.py
+++ b/astropy/nddata/nduncertainty.py
@@ -18,6 +18,7 @@ __all__ = [
"StdDevUncertainty",
"UnknownUncertainty",
"VarianceUncertainty",
+ "convert_uncertainty",
]
# mapping from collapsing operations to the complementary methods used for `to_variance`
@@ -485,6 +486,78 @@ class NDUncertainty(metaclass=ABCMeta):
)
return from_variance(as_variance())
+ def to_stddev(self):
+ """Convert this uncertainty to standard deviation uncertainty.
+
+ Returns
+ -------
+ stddev_uncertainty : `StdDevUncertainty`
+ The uncertainty converted to standard deviation.
+
+ Raises
+ ------
+ TypeError
+ If the conversion is not supported.
+
+ Examples
+ --------
+ >>> from astropy.nddata import VarianceUncertainty
+ >>> import numpy as np
+ >>> var = VarianceUncertainty(np.array([4, 9, 16]))
+ >>> std = var.to_stddev()
+ >>> std.array # doctest: +FLOAT_CMP
+ array([2., 3., 4.])
+ """
+ return self.represent_as(StdDevUncertainty)
+
+ def to_variance(self):
+ """Convert this uncertainty to variance uncertainty.
+
+ Returns
+ -------
+ variance_uncertainty : `VarianceUncertainty`
+ The uncertainty converted to variance.
+
+ Raises
+ ------
+ TypeError
+ If the conversion is not supported.
+
+ Examples
+ --------
+ >>> from astropy.nddata import StdDevUncertainty
+ >>> import numpy as np
+ >>> std = StdDevUncertainty(np.array([2, 3, 4]))
+ >>> var = std.to_variance()
+ >>> var.array # doctest: +FLOAT_CMP
+ array([ 4., 9., 16.])
+ """
+ return self.represent_as(VarianceUncertainty)
+
+ def to_inverse_variance(self):
+ """Convert this uncertainty to inverse variance uncertainty.
+
+ Returns
+ -------
+ ivar_uncertainty : `InverseVariance`
+ The uncertainty converted to inverse variance.
+
+ Raises
+ ------
+ TypeError
+ If the conversion is not supported.
+
+ Examples
+ --------
+ >>> from astropy.nddata import VarianceUncertainty
+ >>> import numpy as np
+ >>> var = VarianceUncertainty(np.array([4, 9, 16]))
+ >>> ivar = var.to_inverse_variance()
+ >>> ivar.array # doctest: +FLOAT_CMP
+ array([0.25 , 0.11111111, 0.0625 ])
+ """
+ return self.represent_as(InverseVariance)
+
class UnknownUncertainty(NDUncertainty):
"""This class implements any unknown uncertainty type.
@@ -1223,3 +1296,62 @@ class InverseVariance(_VariancePropagationMixin, NDUncertainty):
new_array = None if var_uncert.array is None else 1 / var_uncert.array
new_unit = None if var_uncert.unit is None else 1 / var_uncert.unit
return cls(new_array, unit=new_unit)
+
+
+def convert_uncertainty(uncertainty, target_type):
+ """Convert an uncertainty to a different uncertainty type.
+
+ Parameters
+ ----------
+ uncertainty : `NDUncertainty` instance
+ The uncertainty to convert.
+ target_type : str or `NDUncertainty` subclass
+ The target uncertainty type. Can be:
+ - 'stddev' or 'std' for `StdDevUncertainty`
+ - 'variance' or 'var' for `VarianceUncertainty`
+ - 'inverse_variance' or 'ivar' for `InverseVariance`
+ - An `NDUncertainty` subclass
+
+ Returns
+ -------
+ converted_uncertainty : `NDUncertainty` instance
+ The uncertainty converted to the target type.
+
+ Raises
+ ------
+ TypeError
+ If the conversion is not supported.
+ ValueError
+ If the target_type string is not recognized.
+
+ Examples
+ --------
+ >>> from astropy.nddata import StdDevUncertainty, convert_uncertainty
+ >>> import numpy as np
+ >>> std = StdDevUncertainty(np.array([2, 3, 4]))
+ >>> var = convert_uncertainty(std, 'variance')
+ >>> var.array # doctest: +FLOAT_CMP
+ array([ 4., 9., 16.])
+ """
+ # Map string names to classes
+ type_map = {
+ 'stddev': StdDevUncertainty,
+ 'std': StdDevUncertainty,
+ 'variance': VarianceUncertainty,
+ 'var': VarianceUncertainty,
+ 'inverse_variance': InverseVariance,
+ 'ivar': InverseVariance,
+ }
+
+ if isinstance(target_type, str):
+ target_type = target_type.lower()
+ if target_type not in type_map:
+ raise ValueError(
+ f"Unknown uncertainty type '{target_type}'. "
+ f"Valid options are: {', '.join(type_map.keys())}"
+ )
+ target_class = type_map[target_type]
+ else:
+ target_class = target_type
+
+ return uncertainty.represent_as(target_class)