1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| import pandas as pd import numpy as np import os import glob import fnmatch
csv_file_path = 'now_have.csv'
data_pd = pd.read_csv(csv_file_path, header=0)
print("First column (c):")
folder_path = 'dataset\\toushe\\toushe_kongjianguangyuan_aiwantisi_hs2048'
save_name = 'gao_1121_good_light_good_spectrum_green_data.npy'
txt_files = glob.glob(os.path.join(folder_path, '*.txt'))
filtered_txt_files = [file for file in txt_files if not fnmatch.fnmatch(os.path.basename(file), '*gualazhi*')] data_y = []
for file_path_ in txt_files:
file_path = os.path.basename(file_path_) name_parts = file_path.split('-') extracted_part = (int(name_parts[0]), int(name_parts[1]))
matching_rows = data_pd[(data_pd['c'] == extracted_part[0]) & (data_pd['l'] == extracted_part[1])] if not matching_rows.empty: with open(file_path_, 'r') as file: lines = file.readlines()
data_lines = lines[8:] data = []
for line in data_lines: if line.strip(): parts = line.strip().split(';')
wave = float(parts[0]) sample = float(parts[1]) dark = float(parts[2]) reference = float(parts[3]) corrected = float(parts[4])
feature=corrected/(reference-dark) true_label = float(matching_rows['t']) data.append([feature,true_label])
data_np = np.array(data) data_y.append(data_np) print("OK") else: pass
data_y_np = np.array(data_y) transposed_data = np.transpose(data_y_np, (0, 2, 1)) np.save(save_name, transposed_data)
|