crca 1.4.0__py3-none-any.whl → 1.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- CRCA.py +172 -7
- MODEL_CARD.md +53 -0
- PKG-INFO +8 -2
- RELEASE_NOTES.md +17 -0
- STABILITY.md +19 -0
- architecture/hybrid/consistency_engine.py +362 -0
- architecture/hybrid/conversation_manager.py +421 -0
- architecture/hybrid/explanation_generator.py +452 -0
- architecture/hybrid/few_shot_learner.py +533 -0
- architecture/hybrid/graph_compressor.py +286 -0
- architecture/hybrid/hybrid_agent.py +4398 -0
- architecture/hybrid/language_compiler.py +623 -0
- architecture/hybrid/main,py +0 -0
- architecture/hybrid/reasoning_tracker.py +322 -0
- architecture/hybrid/self_verifier.py +524 -0
- architecture/hybrid/task_decomposer.py +567 -0
- architecture/hybrid/text_corrector.py +341 -0
- benchmark_results/crca_core_benchmarks.json +178 -0
- branches/crca_sd/crca_sd_realtime.py +6 -2
- branches/general_agent/__init__.py +102 -0
- branches/general_agent/general_agent.py +1400 -0
- branches/general_agent/personality.py +169 -0
- branches/general_agent/utils/__init__.py +19 -0
- branches/general_agent/utils/prompt_builder.py +170 -0
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/METADATA +8 -2
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/RECORD +303 -20
- crca_core/__init__.py +35 -0
- crca_core/benchmarks/__init__.py +14 -0
- crca_core/benchmarks/synthetic_scm.py +103 -0
- crca_core/core/__init__.py +23 -0
- crca_core/core/api.py +120 -0
- crca_core/core/estimate.py +208 -0
- crca_core/core/godclass.py +72 -0
- crca_core/core/intervention_design.py +174 -0
- crca_core/core/lifecycle.py +48 -0
- crca_core/discovery/__init__.py +9 -0
- crca_core/discovery/tabular.py +193 -0
- crca_core/identify/__init__.py +171 -0
- crca_core/identify/backdoor.py +39 -0
- crca_core/identify/frontdoor.py +48 -0
- crca_core/identify/graph.py +106 -0
- crca_core/identify/id_algorithm.py +43 -0
- crca_core/identify/iv.py +48 -0
- crca_core/models/__init__.py +67 -0
- crca_core/models/provenance.py +56 -0
- crca_core/models/refusal.py +39 -0
- crca_core/models/result.py +83 -0
- crca_core/models/spec.py +151 -0
- crca_core/models/validation.py +68 -0
- crca_core/scm/__init__.py +9 -0
- crca_core/scm/linear_gaussian.py +198 -0
- crca_core/timeseries/__init__.py +6 -0
- crca_core/timeseries/pcmci.py +181 -0
- crca_llm/__init__.py +12 -0
- crca_llm/client.py +85 -0
- crca_llm/coauthor.py +118 -0
- crca_llm/orchestrator.py +289 -0
- crca_llm/types.py +21 -0
- crca_reasoning/__init__.py +16 -0
- crca_reasoning/critique.py +54 -0
- crca_reasoning/godclass.py +206 -0
- crca_reasoning/memory.py +24 -0
- crca_reasoning/rationale.py +10 -0
- crca_reasoning/react_controller.py +81 -0
- crca_reasoning/tool_router.py +97 -0
- crca_reasoning/types.py +40 -0
- crca_sd/__init__.py +15 -0
- crca_sd/crca_sd_core.py +2 -0
- crca_sd/crca_sd_governance.py +2 -0
- crca_sd/crca_sd_mpc.py +2 -0
- crca_sd/crca_sd_realtime.py +2 -0
- crca_sd/crca_sd_tui.py +2 -0
- cuda-keyring_1.1-1_all.deb +0 -0
- cuda-keyring_1.1-1_all.deb.1 +0 -0
- docs/IMAGE_ANNOTATION_USAGE.md +539 -0
- docs/INSTALL_DEEPSPEED.md +125 -0
- docs/api/branches/crca-cg.md +19 -0
- docs/api/branches/crca-q.md +27 -0
- docs/api/branches/crca-sd.md +37 -0
- docs/api/branches/general-agent.md +24 -0
- docs/api/branches/overview.md +19 -0
- docs/api/crca/agent-methods.md +62 -0
- docs/api/crca/operations.md +79 -0
- docs/api/crca/overview.md +32 -0
- docs/api/image-annotation/engine.md +52 -0
- docs/api/image-annotation/overview.md +17 -0
- docs/api/schemas/annotation.md +34 -0
- docs/api/schemas/core-schemas.md +82 -0
- docs/api/schemas/overview.md +32 -0
- docs/api/schemas/policy.md +30 -0
- docs/api/utils/conversation.md +22 -0
- docs/api/utils/graph-reasoner.md +32 -0
- docs/api/utils/overview.md +21 -0
- docs/api/utils/router.md +19 -0
- docs/api/utils/utilities.md +97 -0
- docs/architecture/causal-graphs.md +41 -0
- docs/architecture/data-flow.md +29 -0
- docs/architecture/design-principles.md +33 -0
- docs/architecture/hybrid-agent/components.md +38 -0
- docs/architecture/hybrid-agent/consistency.md +26 -0
- docs/architecture/hybrid-agent/overview.md +44 -0
- docs/architecture/hybrid-agent/reasoning.md +22 -0
- docs/architecture/llm-integration.md +26 -0
- docs/architecture/modular-structure.md +37 -0
- docs/architecture/overview.md +69 -0
- docs/architecture/policy-engine-arch.md +29 -0
- docs/branches/crca-cg/corposwarm.md +39 -0
- docs/branches/crca-cg/esg-scoring.md +30 -0
- docs/branches/crca-cg/multi-agent.md +35 -0
- docs/branches/crca-cg/overview.md +40 -0
- docs/branches/crca-q/alternative-data.md +55 -0
- docs/branches/crca-q/architecture.md +71 -0
- docs/branches/crca-q/backtesting.md +45 -0
- docs/branches/crca-q/causal-engine.md +33 -0
- docs/branches/crca-q/execution.md +39 -0
- docs/branches/crca-q/market-data.md +60 -0
- docs/branches/crca-q/overview.md +58 -0
- docs/branches/crca-q/philosophy.md +60 -0
- docs/branches/crca-q/portfolio-optimization.md +66 -0
- docs/branches/crca-q/risk-management.md +102 -0
- docs/branches/crca-q/setup.md +65 -0
- docs/branches/crca-q/signal-generation.md +61 -0
- docs/branches/crca-q/signal-validation.md +43 -0
- docs/branches/crca-sd/core.md +84 -0
- docs/branches/crca-sd/governance.md +53 -0
- docs/branches/crca-sd/mpc-solver.md +65 -0
- docs/branches/crca-sd/overview.md +59 -0
- docs/branches/crca-sd/realtime.md +28 -0
- docs/branches/crca-sd/tui.md +20 -0
- docs/branches/general-agent/overview.md +37 -0
- docs/branches/general-agent/personality.md +36 -0
- docs/branches/general-agent/prompt-builder.md +30 -0
- docs/changelog/index.md +79 -0
- docs/contributing/code-style.md +69 -0
- docs/contributing/documentation.md +43 -0
- docs/contributing/overview.md +29 -0
- docs/contributing/testing.md +29 -0
- docs/core/crcagent/async-operations.md +65 -0
- docs/core/crcagent/automatic-extraction.md +107 -0
- docs/core/crcagent/batch-prediction.md +80 -0
- docs/core/crcagent/bayesian-inference.md +60 -0
- docs/core/crcagent/causal-graph.md +92 -0
- docs/core/crcagent/counterfactuals.md +96 -0
- docs/core/crcagent/deterministic-simulation.md +78 -0
- docs/core/crcagent/dual-mode-operation.md +82 -0
- docs/core/crcagent/initialization.md +88 -0
- docs/core/crcagent/optimization.md +65 -0
- docs/core/crcagent/overview.md +63 -0
- docs/core/crcagent/time-series.md +57 -0
- docs/core/schemas/annotation.md +30 -0
- docs/core/schemas/core-schemas.md +82 -0
- docs/core/schemas/overview.md +30 -0
- docs/core/schemas/policy.md +41 -0
- docs/core/templates/base-agent.md +31 -0
- docs/core/templates/feature-mixins.md +31 -0
- docs/core/templates/overview.md +29 -0
- docs/core/templates/templates-guide.md +75 -0
- docs/core/tools/mcp-client.md +34 -0
- docs/core/tools/overview.md +24 -0
- docs/core/utils/conversation.md +27 -0
- docs/core/utils/graph-reasoner.md +29 -0
- docs/core/utils/overview.md +27 -0
- docs/core/utils/router.md +27 -0
- docs/core/utils/utilities.md +97 -0
- docs/css/custom.css +84 -0
- docs/examples/basic-usage.md +57 -0
- docs/examples/general-agent/general-agent-examples.md +50 -0
- docs/examples/hybrid-agent/hybrid-agent-examples.md +56 -0
- docs/examples/image-annotation/image-annotation-examples.md +54 -0
- docs/examples/integration/integration-examples.md +58 -0
- docs/examples/overview.md +37 -0
- docs/examples/trading/trading-examples.md +46 -0
- docs/features/causal-reasoning/advanced-topics.md +101 -0
- docs/features/causal-reasoning/counterfactuals.md +43 -0
- docs/features/causal-reasoning/do-calculus.md +50 -0
- docs/features/causal-reasoning/overview.md +47 -0
- docs/features/causal-reasoning/structural-models.md +52 -0
- docs/features/hybrid-agent/advanced-components.md +55 -0
- docs/features/hybrid-agent/core-components.md +64 -0
- docs/features/hybrid-agent/overview.md +34 -0
- docs/features/image-annotation/engine.md +82 -0
- docs/features/image-annotation/features.md +113 -0
- docs/features/image-annotation/integration.md +75 -0
- docs/features/image-annotation/overview.md +53 -0
- docs/features/image-annotation/quickstart.md +73 -0
- docs/features/policy-engine/doctrine-ledger.md +105 -0
- docs/features/policy-engine/monitoring.md +44 -0
- docs/features/policy-engine/mpc-control.md +89 -0
- docs/features/policy-engine/overview.md +46 -0
- docs/getting-started/configuration.md +225 -0
- docs/getting-started/first-agent.md +164 -0
- docs/getting-started/installation.md +144 -0
- docs/getting-started/quickstart.md +137 -0
- docs/index.md +118 -0
- docs/js/mathjax.js +13 -0
- docs/lrm/discovery_proof_notes.md +25 -0
- docs/lrm/finetune_full.md +83 -0
- docs/lrm/math_appendix.md +120 -0
- docs/lrm/overview.md +32 -0
- docs/mkdocs.yml +238 -0
- docs/stylesheets/extra.css +21 -0
- docs_generated/crca_core/CounterfactualResult.md +12 -0
- docs_generated/crca_core/DiscoveryHypothesisResult.md +13 -0
- docs_generated/crca_core/DraftSpec.md +13 -0
- docs_generated/crca_core/EstimateResult.md +13 -0
- docs_generated/crca_core/IdentificationResult.md +17 -0
- docs_generated/crca_core/InterventionDesignResult.md +12 -0
- docs_generated/crca_core/LockedSpec.md +15 -0
- docs_generated/crca_core/RefusalResult.md +12 -0
- docs_generated/crca_core/ValidationReport.md +9 -0
- docs_generated/crca_core/index.md +13 -0
- examples/general_agent_example.py +277 -0
- examples/general_agent_quickstart.py +202 -0
- examples/general_agent_simple.py +92 -0
- examples/hybrid_agent_auto_extraction.py +84 -0
- examples/hybrid_agent_dictionary_demo.py +104 -0
- examples/hybrid_agent_enhanced.py +179 -0
- examples/hybrid_agent_general_knowledge.py +107 -0
- examples/image_annotation_quickstart.py +328 -0
- examples/test_hybrid_fixes.py +77 -0
- image_annotation/__init__.py +27 -0
- image_annotation/annotation_engine.py +2593 -0
- install_cuda_wsl2.sh +59 -0
- install_deepspeed.sh +56 -0
- install_deepspeed_simple.sh +87 -0
- mkdocs.yml +252 -0
- ollama/Modelfile +8 -0
- prompts/__init__.py +2 -1
- prompts/default_crca.py +9 -1
- prompts/general_agent.py +227 -0
- prompts/image_annotation.py +56 -0
- pyproject.toml +17 -2
- requirements-docs.txt +10 -0
- requirements.txt +21 -2
- schemas/__init__.py +26 -1
- schemas/annotation.py +222 -0
- schemas/conversation.py +193 -0
- schemas/hybrid.py +211 -0
- schemas/reasoning.py +276 -0
- schemas_export/crca_core/CounterfactualResult.schema.json +108 -0
- schemas_export/crca_core/DiscoveryHypothesisResult.schema.json +113 -0
- schemas_export/crca_core/DraftSpec.schema.json +635 -0
- schemas_export/crca_core/EstimateResult.schema.json +113 -0
- schemas_export/crca_core/IdentificationResult.schema.json +145 -0
- schemas_export/crca_core/InterventionDesignResult.schema.json +111 -0
- schemas_export/crca_core/LockedSpec.schema.json +646 -0
- schemas_export/crca_core/RefusalResult.schema.json +90 -0
- schemas_export/crca_core/ValidationReport.schema.json +62 -0
- scripts/build_lrm_dataset.py +80 -0
- scripts/export_crca_core_schemas.py +54 -0
- scripts/export_hf_lrm.py +37 -0
- scripts/export_ollama_gguf.py +45 -0
- scripts/generate_changelog.py +157 -0
- scripts/generate_crca_core_docs_from_schemas.py +86 -0
- scripts/run_crca_core_benchmarks.py +163 -0
- scripts/run_full_finetune.py +198 -0
- scripts/run_lrm_eval.py +31 -0
- templates/graph_management.py +29 -0
- tests/conftest.py +9 -0
- tests/test_core.py +2 -3
- tests/test_crca_core_discovery_tabular.py +15 -0
- tests/test_crca_core_estimate_dowhy.py +36 -0
- tests/test_crca_core_identify.py +18 -0
- tests/test_crca_core_intervention_design.py +36 -0
- tests/test_crca_core_linear_gaussian_scm.py +69 -0
- tests/test_crca_core_spec.py +25 -0
- tests/test_crca_core_timeseries_pcmci.py +15 -0
- tests/test_crca_llm_coauthor.py +12 -0
- tests/test_crca_llm_orchestrator.py +80 -0
- tests/test_hybrid_agent_llm_enhanced.py +556 -0
- tests/test_image_annotation_demo.py +376 -0
- tests/test_image_annotation_operational.py +408 -0
- tests/test_image_annotation_unit.py +551 -0
- tests/test_training_moe.py +13 -0
- training/__init__.py +42 -0
- training/datasets.py +140 -0
- training/deepspeed_zero2_0_5b.json +22 -0
- training/deepspeed_zero2_1_5b.json +22 -0
- training/deepspeed_zero3_0_5b.json +28 -0
- training/deepspeed_zero3_14b.json +28 -0
- training/deepspeed_zero3_h100_3gpu.json +20 -0
- training/deepspeed_zero3_offload.json +28 -0
- training/eval.py +92 -0
- training/finetune.py +516 -0
- training/public_datasets.py +89 -0
- training_data/react_train.jsonl +7473 -0
- utils/agent_discovery.py +311 -0
- utils/batch_processor.py +317 -0
- utils/conversation.py +78 -0
- utils/edit_distance.py +118 -0
- utils/formatter.py +33 -0
- utils/graph_reasoner.py +530 -0
- utils/rate_limiter.py +283 -0
- utils/router.py +2 -2
- utils/tool_discovery.py +307 -0
- webui/__init__.py +10 -0
- webui/app.py +229 -0
- webui/config.py +104 -0
- webui/static/css/style.css +332 -0
- webui/static/js/main.js +284 -0
- webui/templates/index.html +42 -0
- tests/test_crca_excel.py +0 -166
- tests/test_data_broker.py +0 -424
- tests/test_palantir.py +0 -349
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/WHEEL +0 -0
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Demo test - Real-world example: Planning an invasion using image annotation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import numpy as np
|
|
8
|
+
import pytest
|
|
9
|
+
import tempfile
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
import cv2
|
|
12
|
+
|
|
13
|
+
# Add parent directory to path
|
|
14
|
+
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
15
|
+
|
|
16
|
+
# Image annotation demo tests are extremely slow and may require LLM access.
|
|
17
|
+
# They are skipped by default; enable explicitly when needed.
|
|
18
|
+
if os.environ.get("CRCA_RUN_IMAGE_ANNOTATION_TESTS") != "1":
|
|
19
|
+
pytest.skip("Set CRCA_RUN_IMAGE_ANNOTATION_TESTS=1 to run image annotation demos", allow_module_level=True)
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
from image_annotation.annotation_engine import ImageAnnotationEngine
|
|
23
|
+
from CRCA import CRCAAgent
|
|
24
|
+
from schemas import AnnotationResult
|
|
25
|
+
IMAGE_ANNOTATION_AVAILABLE = True
|
|
26
|
+
except ImportError:
|
|
27
|
+
IMAGE_ANNOTATION_AVAILABLE = False
|
|
28
|
+
pytest.skip("Image annotation or CR-CA not available", allow_module_level=True)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def create_tactical_map(width=1000, height=1000):
|
|
32
|
+
"""Create a synthetic tactical map for invasion planning."""
|
|
33
|
+
img = np.zeros((height, width, 3), dtype=np.uint8)
|
|
34
|
+
|
|
35
|
+
# Background (terrain)
|
|
36
|
+
cv2.rectangle(img, (0, 0), (width, height), (34, 139, 34), -1) # Green terrain
|
|
37
|
+
|
|
38
|
+
# Rivers (blue lines)
|
|
39
|
+
cv2.line(img, (200, 0), (200, height), (0, 0, 255), 10)
|
|
40
|
+
cv2.line(img, (600, 0), (600, height), (0, 0, 255), 10)
|
|
41
|
+
|
|
42
|
+
# Roads (gray lines)
|
|
43
|
+
cv2.line(img, (0, 300), (width, 300), (128, 128, 128), 8)
|
|
44
|
+
cv2.line(img, (0, 700), (width, 700), (128, 128, 128), 8)
|
|
45
|
+
cv2.line(img, (400, 0), (400, height), (128, 128, 128), 8)
|
|
46
|
+
|
|
47
|
+
# Cities/Buildings (rectangles)
|
|
48
|
+
# Capital city (large)
|
|
49
|
+
cv2.rectangle(img, (800, 800), (950, 950), (139, 69, 19), -1) # Brown
|
|
50
|
+
cv2.rectangle(img, (800, 800), (950, 950), (255, 255, 255), 3)
|
|
51
|
+
|
|
52
|
+
# Secondary cities (medium)
|
|
53
|
+
cv2.rectangle(img, (100, 200), (200, 300), (139, 69, 19), -1)
|
|
54
|
+
cv2.rectangle(img, (100, 200), (200, 300), (255, 255, 255), 2)
|
|
55
|
+
|
|
56
|
+
cv2.rectangle(img, (700, 100), (850, 200), (139, 69, 19), -1)
|
|
57
|
+
cv2.rectangle(img, (700, 100), (850, 200), (255, 255, 255), 2)
|
|
58
|
+
|
|
59
|
+
# Military bases (circles with cross)
|
|
60
|
+
# Base 1
|
|
61
|
+
cv2.circle(img, (300, 500), 40, (255, 0, 0), 3)
|
|
62
|
+
cv2.line(img, (260, 500), (340, 500), (255, 0, 0), 2)
|
|
63
|
+
cv2.line(img, (300, 460), (300, 540), (255, 0, 0), 2)
|
|
64
|
+
|
|
65
|
+
# Base 2
|
|
66
|
+
cv2.circle(img, (750, 400), 40, (255, 0, 0), 3)
|
|
67
|
+
cv2.line(img, (710, 400), (790, 400), (255, 0, 0), 2)
|
|
68
|
+
cv2.line(img, (750, 360), (750, 440), (255, 0, 0), 2)
|
|
69
|
+
|
|
70
|
+
# Border (dashed line effect)
|
|
71
|
+
for i in range(0, width, 30):
|
|
72
|
+
cv2.line(img, (i, 0), (i+15, 0), (255, 255, 0), 5)
|
|
73
|
+
for i in range(0, height, 30):
|
|
74
|
+
cv2.line(img, (0, i), (0, i+15), (255, 255, 0), 5)
|
|
75
|
+
for i in range(0, width, 30):
|
|
76
|
+
cv2.line(img, (i, height-1), (i+15, height-1), (255, 255, 0), 5)
|
|
77
|
+
for i in range(0, height, 30):
|
|
78
|
+
cv2.line(img, (width-1, i), (width-1, i+15), (255, 255, 0), 5)
|
|
79
|
+
|
|
80
|
+
return img
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class TestInvasionPlanningDemo:
|
|
84
|
+
"""Real-world demo: Planning an invasion using image annotation and CR-CA."""
|
|
85
|
+
|
|
86
|
+
def test_tactical_map_analysis(self):
|
|
87
|
+
"""Step 1: Analyze tactical map to identify key targets."""
|
|
88
|
+
engine = ImageAnnotationEngine()
|
|
89
|
+
|
|
90
|
+
# Create tactical map
|
|
91
|
+
tactical_map = create_tactical_map()
|
|
92
|
+
|
|
93
|
+
# Save to temp file
|
|
94
|
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
|
95
|
+
cv2.imwrite(f.name, tactical_map)
|
|
96
|
+
map_path = f.name
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
# Annotate the tactical map
|
|
100
|
+
result = engine.annotate(map_path, output="all")
|
|
101
|
+
|
|
102
|
+
assert isinstance(result, AnnotationResult)
|
|
103
|
+
assert len(result.annotation_graph.entities) > 0
|
|
104
|
+
|
|
105
|
+
# Extract key information
|
|
106
|
+
entities = result.annotation_graph.entities
|
|
107
|
+
labels = result.annotation_graph.labels
|
|
108
|
+
|
|
109
|
+
# Should have detected various primitives
|
|
110
|
+
entity_types = [e.primitive_type for e in entities]
|
|
111
|
+
assert "line" in entity_types or "circle" in entity_types or "contour" in entity_types
|
|
112
|
+
|
|
113
|
+
finally:
|
|
114
|
+
if os.path.exists(map_path):
|
|
115
|
+
os.unlink(map_path)
|
|
116
|
+
|
|
117
|
+
def test_identify_targets(self):
|
|
118
|
+
"""Step 2: Identify military targets and cities."""
|
|
119
|
+
engine = ImageAnnotationEngine()
|
|
120
|
+
|
|
121
|
+
tactical_map = create_tactical_map()
|
|
122
|
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
|
123
|
+
cv2.imwrite(f.name, tactical_map)
|
|
124
|
+
map_path = f.name
|
|
125
|
+
|
|
126
|
+
try:
|
|
127
|
+
# Query for largest city (capital)
|
|
128
|
+
capital_result = engine.query(map_path, "find the largest building or structure")
|
|
129
|
+
|
|
130
|
+
assert isinstance(capital_result, dict)
|
|
131
|
+
assert "entities" in capital_result
|
|
132
|
+
assert "answer" in capital_result
|
|
133
|
+
|
|
134
|
+
# Query for military bases (circles)
|
|
135
|
+
bases_result = engine.query(map_path, "find all circles and identify military installations")
|
|
136
|
+
|
|
137
|
+
assert isinstance(bases_result, dict)
|
|
138
|
+
assert "entities" in bases_result
|
|
139
|
+
|
|
140
|
+
# Query for roads (infrastructure)
|
|
141
|
+
roads_result = engine.query(map_path, "identify all major lines that could be roads or paths")
|
|
142
|
+
|
|
143
|
+
assert isinstance(roads_result, dict)
|
|
144
|
+
|
|
145
|
+
finally:
|
|
146
|
+
if os.path.exists(map_path):
|
|
147
|
+
os.unlink(map_path)
|
|
148
|
+
|
|
149
|
+
def test_measure_critical_distances(self):
|
|
150
|
+
"""Step 3: Measure distances between key targets."""
|
|
151
|
+
engine = ImageAnnotationEngine()
|
|
152
|
+
|
|
153
|
+
tactical_map = create_tactical_map()
|
|
154
|
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
|
155
|
+
cv2.imwrite(f.name, tactical_map)
|
|
156
|
+
map_path = f.name
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
# Measure distance from border to capital
|
|
160
|
+
distance_result = engine.query(
|
|
161
|
+
map_path,
|
|
162
|
+
"measure the distance from the border to the largest city"
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
assert isinstance(distance_result, dict)
|
|
166
|
+
assert "measurements" in distance_result
|
|
167
|
+
|
|
168
|
+
# Measure size of military bases
|
|
169
|
+
base_size_result = engine.query(
|
|
170
|
+
map_path,
|
|
171
|
+
"find all circles and measure their areas and distances from each other"
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
assert isinstance(base_size_result, dict)
|
|
175
|
+
|
|
176
|
+
finally:
|
|
177
|
+
if os.path.exists(map_path):
|
|
178
|
+
os.unlink(map_path)
|
|
179
|
+
|
|
180
|
+
def test_crca_strategic_analysis(self):
|
|
181
|
+
"""Step 4: Use CR-CA agent for strategic causal analysis."""
|
|
182
|
+
# Create CR-CA agent with image annotation tools
|
|
183
|
+
agent = CRCAAgent(
|
|
184
|
+
model_name="gpt-4o-mini",
|
|
185
|
+
use_image_annotation=True,
|
|
186
|
+
use_crca_tools=True
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
tactical_map = create_tactical_map()
|
|
190
|
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
|
191
|
+
cv2.imwrite(f.name, tactical_map)
|
|
192
|
+
map_path = f.name
|
|
193
|
+
|
|
194
|
+
try:
|
|
195
|
+
# Task: Analyze tactical map and plan invasion strategy
|
|
196
|
+
task = f"""
|
|
197
|
+
Analyze this tactical map located at {map_path} and develop an invasion strategy.
|
|
198
|
+
|
|
199
|
+
Steps:
|
|
200
|
+
1. Use query_image to identify the largest city (capital) and measure its defenses
|
|
201
|
+
2. Use query_image to locate all military bases and measure their sizes
|
|
202
|
+
3. Use query_image to identify all roads and rivers (chokepoints)
|
|
203
|
+
4. Extract causal variables for invasion planning:
|
|
204
|
+
- Distance from border to capital
|
|
205
|
+
- Number and size of military bases
|
|
206
|
+
- Road network connectivity
|
|
207
|
+
- River crossings required
|
|
208
|
+
5. Perform causal analysis:
|
|
209
|
+
- What factors affect invasion success?
|
|
210
|
+
- What are the critical chokepoints?
|
|
211
|
+
- What is the optimal approach route?
|
|
212
|
+
|
|
213
|
+
Provide a comprehensive invasion plan with causal reasoning.
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
# Run agent (this would normally call the tools)
|
|
217
|
+
# For testing, we'll just verify the agent is set up correctly
|
|
218
|
+
assert agent is not None
|
|
219
|
+
assert hasattr(agent, 'tools') or hasattr(agent, 'tools_list_dictionary')
|
|
220
|
+
|
|
221
|
+
finally:
|
|
222
|
+
if os.path.exists(map_path):
|
|
223
|
+
os.unlink(map_path)
|
|
224
|
+
|
|
225
|
+
def test_integrated_invasion_planning(self):
|
|
226
|
+
"""Complete integrated test: Full invasion planning workflow."""
|
|
227
|
+
# Initialize both systems
|
|
228
|
+
annotation_engine = ImageAnnotationEngine()
|
|
229
|
+
strategic_agent = CRCAAgent(
|
|
230
|
+
model_name="gpt-4o-mini",
|
|
231
|
+
use_image_annotation=True,
|
|
232
|
+
use_crca_tools=True
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
# Create tactical map
|
|
236
|
+
tactical_map = create_tactical_map()
|
|
237
|
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
|
238
|
+
cv2.imwrite(f.name, tactical_map)
|
|
239
|
+
map_path = f.name
|
|
240
|
+
|
|
241
|
+
try:
|
|
242
|
+
# Phase 1: Intelligence Gathering (Image Annotation)
|
|
243
|
+
intel_report = annotation_engine.query(
|
|
244
|
+
map_path,
|
|
245
|
+
"identify all military installations, cities, roads, and rivers. Measure distances between key targets."
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
assert isinstance(intel_report, dict)
|
|
249
|
+
assert "entities" in intel_report
|
|
250
|
+
assert "measurements" in intel_report
|
|
251
|
+
|
|
252
|
+
# Extract key metrics
|
|
253
|
+
num_targets = len(intel_report["entities"])
|
|
254
|
+
has_measurements = len(intel_report["measurements"]) > 0
|
|
255
|
+
|
|
256
|
+
# Phase 2: Strategic Analysis (CR-CA)
|
|
257
|
+
# Build causal model for invasion planning
|
|
258
|
+
strategic_agent.add_causal_relationship("distance_to_capital", "invasion_difficulty")
|
|
259
|
+
strategic_agent.add_causal_relationship("number_of_bases", "defense_strength")
|
|
260
|
+
strategic_agent.add_causal_relationship("defense_strength", "invasion_difficulty")
|
|
261
|
+
strategic_agent.add_causal_relationship("road_connectivity", "mobility")
|
|
262
|
+
strategic_agent.add_causal_relationship("mobility", "invasion_success")
|
|
263
|
+
|
|
264
|
+
# Set initial state
|
|
265
|
+
initial_state = {
|
|
266
|
+
"distance_to_capital": 200.0, # pixels (from intel)
|
|
267
|
+
"number_of_bases": 2.0, # from intel
|
|
268
|
+
"defense_strength": 0.0, # will be computed
|
|
269
|
+
"road_connectivity": 0.7, # estimated
|
|
270
|
+
"mobility": 0.0, # will be computed
|
|
271
|
+
"invasion_difficulty": 0.0, # will be computed
|
|
272
|
+
"invasion_success": 0.0 # will be computed
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
# Standardize stats
|
|
276
|
+
strategic_agent.set_standardization_stats("distance_to_capital", mean=200.0, std=100.0)
|
|
277
|
+
strategic_agent.set_standardization_stats("number_of_bases", mean=2.0, std=1.0)
|
|
278
|
+
strategic_agent.set_standardization_stats("defense_strength", mean=0.5, std=0.3)
|
|
279
|
+
strategic_agent.set_standardization_stats("road_connectivity", mean=0.5, std=0.2)
|
|
280
|
+
strategic_agent.set_standardization_stats("mobility", mean=0.5, std=0.3)
|
|
281
|
+
strategic_agent.set_standardization_stats("invasion_difficulty", mean=0.5, std=0.3)
|
|
282
|
+
strategic_agent.set_standardization_stats("invasion_success", mean=0.5, std=0.3)
|
|
283
|
+
|
|
284
|
+
# Predict outcomes
|
|
285
|
+
outcomes = strategic_agent.predict_outcomes(initial_state, {})
|
|
286
|
+
|
|
287
|
+
assert isinstance(outcomes, dict)
|
|
288
|
+
assert "invasion_success" in outcomes
|
|
289
|
+
assert "invasion_difficulty" in outcomes
|
|
290
|
+
|
|
291
|
+
# Phase 3: Counterfactual Analysis
|
|
292
|
+
# What if we had better mobility?
|
|
293
|
+
intervention = {"road_connectivity": 0.9}
|
|
294
|
+
counterfactual = strategic_agent.predict_outcomes(initial_state, intervention)
|
|
295
|
+
|
|
296
|
+
assert isinstance(counterfactual, dict)
|
|
297
|
+
# Better mobility should improve invasion success
|
|
298
|
+
if counterfactual.get("invasion_success", 0) > outcomes.get("invasion_success", 0):
|
|
299
|
+
assert True # Expected improvement
|
|
300
|
+
|
|
301
|
+
# Phase 4: Generate Strategic Report
|
|
302
|
+
report = f"""
|
|
303
|
+
INVASION PLANNING REPORT
|
|
304
|
+
========================
|
|
305
|
+
|
|
306
|
+
Intelligence Summary:
|
|
307
|
+
- Targets Identified: {num_targets}
|
|
308
|
+
- Measurements Available: {has_measurements}
|
|
309
|
+
|
|
310
|
+
Causal Analysis:
|
|
311
|
+
- Base Invasion Success Probability: {outcomes.get('invasion_success', 0):.2f}
|
|
312
|
+
- Invasion Difficulty: {outcomes.get('invasion_difficulty', 0):.2f}
|
|
313
|
+
|
|
314
|
+
Counterfactual Scenario (Improved Mobility):
|
|
315
|
+
- Improved Success Probability: {counterfactual.get('invasion_success', 0):.2f}
|
|
316
|
+
- Improvement: {counterfactual.get('invasion_success', 0) - outcomes.get('invasion_success', 0):.2f}
|
|
317
|
+
|
|
318
|
+
Recommendation:
|
|
319
|
+
- Focus on securing road networks to improve mobility
|
|
320
|
+
- Target priority: Largest city and military bases identified
|
|
321
|
+
"""
|
|
322
|
+
|
|
323
|
+
assert len(report) > 0
|
|
324
|
+
assert "INVASION PLANNING REPORT" in report
|
|
325
|
+
|
|
326
|
+
finally:
|
|
327
|
+
if os.path.exists(map_path):
|
|
328
|
+
os.unlink(map_path)
|
|
329
|
+
|
|
330
|
+
def test_multi_phase_operation(self):
|
|
331
|
+
"""Test multi-phase operation with temporal tracking."""
|
|
332
|
+
engine = ImageAnnotationEngine(enable_temporal_tracking=True)
|
|
333
|
+
|
|
334
|
+
# Create sequence of tactical maps (simulating movement)
|
|
335
|
+
maps = []
|
|
336
|
+
temp_files = []
|
|
337
|
+
|
|
338
|
+
for phase in range(3):
|
|
339
|
+
# Create map with different positions
|
|
340
|
+
tactical_map = create_tactical_map()
|
|
341
|
+
# Add phase marker
|
|
342
|
+
cv2.putText(tactical_map, f"Phase {phase+1}", (50, 50),
|
|
343
|
+
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)
|
|
344
|
+
|
|
345
|
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
|
346
|
+
cv2.imwrite(f.name, tactical_map)
|
|
347
|
+
maps.append(f.name)
|
|
348
|
+
temp_files.append(f.name)
|
|
349
|
+
|
|
350
|
+
try:
|
|
351
|
+
# Process each phase with temporal tracking
|
|
352
|
+
results = []
|
|
353
|
+
for i, map_path in enumerate(maps):
|
|
354
|
+
result = engine.annotate(map_path, frame_id=i)
|
|
355
|
+
results.append(result)
|
|
356
|
+
|
|
357
|
+
# Query for target tracking
|
|
358
|
+
if i > 0:
|
|
359
|
+
query_result = engine.query(
|
|
360
|
+
map_path,
|
|
361
|
+
"identify changes in military base positions compared to previous phase"
|
|
362
|
+
)
|
|
363
|
+
assert isinstance(query_result, dict)
|
|
364
|
+
|
|
365
|
+
# Should have processed all phases
|
|
366
|
+
assert len(results) == 3
|
|
367
|
+
assert all(isinstance(r, AnnotationResult) for r in results)
|
|
368
|
+
|
|
369
|
+
finally:
|
|
370
|
+
for f in temp_files:
|
|
371
|
+
if os.path.exists(f):
|
|
372
|
+
os.unlink(f)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
if __name__ == "__main__":
|
|
376
|
+
pytest.main([__file__, "-v", "-s"])
|