Compare commits
6 Commits
463fc041f1
...
c35688a49e
Author | SHA1 | Date | |
---|---|---|---|
c35688a49e | |||
83601c45af | |||
0de9394118 | |||
944bbc6a77 | |||
0d5c72dc1f | |||
7176bc5d57 |
@ -1,7 +1,7 @@
|
||||
[metadata]
|
||||
# replace with your username:
|
||||
name = guan
|
||||
version = 0.1.169
|
||||
version = 0.1.175
|
||||
author = guanjihuan
|
||||
author_email = guanjihuan@163.com
|
||||
description = An open source python package
|
||||
|
@ -1,6 +1,6 @@
|
||||
Metadata-Version: 2.4
|
||||
Name: guan
|
||||
Version: 0.1.169
|
||||
Version: 0.1.175
|
||||
Summary: An open source python package
|
||||
Home-page: https://py.guanjihuan.com
|
||||
Author: guanjihuan
|
||||
|
@ -1,7 +1,7 @@
|
||||
# Module: data_processing
|
||||
|
||||
# 获取运行的日期和时间并写入文件
|
||||
def statistics_with_day_and_time(content='', filename='time_logging', file_format='.txt'):
|
||||
def logging_with_day_and_time(content='', filename='time_logging', file_format='.txt'):
|
||||
import datetime
|
||||
datetime_today = str(datetime.date.today())
|
||||
datetime_time = datetime.datetime.now().strftime('%H:%M:%S')
|
||||
@ -27,6 +27,33 @@ def try_except(function_name, *args, **kwargs):
|
||||
except:
|
||||
pass
|
||||
|
||||
# 打印数组
|
||||
def print_array(array, line_break=0):
|
||||
if line_break == 0:
|
||||
for i0 in array:
|
||||
print(i0)
|
||||
else:
|
||||
for i0 in array:
|
||||
print(i0)
|
||||
print()
|
||||
|
||||
# 以显示编号的样式,打印数组
|
||||
def print_array_with_index(array, show_index=1, index_type=0):
|
||||
if show_index==0:
|
||||
for i0 in array:
|
||||
print(i0)
|
||||
else:
|
||||
if index_type==0:
|
||||
index = 0
|
||||
for i0 in array:
|
||||
print(index, i0)
|
||||
index += 1
|
||||
else:
|
||||
index = 0
|
||||
for i0 in array:
|
||||
index += 1
|
||||
print(index, i0)
|
||||
|
||||
# 获取矩阵的维度(考虑单一数值的矩阵维度为1)
|
||||
def dimension_of_array(array):
|
||||
import numpy as np
|
||||
@ -37,6 +64,64 @@ def dimension_of_array(array):
|
||||
dim = array.shape[0]
|
||||
return dim
|
||||
|
||||
# 检查矩阵是否为厄米矩阵(相对误差为1e-5)
|
||||
def is_hermitian(matrix):
|
||||
import numpy as np
|
||||
matrix = np.array(matrix)
|
||||
if matrix.shape[0] != matrix.shape[1]:
|
||||
return False
|
||||
return np.allclose(matrix, np.conj(matrix.T))
|
||||
|
||||
# 判断一个数是否接近于整数
|
||||
def close_to_integer(value, abs_tol=1e-3):
|
||||
import math
|
||||
result = math.isclose(value, round(value), abs_tol=abs_tol)
|
||||
return result
|
||||
|
||||
# 从列表中删除某个匹配的元素
|
||||
def remove_item_in_one_array(array, item):
|
||||
new_array = [x for x in array if x != item]
|
||||
return new_array
|
||||
|
||||
# 根据子数组的第index个元素对子数组进行排序(index从0开始)
|
||||
def sort_array_by_index_element(original_array, index):
|
||||
sorted_array = sorted(original_array, key=lambda x: x[index])
|
||||
return sorted_array
|
||||
|
||||
# 随机获得一个整数,左闭右闭
|
||||
def get_random_number(start=0, end=1):
|
||||
import random
|
||||
rand_number = random.randint(start, end) # 左闭右闭 [start, end]
|
||||
return rand_number
|
||||
|
||||
# 选取一个种子生成固定的随机整数,左闭右开
|
||||
def generate_random_int_number_for_a_specific_seed(seed=0, x_min=0, x_max=10):
|
||||
import numpy as np
|
||||
np.random.seed(seed)
|
||||
rand_num = np.random.randint(x_min, x_max) # 左闭右开[x_min, x_max)
|
||||
return rand_num
|
||||
|
||||
# 获取两个模式之间的字符串
|
||||
def get_string_between_two_patterns(original_string, start, end, include_start_and_end=0):
|
||||
import re
|
||||
pattern = f'{start}(.*?){end}'
|
||||
result = re.search(pattern, original_string)
|
||||
if result:
|
||||
if include_start_and_end == 0:
|
||||
return result.group(1)
|
||||
else:
|
||||
return start+result.group(1)+end
|
||||
else:
|
||||
return ''
|
||||
|
||||
# 删除某个字符串中两个模式之间的内容,返回新字符串
|
||||
def remove_substrings(original_string, start, end):
|
||||
import re
|
||||
escaped_start = re.escape(start)
|
||||
escaped_end = re.escape(end)
|
||||
pattern = f'{escaped_start}.*?{escaped_end}'
|
||||
return re.sub(pattern, '', original_string, flags=re.DOTALL)
|
||||
|
||||
# 获取旋转矩阵(输入为角度)
|
||||
def get_rotation_matrix(angle_deg):
|
||||
import numpy as np
|
||||
@ -88,11 +173,6 @@ def convert_matrix_data_into_xyz_data(x_array, y_array, matrix):
|
||||
z_array[ix*len_y+iy] = matrix[ix, iy]
|
||||
return x_array, y_array, z_array
|
||||
|
||||
# 从列表中删除某个匹配的元素
|
||||
def remove_item_in_one_array(array, item):
|
||||
new_array = [x for x in array if x != item]
|
||||
return new_array
|
||||
|
||||
# 并行计算前的预处理,把参数分成多份
|
||||
def preprocess_for_parallel_calculations(parameter_array_all, task_num=1, task_index=0):
|
||||
import numpy as np
|
||||
@ -127,78 +207,6 @@ def run_programs_sequentially(program_files=['./a.py', './b.py'], execute='pytho
|
||||
end = time.time()
|
||||
print('Total running time = '+str((end-start)/60)+' min')
|
||||
|
||||
# 判断一个数是否接近于整数
|
||||
def close_to_integer(value, abs_tol=1e-3):
|
||||
import math
|
||||
result = math.isclose(value, round(value), abs_tol=abs_tol)
|
||||
return result
|
||||
|
||||
# 根据子数组的第index个元素对子数组进行排序(index从0开始)
|
||||
def sort_array_by_index_element(original_array, index):
|
||||
sorted_array = sorted(original_array, key=lambda x: x[index])
|
||||
return sorted_array
|
||||
|
||||
# 随机获得一个整数,左闭右闭
|
||||
def get_random_number(start=0, end=1):
|
||||
import random
|
||||
rand_number = random.randint(start, end) # 左闭右闭 [start, end]
|
||||
return rand_number
|
||||
|
||||
# 选取一个种子生成固定的随机整数,左闭右开
|
||||
def generate_random_int_number_for_a_specific_seed(seed=0, x_min=0, x_max=10):
|
||||
import numpy as np
|
||||
np.random.seed(seed)
|
||||
rand_num = np.random.randint(x_min, x_max) # 左闭右开[x_min, x_max)
|
||||
return rand_num
|
||||
|
||||
# 获取两个模式之间的字符串
|
||||
def get_string_between_two_patterns(original_string, start, end, include_start_and_end=0):
|
||||
import re
|
||||
pattern = f'{start}(.*?){end}'
|
||||
result = re.search(pattern, original_string)
|
||||
if result:
|
||||
if include_start_and_end == 0:
|
||||
return result.group(1)
|
||||
else:
|
||||
return start+result.group(1)+end
|
||||
else:
|
||||
return ''
|
||||
|
||||
# 删除某个字符串中两个模式之间的内容,返回新字符串
|
||||
def remove_substrings(original_string, start, end):
|
||||
import re
|
||||
escaped_start = re.escape(start)
|
||||
escaped_end = re.escape(end)
|
||||
pattern = f'{escaped_start}.*?{escaped_end}'
|
||||
return re.sub(pattern, '', original_string, flags=re.DOTALL)
|
||||
|
||||
# 打印数组
|
||||
def print_array(array, line_break=0):
|
||||
if line_break == 0:
|
||||
for i0 in array:
|
||||
print(i0)
|
||||
else:
|
||||
for i0 in array:
|
||||
print(i0)
|
||||
print()
|
||||
|
||||
# 以显示编号的样式,打印数组
|
||||
def print_array_with_index(array, show_index=1, index_type=0):
|
||||
if show_index==0:
|
||||
for i0 in array:
|
||||
print(i0)
|
||||
else:
|
||||
if index_type==0:
|
||||
index = 0
|
||||
for i0 in array:
|
||||
print(index, i0)
|
||||
index += 1
|
||||
else:
|
||||
index = 0
|
||||
for i0 in array:
|
||||
index += 1
|
||||
print(index, i0)
|
||||
|
||||
# 根据一定的字符长度来分割文本
|
||||
def split_text(text, width=100):
|
||||
split_text_list = [text[i:i+width] for i in range(0, len(text), width)]
|
||||
|
@ -37,4 +37,9 @@ def pdf_to_text(pdf_path):
|
||||
import guan
|
||||
print('Warning: The current function name has been deprecated, which will be deleted in the future version. Please change it into guan.pdf_to_text_with_pdfminer3k().')
|
||||
content = guan.pdf_to_text_with_pdfminer3k(pdf_path)
|
||||
return content
|
||||
return content
|
||||
|
||||
def statistics_with_day_and_time(content='', filename='time_logging', file_format='.txt'):
|
||||
import guan
|
||||
print('Warning: The current function name has been deprecated, which will be deleted in the future version. Please change it into guan.logging_with_day_and_time().')
|
||||
guan.logging_with_day_and_time(content=content, filename=filename, file_format=file_format)
|
@ -244,11 +244,16 @@ def write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix,
|
||||
f.write('\n')
|
||||
|
||||
# 创建一个sh文件用于提交任务(PBS)
|
||||
def make_sh_file_for_qsub(sh_filename='a', command_line='python a.py', cpu_num=1, task_name='task', cd_dir=0):
|
||||
def make_sh_file_for_qsub(sh_filename='a', command_line='python a.py', cpu_num=1, task_name='task', cd_dir=0, pbs_q=0, queue_name='bigmem', specific_node=0, node_name='node50.cluster'):
|
||||
sh_content = \
|
||||
'#!/bin/sh\n' \
|
||||
+'#PBS -N '+task_name+'\n' \
|
||||
+'#PBS -l nodes=1:ppn='+str(cpu_num)+'\n'
|
||||
+f'#PBS -N {task_name}\n'
|
||||
if pbs_q==1:
|
||||
sh_content += f'#PBS -q {queue_name}\n'
|
||||
if specific_node==0:
|
||||
sh_content += f'#PBS -l nodes=1:ppn={cpu_num}\n'
|
||||
else:
|
||||
sh_content += f'#PBS -l nodes={node_name}:ppn={cpu_num}\n'
|
||||
if cd_dir==1:
|
||||
sh_content += 'cd $PBS_O_WORKDIR\n'
|
||||
sh_content += command_line
|
||||
@ -256,11 +261,13 @@ def make_sh_file_for_qsub(sh_filename='a', command_line='python a.py', cpu_num=1
|
||||
f.write(sh_content)
|
||||
|
||||
# 创建一个sh文件用于提交任务(Slurm)
|
||||
def make_sh_file_for_sbatch(sh_filename='a', command_line='python a.py', cpu_num=1, task_name='task', cd_dir=0):
|
||||
def make_sh_file_for_sbatch(sh_filename='a', command_line='python a.py', cpu_num=1, task_name='task', cd_dir=0, sbatch_partition=0, partition_name='cpu48'):
|
||||
sh_content = \
|
||||
'#!/bin/sh\n' \
|
||||
+'#SBATCH --job-name='+task_name+'\n' \
|
||||
+'#SBATCH --cpus-per-task='+str(cpu_num)+'\n'
|
||||
+f'#SBATCH --job-name={task_name}\n'
|
||||
if sbatch_partition==1:
|
||||
sh_content += f'#SBATCH --partition={partition_name}\n'
|
||||
sh_content += f'#SBATCH --cpus-per-task={cpu_num}\n'
|
||||
if cd_dir==1:
|
||||
sh_content += 'cd $PBS_O_WORKDIR\n'
|
||||
sh_content += command_line
|
||||
@ -271,10 +278,10 @@ def make_sh_file_for_sbatch(sh_filename='a', command_line='python a.py', cpu_num
|
||||
def make_sh_file_for_bsub(sh_filename='a', command_line='python a.py', cpu_num=1, task_name='task', cd_dir=0, bsub_q=0, queue_name='score'):
|
||||
sh_content = \
|
||||
'#!/bin/sh\n' \
|
||||
+'#BSUB -J '+task_name+'\n' \
|
||||
+'#BSUB -n '+str(cpu_num)+'\n'
|
||||
+f'#BSUB -J {task_name}\n'
|
||||
if bsub_q==1:
|
||||
sh_content += '#BSUB -q '+queue_name+'\n'
|
||||
sh_content += f'#BSUB -q {queue_name}\n'
|
||||
sh_content += f'#BSUB -n {cpu_num}\n'
|
||||
if cd_dir==1:
|
||||
sh_content += 'cd $PBS_O_WORKDIR\n'
|
||||
sh_content += command_line
|
||||
|
@ -1,4 +1,21 @@
|
||||
# functions_using_objects_of_custom_classes
|
||||
# Module: functions_using_objects_of_custom_classes
|
||||
|
||||
# 将原子对象列表转成多个独立列表
|
||||
def convert_atom_object_list_to_multiple_lists(atom_object_list):
|
||||
name_list = []
|
||||
index_list = []
|
||||
x_list = []
|
||||
y_list = []
|
||||
z_list = []
|
||||
energy_list = []
|
||||
for atom_object in atom_object_list:
|
||||
name_list.append(atom_object.name)
|
||||
index_list.append(atom_object.index)
|
||||
x_list.append(atom_object.x)
|
||||
y_list.append(atom_object.y)
|
||||
z_list.append(atom_object.z)
|
||||
energy_list.append(atom_object.energy)
|
||||
return name_list, index_list, x_list, y_list, z_list, energy_list
|
||||
|
||||
# 将原子对象列表转成原子字典列表
|
||||
def convert_atom_object_list_to_atom_dict_list(atom_object_list):
|
||||
|
Loading…
x
Reference in New Issue
Block a user