code: def test_filter_by_rule():
      rules_file_contents = (
          "IF (%s > 2.0, [1.0,-1.0], [-1.0,1.0])\n"
          % ("_".join(("Image", image_measurement_name(0))))
      )
      rules_path = tempfile.mktemp()
      rules_dir, rules_file = os.path.split(rules_path)
      fd = open(rules_path, "wt")
      try:
          fd.write(rules_file_contents)
          fd.close()
          for value, choice, expected in (
              (1.0, 1, 0),
              (3.0, 1, 1),
              (1.0, 2, 1),
              (3.0, 2, 0),
          ):
              module, workspace = make_workspace([value], [])
              flag = module.flags[0]
              assert isinstance(
                  flag, cellprofiler_core.setting.SettingsGroup
              )
              flag.wants_skip.value = False
              measurement = flag.measurement_settings[0]
              assert isinstance(
                  measurement, cellprofiler_core.setting.SettingsGroup
              )
              measurement.source_choice.value = (
                  cellprofiler.modules.flagimage.S_RULES
              )
              measurement.rules_file_name.value = rules_file
              measurement.rules_directory.dir_choice = (
                  cellprofiler_core.preferences.ABSOLUTE_FOLDER_NAME
              )
              measurement.rules_directory.custom_path = rules_dir
              measurement.rules_class.set_value([str(choice)])
              module.run(workspace)
              m = workspace.measurements
              assert isinstance(
                  m, cellprofiler_core.measurement.Measurements
              )
              assert MEASUREMENT_NAME in m.get_feature_names("Image")
              assert (
                  m.get_current_image_measurement(MEASUREMENT_NAME)
                  == expected
              )
      finally:
          os.remove(rules_path)
  
imports: import contextlib
  import os
  import tempfile
  import numpy
  import pytest
  import six.moves
  from cellprofiler_core.constants.measurement import COLTYPE_INTEGER
  from cellprofiler_core.constants.workspace import DISPOSITION_CONTINUE, DISPOSITION_SKIP
  import tests.frontend.modules
  import cellprofiler_core.image
  import cellprofiler_core.measurement
  import cellprofiler.modules.flagimage
  import cellprofiler_core.object
  import cellprofiler_core.pipeline
  import cellprofiler_core.preferences
  import cellprofiler_core.setting
  import cellprofiler_core.workspace
  from .None import test_filterobjects
label_desc: Use image node_id 21 for calling a global function with example usage: test_filter_by_rule() without return types