示例#1
0
    def test_clone_trait(self):
        """ Method to test trait clone from string description.
        """
        # Test first to build trait description from nipype traits and then
        # to instanciate the trait
        to_test_fields = {
            "timing_units": "traits.Enum(('secs', 'scans'))",
            "bases": ("traits.Dict(traits.Enum(('hrf', 'fourier', "
                      "'fourier_han', 'gamma', 'fir')), traits.Any())"),
            "mask_image": "traits.File(Undefined)",
            "microtime_onset": "traits.Float()",
            "mask_threshold": ("traits.Either(traits.Enum(('-Inf',)), "
                               "traits.Float())")
        }
        i = spm.Level1Design()
        for field, result in six.iteritems(to_test_fields):

            # Test to build the trait expression
            trait = i.inputs.trait(field)
            expression = build_expression(trait)
            self.assertEqual(expression, result)

            # Try to clone the trait
            trait = eval_trait(expression)()
            self.assertEqual(build_expression(trait), result)

        to_test_fields = {
            "contrasts": (
                "traits.List(traits.Either(traits.Tuple(traits.Str(), "
                "traits.Enum(('T',)), traits.List(traits.Str()), "
                "traits.List(traits.Float())), traits.Tuple(traits.Str(), "
                "traits.Enum(('T',)), traits.List(traits.Str()), "
                "traits.List(traits.Float()), traits.List(traits.Float())), "
                "traits.Tuple(traits.Str(), traits.Enum(('F',)), "
                "traits.List(traits.Either(traits.Tuple(traits.Str(), "
                "traits.Enum(('T',)), traits.List(traits.Str()), "
                "traits.List(traits.Float())), traits.Tuple(traits.Str(), "
                "traits.Enum(('T',)), traits.List(traits.Str()), "
                "traits.List(traits.Float()), traits.List(traits.Float())"
                "))))))"),
            "use_derivs": "traits.Bool()"
        }
        i = spm.EstimateContrast()
        for field, result in six.iteritems(to_test_fields):

            # Test to build the trait expression
            trait = i.inputs.trait(field)
            expression = build_expression(trait)
            self.assertEqual(expression, result)

            # Try to clone the trait
            trait = eval_trait(expression)()
            self.assertEqual(build_expression(trait), result)

        # Test to clone some traits
        trait_description = ["Float", "Int"]
        handler = clone_trait(trait_description)
        trait = handler.as_ctrait()
        self.assertEqual(trait_description, trait_ids(trait))
示例#2
0
    def test_clone_trait(self):
        """ Method to test trait clone from string description.
        """
        # Test first to build trait description from nipype traits and then
        # to instanciate the trait
        to_test_fields = {
            "timing_units":
            "traits.Enum(('secs', 'scans'))",
            "bases": ("traits.Dict(traits.Enum(('hrf', 'fourier', "
                      "'fourier_han', 'gamma', 'fir')), traits.Any())"),
            "mask_image":
            "traits.File(Undefined)",
            "microtime_onset":
            "traits.Float()",
            "mask_threshold": ("traits.Either(traits.Enum(('-Inf',)), "
                               "traits.Float())")
        }
        i = spm.Level1Design()
        for field, result in six.iteritems(to_test_fields):

            # Test to build the trait expression
            trait = i.inputs.trait(field)
            expression = build_expression(trait)
            self.assertEqual(expression, result)

            # Try to clone the trait
            trait = eval_trait(expression)()
            self.assertEqual(build_expression(trait), result)

        to_test_fields = {
            "contrasts":
            ("traits.List(traits.Either(traits.Tuple(traits.Str(), "
             "traits.Enum(('T',)), traits.List(traits.Str()), "
             "traits.List(traits.Float())), traits.Tuple(traits.Str(), "
             "traits.Enum(('T',)), traits.List(traits.Str()), "
             "traits.List(traits.Float()), traits.List(traits.Float())), "
             "traits.Tuple(traits.Str(), traits.Enum(('F',)), "
             "traits.List(traits.Either(traits.Tuple(traits.Str(), "
             "traits.Enum(('T',)), traits.List(traits.Str()), "
             "traits.List(traits.Float())), traits.Tuple(traits.Str(), "
             "traits.Enum(('T',)), traits.List(traits.Str()), "
             "traits.List(traits.Float()), traits.List(traits.Float())"
             "))))))"),
            "use_derivs":
            "traits.Bool()"
        }
        i = spm.EstimateContrast()
        for field, result in six.iteritems(to_test_fields):

            # Test to build the trait expression
            trait = i.inputs.trait(field)
            expression = build_expression(trait)
            self.assertEqual(expression, result)

            # Try to clone the trait
            trait = eval_trait(expression)()
            self.assertEqual(build_expression(trait), result)

        # Test to clone some traits
        trait_description = ["Float", "Int"]
        handler = clone_trait(trait_description)
        trait = handler.as_ctrait()
        self.assertEqual(trait_description, trait_ids(trait))
示例#3
0
def class_factory(func, destination_module_globals):
    """ Dynamically create a process instance from a function

    In order to make the class publicly accessible, we assign the result of
    the function to a variable dynamically using globals().

    Parameters
    ----------
    func: @function (mandatory)
        the function we want encapsulate in a process.
    """
    # Create the process class name
    class_name = title_for(func.__name__)

    # Get the capsul prototype
    capsul_proto = parse_docstring(func.__doc__)

    # Create all the process input and output traits
    process_traits = {}
    for trait_item in capsul_proto:

        # Create the trait
        trait_desc = trait_item["type"]
        trait = clone_trait([trait_desc])

        # Specify the trait
        trait.desc = trait_item.get("desc", "")
        if trait_item.get("role", "") in ["return", "output"]:
            trait.output = True
        else:
            trait.output = False
        if trait_item.get("optional", "False") == "True":
            trait.optional = True
        else:
            trait.optional = False

        # Store the created trait
        process_traits[trait_item["name"]] = trait

    # Clean the docstring
    docstring = func.__doc__
    # python 2.7 only (not working on Centos 6.4)
    # docstring = re.sub(r"<process>.*</process>", "", docstring,
    #                     flags=re.DOTALL)
    res = re.search(r"<process>.*</process>.*", docstring, flags=re.DOTALL)
    if res:
        docstring = docstring.replace(docstring[res.start():res.end()], "")

    # Define the process class parameters
    class_parameters = {
        "__doc__": docstring,
        "__module__": destination_module_globals["__name__"],
        "_id":  destination_module_globals["__name__"] + "." + class_name,
        "_func_name": func.__name__,
        "_func_module": func.__module__,
        "_parameters": capsul_proto,
        "_process_traits": process_traits
    }

    # Get the process instance associated to the function
    destination_module_globals[class_name] = (
        type(class_name, (AutoProcess, ), class_parameters))