From ae88acf40db515db5bf48c8238fe8f7b2dd8c304 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Sat, 22 Jun 2024 06:39:28 +0800 Subject: [PATCH] =?UTF-8?q?0.1.112=5F=E5=A2=9E=E5=8A=A0=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PyPI/setup.cfg | 2 +- PyPI/src/guan.egg-info/PKG-INFO | 2 +- PyPI/src/guan/data_processing.py | 56 +++++++++++++++++++++++ PyPI/src/guan/file_reading_and_writing.py | 51 ++++++++++++++++++--- 4 files changed, 102 insertions(+), 9 deletions(-) diff --git a/PyPI/setup.cfg b/PyPI/setup.cfg index 74bff55..a7caab8 100644 --- a/PyPI/setup.cfg +++ b/PyPI/setup.cfg @@ -1,7 +1,7 @@ [metadata] # replace with your username: name = guan -version = 0.1.111 +version = 0.1.112 author = guanjihuan author_email = guanjihuan@163.com description = An open source python package diff --git a/PyPI/src/guan.egg-info/PKG-INFO b/PyPI/src/guan.egg-info/PKG-INFO index 9e808d4..038ab2f 100644 --- a/PyPI/src/guan.egg-info/PKG-INFO +++ b/PyPI/src/guan.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: guan -Version: 0.1.111 +Version: 0.1.112 Summary: An open source python package Home-page: https://py.guanjihuan.com Author: guanjihuan diff --git a/PyPI/src/guan/data_processing.py b/PyPI/src/guan/data_processing.py index 204a73c..a44887e 100644 --- a/PyPI/src/guan/data_processing.py +++ b/PyPI/src/guan/data_processing.py @@ -123,6 +123,62 @@ def run_programs_sequentially(program_files=['./a.py', './b.py'], execute='pytho end = time.time() print('Total running time = '+str((end-start)/60)+' min') +# 将XYZ数据转成矩阵数据(说明:x_array/y_array的输入和输出不一样。要求z_array数据中y对应的数据为小循环,x对应的数据为大循环) +def convert_xyz_data_into_matrix_data(x_array, y_array, z_array): + import numpy as np + x_array_input = np.array(x_array) + y_array_input = np.array(y_array) + x_array = np.array(list(set(x_array_input))) + y_array = np.array(list(set(y_array_input))) + z_array = np.array(z_array) + len_x = len(x_array) + len_y = len(y_array) + matrix = np.zeros((len_x, len_y)) + for ix in range(len_x): + for iy in range(len_y): + matrix[ix, iy] = z_array[ix*len_y+iy] + return x_array, y_array, matrix + +# 将矩阵数据转成XYZ数据(说明:x_array/y_array的输入和输出不一样。生成的z_array数据中y对应的数据为小循环,x对应的数据为大循环) +def convert_matrix_data_into_xyz_data(x_array, y_array, matrix): + import numpy as np + x_array_input = np.array(x_array) + y_array_input = np.array(y_array) + matrix = np.array(matrix) + len_x = len(x_array_input) + len_y = len(y_array_input) + x_array = np.zeros((len_x*len_y)) + y_array = np.zeros((len_x*len_y)) + z_array = np.zeros((len_x*len_y)) + for ix in range(len_x): + for iy in range(len_y): + x_array[ix*len_y+iy] = x_array_input[ix] + y_array[ix*len_y+iy] = y_array_input[iy] + z_array[ix*len_y+iy] = matrix[ix, iy] + return x_array, y_array, z_array + +# 通过定义计算R^2(基于实际值和预测值) +def calculate_R2_with_definition(y_true_array, y_pred_array): + import numpy as np + y_mean = np.mean(y_true_array) + SS_tot = np.sum((y_true_array - y_mean) ** 2) + SS_res = np.sum((y_true_array - y_pred_array) ** 2) + R2 = 1 - (SS_res / SS_tot) + return R2 + +# 通过sklearn计算R^2,和定义的计算结果一致 +def calculate_R2_with_sklearn(y_true_array, y_pred_array): + from sklearn.metrics import r2_score + R2 = r2_score(y_true_array, y_pred_array) + return R2 + +# 通过scipy计算线性回归后的R^2(基于线性回归模型) +def calculate_R2_after_linear_regression_with_scipy(y_true_array, y_pred_array): + from scipy import stats + slope, intercept, r_value, p_value, std_err = stats.linregress(y_true_array, y_pred_array) + R2 = r_value**2 + return R2 + # 获取函数或类的源码(返回字符串) def get_source(name): import inspect diff --git a/PyPI/src/guan/file_reading_and_writing.py b/PyPI/src/guan/file_reading_and_writing.py index 08932af..935d182 100644 --- a/PyPI/src/guan/file_reading_and_writing.py +++ b/PyPI/src/guan/file_reading_and_writing.py @@ -102,7 +102,7 @@ def read_text_files_in_directory(directory='./', file_format='.md'): file_list.append(root+'/'+files[i0]) content_array = [] for file in file_list: - with open(file, 'r') as f: + with open(file, 'r', encoding='UTF-8') as f: content_array.append(f.read()) return file_list, content_array @@ -118,9 +118,9 @@ def find_words_in_multiple_files(words, directory='./', file_format='.md'): return file_list_with_words # 复制一份文件 -def copy_file(file1='./a.txt', file2='./b.txt'): +def copy_file(old_file='./a.txt', new_file='./b.txt'): import shutil - shutil.copy(file1, file2) + shutil.copy(old_file, new_file) # 打开文件,替代某字符串 def open_file_and_replace_str(file_path='./a.txt', old_str='', new_str=''): @@ -134,7 +134,7 @@ def open_file_and_replace_str(file_path='./a.txt', old_str='', new_str=''): # 复制一份文件,然后再替代某字符串 def copy_file_and_replace_str(old_file='./a.txt', new_file='./b.txt', old_str='', new_str=''): import guan - guan.copy_file(file1=old_file, file2=new_file) + guan.copy_file(old_file=old_file, new_file=new_file) content = guan.read_text_file(file_path=new_file) content = content.replace(old_str, new_str) f = guan.open_file(filename=new_file, file_format='', mode='overwrite') @@ -202,6 +202,24 @@ def read_one_dimensional_complex_data(filename='a', file_format='.txt'): y_array = np.append(y_array, [y_row], axis=0) return x_array, y_array +# 读取文件中的XYZ数据(一行一组x, y, z) +def read_xyz_data(filename='a', file_format='.txt'): + import numpy as np + f = open(filename+file_format, 'r') + text = f.read() + f.close() + row_list = np.array(text.split('\n')) + x_array = np.array([]) + y_array = np.array([]) + z_array = np.array([]) + for row in row_list: + column = np.array(row.split()) + if column.shape[0] != 0: + x_array = np.append(x_array, [float(column[0])], axis=0) + y_array = np.append(y_array, [float(column[1])], axis=0) + z_array = np.append(z_array, [float(column[2])], axis=0) + return x_array, y_array, z_array + # 读取文件中的二维数据(第一行和第一列分别为横纵坐标) def read_two_dimensional_data(filename='a', file_format='.txt'): import numpy as np @@ -260,7 +278,7 @@ def read_two_dimensional_complex_data(filename='a', file_format='.txt'): matrix = np.append(matrix, [matrix_row], axis=0) return x_array, y_array, matrix -# 读取文件中的二维数据(不包括x和y) +# 读取文件中的二维数据(不包括横纵坐标) def read_two_dimensional_data_without_xy_array(filename='a', file_format='.txt'): import numpy as np matrix = np.loadtxt(filename+file_format) @@ -288,6 +306,25 @@ def write_one_dimensional_data_without_opening_file(x_array, y_array, f): f.write('\n') i0 += 1 +# 在文件中写入XYZ数据(一行一组x, y, z) +def write_one_dimensional_data(x_array, y_array, z_array, filename='a', file_format='.txt'): + import guan + with open(filename+file_format, 'w', encoding='UTF-8') as f: + write_xyz_data_without_opening_file(x_array, y_array, z_array, f) + +# 在文件中写入XYZ数据(一行一组x, y, z)(需要输入已打开的文件) +def write_xyz_data_without_opening_file(x_array, y_array, z_array, f): + import numpy as np + x_array = np.array(x_array) + y_array = np.array(y_array) + z_array = np.array(z_array) + i0 = 0 + for x0 in x_array: + f.write(str(x0)+' ') + f.write(str(y_array[i0])+' ') + f.write(str(z_array[i0])+'\n') + i0 += 1 + # 在文件中写入二维数据(第一行和第一列分别为横纵坐标) def write_two_dimensional_data(x_array, y_array, matrix, filename='a', file_format='.txt'): import guan @@ -314,13 +351,13 @@ def write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f) f.write('\n') i0 += 1 -# 在文件中写入二维数据(不包括x和y) +# 在文件中写入二维数据(不包括横纵坐标) def write_two_dimensional_data_without_xy_array(matrix, filename='a', file_format='.txt'): import guan with open(filename+file_format, 'w', encoding='UTF-8') as f: guan.write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f) -# 在文件中写入二维数据(不包括x和y)(需要输入已打开的文件) +# 在文件中写入二维数据(不包括横纵坐标)(需要输入已打开的文件) def write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f): for row in matrix: for element in row: