0

I am trying to build a custom interface that will dynamically generate the outputspec attribute based on the dictionary input. However, the output is not showing up when I do .help()

Here is what I have previously and if I run help() it will show the inputs and outputs of the node.

class _GatherInputsOutputSpec(TraitedSpec):

    a_file = traits.Str()
    b_file = traits.Str()
    c_file = traits.Str()


class GatherInputs(SimpleInterface):

    def __init__(self, dict_a):
        super().__init__()
        self.dict_a = dict_a
        self.outputspec = _GatherInputsOutputSpec

   

But when I tried to build the outputspec class dynamically like below, the outputs will not show up when I do help() and run() function is not working as well.

class Context(SimpleInterface):

    def __init__(self, dict_a):
        super().__init__()
        self.dict_a = dict_a
        self.outputspec = self._outputspec_build(dict_a)

    @staticmethod
    def _outputspec_build(dict_a):
        outputspec_class = type("ConfigOutputSpec", (nipype.interfaces.base.specs.TraitedSpec, ),{'__module__': '__main__'})
        for k in dict_a.get('inputs').keys():
            setattr(outputspec_class, k, traits.Str())
            
        return outputspec_class

Am I setting the attribute incorrectly?

amyleeslie
  • 21
  • 1
  • 3
  • Doesn't `B.functionA` require an argument related to or determined by the value of `k`? Is building that association where you have the problem? – chepner May 28 '21 at 14:36
  • No B.functionA() does not need to determined by the value of K, will be any string. Yes, I am having trouble to build the attribute of the class. Well, actually I'm not sure if the object I am building is correct too. – amyleeslie May 28 '21 at 14:38
  • No, I mean, you used `test-1` to initialize `var_a`, `test-2` for `var_b`, etc. If you just call `B.functionA()` with no arguments for each attribute, why do you need to call it multiple times (and why do you have multiple attributes)? – chepner May 28 '21 at 14:45
  • Oh so the `B.functionA()` is not exactly a function but it is a traits.api.Str, so which is a required attribute object for me to use the packageA. – amyleeslie May 28 '21 at 14:56
  • You're going to have to describe exactly *what* isn't working, and ideally provide a working example that we could use to reproduce that problem. – chepner May 28 '21 at 14:57
  • Well the first example above is the working example and I'm trying to build the class attribute dynamically based on a json dictionary key. But I will try to make it clearer. – amyleeslie May 28 '21 at 15:06
  • Done. Hope that helps? Sorry again I'm not so sure how to phrase the question as well.. – amyleeslie May 28 '21 at 15:11
  • Without knowing what `traits` is or how it is defined, the error message is meaningless. You still don't have a [mcve]. – chepner May 28 '21 at 15:21
  • I tried my best in explaining, imo it is a specific use case, sorry again. – amyleeslie May 28 '21 at 16:17
  • What is `None`: the value returned by `build_object` (seems impossible) or the value of a particular class attribute of that type? The latter only seems possible if `traits.Str()` returns `None`, which we have no way of knowing if it can or not, because we don't know what `traits` is. – chepner May 28 '21 at 18:28
  • That is: if I try to run the code you have shown, I'll just get a `NameError` because `traits` isn't defined. You need to provide code that *includes* a definition of `traits` so that the code, when run, produces the same `None` value you claim to see. – chepner May 28 '21 at 18:29
  • Sorry I just changed it and focus on the program I am building. Realized that would be easier. Sorry again. – amyleeslie May 28 '21 at 20:04

0 Answers0