[docs]deftransform(obj):""" Transform an object to its NEURON representation, if the ``__neuron__`` magic method is present. """ifhasattr(obj,"__neuron__"):returnobj.__neuron__()returnobj
[docs]deftransform_netcon(obj):""" Transform an object into the pointer that should be connected, if the ``__netcon__`` magic method is present. """ifhasattr(obj,"__netcon__"):returnobj.__netcon__()returntransform(obj)
[docs]deftransform_record(obj):""" Transform an object into the pointer that should be recorded, if the ``__record__`` magic method is present. """ifhasattr(obj,"__record__"):returnobj.__record__()returntransform(obj)
[docs]deftransform_arc(obj,*args,**kwargs):""" Get an arclength object on a NEURON object. Calls the ``__arc__`` magic method on the callable object if present, otherwise returns the transformed object. """ifhasattr(obj,"__arc__"):returntransform(obj(obj.__arc__(*args,**kwargs)))else:returntransform(obj)
[docs]defassert_connectable(obj,label=None):""" Assert whether an object could be used as a :class:`~.objects.Connectable`. :param label: Optional label to display to describe the object if the assertion fails. :type label: str """ifnothasattr(obj,"_connections"):raiseNotConnectableError((labeliflabelisnotNoneelsestr(obj))+" is not connectable."+"It lacks attribute `_connections` required to form NetCons.")
[docs]defis_section(obj):""" Check if an object is a section. """cls=type(transform(obj))returncls.__module__=="nrn"andcls.__name__=="Section"
[docs]defis_segment(obj):""" Check if an object is a segment. """cls=type(transform(obj))returncls.__module__=="nrn"andcls.__name__=="Segment"
[docs]defis_nrn_scalar(obj):nv=get_neuron_version()cls=type(transform(obj))ifcls.__module__!="hoc"orcls.__name__!="HocObject":returnFalsetry:ifnv<"9.0a0":return"pointer to hoc scalar"instr(obj)else:returnstr(obj).startswith("data_handle<")exceptException:returnFalse
[docs]defis_point_process(obj:Union[str,"neuron.hoc.HocObject"]):""" Check if obj is a (name of a) PointProcess on the ``HocInterpreter``. :param obj: HocObject or name of the PointProcess to look for. Needs to be a known attribute of ``neuron.h``. :rtype: bool """fromneuronimporth,hoctry:ifnotisinstance(obj,hoc.HocObject):obj=getattr(h,obj)exceptException:returnFalsereturnall(kindir(obj)forkin["get_loc","has_loc","loc","get_segment"])
[docs]defis_density_mechanism(obj:Union[str,"neuron.hoc.HocObject"]):""" Check if obj is a (name of a) DensityMechanism on the ``HocInterpreter``. :param obj: HocObject or name of the DensityMechanism to look for. Needs to be a known attribute of ``neuron.h``. :rtype: bool """importnrnfromneuronimporth,hocifisinstance(obj,nrn.Mechanism):returnTruetry:ifnotisinstance(obj,hoc.HocObject):obj=getattr(h,obj)hname=str(obj)return"neuron.DensityMechanism"inhnameexceptTypeErrorase:return"mechanism"instr(e)exceptException:returnFalse