diff --git a/openhcs/core/orchestrator/orchestrator.py b/openhcs/core/orchestrator/orchestrator.py index 79752d3746..cf113b966c 100644 --- a/openhcs/core/orchestrator/orchestrator.py +++ b/openhcs/core/orchestrator/orchestrator.py @@ -673,13 +673,18 @@ def create_context( return context - def source_workspace_projection(self): + def source_workspace_projection( + self, *, resolved_config: GlobalPipelineConfig | None = None + ): """Return the canonical resolved source state for the initialized plate. The projection carries every named alias, sample/well, site, channel, Z, timepoint, and backend-owned pixel reference used by compilation, runtime, and UI inspection. Consumers must query this view rather than constructing a second metadata model. + + Compilation supplies its already-resolved configuration; live inspection + resolves the current saved configuration at this query boundary. """ if not self.is_initialized(): raise RuntimeError( @@ -699,7 +704,10 @@ def source_workspace_projection(self): metadata_handler=self.microscope_handler.metadata_handler, filemanager=self.filemanager, source_bindings=source_bindings_defaults_to_base( - self.get_effective_config().source_bindings_config + ( + self.get_effective_config() + if resolved_config is None else resolved_config + ).source_bindings_config ), ).projection_if_available() if projection is not None: @@ -801,6 +809,8 @@ def get_component_keys( self, component: Union["AllComponents", "VariableComponents"], component_filter: Optional[List[Union[str, int]]] = None, + *, + resolved_config: GlobalPipelineConfig | None = None, ) -> List[str]: """ Generic method to get component keys using VariableComponents directly. @@ -814,6 +824,8 @@ def get_component_keys( component: AllComponents or VariableComponents enum specifying which component to extract (also accepts GroupBy enum which will be converted to AllComponents) component_filter: Optional list of component values to filter by + resolved_config: Existing compilation configuration. Omit for live + inspection of the current saved declaration. Returns: List of component values as strings, sorted @@ -833,8 +845,11 @@ def get_component_keys( # Convert to AllComponents for cache lookup (includes multiprocessing axis) component = convert_enum_by_value(component, AllComponents) or component + effective_config = ( + self.get_effective_config() if resolved_config is None else resolved_config + ) source_bindings = source_bindings_defaults_to_base( - self.get_effective_config().source_bindings_config + effective_config.source_bindings_config ) # Use component directly - let natural errors occur for wrong types component_name = component.value @@ -842,7 +857,11 @@ def get_component_keys( # Try metadata cache first (preferred source) cached_metadata = self._metadata_cache_service.get_cached_metadata(component) if source_bindings.source_filter_declarations: - all_components = list(self.source_workspace_projection().component_values(component)) + all_components = list( + self.source_workspace_projection( + resolved_config=effective_config + ).component_values(component) + ) elif cached_metadata: all_components = list(cached_metadata.keys()) logger.debug( diff --git a/openhcs/core/pipeline/compiler.py b/openhcs/core/pipeline/compiler.py index c388768027..53d55a85a6 100644 --- a/openhcs/core/pipeline/compiler.py +++ b/openhcs/core/pipeline/compiler.py @@ -389,7 +389,8 @@ def _compile_sequential_runtime_filter_plan( for seq_component in seq_config.sequential_components if len( session.orchestrator.get_component_keys( - AllComponents(seq_component.value) + AllComponents(seq_component.value), + resolved_config=session.global_config, ) ) > 1 @@ -504,7 +505,9 @@ def declare_zarr_stores(session: CompilationSession) -> None: context = session.context orchestrator = session.orchestrator - all_wells = orchestrator.get_component_keys(get_multiprocessing_axis()) + all_wells = orchestrator.get_component_keys( + get_multiprocessing_axis(), resolved_config=session.global_config + ) # Access config from merged config (pipeline + global) for proper inheritance vfs_config = session.global_config.vfs_config @@ -690,7 +693,9 @@ def analyze_pipeline_sequential_mode( component_enum = AllComponents(seq_comp) # Get component values from orchestrator's cache (populated from filename parsing) - component_values = orchestrator.get_component_keys(component_enum) + component_values = orchestrator.get_component_keys( + component_enum, resolved_config=global_config + ) if not component_values: logger.warning(f"No {seq_comp} values found in orchestrator cache") @@ -907,7 +912,7 @@ def _axis_values_to_process( ) if well_filter_config and well_filter_config.well_filter is not None: available_wells = orchestrator.get_component_keys( - get_multiprocessing_axis() + get_multiprocessing_axis(), resolved_config=effective_config ) resolved_wells = WellFilterProcessor.resolve_filter_with_mode( well_filter_config.well_filter, @@ -931,6 +936,7 @@ def _axis_values_to_process( return orchestrator.get_component_keys( get_multiprocessing_axis(), resolved_axis_filter, + resolved_config=effective_config, ) @staticmethod @@ -994,6 +1000,7 @@ def _resolve_global_step_axis_filters( pipeline, temp_context, orchestrator, + global_config, ) return temp_context.step_axis_filters @@ -1657,11 +1664,13 @@ def compile_pipelines( filemanager=orchestrator.filemanager, input_dir=orchestrator.input_dir, available_axis_values=orchestrator.get_component_keys( - get_multiprocessing_axis() + get_multiprocessing_axis(), resolved_config=effective_config ), ), source_projections_by_axis=DEFAULT_SOURCE_PROJECTION_CACHE.partition_by_axes( - orchestrator.source_workspace_projection(), + orchestrator.source_workspace_projection( + resolved_config=effective_config + ), axis_ids=axis_values_to_process, ), enable_visualizer_override=enable_visualizer_override, @@ -1734,6 +1743,7 @@ def _resolve_step_axis_filters( pipeline: ResolvedPipelineDefinition, context, orchestrator, + global_config: GlobalPipelineConfig, ): """ Resolve axis filters for steps with any WellFilterConfig instances. @@ -1750,7 +1760,9 @@ def _resolve_step_axis_filters( # Get available axis values from orchestrator using multiprocessing axis - available_axis_values = orchestrator.get_component_keys(get_multiprocessing_axis()) + available_axis_values = orchestrator.get_component_keys( + get_multiprocessing_axis(), resolved_config=global_config + ) if not available_axis_values: logger.warning("No available axis values found for axis filter resolution") return diff --git a/openhcs/core/pipeline/path_planner.py b/openhcs/core/pipeline/path_planner.py index 03caeed86f..0e9ae528a1 100644 --- a/openhcs/core/pipeline/path_planner.py +++ b/openhcs/core/pipeline/path_planner.py @@ -292,7 +292,9 @@ def context_producer_scope( if source_component is None: return PathPlannerGroupScope.ungrouped() source_keys = tuple( - self.planner.orchestrator.get_component_keys(source_component) + self.planner.orchestrator.get_component_keys( + source_component, resolved_config=self.planner.session.global_config + ) ) return ( PathPlannerGroupScope.from_raw(source_keys, component=source_component) @@ -504,7 +506,12 @@ def dynamic_execution_scope_for_group_by( if group_by_component is None: return PathPlannerGroupScope.ungrouped() source_keys = ( - tuple(self.planner.orchestrator.get_component_keys(group_by_component)) + tuple( + self.planner.orchestrator.get_component_keys( + group_by_component, + resolved_config=self.planner.session.global_config, + ) + ) if step.processing_config.input_source is InputSource.PIPELINE_START else () ) diff --git a/tests/unit/test_materialization_filter_inheritance.py b/tests/unit/test_materialization_filter_inheritance.py index 6cacfd9b3f..3b61dc523d 100644 --- a/tests/unit/test_materialization_filter_inheritance.py +++ b/tests/unit/test_materialization_filter_inheritance.py @@ -43,7 +43,7 @@ def _resolved_checkpoint( orchestrator = SimpleNamespace( plate_path=plate_path, pipeline_config=pipeline_config, - get_component_keys=lambda _component: ["A01", "B02"], + get_component_keys=lambda _component, *, resolved_config: ["A01", "B02"], create_context=lambda axis_id, *, resolved_config: SimpleNamespace( axis_id=axis_id, step_axis_filters={}, diff --git a/tests/unit/test_microscope_virtual_workspace_metadata.py b/tests/unit/test_microscope_virtual_workspace_metadata.py index 8e608a3ec4..09dfaff244 100644 --- a/tests/unit/test_microscope_virtual_workspace_metadata.py +++ b/tests/unit/test_microscope_virtual_workspace_metadata.py @@ -2,7 +2,7 @@ import json import pytest -from dataclasses import dataclass +from dataclasses import dataclass, replace from pathlib import Path from types import SimpleNamespace @@ -63,6 +63,88 @@ def test_virtual_workspace_metadata_records_parser_owned_axis_values( ) +def test_compiler_source_queries_borrow_config_while_public_queries_stay_live( + tmp_path, monkeypatch +): + from unittest.mock import Mock + from objectstate.lazy_factory import ensure_global_config_context + from openhcs.core.config import GlobalPipelineConfig, PipelineConfig + from openhcs.core.orchestrator.orchestrator import PipelineOrchestrator + from openhcs.core.source_bindings import ( + SourceBindingsConfig, + LazySourceBindingsConfig, + SourceFilterClause, + SourceFilterMatchType, + SourceFilterSubject, + ) + + source_handler = OperaPhenixHandler(SimpleNamespace()) + (tmp_path / "Images").mkdir() + monkeypatch.setattr( + source_handler.metadata_handler, "get_grid_dimensions", lambda _: (3, 3) + ) + monkeypatch.setattr(source_handler.metadata_handler, "get_pixel_size", lambda _: 1.0) + source_handler.save_virtual_workspace_metadata( + tmp_path, + { + "Images/r01c01f001p001-ch1sk1fk1fl1.tiff": SourcePixelRef( + "disk", "/source/source-a.tiff" + ), + "Images/r02c03f001p001-ch1sk1fk1fl1.tiff": SourcePixelRef( + "disk", "/source/source-b.tiff" + ), + }, + ) + metadata_path = tmp_path / "openhcs_metadata.json" + metadata_document = json.loads(metadata_path.read_text()) + subdirectory = metadata_document[FIELDS.SUBDIRECTORIES]["Images"] + subdirectory[FIELDS.IMAGE_FILES] = list(subdirectory[FIELDS.WORKSPACE_MAPPING]) + metadata_path.write_text(json.dumps(metadata_document)) + source_a = SourceBindingsConfig(source_filters=( + SourceFilterClause( + SourceFilterSubject.FILE, SourceFilterMatchType.CONTAINS, "source-a" + ), + )) + source_b = SourceBindingsConfig(source_filters=( + SourceFilterClause( + SourceFilterSubject.FILE, SourceFilterMatchType.CONTAINS, "source-b" + ), + )) + held_config = GlobalPipelineConfig(source_bindings_config=source_a) + ensure_global_config_context(GlobalPipelineConfig, held_config) + owner = PipelineOrchestrator( + tmp_path, + pipeline_config=PipelineConfig( + source_bindings_config=LazySourceBindingsConfig( + source_filters=source_a.source_filters + ) + ), + resolved_config=held_config, + ) + owner.initialize(resolved_config=held_config) + live_config = Mock(wraps=owner.get_effective_config) + monkeypatch.setattr(owner, "get_effective_config", live_config) + + assert owner.get_component_keys( + AllComponents.WELL, resolved_config=held_config + ) == ["R01C01"] + live_config.assert_not_called() + owner.pipeline_config = replace( + owner.pipeline_config, + source_bindings_config=LazySourceBindingsConfig( + source_filters=source_b.source_filters + ), + ) + owner.initialize() + live_config.reset_mock() + assert owner.get_component_keys(AllComponents.WELL) == ["R02C03"] + assert live_config.call_count == 1 + assert owner.get_component_keys( + AllComponents.WELL, resolved_config=held_config + ) == ["R01C01"] + assert live_config.call_count == 1 + + @pytest.mark.parametrize( "spacing", [ diff --git a/tests/unit/test_path_planner_materialization.py b/tests/unit/test_path_planner_materialization.py index fde898165e..41e10ba59b 100644 --- a/tests/unit/test_path_planner_materialization.py +++ b/tests/unit/test_path_planner_materialization.py @@ -260,7 +260,7 @@ def _artifact_planner_stub() -> PathPlanner: ), ) planner.orchestrator = SimpleNamespace( - get_component_keys=lambda _component: (), + get_component_keys=lambda _component, *, resolved_config: (), ) planner.plans = { 2: CompiledStepPlan( @@ -3757,7 +3757,7 @@ def test_execution_groups_resolve_non_grouped_variable_component_conflicts(): def test_non_dict_group_by_declares_dynamic_scope_without_plate_key_lookup(): planner = _artifact_planner_stub() planner.orchestrator = SimpleNamespace( - get_component_keys=lambda group_by: pytest.fail( + get_component_keys=lambda group_by, *, resolved_config: pytest.fail( "non-dict group_by must not request plate component keys" ) )