python执行另一个脚本_另一个带约束的Python脚本

python执行另一个脚本

In this post you will find a function I wrote solving a problem of randomization with constrains in Python. For a psychological experiment (a shifting/task-switching task) I am planning to conduct I needed a list of stimuli names quasi-randomized.  Fifty percent of the items in the list be followed by a stimulus in the same colour and the remaining 50 % would be followed by a stimulus in a different colour.

在本文中,您将找到我编写的函数,用于解决Python中带有约束的随机化问题。 对于心理实验( 转移/任务转换任务 ),我计划进行操作,我需要一个准随机命名的刺激物名称列表。 列表中百分之五十的项目后面是相同颜色的刺激,其余的50%之后是不同颜色的刺激。

Python脚本 (Python script)


from random import shuffle
from collections import Counter

liststim = []
colors, firstfigures = ['Black', 'Blue'], ['Rect', 'Triangle', 'X', 'Circle']
secondfigures = firstfigures
for col in colors:
    for figure in firstfigures:
        for figure2 in secondfigures:
            liststim.append(col + '-' + figure + '_' + figure2)

def randShift(listostim, ntrials):
    '''
    Will randomize 50 % of shifting trials
    based on that each filename (of the images) starts with Black or Blue-.
    
    listostim is a list of trials
    ntrials is an integer indicating number of trials
    '''
    nEachstim = ntrials/len(liststim) #Number of each stim is going to be even
    stimList = listostim 
    trialTypes = [] 
    stims = []
    countOfStim = dict((el,0) for el in stimList)
    count = {'ns':0,'s':0}
    for i in range(ntrials):
        shuffle(stimList)
        for idx in range(len(stimList)):
            if not stims:
                countOfStim[stimList[idx]] +=1
                stims.append(stimList[idx])
                count['ns'] +=1
                trialTypes.append("No-Shifting")
            elif stims:
                if count['s'] <= ntrials/2:
                    if countOfStim[stimList[idx]] <= nEachstim-1: if stimList[idx][:5] != stims[i-1][:5]: count['s'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("Shifting") else: count['ns'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("No-Shifting") elif count['s'] > ntrials/2:
                    if countOfStim[stimList[idx]] <= nEachstim-1: 
                        if stimList[idx][:5] == stims[i-1][:5]:
                            count['ns'] +=1
                            countOfStim[stimList[idx]] +=1
                            stims.append(stimList[idx])
                            trialTypes.append("No-Shifting")
    #Frequency of the trialtypes
    freq = Counter(trialTypes).values()

from random import shuffle
from collections import Counter

liststim = []
colors, firstfigures = ['Black', 'Blue'], ['Rect', 'Triangle', 'X', 'Circle']
secondfigures = firstfigures
for col in colors:
    for figure in firstfigures:
        for figure2 in secondfigures:
            liststim.append(col + '-' + figure + '_' + figure2)

def randShift(listostim, ntrials):
    '''
    Will randomize 50 % of shifting trials
    based on that each filename (of the images) starts with Black or Blue-.
    
    listostim is a list of trials
    ntrials is an integer indicating number of trials
    '''
    nEachstim = ntrials/len(liststim) #Number of each stim is going to be even
    stimList = listostim 
    trialTypes = [] 
    stims = []
    countOfStim = dict((el,0) for el in stimList)
    count = {'ns':0,'s':0}
    for i in range(ntrials):
        shuffle(stimList)
        for idx in range(len(stimList)):
            if not stims:
                countOfStim[stimList[idx]] +=1
                stims.append(stimList[idx])
                count['ns'] +=1
                trialTypes.append("No-Shifting")
            elif stims:
                if count['s'] <= ntrials/2:
                    if countOfStim[stimList[idx]] <= nEachstim-1: if stimList[idx][:5] != stims[i-1][:5]: count['s'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("Shifting") else: count['ns'] +=1 countOfStim[stimList[idx]] +=1 stims.append(stimList[idx]) trialTypes.append("No-Shifting") elif count['s'] > ntrials/2:
                    if countOfStim[stimList[idx]] <= nEachstim-1: 
                        if stimList[idx][:5] == stims[i-1][:5]:
                            count['ns'] +=1
                            countOfStim[stimList[idx]] +=1
                            stims.append(stimList[idx])
                            trialTypes.append("No-Shifting")
    #Frequency of the trialtypes
    freq = Counter(trialTypes).values()
 

The last part is the crucial part since the above code will not produce the required list 100 % of the time. The script, therefore, first checks that the above script has generated the output needed. That is, it is 50% items that are of a color and then followed by a different color? An additional check is also made to make sure that there are so many items that were  required (i.e-, number of trials; ntrials). Last code will call the function again until the requirements are fulfilled.

最后一部分是关键部分,因为上面的代码不会在100%的时间内生成所需的列表。 因此,该脚本首先检查上述脚本是否已生成所需的输出。 也就是说,是50%的商品是一种颜色,然后是另一种颜色? 还进行了另一项检查,以确保所需的项目太多(即,试验次数;试验)。 最后的代码将再次调用该函数,直到满足要求为止。

The script is solving my problem and it works pretty quick. However, if there is a more elegant method I would love to see it.

该脚本正在解决我的问题,并且运行很快。 但是,如果有更优雅的方法,我很乐意看到。

翻译自: https://www.pybloggers.com/2015/11/another-python-script-for-randomization-with-constraints/

python执行另一个脚本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值