def get_input_tree(self): from tvb.basic.traits import traited_interface models_sub_tree = { 'name': 'model_type', 'type': 'select', 'required': True, 'label': 'model', 'default': 'Generic2dOscillator', 'options': [] } for clz_name, clz in self.available_models.items(): models_sub_tree['options'].append({ 'name': clz._ui_name, # ui-name instaead 'value': clz_name, 'inline_description': self._dfun_math_directives_to_matjax(clz), 'description': multiline_math_directives_to_matjax(clz.__doc__).replace( '&', '&').replace('.. math::', '') }) fragment = _InputTreeFragment() fragment.trait.bound = traited_interface.INTERFACE_ATTRIBUTES_ONLY input_tree = fragment.interface[traited_interface.INTERFACE_ATTRIBUTES] input_tree.insert(1, models_sub_tree) #self.log.warn(json.dumps(input_tree, indent=2, sort_keys=1)) return input_tree
def __handle_nonmapped_subtypes(ownr, obj, intr): """ Populate options for each subtype. This fills in models etc""" for opt in TYPE_REGISTER.subclasses( ownr, KWARG_AVOID_SUBCLASSES in obj.trait.inits.kwd): if hasattr(obj, 'value') and obj.value is not None and isinstance( obj.value, opt): ## fill option currently selected with attributes from instance opt = obj.value opt_class = opt.__class__ else: opt_class = opt opt.trait.bound = INTERFACE_ATTRIBUTES_ONLY description = multiline_math_directives_to_matjax( opt_class.__doc__) intr['options'].append({ 'name': get(opt, '_ui_name', opt_class.__name__), 'value': str_class_name(opt_class, short_form=True), 'class': str_class_name(opt_class, short_form=False), 'description': description, 'attributes': opt.interface['attributes'] })
def __handle_nonmapped_subtypes(ownr, obj): """ Populate options for each subtype. This fills in models etc""" options = [] for opt in TYPE_REGISTER.subclasses( ownr, KWARG_AVOID_SUBCLASSES in obj.trait.inits.kwd): if hasattr(obj, 'value') and obj.value is not None and isinstance( obj.value, opt): ## fill option currently selected with attributes from instance opt = obj.value opt_class = opt.__class__ else: opt_class = opt opt.trait.bound = INTERFACE_ATTRIBUTES_ONLY description = multiline_math_directives_to_matjax( opt_class.__doc__) name = get(opt, '_ui_name', opt_class.__name__) value = str_class_name(opt_class, short_form=True) type_ = str_class_name(opt_class, short_form=False) attributes = opt.interface_experimental options.append( itr.TypeNode(name, value, type_, description, attributes)) return options
def format_doc(doc): return multiline_math_directives_to_matjax(doc).replace('&', '&').replace('.. math::','')
def format_doc(doc): return multiline_math_directives_to_matjax(doc).replace( '&', '&').replace('.. math::', '')
def __get__(self, inst, ownr): obj = inst if inst else ownr if not obj.trait.bound: return {} label = get(obj.trait.inits.kwd, 'label', obj.trait.name) if not label: label = obj.trait.name intr = {'default': (obj.value or obj.trait.value) if hasattr(obj, 'value') else obj.trait.value, 'description': get(obj.trait.inits.kwd, 'doc'), 'label': self.label(label), 'name': obj.trait.name, 'locked': obj.trait.inits.kwd.get('locked', False), 'required': obj.trait.inits.kwd.get('required', True)} range_value = obj.trait.inits.kwd.get('range', False) if range_value: intr['minValue'] = range_value.lo intr['maxValue'] = range_value.hi if range_value.step is not None: intr['stepValue'] = range_value.step else: LOG.debug("Missing Range.step field for attribute %s, we will consider a default." % obj.trait.name) intr['stepValue'] = (range_value.hi - range_value.hi) / 10 noise_configurable = obj.trait.inits.kwd.get('configurable_noise', None) if noise_configurable is not None: intr['configurableNoise'] = noise_configurable if KWARG_FILTERS_UI in obj.trait.inits.kwd: intr[KWARG_FILTERS_UI] = json.dumps([ui_filter.to_dict() for ui_filter in obj.trait.inits.kwd[KWARG_FILTERS_UI]]) if hasattr(obj, 'dtype'): intr['elementType'] = getattr(obj, 'dtype') if get(obj.trait, 'wraps', False): if isinstance(obj.trait.wraps, tuple): intr['type'] = str(obj.trait.wraps[0].__name__) else: intr['type'] = str(obj.trait.wraps.__name__) if intr['type'] == 'dict' and isinstance(intr['default'], dict): intr['attributes'], intr['elementType'] = self.__prepare_dictionary(intr['default']) if len(intr['attributes']) < 1: ## Dictionary without any sub-parameter return {} ##### ARRAY specific processing ######################################## if 'Array' in [str(i.__name__) for i in ownr.mro()]: intr['type'] = 'array' intr['elementType'] = str(inst.dtype) intr['quantifier'] = 'manual' if obj.trait.value is not None and isinstance(obj.trait.value, numpy.ndarray): # Make sure arrays are displayed in a compatible form: [1, 2, 3] intr['default'] = str(obj.trait.value.tolist()) ##### TYPE & subclasses specifics ###################################### elif ('Type' in [str(i.__name__) for i in ownr.mro()] and (obj.__module__ != 'tvb.basic.traits.types_basic' or 'Range' in [str(i.__name__) for i in ownr.mro()]) or 'Enumerate' in [str(i.__name__) for i in ownr.mro()]): # Populate Attributes for current entity attrs = sorted(getattr(obj, 'trait').values(), key=lambda entity: entity.trait.order_number) attrs = [val.interface for val in attrs if val.trait.order_number >= 0] attrs = [attr for attr in attrs if attr is not None and len(attr) > 0] intr['attributes'] = attrs if obj.trait.bound == INTERFACE_ATTRIBUTES_ONLY: # We need to do this, to avoid infinite loop on attributes # of class Type with no subclasses return intr if obj.trait.select_multiple: intr['type'] = 'selectMultiple' else: intr['type'] = 'select' ##### MAPPED_TYPE specifics ############################################ if 'MappedType' in [str(i.__name__) for i in ownr.mro()]: intr['datatype'] = True #### For simple DataTypes, cut options and attributes intr['options'] = [] if not ownr._ui_complex_datatype: intr['attributes'] = [] ownr_class = ownr.__class__ else: ownr_class = ownr._ui_complex_datatype if 'MetaType' in ownr_class.__name__: ownr_class = ownr().__class__ intr['type'] = ownr_class.__module__ + '.' + ownr_class.__name__ else: ##### TYPE (not MAPPED_TYPE) again #################################### intr['attributes'] = [] # Build options list intr['options'] = [] if 'Enumerate' in obj.__class__.__name__: for val in obj.trait.options: intr['options'].append({'name': val, 'value': val}) intr['default'] = obj.trait.value return intr else: for opt in TYPE_REGISTER.subclasses(ownr, KWARG_AVOID_SUBCLASSES in obj.trait.inits.kwd): if hasattr(obj, 'value') and obj.value is not None and isinstance(obj.value, opt): ## fill option currently selected with attributes from instance opt = obj.value opt_class = opt.__class__ else: opt_class = opt opt.trait.bound = INTERFACE_ATTRIBUTES_ONLY description = multiline_math_directives_to_matjax(opt_class.__doc__) intr['options'].append({'name': get(opt, '_ui_name', opt_class.__name__), 'value': str_class_name(opt_class, short_form=True), 'class': str_class_name(opt_class, short_form=False), 'description': description, 'attributes': opt.interface['attributes']}) if intr['default'] is not None and intr['default'].__class__: intr['default'] = str(intr['default'].__class__.__name__) if intr['default'] == 'RandomState': intr['default'] = 'RandomStream' else: intr['default'] = None return intr