Repository: Project-MONAI/MONAI
File: tests/test_bundle_workflow.py (Python)

Code before:
            "dataset#data": [{"image": self.filename}],
            "postprocessing#transforms#2#output_postfix": "seg",
            "output_dir": self.data_dir,
        }
        # test standard MONAI model-zoo config workflow
        inferer = ConfigWorkflow(
            workflow_type="infer",
            config_file=config_file,
            logging_file=os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf"),
            **override,
        )
        self._test_inferer(inferer)

        # test property path
        inferer = ConfigWorkflow(
            config_file=config_file,
            properties_path=os.path.join(os.path.dirname(__file__), "testing_data", "fl_infer_properties.json"),
            logging_file=os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf"),
            **override,
        )
        self._test_inferer(inferer)
        self.assertEqual(inferer.workflow_type, None)

    @parameterized.expand([TEST_CASE_4])
    def test_realtime_inference_config(self, config_file):
        input_loader = LoadImaged(keys="image")
        output_saver = SaveImaged(keys="pred", output_dir=self.data_dir, output_postfix="seg")

        # test standard MONAI model-zoo config workflow
        inferer = ConfigWorkflow(
            workflow_type="infer",
            config_file=config_file,
            logging_file=os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf")
        )
        # FIXME: temp add the property for test, we should add it to some formal realtime infer properties
        inferer.add_property(name="dataflow", required=True, config_id="dataflow")

        inferer.initialize()
        inferer.dataflow.update(input_loader({"image": self.filename}))
        inferer.run()
        output_saver(inferer.dataflow)
        self.assertTrue(os.path.exists(os.path.join(self.data_dir, "image", "image_seg.nii.gz")))

        # bundle is instantiated and idle, just change the input for next inference
        inferer.dataflow.clear()
        inferer.dataflow.update(input_loader({"image": self.filename1}))
        inferer.run()
        output_saver(inferer.dataflow)
        self.assertTrue(os.path.exists(os.path.join(self.data_dir, "image1", "image1_seg.nii.gz")))

        inferer.finalize()

Review comment (suggestion):
Hi @Nic-Ma ,
   When using `LoadImage` , all the meta data is correct for the consequencial preprocessing. However, in a streaming environment, sometimes images are loading from other libraries, how could we ensure that all meta data is ready for using?

Thanks,  
Bin

Code after:
            "dataset#data": [{"image": self.filename}],
            "postprocessing#transforms#2#output_postfix": "seg",
            "output_dir": self.data_dir,
        }
        # test standard MONAI model-zoo config workflow
        inferer = ConfigWorkflow(
            workflow_type="infer",
            config_file=config_file,
            logging_file=os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf"),
            **override,
        )
        self._test_inferer(inferer)

        # test property path
        inferer = ConfigWorkflow(
            config_file=config_file,
            properties_path=os.path.join(os.path.dirname(__file__), "testing_data", "fl_infer_properties.json"),
            logging_file=os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf"),
            **override,
        )
        self._test_inferer(inferer)
        self.assertEqual(inferer.workflow_type, None)

    @parameterized.expand([TEST_CASE_4])
    def test_responsive_inference_config(self, config_file):
        input_loader = LoadImaged(keys="image")
        output_saver = SaveImaged(keys="pred", output_dir=self.data_dir, output_postfix="seg")

        # test standard MONAI model-zoo config workflow
        inferer = ConfigWorkflow(
            workflow_type="infer",
            config_file=config_file,
            logging_file=os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf"),
        )
        # FIXME: temp add the property for test, we should add it to some formal realtime infer properties
        inferer.add_property(name="dataflow", required=True, config_id="dataflow")

        inferer.initialize()
        inferer.dataflow.update(input_loader({"image": self.filename}))
        inferer.run()
        output_saver(inferer.dataflow)
        self.assertTrue(os.path.exists(os.path.join(self.data_dir, "image", "image_seg.nii.gz")))

        # bundle is instantiated and idle, just change the input for next inference
        inferer.dataflow.clear()
        inferer.dataflow.update(input_loader({"image": self.filename1}))
        inferer.run()
        output_saver(inferer.dataflow)
        self.assertTrue(os.path.exists(os.path.join(self.data_dir, "image1", "image1_seg.nii.gz")))

        inferer.finalize()