とりあえずpandasでの公開waferデータ解析メモ。内容はあとで解説

```python
#%%
import numpy as np
from ripser import Rips
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

#%%
df=pd.read_pickle("./LSWMD.pkl")
df.info()

#%%
df = df.drop(['waferIndex'], axis = 1)
def find_dim(x):
    dim0=np.size(x,axis=0)
    dim1=np.size(x,axis=1)
    return dim0,dim1
df['waferMapDim']=df.waferMap.apply(find_dim)
df.sample(5)
uni_waferDim=np.unique(df.waferMapDim, return_counts=True)
uni_waferDim[0].shape[0]
df['failureNum']=df.failureType
df['trainTestNum']=df.trianTestLabel
mapping_type={'Center':0,'Donut':1,'Edge-Loc':2,'Edge-Ring':3,'Loc':4,'Random':5,'Scratch':6,'Near-full':7,'none':8}
mapping_traintest={'Training':0,'Test':1}
df=df.replace({'failureNum':mapping_type, 'trainTestNum':mapping_traintest})

tol_wafers = df.shape[0]
tol_wafers

df_withlabel = df[(df['failureNum']>=0) & (df['failureNum']<=8)]
df_withlabel =df_withlabel.reset_index()
df_withpattern = df[(df['failureNum']>=0) & (df['failureNum']<=7)]
df_withpattern = df_withpattern.reset_index()
df_nonpattern = df[(df['failureNum']==8)]
df_withlabel.shape[0], df_withpattern.shape[0], df_nonpattern.shape[0]

#%%
x = [9,340, 3, 16, 0, 25, 84, 37]
labels2 = ['Center','Donut','Edge-Loc','Edge-Ring','Loc','Random','Scratch','Near-full']

#ind_def = {'Center': 9, 'Donut': 340, 'Edge-Loc': 3, 'Edge-Ring': 16, 'Loc': 0, 'Random': 25,  'Scratch': 84, 'Near-full': 37}
fig, ax = plt.subplots(nrows = 2, ncols = 4, figsize=(20, 10))
ax = ax.ravel(order='C')
for i in range(8):
    img = df_withpattern.waferMap[x[i]]
    ax[i].imshow(img)
    ax[i].set_title(df_withpattern.failureType[x[i]][0][0],fontsize=24)
    ax[i].set_xticks([])
    ax[i].set_yticks([])
plt.tight_layout()
plt.show() 

#%%
data = df_withpattern.waferMap[2]
data = np.where(data == 1, 0, data)
data = np.where(data == 2, 1, data)
#data[data == 1] = 0
#data[data == 2] = 1
for line in data:
    print(line)

#%%
fig, ax = plt.subplots()
ax.imshow(data)
plt.show()

#%%
rips = Rips()
diagrams = rips.fit_transform(df_withpattern.waferMap[340])
rips.plot(diagrams)
print(diagrams[1])

#%%
rips = Rips()
diagrams = rips.fit_transform(data)
rips.plot(diagrams)
print(diagrams[1])

#%%
## 同じラベルのデータを表示
#print(df_withpattern.failureType)
x = np.where(df_withpattern.failureType == "Scratch")[0]
print(x)

#%%
fig, ax = plt.subplots(nrows = 2, ncols = 4, figsize=(20, 10))
ax = ax.ravel(order='C')
for i in range(8):
    img = df_withpattern.waferMap[x[i]]
    ax[i].imshow(img)
    ax[i].set_title(df_withpattern.failureType[x[i]][0][0],fontsize=24)
    ax[i].set_xticks([])
    ax[i].set_yticks([])
plt.tight_layout()
plt.show() 

#%%
#test = df_withpattern.waferMap[x[0]]
def convert_xy(data,size):
    fails = np.where(data == 2)
    l = len(fails[0])
    result = np.zeros((l, 2))
    for i in range(l):
        result[i][0] = fails[0][i] * s + s/2
        result[i][1] = fails[1][i] * s + s/2
    return result

#%%
#rips = Rips()
data = convert_xy(df_withpattern.waferMap[x[0]], df_withpattern.dieSize[x[5]])
diagrams = rips.fit_transform(data)
rips.plot(diagrams)

#%%
data = convert_xy(df_withpattern.waferMap[x[1]], df_withpattern.dieSize[x[5]])
diagrams = rips.fit_transform(data)
rips.plot(diagrams)

#%%
data = convert_xy(df_withpattern.waferMap[x[2]], df_withpattern.dieSize[x[5]])
diagrams = rips.fit_transform(data)
rips.plot(diagrams)


#%%
print(diagrams[1][:8])

#%%
def get_diagram(target_map, size):
    rips = Rips()
    target_map = convert_xy(target_map, size)
    return rips.fit_transform(target_map)

def cos_sim(v1, v2):
    return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))

#%%
def calc_diff(src, size, dst, dst_sizes):
    src = get_diagram(src, size)
    src = src[1][:8].flatten()
    dst = get_diagram(dst, dst_size)
    dst = dst[1][:8].flatten()
    return cos_sim(src, dst)

#%%
src_map = df_withpattern.waferMap[x[0]]
src_size = df_withpattern.dieSize[x[0]]
dst_map = df_withpattern.waferMap[x[1]]
dst_size = df_withpattern.dieSize[x[1]]
print(calc_diff(src_map, src_size, dst_map, dst_size))


#%%
src_map = df_withpattern.waferMap[0]
src_size = df_withpattern.dieSize[0]
dst_map = df_withpattern.waferMap[1]
dst_size = df_withpattern.dieSize[1]
print(calc_diff(src_map, src_size, dst_map, dst_size))



#%%

```