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 get_traited_subclasses(parent_class): """ :param parent_class: SuperClass, to return valid sub-classes of this (e.g. Model). :return: {class_name: sub_class_instance} e.g. {'WilsonCowan': WilsonCowan, ....} """ classes_list = TYPE_REGISTER.subclasses(parent_class) result = {} for class_instance in classes_list: result[class_instance.__name__] = class_instance return result
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 __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 intr['stepValue'] = range_value.step 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 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': opt_class.__doc__, '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
def test_subclasses_retrieved(self): """ Test that the subclasses for a traited abstract class are correctly retrieved """ subclasses = TYPE_REGISTER.subclasses(Model) assert len(subclasses) >= 5
def test_subclasses_retrieved(self): """ Test that the subclasses for a traited abstract class are correctly retrieved """ subclasses = TYPE_REGISTER.subclasses(Model) self.assertTrue(len(subclasses) >= 5)