utils.py 17.7 KB
 Emmanuel PIGNAT committed Apr 24, 2017 1 2 3 4 5 6 7 from copy import deepcopy import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cmap import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D  Emmanuel PIGNAT committed Oct 13, 2017 8 from scipy.special import factorial  Emmanuel PIGNAT committed Apr 24, 2017 9 10 11  plt.style.use('ggplot')  Emmanuel PIGNAT committed May 04, 2018 12 import scipy.sparse as ss  Emmanuel PIGNAT committed Oct 13, 2017 13   Emmanuel PIGNAT committed Feb 14, 2018 14 def get_canonical(nb_dim, nb_deriv=2, dt=0.01):  Emmanuel PIGNAT committed Oct 13, 2017 15 16 17 18 19 20 21 22 23 24 25  A1d = np.zeros((nb_deriv, nb_deriv)) for i in range(nb_deriv): A1d += np.diag(np.ones(nb_deriv - i), i) * np.power(dt, i) / factorial(i) B1d = np.zeros((nb_deriv, 1)) for i in range(1, nb_deriv + 1): B1d[nb_deriv - i] = np.power(dt, i) / factorial(i) return np.kron(A1d, np.eye(nb_dim)), np.kron(B1d, np.eye(nb_dim))  Emmanuel PIGNAT committed Feb 14, 2018 26   Emmanuel PIGNAT committed Oct 13, 2017 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 def lifted_noise_matrix(A=None, B=None, nb_dim=3, dt=0.01, horizon=50): r""" Given a linear system with white noise, as in LQG, .. math:: \xi_{t+1} = \mathbf{A} (\xi_t + w_i) + \mathbf{B} u_t + v_i returns the lifted form for noise addition, s_v, s_w, .. math:: \mathbf{\xi} = \mathbf{S}_{\xi} \xi_0 + \mathbf{S}_u \mathbf{u} + \mathbf{S}_v + \mathbf{S}_w :return: s_u """ if A is None or B is None: A, B = get_canonical(nb_dim, 2, dt) s_v = np.zeros((A.shape[0] * horizon, A.shape[0] * horizon)) A_p = np.eye(A.shape[0]) At_b_tmp = [] for i in range(horizon): # s_xi[i * A.shape[0]:(i + 1) * A.shape[0]] = A_p At_b_tmp += [A_p] A_p = A_p.dot(A) for i in range(horizon):  Emmanuel PIGNAT committed Feb 14, 2018 55 56 57  for j in range(i + 1): s_v[i * A.shape[0]:(i + 1) * A.shape[0], j * A.shape[1]:(j + 1) * A.shape[1]] = \ At_b_tmp[i - j - 1]  Emmanuel PIGNAT committed Oct 13, 2017 58 59 60  return s_v  Emmanuel PIGNAT committed Feb 14, 2018 61   Emmanuel PIGNAT committed May 04, 2018 62 def lifted_transfer_matrix(A=None, B=None, nb_dim=3, dt=0.01, horizon=50, sparse=False):  Emmanuel PIGNAT committed Oct 13, 2017 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83  r""" Given a linear system .. math:: \xi_{t+1} = \mathbf{A} \xi_t + \mathbf{B} u_t returns the lifted form for T timesteps .. math:: \mathbf{\xi} = \mathbf{S}_{\xi} \xi_0 + \mathbf{S}_u \mathbf{u} """ if A is None or B is None: A, B = get_canonical(nb_dim, 2, dt) s_xi = np.zeros((A.shape[0] * horizon, A.shape[1])) A_p = np.eye(A.shape[0]) At_b_tmp = [] for i in range(horizon):  Emmanuel PIGNAT committed Feb 14, 2018 84  s_xi[i * A.shape[0]:(i + 1) * A.shape[0]] = A_p  Emmanuel PIGNAT committed Oct 13, 2017 85 86 87 88 89 90 91  At_b_tmp += [np.copy(A_p.dot(B))] A_p = A_p.dot(A) s_u = np.zeros((B.shape[0] * horizon, B.shape[1] * horizon)) for i in range(horizon): for j in range(i):  Emmanuel PIGNAT committed Feb 14, 2018 92 93  s_u[i * B.shape[0]:(i + 1) * B.shape[0], j * B.shape[1]:(j + 1) * B.shape[1]] = \ At_b_tmp[i - j - 1]  Emmanuel PIGNAT committed Oct 13, 2017 94   Emmanuel PIGNAT committed May 04, 2018 95 96 97 98  if sparse: return ss.csc_matrix(s_xi), ss.csc_matrix(s_u) else: return s_xi, s_u  Emmanuel PIGNAT committed Oct 13, 2017 99   Emmanuel PIGNAT committed Apr 24, 2017 100 101  def gu_pinv(A, rcond=1e-15):  Emmanuel PIGNAT committed Feb 14, 2018 102 103 104 105  I = A.shape[0] J = A.shape[1] return np.array([[np.linalg.pinv(A[i, j]) for j in range(J)] for i in range(I)])  Emmanuel PIGNAT committed Apr 24, 2017 106   Emmanuel PIGNAT committed Oct 08, 2018 107 def create_relative_time(q, start=-1.):  Emmanuel PIGNAT committed Feb 14, 2018 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141  """ :param q: [list of int] List of state indicator. ex: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, ...] :return time: [np.array(nb_timestep,)] Phase for each of the timestep """ # find the index of states changes state_idx = np.array([-1] + np.nonzero(np.diff(q))[0].tolist() + [len(q) - 1]) time = np.zeros(len(q)) for i, t in enumerate(state_idx[:-1]): start_phase = start if i == 0 else -1. l = state_idx[i + 1] - state_idx[i] time[state_idx[i] + 1:state_idx[i + 1] + 1] = np.linspace(start_phase, 1, l) return time, state_idx def align_trajectories_hsmm(data, nb_states=5): from ..hsmm import HSMM if data[0].ndim > 2: # if more than rank 2, flatten last dims data_vectorized = [np.reshape(d, (d.shape[0], -1)) for d in data] else: data_vectorized = data model = HSMM(nb_dim=data[0].shape[1], nb_states=nb_states) model.init_hmm_kbins(data_vectorized) qs = [model.viterbi(d) for d in data_vectorized]  Emmanuel PIGNAT committed Oct 08, 2018 142  time, sqs = zip(*[create_relative_time(q) for q in qs])  Emmanuel PIGNAT committed Feb 14, 2018 143 144 145 146 147 148 149 150 151 152 153  start_idx = [np.array((np.nonzero(np.diff(q))[0] + 1).tolist()) for q in qs] for s_idxs, t in zip(start_idx, time): for s_idx in s_idxs: t[s_idx:] += 2. + (t[s_idx + 1] - t[s_idx]) return time def align_trajectories(data, additional_data=[], hsmm=True, nb_states=5):  Emmanuel PIGNAT committed Apr 24, 2017 154 155 156 157 158 159  """ :param data: [list of np.array([nb_timestep, M, N, ...])] :return: """ from dtw import dtw  Emmanuel PIGNAT committed Feb 14, 2018 160 161  if hsmm: time = align_trajectories_hsmm(data, nb_states)  Emmanuel PIGNAT committed Apr 24, 2017 162 163 164 165  ls = np.argmax([d.shape[0] for d in data]) # select longest as basis data_warp = []  Emmanuel PIGNAT committed Feb 14, 2018 166  additional_data_warp = [[] for d in additional_data]  Emmanuel PIGNAT committed Apr 24, 2017 167   Emmanuel PIGNAT committed Feb 14, 2018 168 169 170 171 172 173 174  for j, d in enumerate(data): if hsmm: dist, cost, acc, path = dtw(time[ls], time[j], dist=lambda x, y: np.linalg.norm(x - y)) else: dist, cost, acc, path = dtw(data[ls], d, dist=lambda x, y: np.linalg.norm(x - y, ord=1))  Emmanuel PIGNAT committed Apr 24, 2017 175 176 177  data_warp += [d[path[1]][:data[ls].shape[0]]]  Emmanuel PIGNAT committed Feb 14, 2018 178 179  for i, ad in enumerate(additional_data): additional_data_warp[i] += [ad[j][path[1]][:data[ls].shape[0]]]  Emmanuel PIGNAT committed Apr 24, 2017 180   Emmanuel PIGNAT committed Feb 14, 2018 181 182 183 184  if len(additional_data): return [data_warp] + additional_data_warp else: return data_warp  Emmanuel PIGNAT committed Apr 24, 2017 185 186 187  def angle_to_rotation(theta):  Emmanuel PIGNAT committed Feb 14, 2018 188 189  return np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])  Emmanuel PIGNAT committed Apr 24, 2017 190 191  def feature_to_slice(nb_dim=2, nb_frames=None, nb_attractor=2,  Emmanuel PIGNAT committed Feb 14, 2018 192  features=None):  Emmanuel PIGNAT committed Apr 24, 2017 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210  # type: (int, list of int, int, list of list of string) -> object index = [] l = 0 for i, nb_frame, feature in zip(range(nb_attractor), nb_frames, features): index += [[]] for m in range(nb_frame): index[i] += [{}] for f in feature: index[i][m][f] = slice(l, l + nb_dim) l += nb_dim return index def dtype_to_index(dtype): last_idx = 0 idx = {} for name in dtype.names:  Emmanuel PIGNAT committed Feb 14, 2018 211  idx[name] = range(last_idx, last_idx + dtype[name].shape[0])  Emmanuel PIGNAT committed Apr 24, 2017 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237  last_idx += dtype[name].shape[0] return idx def gu_pinv(A, rcond=1e-15): I = A.shape[0] J = A.shape[1] return np.array([[np.linalg.pinv(A[i, j]) for j in range(J)] for i in range(I)]) # # def gu_pinv(a, rcond=1e-15): # a = np.asarray(a) # swap = np.arange(a.ndim) # swap[[-2, -1]] = swap[[-1, -2]] # u, s, v = np.linalg.svd(a) # cutoff = np.maximum.reduce(s, axis=-1, keepdims=True) * rcond # mask = s > cutoff # s[mask] = 1. / s[mask] # s[~mask] = 0 # # return np.einsum('...uv,...vw->...uw', # np.transpose(v, swap) * s[..., None, :], # np.transpose(u, swap))  Emmanuel PIGNAT committed Feb 14, 2018 238 def plot_model_time(model, demos, figsize=(10, 2), dim_idx=[1], demo_idx=0):  Emmanuel PIGNAT committed Apr 24, 2017 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267  nb_dim = len(dim_idx) nb_samples = len(demos) fig = plt.figure(3, figsize=(figsize[0], figsize[1] * nb_dim)) # fig.suptitle("Reproduction", fontsize=14, fontweight='bold') nb_plt = nb_dim ax = [] # subplots label_size = 15 ### specify subplots ### gs = gridspec.GridSpec(nb_dim, 1) for j in range(nb_plt): # [0, 2, 1, 3, ...] ax.append(fig.add_subplot(gs[j])) for a in ax: a.set_axis_bgcolor('white') fig.suptitle("Demonstration", fontsize=14, fontweight='bold') idx = np.floor(np.linspace(1, 255, model.nb_states)).astype(int) color = cmap.viridis(range(256))[idx, 0:3] # for states state_sequ = [] for d in demos: state_sequ += [model.viterbi(d['Data'])] d = demos[demo_idx] s = state_sequ[demo_idx] for dim, a in zip(dim_idx, ax):  Emmanuel PIGNAT committed Feb 14, 2018 268  a.plot(d['Data'][dim, :])  Emmanuel PIGNAT committed Apr 24, 2017 269 270  for x_s, x_e, state in zip([0] + np.where(np.diff(s))[0].tolist(), # start step  Emmanuel PIGNAT committed Feb 14, 2018 271 272 273  np.where(np.diff(s))[0].tolist() + [len(s)], # end step np.array(s)[[0] + ( np.where(np.diff(s))[0] + 1).tolist()]): # state idx  Emmanuel PIGNAT committed Apr 24, 2017 274 275 276  a.axvline(x=x_e, ymin=0, ymax=1, c='k', lw=2, ls='--') mean = model.Mu[dim, state]  Emmanuel PIGNAT committed Feb 14, 2018 277  var = np.sqrt(model.Sigma[dim, dim, state])  Emmanuel PIGNAT committed Apr 24, 2017 278 279  a.plot([x_s, x_e], [mean, mean], c='k', lw=2)  Emmanuel PIGNAT committed Feb 14, 2018 280 281  a.fill_between([x_s, x_e], [mean + var, mean + var], [mean - var, mean - var], alpha=0.5, color=color[state])  Emmanuel PIGNAT committed Apr 24, 2017 282 283 284  plt.show()  Emmanuel PIGNAT committed Feb 14, 2018 285   Emmanuel PIGNAT committed Apr 24, 2017 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 def plot_demos_3d(demos, figsize=(15, 5), angle=[60, 45]): nb_samples = len(demos) fig = plt.figure(1, figsize=figsize) fig.suptitle("Demonstration", fontsize=14, fontweight='bold') nb_plt = 2 ax = [] label_size = 15 idx = np.floor(np.linspace(1, 255, nb_samples)).astype(int) color_demo = cmap.viridis(range(256))[idx, 0:3] # for states nb = 0 gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1]) for j in [0, 1]: ax.append(fig.add_subplot(gs[j], projection='3d', axisbg='white')) ax[nb].set_title(r'$\mathrm{Skill\ A}$') for ax_ in ax: ax_.view_init(angle[0], angle[1]) for i, c in zip(range(nb_samples), color_demo): a = 1 ax[nb].plot(demos[i]['Data'][0, :], demos[i]['Data'][1, :], demos[i]['Data'][2, :], color=c, lw=1, alpha=a) # ax[nb].plot(demos[i]['Data'][7,:], demos[i]['Data'][8,:],'H',color=c,ms=10,alpha=a) nb += 1 ax[nb].set_title(r'$\mathrm{Skill\ B}$') for i, c in zip(range(nb_samples), color_demo): a = 1 ax[nb].plot(demos[i]['Data'][3, :], demos[i]['Data'][4, :], demos[i]['Data'][5, :], color=c, lw=1, alpha=a) # ax[nb].plot(demos[i]['Data'][7,:], demos[i]['Data'][8,:],'H',color=c,ms=10,alpha=a) def repro_plot(model, demos, save=False, tp_list=[], figsize=(3.5, 5)): nb_states = model.nb_states nb_tp = len(tp_list) idx = np.floor(np.linspace(1, 255, model.nb_states)).astype(int) color = cmap.viridis(range(256))[idx, 0:3] # for states fig = plt.figure(3, figsize=(figsize[0] * nb_tp, figsize[1])) # fig.suptitle("Reproduction", fontsize=14, fontweight='bold') nb_plt = nb_tp * 2 ax = [] # subplots label_size = 15 t = 50 # timestep for reproduction # regress in first configuration i_in = [6, 7, 8] # input dimension i_out = [0, 1, 2] # output ### specify subplots ### gs = gridspec.GridSpec(2, nb_tp, height_ratios=[4, 1]) rn = [] for i in range(nb_tp): rn += [i, i + nb_tp] for j in rn: # [0, 2, 1, 3, ...] ax.append(fig.add_subplot(gs[j])) for a in ax: a.set_axis_bgcolor('white') for i in range(0, nb_tp * 2, 2): tp = tp_list[i / 2] data_in = tp[0]['b'] model.regress(data_in - tp[1]['b'], i_in, i_out) prod_1 = model.prodgmm(tp) nb = i # subplots counter ax[nb].set_title(r'$\mathrm{(a)}$') item_plt, = ax[nb].plot(data_in[0], data_in[1], '^', color=color[3], ms=12) pblt.plot_gmm(prod_1.Mu, prod_1.Sigma, dim=[0, 1], color=color, alpha=model.PriorsR * nb_states, ax=ax[nb], nb=2) ### plot state sequence ### nb = i + 1 ### get state sequence ### h = model.forward_variable_priors(t, model.PriorsR, start_priors=model.StatesPriors) for i in range(nb_states): ax[nb].plot(h[i, :], color=color[i]) """LEGEND, LABEL, ...""" for i in range(0, nb_plt, 2): # rob_plt, = ax[i].plot(40,40,'s',color=(1,0.4,0),ms=8,zorder=30) ax[i].set_aspect('equal', 'datalim') for j in [3, 4, 5, 6, 2]: demo_plt, = ax[i].plot(demos[j]['Glb'][0, :], demos[j]['Glb'][1, :], 'k:', lw=1, alpha=1) for i in range(1, nb_plt, 2): # ax[i].set_title(r'$\mathrm{forward\ variable}\, \alpha_t(z_n)$') ax[i].set_title(r'$\alpha_t(z_n)$', fontsize=16) ax[i].set_xlabel(r'$t\, \mathrm{[timestep]}$', fontsize=16) ax[i].set_ylim([-0.1, 1.1]) ax[i].set_yticks(np.linspace(0, 1, 3)) lgd = fig.legend([item_plt, demo_plt], ['obstacle position', 'Demonstrations'] , frameon=True, ncol=3, bbox_to_anchor=(0.1, -0.01), loc='lower left', numpoints=1) frame = lgd.get_frame() # frame.set_facecolor('White') plt.tight_layout(pad=2.4, w_pad=0.9, h_pad=1.0) if save: plt.savefig('/home/idiap/epignat/thesis/paper/images/' + skill_name + '_repro.pdf', bbox_extra_artists=(lgd,), bbox_inches='tight') plt.show() def plot_model(model, demos, figsize=(8, 3.5), skill_name='temp', save=False): nb_samples = len(demos) fig = plt.figure(2, figsize=figsize) # fig.suptitle("Model", fontsize=14, fontweight='bold') nb_plt = 3 ax = [] label_size = 15 # plt.style.use('bmh') plt.style.use('ggplot') idx = np.floor(np.linspace(1, 255, model.nb_states)).astype(int) color = cmap.viridis(range(256))[idx, 0:3] # for states nb = 0 gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 0.8]) for j in range(nb_plt): ax.append(fig.add_subplot(gs[j])) ax[j].set_axis_bgcolor('white') ax[nb].set_title(r'$(a)\ j=1$') # for i in range(nb_samples): # ax[nb].plot(demos[i]['Data'][4,0], demos[i]['Data'][5,0],'^',color=color[3],ms=10,alpha=0.5,zorder=30) for i in range(nb_samples): ax[nb].plot(demos[i]['Data'][0, :], demos[i]['Data'][1, :], 'k:', lw=1, alpha=1) pblt.plot_gmm(model.Mu, model.Sigma, dim=[0, 1], color=color, alpha=0.8, linewidth=1, ax=ax[nb], nb=1) ax[nb].set_ylabel('z position [cm]') nb += 1 ax[nb].set_title(r'$(b)\ j=2$') for i in range(nb_samples): demos_plt, = ax[nb].plot(demos[i]['Data'][3, :], demos[i]['Data'][4, :], 'k:', lw=1, alpha=1) # ax[nb].plot(demos[i]['Data'][2,0], demos[i]['Data'][3,0],'H',color=c,ms=10) pblt.plot_gmm(model.Mu, model.Sigma, dim=[3, 4], color=color, alpha=0.8, linewidth=1, ax=ax[nb], nb=1) nb += 1 ax[nb].set_title(r'$(c)\ \mathrm{sensory}$') for i in range(nb_samples): sense_plt, = ax[nb].plot(demos[i]['Data'][6, 0], demos[i]['Data'][7, 0], '^', color=color[3], ms=12, zorder=30) pblt.plot_gmm(model.Mu, model.Sigma, dim=[6, 7], color=color, alpha=0.5, ax=ax[nb], nb=1) # ax[nb].set_xlim([-20,140]) plt.tight_layout() lgd = fig.legend([demos_plt, sense_plt], ['demonstrations', 'hand position'], frameon=True, ncol=2, bbox_to_anchor=(0.4, -0.01), loc='lower left', numpoints=1) # frame = lgd.get_frame() # frame.set_facecolor('White') for i in range(nb_plt): # ax[i].plot(0, 0,'+',color='k',ms=20,zorder=30,lw=2) ax[i].set_xlabel('x position [cm]') plt.tight_layout(pad=2.8, w_pad=0.2, h_pad=-1.0) if save: plt.savefig('/home/idiap/epignat/thesis/paper/images/' + skill_name + '_model.pdf', bbox_extra_artists=(lgd,), bbox_inches='tight')  Emmanuel PIGNAT committed Feb 14, 2018 480 481  def plot_demos(demos, data_dim, figsize=(8, 5)):  Emmanuel PIGNAT committed Apr 24, 2017 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499  nb_samples = len(demos) fig = plt.figure(2, figsize=figsize) # fig.suptitle("Model", fontsize=14, fontweight='bold') nb_plt = len(data_dim) ax = [] label_size = 15 # plt.style.use('bmh') plt.style.use('ggplot') nb = 0 gs = gridspec.GridSpec(nb_plt, 1) for j in range(nb_plt): ax.append(fig.add_subplot(gs[j])) ax[j].set_axis_bgcolor('white') for j, dim in enumerate(data_dim): for i in range(nb_samples):  Emmanuel PIGNAT committed Feb 14, 2018 500 501  ax[j].plot(demos[i]['Data'][dim, :].T)  Emmanuel PIGNAT committed Apr 24, 2017 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561  def train_test(demos, demo_idx=0, nb_states=5, test=True, sensory=True, kbins=True, hmmr=True, nb_dim=3, nb_frames=2): demos_train = deepcopy(demos) nb_samples = len(demos) if test: demos_train.pop(demo_idx) nb_s = nb_samples - 1 else: nb_s = nb_samples model = pbd.TP_HMM(nb_states, nb_dim=nb_dim, nb_frames=nb_frames) dep = [[0, 1], [2, 3], [4, 5]] Data_train = np.hstack([d['Data'] for d in demos_train]) # model.init_hmm_kmeans(Data, nb_states, nb_samples, dep=dep) best = {'model': None, 'score': np.inf} for i in range(10): if sensory: model.init_hmm_gmm(Data_train, nb_states, nb_samples, dep=dep) scale = 8. else: if kbins: model.init_hmm_kbins(Data_train, nb_states, nb_s, dep=dep) else: model.init_hmm_kmeans(Data_train, nb_states, nb_samples, dep=dep, dim_init=range(6)) scale = 1e10 if sensory: score = model.em_hmm(demos_train, dep=dep, reg=0.0002, reg_diag=[1., 1., 1., 1., 1., 1., scale, scale, scale]) else: score = model.em_hmm(demos_train, dep=dep, reg=0.0002, reg_diag=[1., 1., 1., 1., 1., 1., scale, scale, scale], end_cov=True) if score < best['score']: best['score'] = score best['model'] = deepcopy(model) print 'Best :', best['score'] model = best['model'] model.compute_duration(demos_train) # model.init_hmm_kbins(Data, nb_states, nb_samples, dep=dep) if hmmr: hmmr = pbd.hmmr.HMMR(model, nb_dim=3) min_dist = pow(5e-2, 3) hmmr.to_gmr(demos_train, mix_std=0.1, reg=min_dist, plot_on=False) else: hmmr = None return model, hmmr  Emmanuel PIGNAT committed Feb 14, 2018 562 def repro_demo(model, hmmr, demos, demo_idx=0, start_point=None, plot_on=False):  Emmanuel PIGNAT committed Apr 24, 2017 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633  nb_states = model.nb_states nb_samples = len(demos) t = 50 # timestep for reproduction # regress in first configuration i_in = [6, 7, 8] # input dimension i_out = [0, 1, 2] # output tp = deepcopy(demos[demo_idx]['TPs']) data_in = tp[0]['b'] model.regress(data_in - tp[1]['b'], i_in, i_out, reg=0.01) prod_1 = model.prodgmm(tp) ### get state sequence ### # print model.PriorsR h_1 = model.forward_variable_priors(t, model.PriorsR, start_priors=model.StatesPriors) hmmr.create_distribution_fwd(h_1, start_pos=None) # 64.3 ms ~1.5 ms per timestep prod_ph_1 = hmmr.prodgmm(tp) lqr = pbd.LQR(canonical=True, horizon=70, rFactor=-2.0, nb_dim=3) q = np.argmax(h_1, axis=0) # print q # make a rest at the end q = np.concatenate([q, np.ones(20) * q[-1]]) lqr.set_hmm_problem(prod_ph_1, range(50) + [49] * 20) lqr.evaluate_gains_infiniteHorizon() plan, command = lqr.solve_hmm_problem(start_point) if plot_on: label_size = 15 idx = np.floor(np.linspace(1, 255, 50)).astype(int) color_gmr = cmap.viridis(range(256))[idx, 0:3] # for states idx = np.floor(np.linspace(1, 255, nb_states)).astype(int) color = cmap.viridis(range(256))[idx, 0:3] # for states fig = plt.figure(3 + demo_idx, figsize=(5, 5)) # fig.suptitle("Reproduction", fontsize=14, fontweight='bold') nb_plt = 2 ax = [] # subplots ### specify subplots ### gs = gridspec.GridSpec(2, 1, width_ratios=[1], height_ratios=[4, 1]) for j in [0, 1]: ax.append(fig.add_subplot(gs[j])) for a in ax: a.set_axis_bgcolor('white') ### plot regressed HMM ### nb = 0 # subplots counter ax[nb].set_title(r'$\mathrm{(a)}$') for j in range(nb_samples): demo_plt, = ax[0].plot(demos[j]['Glb'][0, :], demos[j]['Glb'][1, :], 'k:', lw=1, alpha=1) ax[nb].plot(data_in[0], data_in[1], '^', color=color[-1], ms=12) pblt.plot_gmm(prod_1.Mu, prod_1.Sigma, dim=[0, 1], color=color, alpha=model.PriorsR * nb_states, ax=ax[nb], nb=2) ### plot state sequence ### nb += 1 for i in range(nb_states): ax[nb].plot(h_1[i, :], color=color[i]) ax[nb].set_ylim([-0.1, 1.1])  Emmanuel PIGNAT committed Feb 14, 2018 634 635  pblt.plot_gmm(prod_ph_1.Mu, prod_ph_1.Sigma, dim=[0, 1], color=color_gmr, ax=ax[nb - 1],  Emmanuel PIGNAT committed Apr 24, 2017 636 637 638 639 640 641  nb=1) ax[0].plot(plan[0, :], plan[1, :], 'w', lw=2, zorder=50) ax[0].plot(demos[demo_idx]['Glb'][0, :], demos[demo_idx]['Glb'][1, :], 'k--', lw=3, alpha=1, zorder=49)  Emmanuel PIGNAT committed Feb 14, 2018 642  return np.copy(plan)