def update_fit_R3(db_read, db_rds, sample, mesa='%', commit=False): """ :type db_read: Database :type X: int or str :type Y: int or str """ q = ('SELECT device_id, mesa, X, Y, area FROM v04_device ' 'WHERE sample=%s AND mesa LIKE %s') params = db_rds.q_all(q, (sample, mesa,)) for devid, mesa, X, Y, area in params: print('update_fit_R3: devid{} {} {} X{} Y{} querying...'. format(devid, sample, mesa, X, Y), end=' ', flush=True) q = 'SELECT V, I FROM v_py_fitR3 WHERE device_id=%s' vis = db_read.pdq(q, (devid,)) vis_100meV = vis[(-0.1 <= vis.V) & (vis.V <= 0.1)] if vis_100meV.empty: print('VIs (100meV) empty. skip.') continue c1, _, _, _ = fit_R3(vis_100meV) _, _, _, R2 = fit_R3(vis) R0 = 1/c1 RA0 = area/c1 oper = ('UPDATE device SET suss_R0=%s, suss_RA0=%s, suss_R2=%s ' 'WHERE device_id=%s') db_rds.cursor.execute(oper, (R0, RA0, R2, devid,)) if commit: db_rds.cnx.commit() print('Updated.')
def update_fit_R3(db_read, db_rds, sample, mesa='%', commit=False): """ :type db_read: Database :type X: int or str :type Y: int or str """ q = ('SELECT device_id, mesa, X, Y, area FROM v04_device ' 'WHERE sample=%s AND mesa LIKE %s') params = db_rds.q_all(q, ( sample, mesa, )) for devid, mesa, X, Y, area in params: print('update_fit_R3: devid{} {} {} X{} Y{} querying...'.format( devid, sample, mesa, X, Y), end=' ', flush=True) q = 'SELECT V, I FROM v_py_fitR3 WHERE device_id=%s' vis = db_read.pdq(q, (devid, )) vis_100meV = vis[(-0.1 <= vis.V) & (vis.V <= 0.1)] if vis_100meV.empty: print('VIs (100meV) empty. skip.') continue c1, _, _, _ = fit_R3(vis_100meV) _, _, _, R2 = fit_R3(vis) R0 = 1 / c1 RA0 = area / c1 oper = ('UPDATE device SET suss_R0=%s, suss_RA0=%s, suss_R2=%s ' 'WHERE device_id=%s') db_rds.cursor.execute(oper, ( R0, RA0, R2, devid, )) if commit: db_rds.cnx.commit() print('Updated.')
def iv_matrix(db, sample, mesa, xlim=(-1.0, 1.0), XXYY=None, inst='%', save_path=None, R2_9th_lim=1.5, v_fit=(-0.1, 0.1), grayout_remarks={'i', 'lin', 'c', 'bd'}, edit_remarks=False, db_rds=None, remarks_path=None, commit=False): """ :param XXYY: (X_min, X_max, Y_min, Y_max). if is None -> all XYs """ plt.close() sns.set_style("white") sql = ('SELECT device_id AS did, X, Y, area, suss_R0 AS R0, suss_RA0 AS RA0, ' 'VI_remarks as rem ' 'FROM v04_device WHERE sample=%s AND mesa=%s') df = db.pdq(sql, (sample, mesa,)) if XXYY is None: X_min, X_max, Y_min, Y_max = \ df.X.min(), df.X.max(), df.Y.min(), df.Y.max() else: X_min, X_max, Y_min, Y_max = XXYY # Number of columns and rows in matrix plot. numX = X_max - X_min + 1 numY = Y_max - Y_min + 1 if edit_remarks: devid_matrix = [[None for x in range(numX)] for y in range(numY)] remarks_matrix = [['NULL' for x in range(numX)] for y in range(numY)] # Takes long time. print('Making subplots frame...') # http://matplotlib.org/api/pyplot_api.html subplot_kw = {'xlim': xlim, 'ylim': (-1, 1), 'xticks': [], 'yticks': [0]} f, axarr = plt.subplots(numY, numX, squeeze=False, subplot_kw=subplot_kw, figsize=(numX, numY), facecolor='w') f.subplots_adjust(top=1, bottom=0, left=0, right=1, wspace=0, hspace=0) for i, d in df.iterrows(): if not ((X_min <= d.X <= X_max) and (Y_min <= d.Y <= Y_max)): continue row = Y_max - d.Y col = -X_min + d.X ax = axarr[row, col] # row, col ax.yaxis.set_major_formatter(plt.NullFormatter()) # Hide ticks labels if edit_remarks: devid_matrix[row][col] = d.did remarks_matrix[row][col] = d.rem tx_lb = d.rem.replace(' ', '\n') # text left bottom ax.text(0.1, 0, tx_lb, ha='left', va='bottom', transform=ax.transAxes, size='x-small') print('Querying vis... (X{} Y{} {} device id: {})'. format(d.X, d.Y, mesa, d.did), end=' ', flush=True) sql = 'SELECT V, I FROM v_py_matrix ' \ 'WHERE device_id=%s AND inst LIKE %s' VI = db.pdq(sql, (d.did, inst,)) print('Done.', end=' ', flush=True) if VI.empty: print() continue # fit VI_fit = VI[(v_fit[0] <= VI.V) & (VI.V <= v_fit[1])] c1, c2, c3, R2 = al.fit_R3(VI_fit) if not math.isclose(1/c1, d.R0, rel_tol=1e-2): # tolerance: 2 digits print('R0 difference: db{} calc{}'.format(d.R0, 1/c1), end=' ', flush=True) # Vs, Is, Rs VI['R'] = VI.V / VI.I VI['Ifit'] = c1 * VI.V + c2 * VI.V ** 2 + c3 * VI.V ** 3 VI['Rfit'] = VI.V / VI.Ifit # Normalize to ylim(-1, 1) I_scale = VI.I.abs().max() VI['In'] = VI.I / I_scale # [-1, 1] VI['Ifitn'] = VI.Ifit / I_scale # [-1, 1] # Intervals: assuming R > 0 R_scale = VI.R[(VI.R > 0) & (VI.R != np.inf)].max() R_scale = VI.Rfit.max() VI['Rn'] = (VI.R / R_scale) * 0.9 # (0, 0.9] VI['Rn'] = VI.Rn*2 - 1 # (-1, 0.9] VI['Rfitn'] = (VI.Rfit / R_scale)*0.9*2 - 1 # (-1, 0.9] R2_num_9th = al.num_9th(R2) if (R2_num_9th <= R2_9th_lim) or \ (grayout_remarks & set(d.rem.split(' ')) != set()): blue, _, red = sns.color_palette('pastel')[:3] else: blue, _, red = sns.color_palette()[:3] ax.plot(VI.V, VI.Rn, c=red, lw=0.5) ax.plot(VI.V, VI.In, c=blue, lw=0.5) ax.plot(VI.V, VI.Rfitn, '--', c='gray', lw=0.5) ax.plot(VI.V, VI.Ifitn, '--', c='gray', lw=0.5) # X Y R2-#9th R0 RA0 tx_lt = 'X{}Y{}\n{:d}'.format(d.X, d.Y, d.did) ax.text(0, 1, tx_lt, ha='left', va='top', transform=ax.transAxes, size='x-small') tx_rb = 'R2 {:.2f} {:.1f}\nR{:.1e}\nRA{:.1e}'. \ format(R2, R2_num_9th, 1 / c1, d.area / c1) ax.text(1, 0, tx_rb, ha='right', va='bottom', transform=ax.transAxes, size='x-small') print('') # newline if edit_remarks: with open(remarks_path, 'w') as f: f.write('\n'.join([','.join(row) for row in remarks_matrix])) if save_path is None: plt.show() else: plt.savefig(save_path) pass if edit_remarks: print('Edit', remarks_path, '.') input() with open(remarks_path, 'r') as f: remarks_matrix2 = f.readlines() remarks_matrix2 = [row.split(',') for row in remarks_matrix2] pairs_remarks_devid = [] for row in range(numY): for col in range(numX): if devid_matrix[row][col] is None: continue remarks = remarks_matrix2[row][col].strip() pairs_remarks_devid.append((remarks, devid_matrix[row][col])) oper = 'UPDATE device SET VI_remarks=%s WHERE device_id=%s' db_rds.exem(oper, pairs_remarks_devid) if commit: db_rds.cnx.commit() print('Edit comitted.')