hcq_lib.py
更新时间:20180410
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import time
import datetime
import os
import shutil
## utils --> bar by hy
import sys
import math
import torch.nn as nn
import torch.nn.init as init
def hcq_write(save_info_path, is_write_time, is_print, write_content):
i = datetime.datetime.now()
date = str(i.year) + str("%02d"%i.month) + str("%02d"%i.day) + "-" + str("%02d"%i.hour) + str("%02d"%i.minute) + str("%02d"%i.second)
with open(save_info_path, "a+") as save_info_txt:
if(is_write_time):
if(is_print):
print("[{}] {}".format(date, write_content))
save_info_txt.writelines("[{}] {}".format(date, write_content) + "\n")
else:
if(is_print):
print(write_content)
save_info_txt.writelines("{}".format(write_content) + "\n")
def hcq_backup_txt_rename(txt_path):
i = datetime.datetime.now()
date = str(i.year) + str("%02d"%i.month) + str("%02d"%i.day) + "-" + str("%02d"%i.hour) + str("%02d"%i.minute) + str("%02d"%i.second)
if os.path.exists(txt_path):
new_name = txt_path +".bak" + date
os.rename(txt_path, new_name)
# copy(new_name, "./backup/")
print("copied and deleted file, new_name = {}".format(new_name))
def hcq_backup_txt_rename_index(txt_path, index):
if os.path.exists(txt_path):
new_name = txt_path + "_" + str(index)
os.rename(txt_path, new_name)
# copy(new_name, "./backup/")
print("copied and deleted file, new_name = {}".format(new_name))
def hcq_bubble_sort(active_train_samples_list):
print("hcq_bubble_sort...")
for i in range(len(active_train_samples_list)-1):
current_status = False
for j in range(len(active_train_samples_list) - i -1):
if active_train_samples_list[j].p_pred_max > active_train_samples_list[j+1].p_pred_max:
temp = active_train_samples_list[j]
active_train_samples_list[j] = active_train_samples_list[j+1]
active_train_samples_list[j+1] = temp
# active_train_samples_list[j], active_train_samples_list[j+1] = active_train_samples_list[j+1], active_train_samples_list[j]
current_status = True
if not current_status:
break
return active_train_samples_list
# def hcq_random_select(active_train_samples_list):
# print("hcq_random_select...")
# active_train_samples_list =
def hcq_create_dir(dir_path):
if not os.path.exists(dir_path):
print("Create dir = {}".format(dir_path))
os.makedirs(dir_path)
def hcq_move(old_file, new_file):
shutil.move(old_file, new_file)
#### progress_bar
#### from hy, 20180310
_, term_width = os.popen('stty size', 'r').read().split()
term_width = int(term_width)
TOTAL_BAR_LENGTH = 10.
last_time = time.time()
begin_time = last_time
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
### class
### author: houchaoqun
### 2018.03.24
class active_train_samples:
def __init__(self, image_path, p_pred_max, result):
self.image_path = image_path
self.p_pred_max = p_pred_max ## model predict the p of real lable
self.result = result
# def __repr__(self):
# return repr((self.name, self.grade, self.age))
备份函数:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import datetime
import shutil
def backup_txt_rename(txt_path):
if os.path.exists(txt_path):
i = datetime.datetime.now()
date = str(i.year) + str("%02d"%i.month) + str("%02d"%i.day) + str("%02d"%i.hour) + str("%02d"%i.minute) + str("%02d"%i.second)
new_name = txt_path +".bak" + date
os.rename(txt_path, new_name)
print("copied and deleted file, new_name = {}".format(new_name))
explain:
1)txt 文件备份,当文件存在时,先备份再生成相同的文件名;
2)txt_path:文件路径,如:
"/home/reserch/documents/deeplearning/alzheimers_disease_DL/pytorch/subject_id/test/file.txt"
3)效果如下: