def kernel_NGS(dat, SHUF=0, kerwin=3): _td = _rt.return_hnd_dat(dat) Tgame = _td.shape[0] cprobs = _N.zeros((3, 3, Tgame - 1)) stay_win, dn_win, up_win, stay_tie, dn_tie, up_tie, stay_los, dn_los, up_los, win_cond, tie_cond, los_cond = _rt.get_ME_WTL( _td, 0, Tgame) gk = _Am.gauKer(kerwin) gk /= _N.sum(gk) all_cnd_tr = _N.zeros((3, 3, Tgame - 1)) ker_all_cnd_tr = _N.ones((3, 3, Tgame - 1)) * -100 all_cnd_tr[0, 0, stay_win] = 1 all_cnd_tr[0, 1, dn_win] = 1 all_cnd_tr[0, 2, up_win] = 1 all_cnd_tr[1, 0, stay_tie] = 1 all_cnd_tr[1, 1, dn_tie] = 1 all_cnd_tr[1, 2, up_tie] = 1 all_cnd_tr[2, 0, stay_los] = 1 all_cnd_tr[2, 1, dn_los] = 1 all_cnd_tr[2, 2, up_los] = 1 for iw in range(3): if iw == 0: cond = _N.sort(win_cond) elif iw == 1: cond = _N.sort(tie_cond) elif iw == 2: cond = _N.sort(los_cond) for it in range(3): print(all_cnd_tr[iw, it, cond]) ker_all_cnd_tr[iw, it, cond] = _N.convolve(all_cnd_tr[iw, it, cond], gk, mode="same") for n in range(1, Tgame - 1): if ker_all_cnd_tr[iw, it, n] == -100: ker_all_cnd_tr[iw, it, n] = ker_all_cnd_tr[iw, it, n - 1] n = 0 while ker_all_cnd_tr[iw, it, n] == -100: n += 1 ker_all_cnd_tr[iw, it, 0:n] = ker_all_cnd_tr[iw, it, n] for iw in range(3): for_cond = _N.sum(ker_all_cnd_tr[iw], axis=0) for it in range(3): print(ker_all_cnd_tr[iw, it].shape) ker_all_cnd_tr[iw, it] /= for_cond return ker_all_cnd_tr
def give_me_the_time(partID, fields): hnd_dat, start, end = return_hnd_dat(partID, has_useragent=True, has_constructor=True) since_page_load = hnd_dat[-1, 3] all_seconds = int(_N.round(since_page_load/1000)) minutes = all_seconds // 60 seconds = all_seconds - minutes*60 fields[1] -= minutes fields[2] -= seconds if fields[2] < 0: fields[1] -= 1 fields[2] += 60 if fields[1] < 0: fields[0] -= 1 fields[1] += 60
def CRs(dat, expt="EEG1", visit=None, block=1, hnd_dat=None): if hnd_dat is None: td, start_time, end_time, UA, cnstr = _rt.return_hnd_dat( dat, has_useragent=True, has_start_and_end_times=True, has_constructor=True, visit=visit, expt=expt, block=block) #td, start_tm, end_tm = _rt.return_hnd_dat(dat, has_useragent=True, has_start_and_end_times=True, has_constructor=True, flip_human_AI=False) else: td = hnd_dat Tgame = td.shape[0] CRs = _N.ones(Tgame - 1, dtype=int) * -1 for g in range(Tgame - 1): if td[g, 0] == td[g + 1, 0]: # STAY if (td[g, 2] == 1): CRs[g] = st_win elif (td[g, 2] == 0): CRs[g] = st_tie elif (td[g, 2] == -1): CRs[g] = st_los ## from here, we're going to work with R=1, S=2, P=3 # 1->2 R to S elif (((td[g, 0] == 1) and (td[g + 1, 0] == 2)) or # DOWN ((td[g, 0] == 2) and (td[g + 1, 0] == 3)) or ((td[g, 0] == 3) and (td[g + 1, 0] == 1))): if (td[g, 2] == 1): CRs[g] = dn_win elif (td[g, 2] == 0): CRs[g] = dn_tie elif (td[g, 2] == -1): CRs[g] = dn_los elif (((td[g, 0] == 1) and (td[g + 1, 0] == 3)) or # UP ((td[g, 0] == 2) and (td[g + 1, 0] == 1)) or ((td[g, 0] == 3) and (td[g + 1, 0] == 2))): if (td[g, 2] == 1): CRs[g] = up_win elif (td[g, 2] == 0): CRs[g] = up_tie elif (td[g, 2] == -1): CRs[g] = up_los return CRs
def only_complete_data(partIDs, TO): pid = -1 incomplete_data = [] for partID in partIDs: pid += 1 td, start_time, end_time, UA, cnstr = _rt.return_hnd_dat( partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True, visit=1, expt=data, block=1) if td.shape[0] < TO: incomplete_data.append(pid) for inc in incomplete_data[::-1]: # remove from list partIDs.pop(inc) return partIDs, incomplete_data
from AIiRPS.utils.dir_util import getResultFN partIDs = ["20210609_1230-28", "20210609_1248-16", "20210609_1321-35", "20210609_1747-07", "20210526_1318-12", "20210526_1358-27", "20210526_1416-25", "20210526_1503-39", "20200108_1642-20", "20200109_1504-32", "20200812_1331-06", "20200812_1252-50", "20200818_1546-13", "20200818_1603-42", "20200818_1624-01", "20200818_1644-09"]#, "20200601_0748-03", "20210529_1923-44", "20210529_1419-14", "20210606_1237-17", "20201122_1108-25", "20201121_1959-30", "20201121_2131-38"] running_total = _N.zeros((3, 3), dtype=_N.int) individual_totals = [] for partID in partIDs: totals = _N.zeros((3, 3), dtype=_N.int) _hnd_dat, start_time, end_time = _rt.return_hnd_dat(partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True) for i in range(_hnd_dat.shape[0]-1): if _hnd_dat[i+1, 2] == 1: # WIN row = 0 elif _hnd_dat[i+1, 2] == 0: # TIE row = 1 else: row = 2 if (((_hnd_dat[i, 0] == 1) and (_hnd_dat[i+1, 0] == 2)) or # R to S ((_hnd_dat[i, 0] == 2) and (_hnd_dat[i+1, 0] == 3)) or # S to P ((_hnd_dat[i, 0] == 3) and (_hnd_dat[i+1, 0] == 1))): # P to R # DOWNGRADE totals[row, 0] += 1 running_total[row, 0] += 1
def pkg_all_data(partID): """ d_reprtd_start: delay RPSM game start relative to DSi - as calculated by reported JS start time and DSi start time. dt_sys_clks: how much system times are off. d_reprtd_start = RPSM(start) - DSi(start) RPSM say 12:00:00, DSi says 11:59:30 +30 if same_time_RPSM is 12:00:00 and same_time_EEG is 11:59:35, then d_sys_clks = 25 11:59:35 (RPSM) and 11:59:30 (DSi) als d_start = d_reprtd_start - d_sys_clks if d_start > 0, RPSM started AFTER EEG if d_start < 0, RPSM started BEFORE EEG """ print("partID %s" % partID) dat_pkg = {} dsi_fn = rpsms.rpsm_partID_as_key[partID] #same_time_RPSM = params4partID[partID][0] # from calibration.html #same_time_EEG = params4partID[partID][1] # from calibration.html rpsm_fn = "rpsm_%s.dat" % partID _hnd_dat, start_time, end_time, UA, cnstr = _rt.return_hnd_dat( partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True, visit=None, expt="EEG1") reprtd_start_RPSM_str = start_time #rpsm_key[8:15] # from key name #if dsi_fn is not None: # reprtd_start_EEG_str = dsi_fn[10:18] #day = partID[0:8] #time= partID[9:] #eeg_dir = "~/Sites/taisen/DATA/EEG1/%(day)s/%(dt)s/" % {"day" : day, "dt" : partID} trials = _hnd_dat.shape[0] print("!!!!! trials %d" % trials) # the weight each one should have is just proportional to the # of events # of the condition (WTL) observed. tr0 = 0 tr1 = trials hnd_dat = _N.array(_hnd_dat[tr0:tr1]) #0:trials-trim_trials]) dat_pkg["lat"] = [] dat_pkg["behv"] = [] dat_pkg["behv_ts"] = [] #savgol_win = params4partID[partID][5] #%(dat)s,%(rel)s,%(cov)s%(ran)s sum_chosen_behv_sig = None #_N.zeros((len(behv_list), Tm1)) sigcov_behv_sig = None sigcov_behv_fsig = None behv_list = _N.array([0, 1, 2], _N.int) for bi in range(0, 1): if behv_list[bi] == _cnst._WTL: sig_cov = _N.array([_W, _T, _L]) else: sig_cov = _N.array([_R, _P, _S]) sig_cov = behv_list[bi] behv_file = fns[bi] print( getResultFN("%(rpsm)s/%(lb)d/%(fl)s.dmp" % { "rpsm": partID, "fl": behv_file, "lb": label })) dmp = depickle( getResultFN("%(rpsm)s/%(lb)d/%(fl)s.dmp" % { "rpsm": partID, "fl": behv_file, "lb": label })) cond_probs = dmp["cond_probs"] cond_probs = cond_probs.reshape((3, 3, cond_probs.shape[1])) s1 = _N.sum(_hnd_dat[0:TO_GAME, 2]) s2 = _N.sum(_hnd_dat[:, 2]) print("%(1)d %(2)d" % {"1": s1, "2": s2}) return cond_probs, _N.sum(_hnd_dat[0:TO_GAME, 2])
if stds[SHUFFLE, col] > srtd[-1]: return SHUFFLE + 1, srtd elif stds[SHUFFLE, col] < srtd[0]: return -1, srtd else: return rank[0], srtd print("woops %(0).3f %(-1).3f %(val).3f" % { "0": srtd[0], "-1": srtd[-1], "val": stds[SHUFFLE, col] }) return None, srtd if not know_gt: hnd_dat = _rt.return_hnd_dat(dat_fn) else: hnd_dat = _N.loadtxt( "/Users/arai/nctc/Workspace/AIiRPS_SimDat/rpsm_%s.dat" % dat_fn, dtype=_N.int) SHUFFLE = 500 stds = _N.empty((SHUFFLE + 1, 9)) means = _N.empty((SHUFFLE + 1, 9)) lvs = _N.empty((SHUFFLE + 1, 9)) lens = _N.zeros((SHUFFLE + 1, 9), dtype=_N.int) win_st, win_dn, win_up, tie_st, tie_dn, tie_up, los_st, los_dn, los_up, wins, ties, loss = _rt.get_ME_WTL( hnd_dat, 0, hnd_dat.shape[0]) win_cons, _win_st_cons, _win_dn_cons, _win_up_cons = one_condition(
entropyU = _N.empty(len(partIDs)) entropyW = _N.empty(len(partIDs)) entropyT = _N.empty(len(partIDs)) entropyL = _N.empty(len(partIDs)) cpss = [] ip = 0 pid = -1 for partID in partIDs: pid += 1 _hnd_dat, start_time, end_time, UA, cnstr = _rt.return_hnd_dat( partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True, visit=None, expt="EEG1") dmp = depickle( getResultFN("%(rpsm)s/%(lb)d/WTL.dmp" % { "rpsm": partID, "lb": label })) _prob_mvs = dmp["cond_probs"] prob_mvs = _prob_mvs[:, 0:_hnd_dat.shape[0] - win] # is bigger than hand by win size prob_mvs = prob_mvs.reshape((3, 3, prob_mvs.shape[1])) dbehv = _emp.get_dbehv(prob_mvs, gk) maxs = _N.where((dbehv[0:-1] >= 0) & (dbehv[1:] < 0))[0] + (
random_walk = True flip_human_AI = False ######################################### process_keyval_args(globals(), sys.argv[1:]) a_q2, B_q2 = labels.get_a_B(label) scov = "WTL" ssig = "ME" if tr1 < 0: tr1 = None sran = "" s_flip = "_flip" if flip_human_AI else "" if not know_gt: _hnd_dat = _rd.return_hnd_dat(dat_fn, flip_human_AI=flip_human_AI) else: _hnd_dat = _N.loadtxt( "/Users/arai/nctc/Workspace/AIiRPS_SimDAT/rpsm_%s.dat" % dat_fn, dtype=_N.int) lmGT = depickle("/Users/arai/nctc/Workspace/AIiRPS_SimDAT/rpsm_%s.dmp" % dat_fn) Ts = lmGT["Ts_timeseries"] if tr1 is None: tr1 = _hnd_dat.shape[0] rr_hnd_dat = _N.array(_hnd_dat) else: rr_hnd_dat = _N.array(_hnd_dat[tr0:tr1]) inds = _N.arange(tr1)
flip_human_AI=False ######################################### process_keyval_args(globals(), sys.argv[1:]) a_q2, B_q2 = labels.get_a_B(label) scov = "WTL" ssig = "ME" if tr1 < 0: tr1 = None sran = "" s_flip = "_flip" if flip_human_AI else "" if not know_gt: #_hnd_dat = _rd.return_hnd_dat(dat_fn, flip_human_AI=flip_human_AI) _hnd_dat = _rd.return_hnd_dat(dat_fn, flip_human_AI=flip_human_AI, has_useragent=True, has_start_and_end_times=True, has_constructor=True) else: _hnd_dat = _N.loadtxt("/Users/arai/nctc/Workspace/AIiRPS_SimDAT/rpsm_%s.dat" % dat_fn, dtype=_N.int) lmGT = depickle("/Users/arai/nctc/Workspace/AIiRPS_SimDAT/rpsm_%s.dmp" % dat_fn) Ts = lmGT["Ts_timeseries"] if tr1 is None: tr1 = _hnd_dat.shape[0] rr_hnd_dat = _N.array(_hnd_dat) else: rr_hnd_dat = _N.array(_hnd_dat[tr0:tr1]) inds = _N.arange(tr1) if rndmz: _N.random.shuffle(inds) sran = "rndmz"
wtl_num_CNTR_nonCNTR = _N.zeros((len(datetms), 3, 3, 2)) di = -1 cts = _N.zeros((len(datetms), 3, 3, 3), dtype=_N.int) # date, Cond, rule, cntr,keep, ncntr conditions = [_W, _T, _L] for datetm in datetms: print("%s------------------------" % datetm) di += 1 CRs = _em.CRs(datetm) td, start_tm, end_tm = _rt.return_hnd_dat(datetm, has_useragent=True, has_start_and_end_times=True, has_constructor=True, flip_human_AI=False) # First find all the win conditions # _W followed by a win #ic = -1 for ic in range(3): #condition in range(3): #ic += 1 rep_cntr_noncntr, s_rep_cntr_noncntr = getTargetCR_cntrmvs(ic) #iWTLs = _N.where(td[0:-1, 2] == conditions[ic])[0] iWTLs = _N.where(td[0:-2, 2] == conditions[ic])[0]
def empirical_NGS(dat, SHUF=0, win=20, flip_human_AI=False, expt="EEG1", visit=None, dither_unobserved=False): _td, start_tm, end_tm, UA, cnstr, inp_meth, ini_percep, fin_percep = _rt.return_hnd_dat( dat, has_useragent=True, has_start_and_end_times=True, has_constructor=True, flip_human_AI=flip_human_AI, expt=expt, visit=visit) if _td is None: return None, None Tgame = _td.shape[0] ############ Several different dynamic conditional probabilities ############ We don't know what players look at, and how they think ############ about next move? Do they think in terms of RPS, or ############ do they think in terms of upgrades, downgrades or stays? ############ Using a model that more closely matches the way they think ############ will probably better capture their behavior cprobs = _N.zeros((SHUF + 1, 9, Tgame - win)) # UDS | WTL cprobsRPS = _N.zeros((SHUF + 1, 9, Tgame - win)) # RPS | WTL cprobsDSURPS = _N.zeros((SHUF + 1, 9, Tgame - win)) # UDS | RPS cprobsSTSW = _N.zeros((SHUF + 1, 6, Tgame - win)) # Stay,Switch | WTL ############ Raw move game-by-game data all_tds = _N.empty((SHUF + 1, _td.shape[0], _td.shape[1]), dtype=int) for shf in range(SHUF + 1): ########### allow randomly shuffling the data if shf > 0: inds = _N.arange(_td.shape[0]) _N.random.shuffle(inds) td = _N.array(_td[inds]) else: td = _td all_tds[shf] = td scores_wtl1 = _N.zeros(Tgame - 1, dtype=int) scores_rps0 = _N.zeros(Tgame - 1, dtype=int) scores_rps1 = _N.zeros(Tgame - 1, dtype=int) scores_tr10 = _N.zeros(Tgame - 1, dtype=int) # transition ################################# wtl 1 steps back wins_m1 = _N.where(td[0:Tgame - 1, 2] == 1)[0] ties_m1 = _N.where(td[0:Tgame - 1, 2] == 0)[0] loss_m1 = _N.where(td[0:Tgame - 1, 2] == -1)[0] ################################# rps 1 steps back R_m1 = _N.where(td[0:Tgame - 1, 0] == 1)[0] S_m1 = _N.where(td[0:Tgame - 1, 0] == 2)[0] P_m1 = _N.where(td[0:Tgame - 1, 0] == 3)[0] scores_wtl1[wins_m1] = 2 scores_wtl1[ties_m1] = 1 scores_wtl1[loss_m1] = 0 scores_rps1[R_m1] = 2 scores_rps1[S_m1] = 1 scores_rps1[P_m1] = 0 ################################# tr from 1->0 #####STAYS stays = _N.where(td[0:Tgame - 1, 0] == td[1:Tgame, 0])[0] scores_tr10[stays] = 2 #####DNGRAD dngrd = _N.where(((td[0:Tgame - 1, 0] == 1) & (td[1:Tgame, 0] == 2)) | ((td[0:Tgame - 1, 0] == 2) & (td[1:Tgame, 0] == 3)) | ((td[0:Tgame - 1, 0] == 3) & (td[1:Tgame, 0] == 1)))[0] scores_tr10[dngrd] = 1 #####UPGRAD upgrd = _N.where(((td[0:Tgame - 1, 0] == 1) & (td[1:Tgame, 0] == 3)) | ((td[0:Tgame - 1, 0] == 2) & (td[1:Tgame, 0] == 1)) | ((td[0:Tgame - 1, 0] == 3) & (td[1:Tgame, 0] == 2)))[0] scores_tr10[upgrd] = 0 #####ROCK rocks = _N.where(td[1:Tgame, 0] == 1)[0] scores_rps0[rocks] = 0 #####SCISSOR scissors = _N.where(td[1:Tgame, 0] == 2)[0] scores_rps0[scissors] = 1 #####PAPER papers = _N.where(td[1:Tgame, 0] == 3)[0] scores_rps0[papers] = 2 # UP | LOS = scores 0 # UP | TIE = scores 1 # UP | WIN = scores 2 # DN | LOS = scores 3 # DN | TIE = scores 4 # DN | WIN = scores 5 # ST | LOS = scores 6 # ST | TIE = scores 7 # ST | WIN = scores 8 scores = scores_wtl1 + 3 * scores_tr10 # UDS | WTL scoresRPS = scores_wtl1 + 3 * scores_rps0 # RPS | WTL scoresDSURPS = scores_rps1 + 3 * scores_tr10 # UDS | RPS scores_pr = scores_wtl1 i = 0 for i in range(0, Tgame - win): ###################################### n_win = len(_N.where(scores_pr[i:i + win] == 2)[0]) n_win_st = len(_N.where(scores[i:i + win] == 8)[0]) n_win_dn = len(_N.where(scores[i:i + win] == 5)[0]) n_win_up = len(_N.where(scores[i:i + win] == 2)[0]) n_win_R = len(_N.where(scoresRPS[i:i + win] == 8)[0]) n_win_S = len(_N.where(scoresRPS[i:i + win] == 5)[0]) n_win_P = len(_N.where(scoresRPS[i:i + win] == 2)[0]) ###################################### n_tie = len(_N.where(scores_pr[i:i + win] == 1)[0]) n_tie_st = len(_N.where(scores[i:i + win] == 7)[0]) n_tie_dn = len(_N.where(scores[i:i + win] == 4)[0]) n_tie_up = len(_N.where(scores[i:i + win] == 1)[0]) n_tie_R = len(_N.where(scoresRPS[i:i + win] == 7)[0]) n_tie_S = len(_N.where(scoresRPS[i:i + win] == 4)[0]) n_tie_P = len(_N.where(scoresRPS[i:i + win] == 1)[0]) ###################################### n_los = len(_N.where(scores_pr[i:i + win] == 0)[0]) n_los_st = len(_N.where(scores[i:i + win] == 6)[0]) n_los_dn = len(_N.where(scores[i:i + win] == 3)[0]) n_los_up = len(_N.where(scores[i:i + win] == 0)[0]) n_los_R = len(_N.where(scoresRPS[i:i + win] == 6)[0]) n_los_S = len(_N.where(scoresRPS[i:i + win] == 3)[0]) n_los_P = len(_N.where(scoresRPS[i:i + win] == 0)[0]) ###################################### n_R = len(_N.where(scores_rps1[i:i + win] == 2)[0]) n_R_st = len(_N.where(scoresDSURPS[i:i + win] == 8)[0]) n_R_dn = len(_N.where(scores[i:i + win] == 5)[0]) n_R_up = len(_N.where(scores[i:i + win] == 2)[0]) ###################################### n_S = len(_N.where(scores_rps1[i:i + win] == 1)[0]) n_S_st = len(_N.where(scoresDSURPS[i:i + win] == 7)[0]) n_S_dn = len(_N.where(scores[i:i + win] == 4)[0]) n_S_up = len(_N.where(scores[i:i + win] == 1)[0]) ###################################### n_P = len(_N.where(scores_rps1[i:i + win] == 0)[0]) n_P_st = len(_N.where(scoresDSURPS[i:i + win] == 6)[0]) n_P_dn = len(_N.where(scores[i:i + win] == 3)[0]) n_P_up = len(_N.where(scores[i:i + win] == 0)[0]) if n_win > 0: #cprobs[shf, 0, i] = n_win_st / n_win cprobs[shf, 0, i] = n_win_dn / n_win cprobs[shf, 1, i] = n_win_st / n_win cprobs[shf, 2, i] = n_win_up / n_win cprobsRPS[shf, 0, i] = n_win_R / n_win cprobsRPS[shf, 1, i] = n_win_S / n_win cprobsRPS[shf, 2, i] = n_win_P / n_win cprobsSTSW[shf, 0, i] = n_win_st / n_win cprobsSTSW[shf, 1, i] = (n_win_dn + n_win_up) / n_win else: # no wins observed, continue with last value cprobs[shf, 0, i] = cprobs[shf, 0, i - 1] cprobs[shf, 1, i] = cprobs[shf, 1, i - 1] cprobs[shf, 2, i] = cprobs[shf, 2, i - 1] cprobsRPS[shf, 0, i] = cprobsRPS[shf, 0, i - 1] cprobsRPS[shf, 1, i] = cprobsRPS[shf, 1, i - 1] cprobsRPS[shf, 2, i] = cprobsRPS[shf, 2, i - 1] cprobsSTSW[shf, 0, i] = cprobsSTSW[shf, 0, i - 1] cprobsSTSW[shf, 1, i] = cprobsSTSW[shf, 1, i - 1] if n_tie > 0: #cprobs[shf, 3, i] = n_tie_st / n_tie cprobs[shf, 3, i] = n_tie_dn / n_tie cprobs[shf, 4, i] = n_tie_st / n_tie cprobs[shf, 5, i] = n_tie_up / n_tie cprobsRPS[shf, 3, i] = n_tie_R / n_tie cprobsRPS[shf, 4, i] = n_tie_S / n_tie cprobsRPS[shf, 5, i] = n_tie_P / n_tie cprobsSTSW[shf, 2, i] = n_tie_st / n_tie cprobsSTSW[shf, 3, i] = (n_tie_dn + n_tie_up) / n_tie else: # no ties observed, continue with last value cprobs[shf, 3, i] = cprobs[shf, 3, i - 1] cprobs[shf, 4, i] = cprobs[shf, 4, i - 1] cprobs[shf, 5, i] = cprobs[shf, 5, i - 1] cprobsRPS[shf, 3, i] = cprobsRPS[shf, 3, i - 1] cprobsRPS[shf, 4, i] = cprobsRPS[shf, 4, i - 1] cprobsRPS[shf, 5, i] = cprobsRPS[shf, 5, i - 1] cprobsSTSW[shf, 2, i] = cprobsSTSW[shf, 2, i - 1] cprobsSTSW[shf, 3, i] = cprobsSTSW[shf, 3, i - 1] if n_los > 0: #cprobs[shf, 6, i] = n_los_st / n_los cprobs[shf, 6, i] = n_los_dn / n_los cprobs[shf, 7, i] = n_los_st / n_los cprobs[shf, 8, i] = n_los_up / n_los cprobsRPS[shf, 6, i] = n_los_R / n_los cprobsRPS[shf, 7, i] = n_los_S / n_los cprobsRPS[shf, 8, i] = n_los_P / n_los cprobsSTSW[shf, 4, i] = n_los_st / n_los cprobsSTSW[shf, 5, i] = (n_los_dn + n_los_up) / n_los else: cprobs[shf, 6, i] = cprobs[shf, 6, i - 1] cprobs[shf, 7, i] = cprobs[shf, 7, i - 1] cprobs[shf, 8, i] = cprobs[shf, 8, i - 1] cprobsRPS[shf, 6, i] = cprobsRPS[shf, 6, i - 1] cprobsRPS[shf, 7, i] = cprobsRPS[shf, 7, i - 1] cprobsRPS[shf, 8, i] = cprobsRPS[shf, 8, i - 1] cprobsSTSW[shf, 4, i] = cprobsSTSW[shf, 4, i - 1] cprobsSTSW[shf, 5, i] = cprobsSTSW[shf, 5, i - 1] ###################### if n_R > 0: #cprobs[shf, 0, i] = n_win_st / n_win cprobsDSURPS[shf, 0, i] = n_R_dn / n_R cprobsDSURPS[shf, 1, i] = n_R_st / n_R cprobsDSURPS[shf, 2, i] = n_R_up / n_R else: cprobsDSURPS[shf, 0, i] = cprobsDSURPS[shf, 0, i - 1] cprobsDSURPS[shf, 1, i] = cprobsDSURPS[shf, 1, i - 1] cprobsDSURPS[shf, 2, i] = cprobsDSURPS[shf, 2, i - 1] if n_S > 0: #cprobs[shf, 0, i] = n_win_st / n_win cprobsDSURPS[shf, 3, i] = n_S_dn / n_S cprobsDSURPS[shf, 4, i] = n_S_st / n_S cprobsDSURPS[shf, 5, i] = n_S_up / n_S else: cprobsDSURPS[shf, 3, i] = cprobsDSURPS[shf, 3, i - 1] cprobsDSURPS[shf, 4, i] = cprobsDSURPS[shf, 4, i - 1] cprobsDSURPS[shf, 5, i] = cprobsDSURPS[shf, 5, i - 1] if n_P > 0: #cprobs[shf, 0, i] = n_win_st / n_win cprobsDSURPS[shf, 6, i] = n_P_dn / n_P cprobsDSURPS[shf, 7, i] = n_P_st / n_P cprobsDSURPS[shf, 8, i] = n_P_up / n_P else: cprobsDSURPS[shf, 6, i] = cprobsDSURPS[shf, 6, i - 1] cprobsDSURPS[shf, 7, i] = cprobsDSURPS[shf, 7, i - 1] cprobsDSURPS[shf, 8, i] = cprobsDSURPS[shf, 8, i - 1] if dither_unobserved: # changing probability at next observation after # long period of not observing a condition (say W) for i in range(Tgame - win - 1, 0, -1): if cprobs[shf, 0, i] == cprobs[shf, 0, i - 1]: if _N.random.rand() < 0.5: # push it back cprobs[shf, 0, i - 1] = cprobs[shf, 0, i - 2] cprobs[shf, 1, i - 1] = cprobs[shf, 1, i - 2] cprobs[shf, 2, i - 1] = cprobs[shf, 2, i - 2] if cprobs[shf, 3, i] == cprobs[shf, 3, i - 1]: if _N.random.rand() < 0.5: # push it back cprobs[shf, 3, i - 1] = cprobs[shf, 3, i - 2] cprobs[shf, 4, i - 1] = cprobs[shf, 4, i - 2] cprobs[shf, 5, i - 1] = cprobs[shf, 5, i - 2] if cprobs[shf, 6, i] == cprobs[shf, 6, i - 1]: if _N.random.rand() < 0.5: # push it back cprobs[shf, 6, i - 1] = cprobs[shf, 6, i - 2] cprobs[shf, 7, i - 1] = cprobs[shf, 7, i - 2] cprobs[shf, 8, i - 1] = cprobs[shf, 8, i - 2] for i in range(Tgame - win - 1, 0, -1): if cprobsRPS[shf, 0, i] == cprobsRPS[shf, 0, i - 1]: if _N.random.rand() < 0.5: # push it back cprobsRPS[shf, 0, i - 1] = cprobsRPS[shf, 0, i - 2] cprobsRPS[shf, 1, i - 1] = cprobsRPS[shf, 1, i - 2] cprobsRPS[shf, 2, i - 1] = cprobsRPS[shf, 2, i - 2] if cprobsRPS[shf, 3, i] == cprobsRPS[shf, 3, i - 1]: if _N.random.rand() < 0.5: # push it back cprobsRPS[shf, 3, i - 1] = cprobsRPS[shf, 3, i - 2] cprobsRPS[shf, 4, i - 1] = cprobsRPS[shf, 4, i - 2] cprobsRPS[shf, 5, i - 1] = cprobsRPS[shf, 5, i - 2] if cprobsRPS[shf, 6, i] == cprobsRPS[shf, 6, i - 1]: if _N.random.rand() < 0.5: # push it back cprobsRPS[shf, 6, i - 1] = cprobsRPS[shf, 6, i - 2] cprobsRPS[shf, 7, i - 1] = cprobsRPS[shf, 7, i - 2] cprobsRPS[shf, 8, i - 1] = cprobsRPS[shf, 8, i - 2] for i in range(Tgame - win - 1, 0, -1): if cprobsDSURPS[shf, 0, i] == cprobsDSURPS[shf, 0, i - 1]: if _N.random.rand() < 0.5: # push it back cprobsDSURPS[shf, 0, i - 1] = cprobsDSURPS[shf, 0, i - 2] cprobsDSURPS[shf, 1, i - 1] = cprobsDSURPS[shf, 1, i - 2] cprobsDSURPS[shf, 2, i - 1] = cprobsDSURPS[shf, 2, i - 2] if cprobsDSURPS[shf, 3, i] == cprobsDSURPS[shf, 3, i - 1]: if _N.random.rand() < 0.5: # push it back cprobsDSURPS[shf, 3, i - 1] = cprobsDSURPS[shf, 3, i - 2] cprobsDSURPS[shf, 4, i - 1] = cprobsDSURPS[shf, 4, i - 2] cprobsDSURPS[shf, 5, i - 1] = cprobsDSURPS[shf, 5, i - 2] if cprobsDSURPS[shf, 6, i] == cprobsDSURPS[shf, 6, i - 1]: if _N.random.rand() < 0.5: # push it back cprobsDSURPS[shf, 6, i - 1] = cprobsDSURPS[shf, 6, i - 2] cprobsDSURPS[shf, 7, i - 1] = cprobsDSURPS[shf, 7, i - 2] cprobsDSURPS[shf, 8, i - 1] = cprobsDSURPS[shf, 8, i - 2] return cprobs, cprobsRPS, cprobsDSURPS, cprobsSTSW, all_tds, Tgame
def empirical_NGS_concat_conds(dat, SHUF=0, win=20, flip_human_AI=False, expt="EEG1", visit=None, know_gt=False): """ concatenate given 1 2 3 4 5 6 7 8 9 10 11 12 DW UT ST UL DW DL UT SW ST UT UW SL concat: 4 Wins at a time 1 5 8 11 DW DW SW UW now p(D|W) previous method: 4 moves at a time DW UT ST UL """ _td, start_tm, end_tm, UA, cnstr, inp_meth, ini_percep, fin_percep, lmGT = _rt.return_hnd_dat( dat, has_useragent=True, has_start_and_end_times=True, has_constructor=True, flip_human_AI=flip_human_AI, expt=expt, visit=visit, know_gt=know_gt) cWin = win if _td is None: print("_td is None") return None, None Tgame = _td.shape[0] ############ Several different dynamic conditional probabilities ############ We don't know what players look at, and how they think ############ about next move? Do they think in terms of RPS, or ############ do they think in terms of upgrades, downgrades or stays? ############ Using a model that more closely matches the way they think ############ will probably better capture their behavior cprobsDSU_WTL = _N.ones((SHUF + 1, 9, Tgame - win)) * -1 # UDS | WTL cprobsRPS_WTL = _N.ones((SHUF + 1, 9, Tgame - win)) * -1 # RPS | WTL cprobsDSU_RPS = _N.ones((SHUF + 1, 9, Tgame - win)) * -1 # UDS | RPS cprobsDSU_AIRPS = _N.ones((SHUF + 1, 9, Tgame - win)) * -1 # UDS | RPS cprobsSTSW = _N.ones((SHUF + 1, 6, Tgame - win)) * -1 # Stay,Switch | WTL pConds = [ cprobsDSU_WTL, cprobsRPS_WTL, cprobsDSU_RPS, cprobsDSU_AIRPS, cprobsSTSW ] ############ Raw move game-by-game data all_tds = _N.empty((SHUF + 1, _td.shape[0], _td.shape[1]), dtype=int) mCRs0 = marginalCR(_td) for shf in range(SHUF + 1): ########### allow randomly shuffling the data if shf > 0: #OK = False #while not OK: inds = _N.arange(_td.shape[0]) _N.random.shuffle(inds) td = _N.array(_td[inds]) # mCRs = marginalCR(td) #probdiff = _N.abs(mCRs0 - mCRs) # if len(_N.where(probdiff > 0.2)[0]) < 2: # print("found") # OK = True else: td = _td all_tds[shf] = td ################################# wtl 1 steps back wins_m1 = _N.where(td[0:Tgame - 1, 2] == 1)[0] ties_m1 = _N.where(td[0:Tgame - 1, 2] == 0)[0] loss_m1 = _N.where(td[0:Tgame - 1, 2] == -1)[0] ################################# rps 1 steps back R_m1 = _N.where(td[0:Tgame - 1, 0] == 1)[0] S_m1 = _N.where(td[0:Tgame - 1, 0] == 2)[0] P_m1 = _N.where(td[0:Tgame - 1, 0] == 3)[0] ################################# rps 1 steps back AI_R_m1 = _N.where(td[0:Tgame - 1, 1] == 1)[0] AI_S_m1 = _N.where(td[0:Tgame - 1, 1] == 2)[0] AI_P_m1 = _N.where(td[0:Tgame - 1, 1] == 3)[0] icndT = -1 for conds in [[wins_m1, ties_m1, loss_m1], [wins_m1, ties_m1, loss_m1], [R_m1, S_m1, P_m1], [AI_R_m1, AI_S_m1, AI_P_m1], [wins_m1, ties_m1, loss_m1]]: icndT += 1 #print("%(wm)d %(tm)d %(lm)d" % {"wm" : wins_m1[-1], "tm" : ties_m1[-1], "lm" : loss_m1[-1]}) pCond = pConds[icndT] icnd = -1 for cond_m1 in conds: icnd += 1 if (icndT == 0) or (icndT == 2) or ( icndT == 3): # DSU | WTL or DSU | RPS or DSU | AI_RPS for ig in range(len(cond_m1) - cWin): tMid = (cond_m1[ig] + cond_m1[ig + cWin - 1]) // 2 if tMid == Tgame - cWin: print("!!!!!! PROBLEM") tMid -= 1 # stays if (icndT == 0) or (icndT == 2): n_stays = len( _N.where(td[cond_m1[ig:ig + cWin], 0] == td[cond_m1[ig:ig + cWin] + 1, 0])[0]) #####UPGRAD n_dngrd = len( _N.where( ((td[cond_m1[ig:ig + cWin], 0] == 1) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 2)) | ((td[cond_m1[ig:ig + cWin], 0] == 2) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 3)) | ((td[cond_m1[ig:ig + cWin], 0] == 3) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 1)))[0]) n_upgrd = len( _N.where( ((td[cond_m1[ig:ig + cWin], 0] == 1) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 3)) | ((td[cond_m1[ig:ig + cWin], 0] == 2) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 1)) | ((td[cond_m1[ig:ig + cWin], 0] == 3) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 2)))[0]) else: n_stays = len( _N.where(td[cond_m1[ig:ig + cWin], 1] == td[cond_m1[ig:ig + cWin] + 1, 0])[0]) #####UPGRAD n_dngrd = len( _N.where( ((td[cond_m1[ig:ig + cWin], 1] == 1) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 2)) | ((td[cond_m1[ig:ig + cWin], 1] == 2) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 3)) | ((td[cond_m1[ig:ig + cWin], 1] == 3) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 1)))[0]) n_upgrd = len( _N.where( ((td[cond_m1[ig:ig + cWin], 1] == 1) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 3)) | ((td[cond_m1[ig:ig + cWin], 1] == 2) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 1)) | ((td[cond_m1[ig:ig + cWin], 1] == 3) & (td[cond_m1[ig:ig + cWin] + 1, 0] == 2)))[0]) # DN | cond ## This probability is at t in middle of window pCond[shf, icnd * 3, tMid] = n_dngrd / cWin pCond[shf, icnd * 3 + 1, tMid] = n_stays / cWin pCond[shf, icnd * 3 + 2, tMid] = n_upgrd / cWin elif icndT == 1: # RSP | WTL for ig in range(len(cond_m1) - cWin): tMid = (cond_m1[ig] + cond_m1[ig + cWin - 1]) // 2 if tMid == Tgame - cWin: print("!!!!!! PROBLEM") tMid -= 1 #####R n_R = len( _N.where((td[cond_m1[ig:ig + cWin] + 1, 0] == 1))[0]) n_S = len( _N.where((td[cond_m1[ig:ig + cWin] + 1, 0] == 2))[0]) n_P = len( _N.where((td[cond_m1[ig:ig + cWin] + 1, 0] == 3))[0]) pCond[shf, icnd * 3, tMid] = n_R / cWin pCond[shf, icnd * 3 + 1, tMid] = n_S / cWin pCond[shf, icnd * 3 + 2, tMid] = n_P / cWin elif (icndT == 4): # ST,SW | WTL for ig in range(len(cond_m1) - cWin): tMid = (cond_m1[ig] + cond_m1[ig + cWin - 1]) // 2 if tMid == Tgame - cWin: print("!!!!!! PROBLEM") tMid -= 1 # stays n_stays = len( _N.where(td[cond_m1[ig:ig + cWin], 0] == td[cond_m1[ig:ig + cWin] + 1, 0])[0]) n_switch = len( _N.where( td[cond_m1[ig:ig + cWin], 0] != td[cond_m1[ig:ig + cWin] + 1, 0])[0]) # DN | cond pCond[shf, icnd * 2, tMid] = n_stays / cWin pCond[shf, icnd * 2 + 1, tMid] = n_switch / cWin ############################################### ############################################### # go back, fill in the -1s ############################################### ############################################### if icndT != 4: definedTs = _N.where(pCond[shf, icnd * 3] != -1)[0] definedTs_last = _N.array(definedTs.tolist() + [Tgame - win]) # first -1s fill with first observed value pCond[shf, icnd * 3, 0:definedTs_last[0]] = pCond[shf, icnd * 3, definedTs_last[0]] pCond[shf, icnd * 3 + 1, 0:definedTs_last[0]] = pCond[shf, icnd * 3 + 1, definedTs_last[0]] pCond[shf, icnd * 3 + 2, 0:definedTs_last[0]] = pCond[shf, icnd * 3 + 2, definedTs_last[0]] # if I find a defined probability, go forward in time until next defined one, and fill it with my current value for itd in range(len(definedTs_last) - 1): pCond[shf, icnd * 3, definedTs_last[itd] + 1:definedTs_last[itd + 1]] = pCond[shf, icnd * 3, definedTs_last[itd]] pCond[shf, icnd * 3 + 1, definedTs_last[itd] + 1:definedTs_last[itd + 1]] = pCond[shf, icnd * 3 + 1, definedTs_last[itd]] pCond[shf, icnd * 3 + 2, definedTs_last[itd] + 1:definedTs_last[itd + 1]] = pCond[shf, icnd * 3 + 2, definedTs_last[itd]] #print(pCond[shf, icnd*3]) elif icndT == 4: definedTs = _N.where(pCond[shf, icnd * 2] != -1)[0] definedTs_last = _N.array(definedTs.tolist() + [Tgame - win]) # first -1s fill with first observed value pCond[shf, icnd * 2, 0:definedTs_last[0]] = pCond[shf, icnd * 2, definedTs_last[0]] pCond[shf, icnd * 2 + 1, 0:definedTs_last[0]] = pCond[shf, icnd * 2 + 1, definedTs_last[0]] # if I find a defined probability, go forward in time until next defined one, and fill it with my current value for itd in range(len(definedTs_last) - 1): pCond[shf, icnd * 2, definedTs_last[itd] + 1:definedTs_last[itd + 1]] = pCond[shf, icnd * 2, definedTs_last[itd]] pCond[shf, icnd * 2 + 1, definedTs_last[itd] + 1:definedTs_last[itd + 1]] = pCond[shf, icnd * 2 + 1, definedTs_last[itd]] # for conds in [[wins_m1, ties_m1, loss_m1], [wins_m1, ties_m1, loss_m1], [R_m1, S_m1, P_m1], [wins_m1, ties_m1, loss_m1]]: return cprobsDSU_WTL, cprobsRPS_WTL, cprobsDSU_RPS, cprobsDSU_AIRPS, all_tds, Tgame
def empirical_NGS(dat, SHUF=0, win=20, flip_human_AI=False, covariates=_AIconst._WTL, expt="EEG1", visit=None): _td, start_tm, end_tm, UA, cnstr, inp_meth, ini_percep, fin_percep = _rt.return_hnd_dat(dat, has_useragent=True, has_start_and_end_times=True, has_constructor=True, flip_human_AI=flip_human_AI, expt=expt, visit=visit) if _td is None: return None, None Tgame= _td.shape[0] cprobs = _N.zeros((SHUF+1, 9, Tgame-win)) cprobsSTSW = _N.zeros((SHUF+1, 6, Tgame-win)) all_tds = _N.empty((SHUF+1, _td.shape[0], _td.shape[1]), dtype=_N.int) for shf in range(SHUF+1): if shf > 0: inds = _N.arange(_td.shape[0]) _N.random.shuffle(inds) td = _N.array(_td[inds]) else: td = _td all_tds[shf] = td scores_wtl1 = _N.zeros(Tgame-1, dtype=_N.int) scores_tr10 = _N.zeros(Tgame-1, dtype=_N.int) # transition ################################# wtl 1 steps back if covariates == _AIconst._WTL: wins_m1 = _N.where(td[0:Tgame-1, 2] == 1)[0] ties_m1 = _N.where(td[0:Tgame-1, 2] == 0)[0] loss_m1 = _N.where(td[0:Tgame-1, 2] == -1)[0] elif covariates == _AIconst._HUMRPS: wins_m1 = _N.where(td[0:Tgame-1, 0] == 1)[0] ties_m1 = _N.where(td[0:Tgame-1, 0] == 2)[0] loss_m1 = _N.where(td[0:Tgame-1, 0] == 3)[0] elif covariates == _AIconst._AIRPS: wins_m1 = _N.where(td[0:Tgame-1, 1] == 1)[0] ties_m1 = _N.where(td[0:Tgame-1, 1] == 2)[0] loss_m1 = _N.where(td[0:Tgame-1, 1] == 3)[0] scores_wtl1[wins_m1] = 2 scores_wtl1[ties_m1] = 1 scores_wtl1[loss_m1] = 0 ################################# tr from 1->0 #####STAYS stays = _N.where(td[0:Tgame-1, 0] == td[1:Tgame, 0])[0] scores_tr10[stays] = 2 #####DNGRAD dngrd = _N.where(((td[0:Tgame-1, 0] == 1) & (td[1:Tgame, 0] == 2)) | ((td[0:Tgame-1, 0] == 2) & (td[1:Tgame, 0] == 3)) | ((td[0:Tgame-1, 0] == 3) & (td[1:Tgame, 0] == 1)))[0] scores_tr10[dngrd] = 1 #####UPGRAD upgrd = _N.where(((td[0:Tgame-1, 0] == 1) & (td[1:Tgame, 0] == 3)) | ((td[0:Tgame-1, 0] == 2) & (td[1:Tgame, 0] == 1)) | ((td[0:Tgame-1, 0] == 3) & (td[1:Tgame, 0] == 2)))[0] scores_tr10[upgrd] = 0 # UP | LOS = scores 0 # UP | TIE = scores 1 # UP | WIN = scores 2 # DN | LOS = scores 3 # DN | TIE = scores 4 # DN | WIN = scores 5 # ST | LOS = scores 6 # ST | TIE = scores 7 # ST | WIN = scores 8 scores = scores_wtl1 + 3*scores_tr10 scores_pr = scores_wtl1 i = 0 for i in range(0, Tgame-win): n_win = len(_N.where(scores_pr[i:i+win] == 2)[0]) n_win_st = len(_N.where(scores[i:i+win] == 8)[0]) n_win_dn = len(_N.where(scores[i:i+win] == 5)[0]) n_win_up = len(_N.where(scores[i:i+win] == 2)[0]) n_tie = len(_N.where(scores_pr[i:i+win] == 1)[0]) n_tie_st = len(_N.where(scores[i:i+win] == 7)[0]) n_tie_dn = len(_N.where(scores[i:i+win] == 4)[0]) n_tie_up = len(_N.where(scores[i:i+win] == 1)[0]) n_los = len(_N.where(scores_pr[i:i+win] == 0)[0]) n_los_st = len(_N.where(scores[i:i+win] == 6)[0]) n_los_dn = len(_N.where(scores[i:i+win] == 3)[0]) n_los_up = len(_N.where(scores[i:i+win] == 0)[0]) if n_win > 0: #cprobs[shf, 0, i] = n_win_st / n_win cprobs[shf, 0, i] = n_win_dn / n_win cprobs[shf, 1, i] = n_win_st / n_win cprobs[shf, 2, i] = n_win_up / n_win cprobsSTSW[shf, 0, i] = n_win_st / n_win cprobsSTSW[shf, 1, i] = (n_win_dn+n_win_up) / n_win else: cprobs[shf, 0, i] = cprobs[shf, 0, i-1] cprobs[shf, 1, i] = cprobs[shf, 1, i-1] cprobs[shf, 2, i] = cprobs[shf, 2, i-1] cprobsSTSW[shf, 0, i] = cprobsSTSW[shf, 0, i-1] cprobsSTSW[shf, 1, i] = cprobsSTSW[shf, 1, i-1] if n_tie > 0: #cprobs[shf, 3, i] = n_tie_st / n_tie cprobs[shf, 3, i] = n_tie_dn / n_tie cprobs[shf, 4, i] = n_tie_st / n_tie cprobs[shf, 5, i] = n_tie_up / n_tie cprobsSTSW[shf, 2, i] = n_tie_st / n_tie cprobsSTSW[shf, 3, i] = (n_tie_dn+n_tie_up) / n_tie else: cprobs[shf, 3, i] = cprobs[shf, 3, i-1] cprobs[shf, 4, i] = cprobs[shf, 4, i-1] cprobs[shf, 5, i] = cprobs[shf, 5, i-1] cprobsSTSW[shf, 2, i] = cprobsSTSW[shf, 2, i-1] cprobsSTSW[shf, 3, i] = cprobsSTSW[shf, 3, i-1] if n_los > 0: #cprobs[shf, 6, i] = n_los_st / n_los cprobs[shf, 6, i] = n_los_dn / n_los cprobs[shf, 7, i] = n_los_st / n_los cprobs[shf, 8, i] = n_los_up / n_los cprobsSTSW[shf, 4, i] = n_los_st / n_los cprobsSTSW[shf, 5, i] = (n_los_dn+n_los_up) / n_los else: cprobs[shf, 6, i] = cprobs[shf, 6, i-1] cprobs[shf, 7, i] = cprobs[shf, 7, i-1] cprobs[shf, 8, i] = cprobs[shf, 8, i-1] cprobsSTSW[shf, 4, i] = cprobsSTSW[shf, 4, i-1] cprobsSTSW[shf, 5, i] = cprobsSTSW[shf, 5, i-1] return cprobs, cprobsSTSW, all_tds, Tgame
win = 3 maxT = 300 expt = "SIMHUM1" trig_infrd = [] trig_GT_rc = [] #for date in ["20110101_0000-00", "20110101_0000-05", "20110101_0000-10", "20110101_0000-20", "20110101_0000-25", "20110101_0000-30", "20110101_0000-35", "20110101_0000-40", "20110101_0000-45", "20110101_0000-50", "20110101_0000-55"]: for sec in secs_as_string( 0, 60): #, "01", "02", "03", "04", "05", "06", "07", "08", "09"]: #for date in ["20110101_0000-00"]: date = "20110101_0000-%s" % sec td, start_time, end_time, UA, cnstr, inp_meth, ini_percep, fin_percep = _rt.return_hnd_dat( date, has_useragent=True, has_start_and_end_times=True, has_constructor=True, expt=expt, visit=1) ngs, ngsRPS, ngsDSURPS, ngsSTSW, all_tds, TGames = empirical.empirical_NGS_concat_conds( date, win=win, SHUF=0, flip_human_AI=False, expt=expt, visit=1) datdmp = "/Users/arai/Sites/taisen/DATA/%(e)s/20110101/%(d)s/1/block1_AI.dmp" % { "d": date, "e": expt } lm = depickle(datdmp) gk_w = 2 gk = _Am.gauKer(gk_w)
def pkg_all_data(partID): """ d_reprtd_start: delay RPSM game start relative to DSi - as calculated by reported JS start time and DSi start time. dt_sys_clks: how much system times are off. d_reprtd_start = RPSM(start) - DSi(start) RPSM say 12:00:00, DSi says 11:59:30 +30 if same_time_RPSM is 12:00:00 and same_time_EEG is 11:59:35, then d_sys_clks = 25 11:59:35 (RPSM) and 11:59:30 (DSi) als d_start = d_reprtd_start - d_sys_clks if d_start > 0, RPSM started AFTER EEG if d_start < 0, RPSM started BEFORE EEG """ print("partID %s" % partID) dat_pkg = {} dsi_fn = rpsms.rpsm_partID_as_key[partID] same_time_RPSM = params4partID[partID][0] # from calibration.html same_time_EEG = params4partID[partID][1] # from calibration.html rpsm_fn = "rpsm_%s.dat" % partID _hnd_dat, start_time, end_time = _rt.return_hnd_dat( partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True) reprtd_start_RPSM_str = start_time #rpsm_key[8:15] # from key name if dsi_fn is not None: reprtd_start_EEG_str = dsi_fn[10:18] day = partID[0:8] time = partID[9:] print(day) print(time) eeg_dir = "~/Sites/taisen/DATA/EEG1/%(day)s/%(dt)s/" % { "day": day, "dt": partID } trials = _hnd_dat.shape[0] print("!!!!! trials %d" % trials) # the weight each one should have is just proportional to the # of events # of the condition (WTL) observed. tr0 = 0 tr1 = trials label = params4partID[partID][2] #covWTLRPS = dats[rpsm_key][6] if dats[rpsm_key][6] is not None else [_N.array([_W, _T, _L]), _N.array([_R, _P, _S])] #covWTLRPS = dats[rpsm_key][6] if dats[rpsm_key][6] is not None else [_N.array([_W, _T, _L])] armv_ver = params4partID[partID][3] gcoh_ver = params4partID[partID][4] win_spec, slideby_spec = _ppv.get_win_slideby(gcoh_ver) win_gcoh = win_spec slideby_gcoh = slideby_spec gcoh_fn = "gcoh_%(ws)d_%(ss)d" % {"ws": win_spec, "ss": slideby_spec} hnd_dat = _N.array(_hnd_dat[tr0:tr1]) #0:trials-trim_trials]) """ ## calculate weight each conditional stay_win, strg_win, wekr_win, stay_tie, wekr_tie, strg_tie, stay_los, wekr_los, strg_los, win_cond, tie_cond, los_cond = _rt.get_ME_WTL(hnd_dat, tr0, tr1) # p(stay | W) nWins = len(win_cond) nTies = len(tie_cond) nLoss = len(los_cond) cond_events = [[stay_win, wekr_win, strg_win], [stay_tie, wekr_tie, strg_tie], [stay_los, wekr_los, strg_los]] marg_cond_events = [win_cond, tie_cond, los_cond] cond_wgts = _N.array([[[win_cond.shape[0]]], [[tie_cond.shape[0]]], [[los_cond.shape[0]]]]) cond_wgts = cond_wgts / _N.sum(cond_wgts) """ dat_pkg["lat"] = [] dat_pkg["behv"] = [] dat_pkg["behv_ts"] = [] savgol_win = params4partID[partID][5] #%(dat)s,%(rel)s,%(cov)s%(ran)s sum_chosen_behv_sig = None #_N.zeros((len(behv_list), Tm1)) sigcov_behv_sig = None sigcov_behv_fsig = None behv_list = _N.array([0, 1, 2], _N.int) fig = _plt.figure(figsize=(10, 8)) for bi in range(3): #covWTLRPS = _N.array([_W, _T, _L])# if behv_list[bi] == _ME_WTL else _N.array([_R, _P, _S]) if behv_list[bi] == _cnst._WTL: sig_cov = _N.array([_W, _T, _L]) else: sig_cov = _N.array([_R, _P, _S]) sig_cov = behv_list[bi] behv_file = fns[bi] print( getResultFN("%(rpsm)s/%(lb)d/%(fl)s.dmp" % { "rpsm": partID, "fl": behv_file, "lb": label })) dmp = depickle( getResultFN("%(rpsm)s/%(lb)d/%(fl)s.dmp" % { "rpsm": partID, "fl": behv_file, "lb": label })) cond_probs = dmp["cond_probs"] cond_probs = cond_probs.reshape((3, 3, cond_probs.shape[1])) Tm1 = cond_probs.shape[2] # because AR1 filter is shorter by 1 # dNGS then should at most be Tm1 - 1 print("trials %(tr)d Tm1 %(Tm1)d" % {"tr": trials, "Tm1": Tm1}) if sigcov_behv_sig is None: # Tm1 - 1 (because derivative) sigcov_behv_sig = _N.zeros((3, Tm1 - 1)) sigcov_behv_fsig = _N.zeros((3, Tm1 - 1)) prob_mvs = cond_probs #prob_mvs[1] *= 0.001 # make TIE condition contribution small prob_fmvs = _N.zeros((3, 3, Tm1)) for iw in range(3): for ix in range(3): # if savgol_win is not None: # prob_fmvs[iw, ix] = savgol_filter(prob_mvs[iw, ix], savgol_win, 3) # window size 51, polynomial ord # else: prob_fmvs[iw, ix] = prob_mvs[iw, ix] these_covs = _N.array([0, 1, 2]) # sum over WTL condition first sigcov_behv_sig[bi] = _N.sum(_N.sum(_N.abs( _N.diff(prob_mvs[these_covs], axis=2)), axis=1), axis=0) sigcov_behv_fsig[bi] = _N.sum(_N.sum(_N.abs( _N.diff(prob_fmvs[these_covs], axis=2)), axis=1), axis=0) n = 0 fig.add_subplot(3, 2, bi * 2 + 1) bhv = sigcov_behv_sig[bi] _plt.acorr(bhv - _N.mean(bhv), maxlags=30) _plt.grid() fig.add_subplot(3, 2, bi * 2 + 2) fbhv = sigcov_behv_fsig[bi] _plt.acorr(fbhv - _N.mean(fbhv), maxlags=30) _plt.grid() print(".................................. %d" % bi) print(sigcov_behv_sig[bi]) print(sigcov_behv_fsig[bi]) # bhv1 = sigcov_behv_sig[0] # bhv2 = sigcov_behv_sig[1] # fig.add_subplot(3, 2, 5) # _plt.xcorr(bhv1 - _N.mean(bhv1), bhv2 - _N.mean(bhv2), maxlags=30) # bhv1 = sigcov_behv_fsig[0] # bhv2 = sigcov_behv_fsig[1] # fig.add_subplot(3, 2, 6) # _plt.xcorr(bhv1 - _N.mean(bhv1), bhv2 - _N.mean(bhv2), maxlags=30) print(sigcov_behv_sig) dat_pkg["behv"] = sigcov_behv_sig print(sigcov_behv_fsig) dat_pkg["fbehv"] = sigcov_behv_fsig dat_pkg["savgol_win"] = savgol_win # It is 2: because derivative of filter signal. # original hand data: size N. N-1 filtered time points, N-2 derivative points. So our behavioral data is size N-2 dat_pkg["behv_ts"] = hnd_dat[2:, 3] print("behv_ts shape") print(dat_pkg["behv_ts"].shape) dat_pkg["hnd_dat"] = hnd_dat #chg_rps = _N.loadtxt("Results/%s/mdl7b_chg_rps_mns" % rpsm_key) #btp_rps = _N.loadtxt("Results/%s/mdl7b_btp_rps_mns" % rpsm_key) # combine this with time stamp ########### Reported start times of RPS, EEG print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") reprtd_start_RPSM = int(reprtd_start_RPSM_str[8:10]) * 3600 + 60 * int( reprtd_start_RPSM_str[10:12]) + int(reprtd_start_RPSM_str[13:15]) if gcoh_fn is not None: reprtd_start_EEG = int(reprtd_start_EEG_str[0:2]) * 3600 + 60 * int( reprtd_start_EEG_str[3:5]) + int(reprtd_start_EEG_str[6:8]) else: reprtd_start_EEG = reprtd_start_RPSM d_reprtd_start = reprtd_start_RPSM - reprtd_start_EEG rpsm_hrs, rpsm_min, rpsm_secs = same_time_RPSM.split(":") eeg_hrs, eeg_min, eeg_secs = same_time_EEG.split(":") t_rpsm = int(rpsm_hrs) * 3600 + int(rpsm_min) * 60 + int(rpsm_secs) t_eeg = int(eeg_hrs) * 3600 + int(eeg_min) * 60 + int(eeg_secs) d_start = d_reprtd_start - (t_rpsm - t_eeg) print( "recording start time difference between page load and EEG hit record (negative means EEG started earlier) %d" % d_start) ####################################### hnd_dat[:, 3] += d_start * 1000 # RPS hands N pts # latent state is paired with previous hand and next hand obs (N-1) pts # diff latent state is difference between points (N-2) pts #dchg_wtl_with_ts[:, 3] = hnd_dat[2:, 3] #dbtp_wtl_with_ts[:, 3] = hnd_dat[2:, 3] #dat_pkg["dchg_wtl_with_ts"] = dchg_wtl_with_ts #dat_pkg["dbtp_wtl_with_ts"] = dbtp_wtl_with_ts if gcoh_fn is not None: eeg_dat = _N.loadtxt( "../DSi_dat/%(dsf)s_artfctrmvd/v%(av)d/%(dsf)s_artfctrmvd_v%(av)d.dat" % { "dsf": dsi_fn, "av": armv_ver, "gv": gcoh_ver }) gcoh_lm = depickle( "../DSi_dat/%(dsf)s_artfctrmvd/v%(av)d/%(dsf)s_%(gf)s_v%(av)d%(gv)d.dmp" % { "gf": gcoh_fn, "dsf": dsi_fn, "av": armv_ver, "gv": gcoh_ver }) if not os.access(getResultFN("%(dsf)s" % {"dsf": dsi_fn}), os.F_OK): os.mkdir(getResultFN("%(dsf)s" % {"dsf": dsi_fn})) savedir = getResultFN("%(dsf)s/v%(av)d%(gv)d" % { "gf": gcoh_fn, "dsf": dsi_fn, "av": armv_ver, "gv": gcoh_ver }) if not os.access(savedir, os.F_OK): os.mkdir(savedir) if gcoh_fn is not None: gcoh_fs = gcoh_lm["fs"] imag_evs = gcoh_lm["VEC"][:, :, 0:2] gcoh = gcoh_lm["Cs"] print("imag_evs.shape") print(imag_evs.shape) num_f_lvls = len(gcoh_fs) L_gcoh = imag_evs.shape[0] nChs = imag_evs.shape[3] real_evs = _N.empty((2, L_gcoh, num_f_lvls, nChs)) print("real_evs.shape") print(real_evs.shape) for ti in range(L_gcoh): real_evs[0, ti] = _N.abs(imag_evs[ti, :, 0]) real_evs[1, ti] = _N.abs(imag_evs[ti, :, 1]) """ mn = _N.mean(real_evs, axis=0) sd = _N.std(real_evs, axis=0) OUTLR = 10 outlrs = [] for ifr in range(num_f_lvls): for ich in range(nChs): abv = _N.where(real_evs[:, ifr, ich] > mn[ifr, ich] + OUTLR*sd[ifr, ich])[0] bel = _N.where(real_evs[:, ifr, ich] < mn[ifr, ich] - OUTLR*sd[ifr, ich])[0] outlrs.extend(abv) outlrs.extend(bel) unq_outlrs = _N.unique(outlrs) for io in unq_outlrs[0:-1]: real_evs[io+1] = real_evs[io] + 0.1*sd*_N.random.randn() """ dat_pkg["EIGVS"] = real_evs dat_pkg["fs"] = gcoh_fs dat_pkg["Cs"] = gcoh print(eeg_dat.shape[0]) print(win_gcoh) print(slideby_gcoh) dat_pkg["ts_gcoh"] = overlapping_window_center_times( eeg_dat.shape[0], win_gcoh, slideby_gcoh, 300.) print(dat_pkg["ts_gcoh"]) dat_pkg["gcoh_fn"] = gcoh_fn #### time ts_eeg_dfind = _N.linspace(0, eeg_dat.shape[0] / 300., eeg_dat.shape[0]) ch_spectrograms = [] maxHz = 50 for ch in range(21): spectrograms = [] fs, ts, Sxx = _ss.spectrogram(eeg_dat[:, ch], fs=300, nperseg=win_spec, noverlap=(win_spec - slideby_spec)) use_fs = _N.where(fs < maxHz)[0] for ihz in use_fs: spectrograms.append(Sxx[ihz]) ch_spectrograms.append(_N.array(spectrograms)) dat_pkg["ch_spectrograms"] = ch_spectrograms dat_pkg["ts_spectrograms"] = ts dat_pkg["fs_spectrograms"] = fs dat_pkg["eeg_smp_dt"] = 1. / 300 dat_pkg["win_gcoh"] = win_gcoh dat_pkg["slide_gcoh"] = slideby_gcoh dat_pkg["win_spec"] = win_spec dat_pkg["slide_spec"] = slideby_spec return savedir, dat_pkg, win_gcoh, slideby_gcoh, armv_ver, gcoh_ver, label
"FixedSequence(__moRSP__, [3, 1, 2, 3, 2, 1, 2, 3, 3, 1, 1, 1, 2, 1, 3, 3, 2, 1, 2, 3, 3, 1, 2, 1, 2, 1, 3, 2, 2, 3, 2, 1, 3, 3, 2, 2, 3, 1, 3, 1]);", "Mimic(__moRSP__, 0, 0.2);", "WTL(__moRSP__, [0.05, 0.85, 0.1], [1/3, 1/3, 1/3], [1/3, 1/3, 1/3], false);"] pid = 0 for partID in partIDs: pid += 1 AQ28scrs[pid-1], soc_skils[pid-1], rout[pid-1], switch[pid-1], imag[pid-1], fact_pat[pid-1] = _rt.AQ28("/Users/arai/Sites/taisen/DATA/TMB1/%(date)s/%(pID)s/AQ29.txt" % {"date" : partIDs[pid-1][0:8], "pID" : partIDs[pid-1]}) frmgm = 20 for blk in range(4): # the blocks here are relative to 'the_cnstrs' pid = 0 for partID in partIDs: pid += 1 print("partID %s" % partID) _hnd_dat, start_time, end_time, UA, cnstr = _rt.return_hnd_dat(partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True, visit=1, expt="TMB1", block=(blk+1)) ind = the_cnstrs.index(cnstr) netwins[ind, pid-1] = _N.sum(_hnd_dat[frmgm:, 2]) ############# HP wins case wins = _N.where(_hnd_dat[frmgm:-1, 2] == 1)[0]+frmgm # HP wins ww = _N.where(_hnd_dat[wins+1, 2] == 1)[0] lw = _N.where(_hnd_dat[wins+1, 2] == -1)[0] win_aft_win[ind, pid-1] = len(ww) / len(wins) netwin_aft_win[ind, pid-1] = (len(ww)-len(lw)) / len(wins) loses = _N.where(_hnd_dat[frmgm:-1, 2] == -1)[0]+frmgm if len(loses) > 0: wl = _N.where(_hnd_dat[loses+1, 2] == 1)[0] ll = _N.where(_hnd_dat[loses+1, 2] == -1)[0]
imag = _N.empty(len(partIDs)) fact_pat = _N.empty(len(partIDs)) netwins = _N.zeros(len(partIDs)) td_all = _N.empty((len(partIDs), TO, 4)) for partID in partIDs: di += 1 CRs = _em.CRs(partID, expt=data, visit=1) #td, start_tm, end_tm = _rt.return_hnd_dat(partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True, flip_human_AI=False) td, start_time, end_time, UA, cnstr = _rt.return_hnd_dat( partID, has_useragent=True, has_start_and_end_times=True, has_constructor=True, visit=1, expt=data, block=1) td_all[di] = td AQ28scrs[di], soc_skils[di], rout[di], switch[di], imag[di], fact_pat[ di] = _rt.AQ28( "/Users/arai/Sites/taisen/DATA/%(data)s/%(date)s/%(pID)s/AQ29.txt" % { "date": partID[0:8], "pID": partID, "data": data }) netwins[di] = _N.sum(td[:, 2]) # / td.shape[0]