Пример #1
0
def add():
    form = AddForm(csrf_enabled=False)
    if request.method == "POST" and form.validate_on_submit():
        user = Data()
        print form.data
        code = user.add(
            form.data["firstname"], form.data["lastname"], form.data["number"], middlename=form.data["middlename"]
        )

        return render_template("add.done.html", code=code, data=form.data)
    else:
        return render_template("add.html", form=form)
Пример #2
0
def add(filename):
    user = Data()
    with open(filename) as fp:
        reader = csv.reader(fp)
        for row in reader:
            name = row[0]
            firstname = row[1]
            lastname = row[2]
            middlename = row[3]
            guests = row[4]
            code = user.add(firstname, lastname,
                            guests, middlename=middlename, name=name)
            print code
Пример #3
0
def main():
    feed = Data(60*30)
    account = Account(1000,0.02)
    signals = []

    with open(sys.argv[1]) as f:

        #price loop
        for line in f:
            line = line.split(";")
            date = datetime.strptime(line[0], "%Y%m%d %H%M%S")
            line = [float(x) for x in line[1:]]

            #get tick
            tick = Tick('EURUSD', date, line[3],line[3])

            #update data
            feed.add(tick.bid)

            #execute
            signals = execute(tick, account, signals)

            #strategy
            signals = generate_signals(tick, account, feed, signals)
Пример #4
0
class Processor:
	def __init__(self, netController = None, filename: str = '', debugLevel = 0):
		# from net import NetController
		'''
		init a processor

		`filename` is the DNS data file

		`debugLevel` should in `[0, 1, 2]`
		'''
		self.debugLevel = debugLevel
		self.data = Data(filename, debugLevel)
		self.net = netController
		# 请求列表
		self.queryList = {}
		self.defaultIp = '127.0.0.1'
		

	def parse(self, data: dict):
		'''
		may be called many times simultaneously, need concurrency control
		'''
		# TODO: parse data and get result
		t1 = threading.Thread(target=self.doParse, args = (data,))
		t1.start()
		# self.doParse(data)


	def doParse(self, data: dict):
		newData = data.copy();
		if data['data']['header']['qr']:
			if self.queryList.get(data['data']['header']['id'], None) != None:
				self.parseNames(newData)
				data['address'] = self.queryList.pop(data['data']['header']['id'], {})
				self.net.reply(data)
				for i in range(0, newData['data']['header']['ancount']):
					if newData['data']['answer'][i]['type'] == 1 and len(newData['data']['answer'][i]['rdata']) == 4:
						self.data.add(bytesNameToStr(newData['data']['question'][0]['qname'][:-1]), 0, 0, 0, bytesIpToStr(newData['data']['answer'][i]['rdata']))
			return;
		else:
			# 回复给客户端的信息
			replyData = {
				'address': {
					'ip': data['address']['ip'],
					'port': data['address']['port'],
				},
				'data': {
					'header': {
						'id': data['data']['header']['id'],
						'qr': True,
						'opcode': data['data']['header']['opcode'],
						'aa': False,
						'tc': False,
						'rd': True,
						'ra': False,
						'rcode': 0,
						'qdcount': data['data']['header']['qdcount'],
						'ancount': 0,
						'nscount': 0,
						'arcount': 0
					},
					'question': newData['data']['question'],
					'answer': [],
					'authority': [],
					'additional': []
				},
				'rawData': data['rawData']
			}

			# 处理数据
			self.parseNames(newData)
			name = bytesNameToStr(newData['data']['question'][0]['qname'])[:-1]
			ipStr = self.data.find(name)											# 进行查询
			if ipStr[0] == '' or newData['data']['question'][0]['qtype'] != 1:		# 没有记录,向服务器查询
				self.queryList[data['data']['header']['id']] = data['address']
				self.net.query(data)
				return;
			elif ipStr[0] == '0.0.0.0':												# 无效域名
				replyData['data']['header']['rcode'] = 3
			else:																	# 有效域名
				for j in range(0, len(ipStr)):
					ip = bytes(bytearray(list(map(int, ipStr[j].split('.')))))
					replyData['data']['header']['ancount'] += 1
					replyData['data']['answer'].append({
						'name': newData['data']['question'][0]['qname'],
						'type': 1,
						'class': 1,
						'ttl': 86400,
						'rdlength': 4,
						'rdata': ip
					})
			self.net.reply(refdict(replyData))
		
	def parseNames(self, newData: dict):
		rawData = newData['rawData']
			
		#deal all the names
		for i in range(0, newData['data']['header']['qdcount']):
			qname = newData['data']['question'][i]['qname']
			qname = dealName(qname, rawData)
			newData['data']['question'][i]['qname'] = qname
				
		# build a array of counts and another of names
		packsLen = [newData['data']['header']['ancount'], newData['data']['header']['nscount'], newData['data']['header']['arcount']]
		packsName = ['answer','authority','additional']

		for k in range(0, 3):
			for i in range(0, packsLen[k]):
				kname = newData['data'][packsName[k]][i]['name']
				kname = dealName(kname, rawData)
				newData['data'][packsName[k]][i]['name'] = kname
Пример #5
0
class Robot():
    def __init__(self, scene):
        self.scene = scene
        self.dynamics = 1

        # dynamics parameters
        self.l = 0.331

        # state
        self.xi = State(0, 0, 0, self)
        self.xid = State(3, 0, 0, self)
        self.xid0 = State(3, 0, math.pi / 4, self)
        self.reachedGoal = False
        # Control parameters
        self.kRho = 1
        self.kAlpha = 6
        self.kPhi = -1
        self.kV = 3.8
        self.gamma = 0.15

        #
        self.pointCloud = PointCloud(self)

        # Data to be recorded
        self.recordData = False
        self.data = Data(self)
        self.v1Desired = 0
        self.v2Desired = 0

        self.role = None
        self.neighbors = []
        self.leader = None  # Only for data recording purposes

        self.ctrl1_sm = []
        self.ctrl2_sm = []

    def propagateDesired(self):
        if self.dynamics == 5:
            pass
        elif self.dynamics == 4 or self.dynamics == 11:
            # Circular desired trajectory, depricated.
            t = self.scene.t
            radius = 2
            omega = 0.2
            theta0 = math.atan2(self.xid0.y, self.xid0.x)
            rho0 = (self.xid0.x**2 + self.xid0.y**2)**0.5
            self.xid.x = (radius * math.cos(omega * t) +
                          rho0 * math.cos(omega * t + theta0))
            self.xid.y = (radius * math.sin(omega * t) +
                          rho0 * math.sin(omega * t + theta0))
            self.xid.vx = -(radius * omega * math.sin(omega * t) +
                            rho0 * omega * math.sin(omega * t + theta0))
            self.xid.vy = (radius * omega * math.cos(omega * t) +
                           rho0 * omega * math.cos(omega * t + theta0))
            self.xid.theta = math.atan2(self.xid.vy, self.xid.vx)
            #self.xid.omega = omega

            c = self.l / 2
            self.xid.vxp = self.xid.vx - c * math.sin(self.xid.theta) * omega
            self.xid.vyp = self.xid.vy + c * math.cos(self.xid.theta) * omega
        elif self.dynamics == 16:
            # Linear desired trajectory
            t = self.scene.t
            #dt = self.scene.dt
            x = self.scene.xid.x
            y = self.scene.xid.y
            #print('x = ', x, 'y = ', y)
            theta = self.scene.xid.theta
            #print('theta = ', theta)
            sDot = self.scene.xid.sDot
            thetaDot = self.scene.xid.thetaDot

            phii = math.atan2(self.xid0.y, self.xid0.x)
            rhoi = (self.xid0.x**2 + self.xid0.y**2)**0.5
            #print('phii = ', phii)
            self.xid.x = x + rhoi * math.cos(phii)
            self.xid.y = y + rhoi * math.sin(phii)
            self.xid.vx = sDot * math.cos(theta)
            self.xid.vy = sDot * math.sin(theta)
            #print('vx: ', self.xid.vx, 'vy:', self.xid.vy)
            #print('v', self.index, ' = ', (self.xid.vx**2 + self.xid.vy**2)**0.5)

            dpbarx = -self.scene.xid.dpbarx
            dpbary = -self.scene.xid.dpbary
            if (dpbarx**2 + dpbary**2)**0.5 > 1e-1:
                self.xid.theta = math.atan2(dpbary, dpbarx)
            #if (self.xid.vx**2 + self.xid.vy**2)**0.5 > 1e-3:
            #    self.xid.theta = math.atan2(self.xid.vy, self.xid.vx)
            #self.xid.omega = omega

            c = self.l / 2
            self.xid.vxp = self.xid.vx - c * math.sin(
                self.xid.theta) * thetaDot
            self.xid.vyp = self.xid.vy + c * math.cos(
                self.xid.theta) * thetaDot
            self.xid.vRef = self.scene.xid.vRef
        elif self.dynamics == 17 or self.dynamics == 18:
            self.xid.theta = self.scene.xid.vRefAng

    def precompute(self):
        self.xi.transform()
        self.xid.transform()
        self.updateNeighbors()

    def propagate(self):
        if self.scene.vrepConnected == False:
            self.xi.propagate(self.control)
        else:
            omega1, omega2 = self.control()
            vrep.simxSetJointTargetVelocity(self.scene.clientID,
                                            self.motorLeftHandle, omega1,
                                            vrep.simx_opmode_oneshot)
            vrep.simxSetJointTargetVelocity(self.scene.clientID,
                                            self.motorRightHandle, omega2,
                                            vrep.simx_opmode_oneshot)

    def updateNeighbors(self):
        self.neighbors = []
        self.leader = None
        for j in range(len(self.scene.robots)):
            if self.scene.adjMatrix[self.index, j] == 0:
                continue
            robot = self.scene.robots[j]  # neighbor
            self.neighbors.append(robot)
            if robot.role == self.scene.ROLE_LEADER:
                if self.leader is not None:
                    raise Exception(
                        'There cannot be more than two leaders in a scene!')
                self.leader = robot

    def control(self):
        if self.learnedController is not None:
            mode = self.learnedController()
            observation, action_1 = self.data.getObservation(mode)
            if observation is None:
                action = np.array([[0, 0]])
            else:
                action = self.learnedController(observation, action_1)
            #action = np.array([[0, 0]])
            v1 = action[0, 0]
            v2 = action[0, 1]

            self.ctrl1_sm.append(v1)
            self.ctrl2_sm.append(v2)
            if len(self.ctrl1_sm) < 10:
                v1 = sum(self.ctrl1_sm) / len(self.ctrl1_sm)
                v2 = sum(self.ctrl2_sm) / len(self.ctrl2_sm)
            else:
                v1 = sum(self.ctrl1_sm[len(self.ctrl1_sm) -
                                       10:len(self.ctrl1_sm)]) / 10
                v2 = sum(self.ctrl2_sm[len(self.ctrl2_sm) -
                                       10:len(self.ctrl2_sm)]) / 10

            #print(v1,v2,'dnn')

        elif self.dynamics == 5:
            K3 = 0.15  # interaction between i and j

            # velocity in transformed space
            #vxp = 0.2
            #vyp = 0.3

            vxp = self.scene.xid.vxp
            vyp = self.scene.xid.vyp

            tauix = 0
            tauiy = 0
            for robot in self.neighbors:
                pijx = self.xi.xp - robot.xi.xp
                pijy = self.xi.yp - robot.xi.yp
                pij0 = self.xi.distancepTo(robot.xi)
                pijd0 = self.xid.distancepTo(robot.xid)
                tauij0 = 2 * (pij0**4 - pijd0**4) / pij0**3
                tauix += tauij0 * pijx / pij0
                tauiy += tauij0 * pijy / pij0
            #tauix, tauiy = saturate(tauix, tauiy, dxypMax)
            vxp += -K3 * tauix
            vyp += -K3 * tauiy

            self.v1Desired = vxp
            self.v2Desired = vyp
            return vxp, vyp

        elif self.dynamics >= 15 and self.dynamics <= 18:
            # For e-puk dynamics
            # Feedback linearization
            # v1: left wheel speed
            # v2: right wheel speed
            K3 = 0.0  # interaction between i and j

            dxypMax = float('inf')
            if self.role == self.scene.ROLE_LEADER:  # I am a leader
                K1 = 1
                K2 = 1
            elif self.role == self.scene.ROLE_FOLLOWER:
                K1 = 0  # Reference position information is forbidden
                K2 = 1
            elif self.role == self.scene.ROLE_PEER:
                K1 = 1
                K2 = 0
                K3 = 0.15  # interaction between i and j
                dxypMax = 0.7

            # sort neighbors by distance
            if True:  #not hasattr(self, 'dictDistance'):
                self.dictDistance = dict()
                for j in range(len(self.scene.robots)):
                    if self.scene.adjMatrix[self.index, j] == 0:
                        continue
                    robot = self.scene.robots[j]  # neighbor
                    self.dictDistance[j] = self.xi.distancepTo(robot.xi)
                self.listSortedDistance = sorted(self.dictDistance.items(),
                                                 key=operator.itemgetter(1))

            # velocity in transformed space
            vxp = 0
            vyp = 0

            tauix = 0
            tauiy = 0
            # neighbors sorted by distances in descending order
            lsd = self.listSortedDistance
            jList = [lsd[0][0], lsd[1][0]]
            m = 2
            while m < len(lsd) and lsd[m][1] < 1.414 * lsd[1][1]:
                jList.append(lsd[m][0])
                m += 1
            #print(self.listSortedDistance)
            for j in jList:
                robot = self.scene.robots[j]
                pijx = self.xi.xp - robot.xi.xp
                pijy = self.xi.yp - robot.xi.yp
                pij0 = self.xi.distancepTo(robot.xi)
                if self.dynamics == 18:
                    pijd0 = self.scene.alpha
                else:
                    pijd0 = self.xid.distancepTo(robot.xid)
                tauij0 = 2 * (pij0**4 - pijd0**4) / pij0**3
                tauix += tauij0 * pijx / pij0
                tauiy += tauij0 * pijy / pij0

            # Achieve and keep formation
            #tauix, tauiy = saturate(tauix, tauiy, dxypMax)
            vxp += -K3 * tauix
            vyp += -K3 * tauiy

            # Velocity control toward goal
            #dCosTheta = math.cos(self.xi.theta) - math.cos(self.xid.theta)
            #print("dCosTheta: ", dCosTheta)
            #print("theta: ", self.xi.theta, "thetad: ", self.xid.theta)
            dxp = self.scene.xid.dpbarx  #+ self.l / 2 * dCosTheta
            dyp = self.scene.xid.dpbary  #+ self.l / 2 * dCosTheta
            # Velocity control toward goal
            #dxp = self.xi.xp - self.xid.xp
            #dyp = self.xi.yp - self.xid.yp
            # Limit magnitude
            dxp, dyp = saturate(dxp, dyp, dxypMax)
            vxp += -K1 * dxp
            vyp += -K1 * dyp

            # Take goal's speed into account
            vxp += K2 * self.xid.vxp
            vyp += K2 * self.xid.vyp

            kk = 1
            theta = self.xi.theta
            M11 = kk * math.sin(theta) + math.cos(theta)
            M12 = -kk * math.cos(theta) + math.sin(theta)
            M21 = -kk * math.sin(theta) + math.cos(theta)
            M22 = kk * math.cos(theta) + math.sin(theta)

            v1 = M11 * vxp + M12 * vyp
            v2 = M21 * vxp + M22 * vyp

            #v1 = 0.3
            #v2 = 0.3

        elif self.dynamics == 20:
            # step signal
            if self.scene.t < 1:
                v1 = 0
                v2 = 0
            else:
                v1 = self.arg2[0]
                v2 = self.arg2[1]

        elif self.dynamics == 21:
            # step signal
            if self.scene.t < 1:
                v1 = 0
                v2 = 0
            elif self.scene.t < 4:
                v1 = self.arg2[0]
                v2 = self.arg2[1]
            elif self.scene.t < 7:
                v1 = -self.arg2[0]
                v2 = -self.arg2[1]
            else:
                v1 = self.arg2[0]
                v2 = self.arg2[1]

        elif self.dynamics == 22:
            # step signal
            w = 0.3
            amp = 2
            t = self.scene.t
            t0 = 1
            if t < t0:
                v1 = 0
                v2 = 0
            else:
                v1 = amp * w * math.sin(w * (t - t0)) * self.arg2[0]
                v2 = amp * w * math.sin(w * (t - t0)) * self.arg2[1]

        else:
            raise Exception("Undefined dynanmics")

        #print("v1 = %.3f" % v1, "m/s, v2 = %.3f" % v2)

        vm = 0.7  # wheel's max linear speed in m/s
        # Find the factor for converting linear speed to angular speed
        if math.fabs(v2) >= math.fabs(v1) and math.fabs(v2) > vm:
            alpha = vm / math.fabs(v2)
        elif math.fabs(v2) < math.fabs(v1) and math.fabs(v1) > vm:
            alpha = vm / math.fabs(v1)
        else:
            alpha = 1
        v1 = alpha * v1
        v2 = alpha * v2

        # Limit maximum acceleration (deprecated)

        if LIMIT_MAX_ACC:

            dvMax = accMax * self.scene.dt

            # limit v1
            dv1 = v1 - self.v1Desired
            if dv1 > dvMax:
                self.v1Desired += dvMax
            elif dv1 < -dvMax:
                self.v1Desired -= dvMax
            else:
                self.v1Desired = v1
            v1 = self.v1Desired

            # limit v2
            dv2 = v2 - self.v2Desired
            if dv2 > dvMax:
                self.v2Desired += dvMax
            elif dv2 < -dvMax:
                self.v2Desired -= dvMax
            else:
                self.v2Desired = v2
            v2 = self.v2Desired
        elif not LIMIT_MAX_ACC:
            self.v1Desired = v1
            self.v2Desired = v2

        # Record data
        if (self.scene.vrepConnected and self.scene.SENSOR_TYPE == "VPL16"
                and self.VPL16_counter == 3 and self.recordData == True):
            self.data.add()

        # print('v = ', pow(pow(v1, 2) + pow(v2, 2), 0.5))

        if self.scene.vrepConnected:
            omega1 = v1 * 10.25
            omega2 = v2 * 10.25
            # return angular speeds of the two wheels
            return omega1, omega2
        else:
            # return linear speeds of the two wheels
            return v1, v2

    def draw(self, image, drawType):
        if drawType == 1:
            xi = self.xi
            #color = (0, 0, 255)
            color = self.scene.getRobotColor(self.index, 255, True)
        elif drawType == 2:
            xi = self.xid
            color = (0, 255, 0)
        r = self.l / 2
        rPix = round(r * self.scene.m2pix())
        dx = -r * math.sin(xi.theta)
        dy = r * math.cos(xi.theta)
        p1 = np.float32([[xi.x + dx, xi.y + dy]])
        p2 = np.float32([[xi.x - dx, xi.y - dy]])
        p0 = np.float32([[xi.x, xi.y]])
        p3 = np.float32([[xi.x + dy / 2, xi.y - dx / 2]])
        p1Pix = self.scene.m2pix(p1)
        p2Pix = self.scene.m2pix(p2)
        p0Pix = self.scene.m2pix(p0)
        p3Pix = self.scene.m2pix(p3)
        if USE_CV2 == True:
            if self.dynamics <= 1 or self.dynamics == 4 or self.dynamics == 5:
                cv2.circle(image, tuple(p0Pix[0]), rPix, color)
            else:
                cv2.line(image, tuple(p1Pix[0]), tuple(p2Pix[0]), color)
                cv2.line(image, tuple(p0Pix[0]), tuple(p3Pix[0]), color)

    def setPosition(self, stateVector=None):
        # stateVector = [x, y, theta]

        z0 = 0.1587
        if stateVector == None:
            x0 = self.xi.x
            y0 = self.xi.y
            theta0 = self.xi.theta
        elif len(stateVector) == 3:
            x0 = stateVector[0]
            y0 = stateVector[1]
            theta0 = stateVector[2]
            self.xi.x = x0
            self.xi.y = y0
            self.xi.theta = theta0
        else:
            raise Exception('Argument error!')
        if self.scene.vrepConnected == False:
            return
        vrep.simxSetObjectPosition(self.scene.clientID, self.robotHandle, -1,
                                   [x0, y0, z0], vrep.simx_opmode_oneshot)
        vrep.simxSetObjectOrientation(self.scene.clientID, self.robotHandle,
                                      -1, [0, 0, theta0],
                                      vrep.simx_opmode_oneshot)
        message = "Robot #" + str(self.index) + "'s pose is set to "
        message += "[{0:.3f}, {1:.3f}, {2:.3f}]".format(x0, y0, theta0)
        self.scene.log(message)

    def readSensorData(self):
        if self.scene.vrepConnected == False:
            return
        if "readSensorData_firstCall" not in self.__dict__:
            self.readSensorData_firstCall = True
        else:
            self.readSensorData_firstCall = False

        # Read robot states
        res, pos = vrep.simxGetObjectPosition(self.scene.clientID,
                                              self.robotHandle, -1,
                                              vrep.simx_opmode_blocking)
        if res != 0:
            raise VrepError("Cannot get object position with error code " +
                            str(res))
        res, ori = vrep.simxGetObjectOrientation(self.scene.clientID,
                                                 self.robotHandle, -1,
                                                 vrep.simx_opmode_blocking)
        if res != 0:
            raise VrepError("Cannot get object orientation with error code " +
                            str(res))
        res, vel, omega = vrep.simxGetObjectVelocity(self.scene.clientID,
                                                     self.robotHandle,
                                                     vrep.simx_opmode_blocking)
        if res != 0:
            raise VrepError("Cannot get object velocity with error code " +
                            str(res))
        #print("Linear speed: %.3f" % (vel[0]**2 + vel[1]**2)**0.5,
        #      "m/s. Angular speed: %.3f" % omega[2])
        #print("pos: %.2f" % pos[0], ", %.2f" % pos[1])
        #print("Robot #", self.index, " ori: %.3f" % ori[0], ", %.3f" % ori[1], ", %.3f" % ori[2])

        self.xi.x = pos[0]
        self.xi.y = pos[1]
        self.xi.alpha = ori[0]
        self.xi.beta = ori[1]
        self.xi.theta = ori[2]
        sgn = np.sign(
            np.dot(
                np.asarray(vel[0:2]),
                np.asarray([math.cos(self.xi.theta),
                            math.sin(self.xi.theta)])))
        self.vActual = sgn * (vel[0]**2 + vel[1]**2)**0.5
        self.omegaActual = omega[2]
        # Read laser/vision sensor data
        if self.scene.SENSOR_TYPE == "2d_":
            # self.laserFrontHandle
            # self.laserRearHandle

            if self.readSensorData_firstCall:
                opmode = vrep.simx_opmode_streaming
            else:
                opmode = vrep.simx_opmode_buffer
            laserFront_points = vrep.simxGetStringSignal(
                self.scene.clientID, self.laserFrontName + '_signal', opmode)
            print(self.laserFrontName + '_signal: ', len(laserFront_points[1]))
            laserRear_points = vrep.simxGetStringSignal(
                self.scene.clientID, self.laserRearName + '_signal', opmode)
            print(self.laserRearName + '_signal: ', len(laserRear_points[1]))
        elif self.scene.SENSOR_TYPE == "2d":  # deprecated
            raise Exception('2d sensor is not supported!!!!')
        elif self.scene.SENSOR_TYPE == "VPL16":
            # self.pointCloudHandle
            velodyne_points = vrep.simxCallScriptFunction(
                self.scene.clientID, self.pointCloudName, 1,
                'getVelodyneData_function', [], [], [], 'abc',
                vrep.simx_opmode_blocking)
            #print(len(velodyne_points[2]))
            #print(velodyne_points[2])
            res = velodyne_points[0]

            # Parse data
            if 'VPL16_counter' not in self.__dict__:
                self.VPL16_counter = 0
            # reset the counter every fourth time
            if self.VPL16_counter == 4:
                self.VPL16_counter = 0
            if self.VPL16_counter == 0:
                # Reset point cloud
                self.pointCloud.clearData()
            #print('VPL16_counter = ', self.VPL16_counter)
            self.pointCloud.addRawData(velodyne_points[2])  # will rotate here

            if self.VPL16_counter == 3:

                #print("Length of point cloud is " + str(len(self.pointCloud.data)))
                if res != 0:
                    raise VrepError("Cannot get point cloud with error code " +
                                    str(res))

                #start = time.clock()
                self.pointCloud.crop()
                #end = time.clock()
                #self.pointCloud.updateScanVector() # option 2
                self.pointCloud.updateOccupancyMap()  # option 1
                #print('Time elapsed: ', end - start)
            self.VPL16_counter += 1

        elif self.scene.SENSOR_TYPE == "kinect":
            pass
        else:
            return

    def getV1V2(self):
        v1 = self.vActual + self.omegaActual * self.l / 2
        v2 = self.vActual - self.omegaActual * self.l / 2
        return np.array([[v1, v2]])
Пример #6
0
Файл: app.py Проект: kevasesk/ai
 def image_save(self):
     data = Data()
     result = self.textEdit.toPlainText()
     data.add(self.convertInputs(self.field), result)
Пример #7
0
class Session(object):
    '''Initializes the session and calls helper classes

    Subclasses:
        soundlib: object that handles sound playback
        gui: object that handles the view
        data: object that collects and stores data
    Attributes:
        iterations: Tracks the number of trials the participant has completed
        currentSound: filename of the sound that is currently playing
        repetitions: The number of trials the participant must complete
        sequenceType: Specifies sequence type (IN-ORDER, LOADED, or RANDOM)
    '''

    def __init__(self):
        '''Initializes classes and begins main loop'''
        options = self.loadConfig()

        self.soundlib = SoundLibrary()
        self.iterations = 0
        self.currentSound = ""
        self.repetitions = options['numRepetitions']
        self.sequenceType = options['sequenceType'].upper()
        self.sequenceName = options['sequenceName']
        self.startTime = None
        self.endTime = None

        self.gui = GUI(None)

    def loadConfig(self):
        '''Loads CONFIG.yml and prepares the parameters for the session'''
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with open(dir_path + "/../CONFIG.yml", 'r') as config:
            options = yaml.load(config)
        return options


    def start(self):
        self.gui.focus_set()
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.gui.iconbitmap(default=dir_path + "/blank.ico")

        self.promptSession()
        self.gui.createPrompt(self.startSession, self.endSession) # Sets up the view

        self.gui.mainloop()

    def promptSession(self):
        ''' Prompts the experimenter for the Subject ID, Session ID,
            and the number of repetitions
        '''

        while (True):
            self.setRepetitions()
            self.specifySequence()

            participantId = askstring(' ','Please enter Subject ID:')
            if participantId == None:
                sys.exit()
            sessionId = askstring(' ','Please enter Session ID:')
            if sessionId == None:
                sys.exit()
            
            sequence = self.sequenceName if self.sequenceType == 'LOADED' else self.sequenceType

            answer = askquestion(' ','Subject: ' + participantId + ', Session: ' + sessionId +
                                 ', Repetitions: ' + str(self.repetitions) +
                                 ', Sequence: ' + sequence + '\n\nIs this correct? ' +
                                 '\n\n(reference CONFIG.yml file for options)')
            if answer == "yes":
                break

        self.data = Data(participantId, sessionId) # Data collection object
    
    def setRepetitions(self):
        '''Prompts the user for a specific number of repetitions if none was specified'''
        while self.repetitions == None or self.repetitions < 1:
            self.repetitions = askinteger(' ','Invalid repetitions specified in CONFIG.yml.'
                                            ' Please enter the number of repetitions:')
            if self.repetitions == None:
                sys.exit()
            elif self.repetitions < 1:
                showwarning(' ','Integer must have a non-negative '
                            'non-zero value.')
    
    def specifySequence(self):
        '''Prompts the user for a sequence type if none was specified'''
        if self.sequenceType == None or self.sequenceType == "LOADED" and self.sequenceName == None:
            self.sequenceName = "None"
            inorder = askquestion(' ', 'No sequence specified in CONFIG.yml.'
                                    ' Should the sounds be played in-order?')
            if inorder == "yes":
                self.sequenceType = "IN-ORDER"
            else:
                while (True):
                    loadseq = askquestion(' ', 'Then should a sequence be loaded?')
                    if loadseq == "yes":
                        self.sequenceName = askstring(' ','Please specify the filename'
                                            ' containing the sequence.')
                        seqname = self.soundlib.loadSequence(self.sequenceName)
                        if len(self.soundlib.sequence) > 0:
                            self.sequenceType = "LOADED"
                            break
                    elif loadseq == "no":
                        self.sequenceType = "RANDOM"
                        break

    def startSession(self, event):
        '''Prompts the user for input, loops a sound and then waits for a
            response.
        '''
        self.startTime = time.time()
        self.gui.createEntryBox()
        self.gui.setLabel("\n\n\n\n\n\n\n\nPlease type what word(s) you hear "
                             "and press ENTER to continue.", self.advanceSession)
        self.gui.greyScreen()

        self.setCurrentSound()


    def advanceSession(self, event):
        '''Adds response to the database, then clears the session'''
        self.endTime = time.time()

        self.data.add(self.currentSound, self.gui.entry.get(), self.endTime - self.startTime,
                      self.soundlib.getCurrentSoundLength())
        self.gui.entry.delete(0, END)  # Clears the response form

        self.iterate()


    def iterate(self):
        '''Stops the sound, greys the screen, and checks if the session
            is over.
        '''
        self.soundlib.stopAllSounds()
        self.gui.greyScreen()
        self.iterations += 1
        if self.iterations == self.repetitions:
            self.gui.setLabel("\n\n\n\n\n\n\n\nThank you for participating!"
                                 "\nPlease inform the researcher that you are"
                                 " finished.", self.endSession)
            self.gui.entry.destroy()
        else:
            self.setCurrentSound()
            self.startTime = time.time()


    def endSession(self, event):
        '''Ends the session and records the data'''
        answer = askquestion(' ','End session?')
        if answer == "yes":
            if self.iterations > 0:
                self.data.recordData()
            sys.exit()

    def setCurrentSound(self):
        '''Sets the next sound based on the sequence type'''
        if (self.sequenceType == "LOADED"):
            self.currentSound = self.soundlib.playSequence(self.iterations)
        elif (self.sequenceType == "IN-ORDER" or self.sequenceType == "IN ORDER"):
            self.currentSound = self.soundlib.playInOrder(self.iterations)
        else:
            # Treat the sequenceType as random
            self.currentSound = self.soundlib.playRandom()