csv PYTHON:创建一个4列的列表,其中2个列表包含2列

j9per5c4  于 2023-01-10  发布在  Python
关注(0)|答案(2)|浏览(146)

我真的很抱歉,如果这个问题已经回答了,我还没有找到正确的主题...我在一个项目与巴甫洛维亚和皮肖皮。
我希望我的参与者将鼠标光标保持在一个移动的框(目标)内。我正在记录数据,我希望:
1.记录鼠标和目标的位置
1.计算鼠标和目标中心之间的距离在每一帧
1.数一数这只老鼠多久离开一次盒子
问题出现在步骤1和步骤2之间:

  • 我可以在文件“Mouse.csv”中记录鼠标的位置(x列和y列)
  • 我可以在文件“Target.csv”中记录目标的位置(x列和y列)
    但我不能在1个文件中有4列的位置(鼠标x ;鼠标y ;目标x ;目标y)

我的问题非常类似于:

但它不能正常工作
列表如下所示:

  • 鼠标:[[“x1”,“y1”],[“x2”,“y2”],[...]]
  • 目标:[['xA','yA'],['xB','yB'],[.]]

我想得到[[x1,y1],[xA,yA],[x2,y2],[xB,yB],[...]]
但是当我可以加入列表时,我只有类似于[[“x1”,“y1”],[“x2”,“y2”],[“xA”,“yA”],[“xB”,“yB”],[...]]的东西:-(

########## FILE 2 ###########
# data to be written row-wise in csv fil
data = MposList
#data  = MposArr   #######_csv.Error: iterable expected, not numpy.float64

# opening the csv file in 'a+' mode
file = open(mousefile, 'a+', newline ='') ;

# writing the data into the file
with file:
    # identifying header
    header = ['Mouse at x', 'Mouse at Y']
    writer = csv.DictWriter(file, fieldnames = header)
    writer.writeheader()
    write = csv.writer(file)
    write.writerows(data)
    
########## FILE 3 ###########
# data to be written row-wise in csv fil
#Tdata = TposArr
Tdata = TposList
del Tdata[0]
del Tdata[0]
# opening the csv file in 'a+' mode
file = open(targetfile, 'a+', newline ='') ;

# writing the data into the file
with file:
    # identifying header
    Theader = ['target at x', 'target at y']
    writer = csv.DictWriter(file, fieldnames = Theader)
    writer.writeheader()
    write = csv.writer(file)
    write.writerows(Tdata)
    
########## FILE 4 ###########
# data to be written row-wise in csv fil
#dataF = []
#dataF.append(mouse.getPos()).append(target.pos)
dataF = pd.DataFrame({'mouse x':mouse.getPos[0], 'mouse y':mouse.getPos[1], 'target x':target.pos[0], 'target y':target.pos[1])
#print(dataF)
#data  = MposArr   #######_csv.Error: iterable expected, not numpy.float64

## opening the csv file in 'a+' mode
#file = open("combiner.csv", 'a+', newline ='') ;

## writing the data into the file
#with file:
#    # identifying header
#    Fheader = ['Mouse at x', 'Mouse at Y', 'target at x', 'target at y']
#    writer = csv.DictWriter(file, fieldnames = Fheader)
#    writer.writeheader()
#    write = csv.writer(file)
#    write.writerows(dataF)

dataF.to_csv(combiner.csv, index=False)

整个代码是

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy3 Experiment Builder (v2020.2.10),
    on February 03, 2021, at 21:08
If you publish work using this script the most relevant publication is:

    Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019)
        PsychoPy2: Experiments in behavior made easy Behav Res 51: 195.
        https://doi.org/10.3758/s13428-018-01193-y

"""

from __future__ import absolute_import, division

from psychopy import locale_setup
from psychopy import prefs
from psychopy import sound, gui, visual, core, data, event, logging, clock
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
                                STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)

import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
                   sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions
import sys  # to get file system encoding
import pandas as pd

from psychopy.hardware import keyboard

# Importing library
import csv


# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)

# Store info about the experiment session
psychopyVersion = '2020.2.10'
expName = 'mouseANDtarget'  # from the Builder filename that created this script
expInfo = {'participant': 'sun', 'session': '01A'}
#dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
#if dlg.OK == False:
#    core.quit()  # user pressed cancel
expInfo['date'] = data.getDateStr()  # add a simple timestamp
expInfo['expName'] = expName
expInfo['psychopyVersion'] = psychopyVersion

## Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
##filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])
filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])
mousefilename = 'mousePosition_' + expInfo['participant'] + '_' + expInfo['session']+'_' + expInfo['date'];
mousefile = mousefilename + '.csv'
targetfilename ='targetPosition_' + expInfo['participant'] + '_' + expInfo['session'] +'_' + expInfo['date'];
targetfile = targetfilename + '.csv'

# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
    extraInfo=expInfo, runtimeInfo=None,
    originPath='C:\\Users\\melis\\Desktop\\untitled.py',
    savePickle=True, saveWideText=True,
    dataFileName=filename)
# save a log file for detail verbose info
logFile = logging.LogFile(filename+'.log', level=logging.EXP)
logging.console.setLevel(logging.WARNING)  # this outputs to the screen, not a file

endExpNow = False  # flag for 'escape' or other condition => quit the exp
frameTolerance = 0.001  # how close to onset before 'same' frame

# Start Code - component code to be run after the window creation

# Setup the Window
win = visual.Window(
    size=(3840, 2160), fullscr=True, screen=0,
    winType='pyglet', allowGUI=False, allowStencil=False,
    monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
    blendMode='avg', useFBO=True,
    units='height') ################################################### size : 1024,769  // 3840,2160 & FullScreen=True
# store frame rate of monitor if we can measure it
expInfo['frameRate'] = win.getActualFrameRate()
if expInfo['frameRate'] != None:
    frameDur = 1.0 / round(expInfo['frameRate'])
else:
    frameDur = 1.0 / 60.0  # could not measure, so guess

# create a default keyboard (e.g. to check for escape)
defaultKeyboard = keyboard.Keyboard()

# Initialize components for Routine "trial"
trialClock = core.Clock()
mouse = event.Mouse(win=win)
x, y = [None, None]
mouse.mouseClock = core.Clock()
MposList=[]
text = visual.TextStim(win=win, name='text',
    text='default text',
    font='Arial',
    pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
    color='white', colorSpace='rgb', opacity=1,
    languageStyle='LTR',
    depth=-3.0);
currentMousePos = visual.TextStim(win=win, name='currentMousePos',
    text='default text',
    font='Arial',
    pos=(0, 0.4), height=0.05, wrapWidth=None, ori=0,
    color='black', colorSpace='rgb', opacity=1,
    languageStyle='LTR',
    depth=-4.0);
TposList=[[],[]]
target = visual.Rect(
    win=win, name='target',units='pix',
    width=(800, 800)[0], height=(800, 800)[1],
    ori=0, pos=(0, 0),
    lineWidth=1, lineColor=[1,1,1], lineColorSpace='rgb',
    fillColor=[1,1,1], fillColorSpace='rgb',
    opacity=1, depth=0.0, interpolate=True)
currentTargetPos = visual.TextStim(win=win, name='currentTargetPos',
    text='default text',
    font='Arial',
    pos=(0, 0.6), height=0.05, wrapWidth=None, ori=0,
    color='black', colorSpace='rgb', opacity=1,
    languageStyle='LTR',
    depth=-4.0);
# Create some handy timers
globalClock = core.Clock()  # to track the time since experiment started
routineTimer = core.CountdownTimer()  # to track time remaining of each (non-slip) routine

# ------Prepare to start Routine "trial"-------
continueRoutine = True
routineTimer.add(120.000000)  ############################### default 10
# update component parameters for each repeat
# setup some python lists for storing info about the mouse
gotValidClick = False  # until a click is received
sameCount = 0
sameThresh = 300
sameWarning = 200
thisCol = "green"
# keep track of which components have finished
trialComponents = [mouse, text, currentMousePos, target,currentTargetPos]
for thisComponent in trialComponents:
    thisComponent.tStart = None
    thisComponent.tStop = None
    thisComponent.tStartRefresh = None
    thisComponent.tStopRefresh = None
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
trialClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
frameN = -1

# -------Run Routine "trial"-------
while continueRoutine and routineTimer.getTime() > 0:
    # get current time
    t = trialClock.getTime()
    tThisFlip = win.getFutureFlipTime(clock=trialClock)
    tThisFlipGlobal = win.getFutureFlipTime(clock=None)
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    # *mouse* updates
    if mouse.status == NOT_STARTED and t >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        mouse.frameNStart = frameN  # exact frame index
        mouse.tStart = t  # local t and not account for scr refresh
        mouse.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(mouse, 'tStartRefresh')  # time at next scr refresh
        mouse.status = STARTED
        mouse.mouseClock.reset()
        prevButtonState = mouse.getPressed()  # if button is down already this ISN'T a new click
    if mouse.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > mouse.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            mouse.tStop = t  # not accounting for scr refresh
            mouse.frameNStop = frameN  # exact frame index
            win.timeOnFlip(mouse, 'tStopRefresh')  # time at next scr refresh
            mouse.status = FINISHED
    if mouse.status == STARTED:  # only update if started and not finished!
        buttons = mouse.getPressed()
        if buttons != prevButtonState:  # button state changed?
            prevButtonState = buttons
            if sum(buttons) > 0:  # state changed to a new click
                # abort routine on response
                continueRoutine = False
    MposList.append(mouse.getPos())
    print("mmmmoooouuuuuussssee ", mouse.getPos())
    Mpos = np.array(MposList)
    MposArr = Mpos.ravel()
    print("MOUSE type ",type(MposArr)) ############ MposArr
    print("MOUSE dimension ",MposArr.ndim) ######### MposArr

    if len(MposList) > 2:
        if MposList[-1][0] == MposList[-2][0]:
            if MposList[-1][1] == MposList[-2][1]:
                #print('same')
                sameCount += 1
        else:
            thisCol = "green"
            sameCount = 0

    if sameCount >= sameWarning:
        thisCol = "red"
    if sameCount >= sameThresh:
        continueRoutine = False
        print(" mouse not moved for " + str(sameCount)+ " frames. Ending experiment")

    # *text* updates
    if text.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        text.frameNStart = frameN  # exact frame index
        text.tStart = t  # local t and not account for scr refresh
        text.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(text, 'tStartRefresh')  # time at next scr refresh
        text.setAutoDraw(True)
    if text.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > text.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            text.tStop = t  # not accounting for scr refresh
            text.frameNStop = frameN  # exact frame index
            win.timeOnFlip(text, 'tStopRefresh')  # time at next scr refresh
            text.setAutoDraw(False)
    if text.status == STARTED:  # only update if drawing
        text.setColor(thisCol, colorSpace='rgb')
        text.setText("Mouse not moved for: " + str(sameCount) + " frames")

    # *currentMousePos* updates
    if currentMousePos.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        currentMousePos.frameNStart = frameN  # exact frame index
        currentMousePos.tStart = t  # local t and not account for scr refresh
        currentMousePos.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(currentMousePos, 'tStartRefresh')  # time at next scr refresh
        currentMousePos.setAutoDraw(True)
    if currentMousePos.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > currentMousePos.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            currentMousePos.tStop = t  # not accounting for scr refresh
            currentMousePos.frameNStop = frameN  # exact frame index
            win.timeOnFlip(currentMousePos, 'tStopRefresh')  # time at next scr refresh
            currentMousePos.setAutoDraw(False)
    if currentMousePos.status == STARTED:  # only update if drawing
        currentMousePos.setText("Current pos: " +str(mouse.getPos()))

    # *target* updates
    if target.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        target.frameNStart = frameN  # exact frame index
        target.tStart = t  # local t and not account for scr refresh
        target.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(target, 'tStartRefresh')  # time at next scr refresh
        target.setAutoDraw(True)
    if target.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > target.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            target.tStop = t  # not accounting for scr refresh
            target.frameNStop = frameN  # exact frame index
            win.timeOnFlip(target, 'tStopRefresh')  # time at next scr refresh
            target.setAutoDraw(False)
    #TposList.append([target.pos[0],target.pos[1]])
    TposList.append(target.pos)
    #del TposList[0]
    print('taaaaggggeeett', target.pos)
    #print("target dimension ",TposList.ndim)
    TposArr = np.array(TposList)
    print("TARGET type ",type(TposArr))
    print("TARGET dimension ",TposArr.ndim)
    #print('0=',TposList[0],'et 1=', TposList[1],'et 2=',TposList[2])
    
    
    # *currentTargetPos* updates
    if currentTargetPos.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        currentTargetPos.frameNStart = frameN  # exact frame index
        currentTargetPos.tStart = t  # local t and not account for scr refresh
        currentTargetPos.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(currentTargetPos, 'tStartRefresh')  # time at next scr refresh
        currentTargetPos.setAutoDraw(True)
    if currentTargetPos.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > currentTargetPos.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            currentTargetPos.tStop = t  # not accounting for scr refresh
            currentTargetPos.frameNStop = frameN  # exact frame index
            win.timeOnFlip(currentTargetPos, 'tStopRefresh')  # time at next scr refresh
            currentTargetPos.setAutoDraw(False)
    if currentTargetPos.status == STARTED:  # only update if drawing
        Tpx = str(target.pos[0])
        Tpy = str(target.pos[1])
        targetPosition = 'Current target pos: x ='+Tpx+' et y = '+Tpy
        currentTargetPos.setText(targetPosition)

################### count(mouse x) pour compter le nombre de ligne (pour la moyenne)

    # check for quit (typically the Esc key)
    if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()

    # check if all components have finished
    if not continueRoutine:  # a component has requested a forced-end of Routine
        break
    continueRoutine = False  # will revert to True if at least one component still running
    for thisComponent in trialComponents:
        if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished

    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

# -------Ending Routine "trial"-------
for thisComponent in trialComponents:
    if hasattr(thisComponent, "setAutoDraw"):
        thisComponent.setAutoDraw(False)
# store data for thisExp (ExperimentHandler)



###############################################################
x, y = mouse.getPos()
buttons = mouse.getPressed()
thisExp.addData('last x', x)
thisExp.addData('last y', y)
#thisExp.addData('mouse.leftButton', buttons[0])
#thisExp.addData('mouse.midButton', buttons[1])
#thisExp.addData('mouse.rightButton', buttons[2])
#thisExp.addData('mouse.started', mouse.tStart)
#thisExp.addData('mouse.stopped', mouse.tStop)
thisExp.addData('x', MposList)
thisExp.nextEntry()
print('MOUSE LIST', MposList)
print('MOUSE ARRAY', MposArr)
#print('last mouse position', MposList[-1][0],  MposList[-1][1])
print('last mouse position', MposList [-1][0],  MposList[-1][1])
print('TARGET LIST', TposList)
print('TARGET ARRAY', TposArr)
#thisExp.addData('text.started', text.tStartRefresh)
#thisExp.addData('text.stopped', text.tStopRefresh)
#thisExp.addData('currentMousePos.started', currentMousePos.tStartRefresh)
#thisExp.addData('currentMousePos.stopped', currentMousePos.tStopRefresh)
thisExp.addData('target.started', target.tStartRefresh)
thisExp.addData('target.stopped', target.tStopRefresh)
###################################################################################################

########## FILE 2 ###########
# data to be written row-wise in csv fil
data = MposList
#data  = MposArr   #######_csv.Error: iterable expected, not numpy.float64

# opening the csv file in 'a+' mode
file = open(mousefile, 'a+', newline ='') ;

# writing the data into the file
with file:
    # identifying header
    header = ['Mouse at x', 'Mouse at Y']
    writer = csv.DictWriter(file, fieldnames = header)
    writer.writeheader()
    write = csv.writer(file)
    write.writerows(data)
    
########## FILE 3 ###########
# data to be written row-wise in csv fil
#Tdata = TposArr
Tdata = TposList
del Tdata[0]
del Tdata[0]
# opening the csv file in 'a+' mode
file = open(targetfile, 'a+', newline ='') ;

# writing the data into the file
with file:
    # identifying header
    Theader = ['target at x', 'target at y']
    writer = csv.DictWriter(file, fieldnames = Theader)
    writer.writeheader()
    write = csv.writer(file)
    write.writerows(Tdata)
    
########## FILE 4 ###########
# data to be written row-wise in csv fil
#dataF = []
#dataF.append(mouse.getPos()).append(target.pos)
dataF = pd.DataFrame({'mouse x':mouse.getPos[0], 'mouse y':mouse.getPos[1], 'target x':target.pos[0], 'target y':target.pos[1])
#print(dataF)
#data  = MposArr   #######_csv.Error: iterable expected, not numpy.float64

## opening the csv file in 'a+' mode
#file = open("combiner.csv", 'a+', newline ='') ;

## writing the data into the file
#with file:
#    # identifying header
#    Fheader = ['Mouse at x', 'Mouse at Y', 'target at x', 'target at y']
#    writer = csv.DictWriter(file, fieldnames = Fheader)
#    writer.writeheader()
#    write = csv.writer(file)
#    write.writerows(dataF)

dataF.to_csv(combiner.csv, index=False)

# Flip one final time so any remaining win.callOnFlip()
# and win.timeOnFlip() tasks get executed before quitting
win.flip()

# these shouldn't be strictly necessary (should auto-save)
thisExp.saveAsWideText('filename'+'.csv', delim='auto')
thisExp.saveAsPickle(filename)
logging.flush()
# make sure everything is closed down
thisExp.abort()  # or data files will save again on exit
win.close()
core.quit()

我想我不明白二维列表是如何工作的,但我读了很多课程和教程,我不知道它的窍门:(
谢谢
编辑:以下是我使用的结果:第一个月

所以我想我需要[['x1', 'y1', 'xA', 'yA'], ['x2', 'y2','xB', 'yB'],[...]]
编辑2:多亏了Akshay Sengal,它成功了!

ct2axkht

ct2axkht1#

你可以用zip来完成这个操作,然后把它扁平化以取回元素,你可以压缩这两个列表的元素(把两个列表的对应元素一起添加到一个元组中)。
发布您可以使用[item for sublist in list for item in sublist],它允许您flatten并将元组分解为相应的项。
把这个列出来-

Mouse = [['x1', 'y1'], ['x2', 'y2']]
Target = [['xA', 'yA'], ['xB', 'yB']]

[item for sublist in zip(Mouse, Target) for item in sublist]
[['x1', 'y1'], ['xA', 'yA'], ['x2', 'y2'], ['xB', 'yB']]
    • 编辑:**

根据您更新的问题,您可以执行以下操作-
一个二个一个一个

js4nwp54

js4nwp542#

对于那些想要测试我的代码的人,组合文件使用i[0],i[1],j[0],j[1]代替i+j

#data to be written row-wise in csv file
dataF = [[i[0],i[1],j[0],j[1],k,abs(i[0]-j[0]),abs(i[1]-j[1])] for i,j,k in zip(MposList,Tdata,mIOt)]

列表的类型/维度不适用于正常代码,我添加了x和y

相关问题