Last active
November 10, 2016 17:22
-
-
Save robinvanemden/81e4dfe945c5723f6fec07dc4e690018 to your computer and use it in GitHub Desktop.
Basic LiF Sims
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import json | |
| import pandas as pd | |
| pd_csv = pd.read_csv('face_clean_with_demo.csv') | |
| ############################################################################## | |
| def matrixpush(m, row): | |
| if not np.all(np.isfinite(values[:,0])): | |
| i = np.count_nonzero(np.logical_not(np.isnan(values[:,0]))) | |
| m[i,] = row | |
| else: | |
| m = np.vstack([m,row]) | |
| m = m[1:,] | |
| return(m) | |
| def getobs( x, max = 5, err=0 ): | |
| if (err==0): | |
| obsr = -pow((x-max),2) | |
| else: | |
| obsr = -pow((x-max),2) + np.random.normal(0,err,1) | |
| return obsr; | |
| ############################################################################## | |
| inttime = 150 # Integration time | |
| amplitude = 8 # Amlitude LIF | |
| learnrate = 0.0006 # Learnrate | |
| x0 = 1.0 # Startvalue | |
| ############################################################################## | |
| variance = 1 # variance in observations | |
| ############################################################################## | |
| values = np.zeros((inttime,4)) | |
| values.fill(np.nan) | |
| track_x0 = [] | |
| track_x = [] | |
| track_t = [] | |
| track_y = [] | |
| x = 0.0 | |
| t = 0.0 | |
| y = 0.0 | |
| omega_1 = 2.63 | |
| omega_2 = 2.51 | |
| omega = (omega_1 + omega_2)/2 | |
| ############################################################################## | |
| for i in range(0,len(pd_csv.t)): | |
| t = pd_csv.t[i] | |
| x = x0 + amplitude * np.cos(omega * t) | |
| y = amplitude * np.cos(omega * t) * pd_csv.y[i] | |
| if np.all(np.isfinite(values[:,0])): | |
| x0 = x0 + learnrate * sum( values[:,2] ) /inttime | |
| row_to_add = np.array([t, x, y, x0]) | |
| values = matrixpush(values, row_to_add) | |
| track_t = np.append(track_t, t) | |
| track_x = np.append(track_x, x) | |
| track_y = np.append(track_y, y) | |
| track_x0 = np.append(track_x0, x0) | |
| ############################################################################## | |
| # plot some vars | |
| plt.plot(track_x) | |
| plt.show() | |
| plt.plot(track_x0) | |
| plt.show() | |
| # print final x0 | |
| print(x0) | |
| #print(values) | |
| print () | |
| ############################################################################## | |
| def _np_nan_fill(rows,columns): | |
| nan_values = np.zeros((rows,columns)) | |
| nan_values.fill(np.nan) | |
| nan_values = json.dumps(nan_values.tolist()) | |
| return nan_values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import json | |
| ############################################################################## | |
| def matrixpush(m, row): | |
| if not np.all(np.isfinite(values[:,0])): | |
| i = np.count_nonzero(np.logical_not(np.isnan(values[:,0]))) | |
| m[i,] = row | |
| else: | |
| m = np.vstack([m,row]) | |
| m = m[1:,] | |
| return(m) | |
| def getobs( x, max = 5, err=0 ): | |
| if (err==0): | |
| obsr = -pow((x-max),2) | |
| else: | |
| obsr = -pow((x-max),2) + np.random.normal(0,err,1) | |
| return obsr; | |
| ############################################################################## | |
| stream = 400 # Length of stream | |
| inttime = 100 # Integration time | |
| amplitude = 1.4 # Amlitude LIF | |
| learnrate = .004 # Learnrate | |
| omega = 0.8 # Omega | |
| x0 = 1.0 # Startvalue | |
| ############################################################################## | |
| variance = 1 # variance in observations | |
| ############################################################################## | |
| values = np.zeros((inttime,4)) | |
| values.fill(np.nan) | |
| track_x0 = [] | |
| track_x = [] | |
| track_t = [] | |
| track_y = [] | |
| x = 0.0 | |
| t = 0.0 | |
| y = 0.0 | |
| ############################################################################## | |
| for i in range(0,stream): | |
| t = i+1 | |
| x = x0 + amplitude * np.cos(omega * t) | |
| y = amplitude * np.cos(omega * t) * getobs(x, 5, 1) | |
| if np.all(np.isfinite(values[:,0])): | |
| #x0 = np.mean(values[:,1]) # either or | |
| x0 = x0 + learnrate * sum( values[:,2] ) /inttime # /inttime | |
| row_to_add = np.array([t, x, y, x0]) | |
| values = matrixpush(values, row_to_add) | |
| track_t = np.append(track_t, t) | |
| track_x = np.append(track_x, x) | |
| track_y = np.append(track_y, y) | |
| track_x0 = np.append(track_x0, x0) | |
| ############################################################################## | |
| # plot some vars | |
| plt.plot(track_x) | |
| plt.show() | |
| plt.plot(track_x0) | |
| plt.show() | |
| # print final x0 | |
| print(x0) | |
| #print(values) | |
| ############################################################################## | |
| def _np_nan_fill(rows,columns): | |
| nan_values = np.zeros((rows,columns)) | |
| nan_values.fill(np.nan) | |
| nan_values = json.dumps(nan_values.tolist()) | |
| return nan_values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import urllib,json,codecs | |
| import pymongo | |
| import ssl | |
| ssl._create_default_https_context = ssl._create_unverified_context | |
| MONGO_IP = "78.46.212.194" | |
| MONGO_PORT = 27017 | |
| def do_chart(qr): | |
| client = pymongo.MongoClient(MONGO_IP, MONGO_PORT) | |
| db = client.logs | |
| fig = plt.figure(figsize=(4.8,4)) | |
| ax = fig.add_subplot(1,1,1) | |
| ax.tick_params(which = 'both', direction = 'out') | |
| ax.grid(which='both') | |
| cursor = db.logs.find({"type": "setreward","q":int(qr)}) \ | |
| .sort([("t", pymongo.ASCENDING)]) | |
| result_list = list(cursor) | |
| client.close(); | |
| t = [ts['t'] for ts in result_list] | |
| x0 = [xs['x0'] for xs in result_list] | |
| plt.plot(t,x0) | |
| plt.show() | |
| def getobs( x, max = 5, err=0 ): | |
| if (err==0): | |
| obsr = -1*pow((x-max),2) | |
| else: | |
| obsr = -1*pow((x-max),2) + np.random.normal(0,err,1) | |
| return obsr; | |
| BASE_URL = 'https://strm.mnds.org:8080' | |
| key = "784537c64" | |
| exp_id = 2 | |
| question_nr = 197152212 | |
| QID = question_nr | |
| ############################################################################## | |
| stream = 400 # Length of stream | |
| inttime = 100 # Integration time | |
| A = 1.4 # Amlitude LIF | |
| gamma = .004 # Learnrate | |
| omega = 0.8 # Omega | |
| x0 = 1.0 # Startvalue | |
| ############################################################################## | |
| p_return = 0.80 # Chance that returns | |
| variance = 1 # variance in observations | |
| ############################################################################## | |
| picked = "T" | |
| lifv = 2 | |
| track_x0 = [] | |
| track_x = [] | |
| track_t = [] | |
| track_y = [] | |
| x = 0.0 | |
| t = 0.0 | |
| y = 0.0 | |
| for i in range(0,stream): | |
| request = BASE_URL + "/" + str(exp_id) | |
| request += "/getAction.json?key="+key | |
| request += "&context={\"question\":"+str(question_nr) | |
| request += ",\"T\":"+str(inttime) | |
| request += ",\"gamma\":"+str(gamma) | |
| request += ",\"omega\":"+str(omega) | |
| request += ",\"lifv\":"+str(lifv) | |
| request += ",\"QID\":"+str(QID)+",\"x0\":"+str(x0)+",\"A\":"+str(A)+"}" | |
| print (request) | |
| response = urllib.request.urlopen(request) | |
| reader = codecs.getreader("utf-8") | |
| obj = json.load(reader(response)) | |
| t = (obj["action"]["t"]) | |
| x = (obj["action"]["x"]) | |
| if np.random.binomial(1, p_return, 1)==1: | |
| y = getobs(x,5,variance) | |
| request = BASE_URL + "/" + str(exp_id) + "/setReward.json" | |
| request += "?key="+key | |
| request += "&context={\"question\":"+str(question_nr) | |
| request += ",\"T\":"+str(inttime) | |
| request += ",\"gamma\":"+str(gamma) | |
| request += ",\"omega\":"+str(omega) | |
| request += ",\"lifv\":"+str(lifv) | |
| request += ",\"dvalue\":\""+str(picked)+"\"" | |
| request += ",\"QID\":"+str(QID)+",\"x0\":"+str(x0)+",\"A\":"+str(A)+"}" | |
| request += "&action={\"x\":" + str(float(x)) | |
| request += ",\"t\":" + str(float(t)) + "}" | |
| request += "&reward=" + str(float(y)) | |
| print (request) | |
| response = urllib.request.urlopen(request) | |
| reader = codecs.getreader("utf-8") | |
| obj = json.load(reader(response)) | |
| track_x = np.append(track_x, x) | |
| plt.plot(track_x) | |
| plt.show() | |
| do_chart(question_nr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment