神经科学实验工具

toon的Python项目详细描述


卡通

imageimageimageimageimage

说明

神经科学实验的附加工具,包括:

  • 在单独进程上轮询输入设备的框架。
  • 基于关键帧的动画框架。

一切都应该在windows/mac/linux上运行。

有关依赖项,请参见requirements.txt

安装

当前版本:

pip install toon

开发版本:

pip install git+https://github.com/aforren1/toon

完整安装(包括设备和演示依赖项):

pip install toon[full]

请参阅setup.py以获取这些依赖项以及特定于设备的子划分的列表。

有关用法示例,请参见demos/文件夹(注意:有些需要psychopy)。

概述

输入

^ }提供了一种从输入设备轮询的框架,包括常见的外设,如鼠标和键盘,可以灵活地处理像眼动仪、运动跟踪器和定制设备之类的不常见的设备(例如,参见{{CD5>})。其目标是使使用各种各样的设备更容易,包括采样率为1kHz的设备,并且对主流程的性能影响最小。

我们使用内置的multiprocessing模块来控制承载设备的单独进程,并与numpy一起,通过共享内存将数据移动到主进程。似乎在典型情况下,我们可以预期单个read()操作所需的时间不到500微秒(通常是100微秒)。有关测量用户端读取性能的示例,请参见demos/bench.py

典型用法如下:

fromtoon.inputimportMpDevicefromtoon.input.mouseimportMousefromtimeitimportdefault_timerdevice=MpDevice(Mouse())withdevice:t1=default_timer()+10whiledefault_timer()<t1:data=device.read()# alternatively, unpack# clicks, pos, scroll = device.read()ifdata.posisnotNone:# N-D array of data (0th dim is time)print(data.pos)# time is 1D array of timestampsprint(data.pos.time)print(data.pos[-1].time)# most recent timestamp

创建自定义设备相对简单,不过有几个复选框要选中。

fromtoon.inputimportBaseDevice,make_obsfromctypesimportc_double# Obs is a class that manages observationsclassMyDevice(BaseDevice):# optional: give a hint for the buffer size (we'll allocate 1s worth of this)sampling_frequency=500# required: each data source gets its own Obs# can have multiple Obs per device# this can either be introduced at the class level, or during __init__# ctype can be a python type, numpy dtype, or ctypePos=make_obs('Pos',shape=(3,),ctype=float)RotMat=make_obs('RotMat',(3,3),c_double)# 2D data# optional. Do not start device communication here, wait until `enter`def__init__(self):pass## Use `enter` and `exit`, rather than `__enter__` and `__exit__`# optional: configure the device, start communicatingdefenter(self):pass# optional: clean up resources, close devicedefexit(self,*args):pass# requireddefread(self):# See demos/ for examples of sharing a time source between the processestime=self.clock()# store new data with a timestamppos=self.Pos(time,(1,2,3))rotmat=self.RotMat(time,[[1,2,3],[4,5,6],[7,8,9]])# can also be explicit, i.e. `self.Returns(pos=pos, rotmat=rotmat)`returnpos,rotmat

然后,可以将此设备传递给toon.input.MpDevice,它预先分配共享内存并处理其他详细信息。

对于由MpDevice返回的数据,需要注意的一些事项:

  • 如果设备只有一个Obs,则MpDevice返回一个TsArray(具有time属性的numpy数组)。否则,MpDevice返回一个命名的观察元组,其中的名称按字母顺序排序,是预定义的Obs的小写版本。
  • 如果一次读取返回的数据是标量(例如1D力传感器),MpDevice将删除第一个维度。
  • 如果没有给定观测值的数据,则返回None。命名元组有一个同时检查所有成员的方法(data.any())。

其他注意事项:

  • 返回的数据是数据的本地副本的viewtoon.input.TsArrays和设备Returns有一个copy方法,如果将其附加到列表以供以后连接,则该方法可能很有用。
  • 关于:连接,通过from toon.input import stack有一个stack函数可用,它类似于numpy的vstack,但保持时间属性不变。它还为TsArrays或Returns适当地分派。
  • 如果在从设备读取数据时接收到成批数据,则可以返回Returns的列表(例如,请参见tests/input/mockdevices.py)。
  • 您可以选择使用device.start()/device.stop()而不是上下文管理器。
  • 您可以使用device.check_error()在任何时候检查远程错误,尽管这在进入上下文管理器和读取时自动发生。
  • 除了python类型/dtypes/cTypes之外,Obs还可以使用ctypes.Structures(参见输入测试或cyberglove示例)。

动画

这仍然是一个正在进行的工作,虽然我认为它有一些实用性。它是Magnum框架中动画组件的一个端口,尽管缺少一些特性(例如轨迹外推、对时间缩放的正确处理)。

示例:

frommathimportsin,pifromtimeimportsleepfromtimeitimportdefault_timerimportmatplotlib.pyplotaspltfromtoon.animimportTrack,Player# see toon/anim/easing.py for all available easingsfromtoon.anim.easingimportlinearclassCircle(object):x=0y=0circle=Circle()# list of (time, value)keyframes=[(0.2,-0.5),(0.5,0),(3,0.5)]x_track=Track(keyframes,easing=linear)# currently, easings can be any function that takes a single# positional argument as input (time normalized to [0, 1]) and returns# a scalar (probably float), generally having a lower asymptote# of 0 and upper asymptote of 1, which is used as the current time# for purposes of interpolationdefelastic_in(x):returnpow(2.0,10.0*(x-1.0))*sin(13.0*pihalf*x)# we can reuse keyframesy_track=Track(keyframes,easing=elastic_in)player=Player(repeats=3)# directly modify an attributeplayer.add(x_track,'x',obj=circle)defy_cb(val,obj):obj.y=val# modify via callbackplayer.add(y_track,y_cb,obj=circle)t0=default_timer()player.start(t0)vals=[]whileplayer.is_playing:player.advance(default_timer())vals.append([circle.x,circle.y])sleep(1/60)plt.plot(vals)plt.show()

其他注意事项:

  • 非数字属性,如颜色字符串,也可以在这个框架中修改(宽松被忽略)。
  • Timeline类(intoon.anim)可用于获取帧之间的时间,或自某个起始时间以来的时间,在timeline.start()处获取。
  • ^ {CD38>}也可用作MIXIN,在这种情况下,{{CD39>}参数可以从^ {CD40}中省略(参见示例{demos/文件夹)。
  • 通过向player.add()中输入一个对象列表,可以同时修改多个对象。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
无法在Netbeans 8.2 JDK8u231中创建java Maven项目(Web应用程序)   java如何以设定的时间间隔生成随机数?   java从socket和inputStream的慢速读取   spring SCORM:Java中基于Web的SCORM播放器   Java将函数传递给方法   java绑定通用服务及其实现和子类型   java如何在运行时从选择列表框中动态选择选项?爪哇硒   java Selenium WebDriver什么是“Selenium客户端和WebDriver语言绑定”   elasticsearch需要elasticsearch高级Java客户端更新ByQueryRequest API帮助   JAVA哈希表查找最大值   WSDL操作中的java soapAction属性为空   java访问封闭类或父类方法的一般方法   eclipse在java中运行带有SeleneTestCase的ANT。lang.NoClassDefFoundError   java Hazelcast不会在节点启动时填充ReplicatedMap   如何在Java中从excel中读取特定行?   html JAVA将本地时间(GMT+8)转换为UTC时间   java将自定义端点添加到Spring数据REST存储库中,并以大摇大摆的方式显示   java计算未来位置