126 lines
3.1 KiB
Python
126 lines
3.1 KiB
Python
import argparse
|
|
import random
|
|
import time
|
|
|
|
from pythonosc import udp_client
|
|
|
|
import csv
|
|
import numpy as np
|
|
|
|
|
|
header=None
|
|
rows=None
|
|
rowid=0
|
|
time_start=0
|
|
|
|
client=None
|
|
lastvalue=[]
|
|
|
|
minvalues=[]
|
|
maxvalues=[]
|
|
meanvalues=[]
|
|
|
|
basetopics=["/3/oscplayer","/oscplayer","/oscplayer"]
|
|
|
|
def init():
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server")
|
|
parser.add_argument("--port", type=int, default=7000, help="The port the OSC server is listening on")
|
|
args = parser.parse_args()
|
|
|
|
global client
|
|
client = udp_client.SimpleUDPClient(args.ip, args.port)
|
|
|
|
csvlist=None
|
|
with open('heart_cluster_max.csv', newline='') as csvfile:
|
|
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
|
csvlist = list(csvreader)
|
|
'''for row in csvreader:
|
|
if header is None:
|
|
header=row
|
|
else:
|
|
rows.vstack(row)
|
|
'''
|
|
global header
|
|
header=csvlist[0]
|
|
global rows
|
|
rows=csvlist[1:]
|
|
|
|
global rowid
|
|
rowid=0 #waiting for this row to send
|
|
|
|
global lastvalue
|
|
lastvalue=np.zeros(len(rows)-1)
|
|
|
|
global minvalues
|
|
global maxvalues
|
|
global meanvalues
|
|
|
|
|
|
minvalues=[float(min(x)) for x in filterEmpty(rows)]
|
|
maxvalues=[float(max(x)) for x in filterEmpty(rows)]
|
|
|
|
#print(np.mean(np.array((filterEmpty(rows))),axis=1)) #TODO: mittelwert ausrechnen funktioniert noch nicht
|
|
#exit()
|
|
#meanvalues=[float(np.mean(x)) for x in filterEmpty(rows)]
|
|
|
|
global time_start
|
|
time_start = time.time()
|
|
|
|
|
|
def update(ctime):
|
|
global minvalues
|
|
global maxvalues
|
|
global meanvalues
|
|
global rowid
|
|
global client
|
|
global lastvalue
|
|
if rowid>=len(rows): #ended?
|
|
return False
|
|
|
|
if ctime-time_start>=float(rows[rowid][0]):
|
|
#print(rows[rowid])
|
|
crow=rows[rowid]
|
|
for i in range(len(header)-1):
|
|
if len(crow[i+1])>0: #value not empty
|
|
value=float(crow[i+1])
|
|
send=False
|
|
if (i==0):
|
|
value=int(mapRange(value,minvalues[i],maxvalues[i],1,40))
|
|
send=True
|
|
if (i==1):
|
|
send=True
|
|
if (i==2):
|
|
send=True
|
|
if (i==3):
|
|
send=True
|
|
if (i==4):
|
|
send=True
|
|
|
|
if send:
|
|
print(basetopics[i]+"/"+header[i+1]+": "+ str(value))
|
|
client.send_message(basetopics[i]+"/"+header[i+1], value)
|
|
|
|
lastvalue[i]=value #save last value
|
|
rowid+=1
|
|
return True
|
|
|
|
|
|
|
|
def filterEmpty(rows):
|
|
m=[np.array(rows)[:,i+1] for i in range(np.shape(np.array(rows))[1]-1)]
|
|
n=[[x if len(x)>0 else None for x in y] for y in m]
|
|
n=[list(filter(None, x)) for x in n]
|
|
return n
|
|
|
|
def mapRange(value, inMin, inMax, outMin, outMax):
|
|
return outMin + (((value - inMin) / (inMax - inMin)) * (outMax - outMin))
|
|
|
|
if __name__ == "__main__":
|
|
init()
|
|
res=True
|
|
while res:
|
|
res=update(time.time())
|