import os
os.chdir('.//')
import numpy as np

# 假设你有一个数据集的列表
# datasets = np.random.rand(100, 65)
file_source_name = "best_fea_RrED.txt"
label_source_name = "amazon_list.txt"
# np.savetxt(file_name, datasets)

max_indices=[]
index=[]

with open(label_source_name, 'r') as file:
    for line in file:
        max_indices.append(int(line.split(" ")[1]))

loaded_source_datasets = np.loadtxt(file_source_name)
#loaded_source_datasets = np.loadtxt("logic_R.txt")
#print(loaded_source_datasets.shape)
#max_indices = [tensor.argmax().item() for tensor in loaded_datasets]
print(max_indices[:50])
#labels = np.random.choice(calss,len(loaded_datasets))
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt


printindex=[i for i in range(31)]


source_label=[]
target_label=[]
array1=[]
array2=[]

for i in range(len(loaded_source_datasets)):
    if max_indices[i] in printindex:
        source_label.append(max_indices[i])
        array1.append(loaded_source_datasets[i])
        
        
array1 = np.array(array1)

# array1 = np.array(loaded_datasets)## np.array([tensor.numpy() for tensor in loaded_datasets])
# array2 = np.array(loaded_source_datasets)
len_data=len(array1)
print(len_data)
##conbined_data = np.concatenate((array1, array2),axis=0) 
##print(len(conbined_data))

#print(combined_data[0])
#tsne = TSNE(n_components=2, learning_rate='auto', init='pca')
tsne =  TSNE(n_components=2, learning_rate='auto', init='pca', random_state=0)
embedded_data = tsne.fit_transform(array1)
# embedded_source_data = tsne.fit_transform(combined_source_data)
print(embedded_data.shape)
x_min, x_max = embedded_data.min(0), embedded_data.max(0)
X_norm = (embedded_data-x_min)/(x_max-x_min) 
colors = plt.cm.rainbow(np.linspace(0, 1, 31))
print(embedded_data[:10])

#for i in range(5):
    #if i==2:
    #    continue
    #mask = []
    #for j in range(len(max_indices)):
    #    if max_indices[j] == i:
    #        mask.append(j)
    #print(i,mask)
#plt.scatter(embedded_data[: , 0], embedded_data[ : , 1],edgecolors='white',color='white')
mask = []

num=0
c=0

idx=0
#while True:
#    if idx==len(source_label):
#        break
#    if idx==0:
#        #plt.scatter(X_norm[idx , 0], X_norm[idx , 1], label =f'class {c}',edgecolors='white',color=colors[c*4])
#        plt.scatter(X_norm[idx , 0], X_norm[idx , 1], color=colors[c],s=1)
#        idx=idx+1
#        continue
#    if source_label[idx]!=source_label[idx-1]:
#        c=c+1
#        num=idx
#        print(idx )
#        #plt.scatter(X_norm[idx , 0], X_norm[idx , 1], label =f'class {c}',edgecolors='white',color=colors[c*4])
#        plt.scatter(X_norm[idx , 0], X_norm[idx , 1],color=colors[c],s=1)
#        idx=idx+1
#        continue
#    plt.scatter(X_norm[idx , 0], X_norm[idx , 1],color=colors[c],s=1)
#    idx=idx+1


idx=len(source_label)-1
while True:
    if idx==-1:
        break
    if idx==len(source_label)-1:
        #plt.scatter(X_norm[idx , 0], X_norm[idx , 1], label =f'class {c}',edgecolors='white',color=colors[c*4])
        plt.scatter(X_norm[idx , 0], X_norm[idx , 1], color=colors[c],s=1)
        idx=idx-1
        continue
    if source_label[idx]!=source_label[idx+1]:
        c=c+1
        num=idx
        print(idx )
        #plt.scatter(X_norm[idx , 0], X_norm[idx , 1], label =f'class {c}',edgecolors='white',color=colors[c*4])
        plt.scatter(X_norm[idx , 0], X_norm[idx , 1],color=colors[c],s=1)
        idx=idx-1
        continue
    plt.scatter(X_norm[idx , 0], X_norm[idx , 1],color=colors[c],s=1)
    idx=idx-1


idx=0
num=0
c=0


#plt.legend(loc="best")
plt.xticks([])
plt.yticks([])


plt.show()


