只读取CSV文件列表的前210000笔数据和后210000笔数据。
import pandas as pd
import matplotlib.pyplot as plt
import time
from pathlib import Path
import logging
from concurrent.futures import ProcessPoolExecutor, as_completed
import argparse
def process_file(file_path, input_directory, output_directory):
data1 = pd.read_csv(file_path)
nRow, nCol = data1.shape
index = 0
plt.figure()
for i in range(nRow):
if data1.columns[i] == 'datetime':
continue
plt.subplot(nRow - 1, 1, index)
index += 1
plt.plot(data1.iloc[:, i])
plt.title(data1.columns[i])
formatted_time = time.strftime('%Y_%m_%d_%H_%M', time.localtime())
file_stem = file_path.stem.replace('@', '_').replace(':', '_')
file_name = f'{formatted_time}_{file_stem}_3000.png'
output_path = Path(output_directory) / file_name
try:
plt.savefig(str(output_path))
logging.info(f"存储图片中... {output_path}")
plt.close()
except Exception as e:
logging.error(f"保存图像 {output_path} 时发生错误: {e}")
return None
return output_path
def animate_charts(output_directory):
paused = False
def on_key_press(event):
nonlocal paused
if event.key == ' ': # 空格键
paused = not paused
print("Paused." if paused else "Continuing...")
paths = sorted(Path(output_directory).glob('*.png'), key=lambda p: p.stat().st_mtime, reverse=True)[:2]
latest_paths = iter(paths)
while True:
for path in latest_paths:
print(f"正在显示文件: {path}")
if paused:
plt.connect('key_press_event', on_key_press)
img = plt.imread(str(path))
fig, ax = plt.subplots()
ax.imshow(img)
ax.axis('off') # 关闭坐标轴
creation_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(path.stat().st_ctime))
ax.set_title(f'File Created at: {creation_time}')
plt.show(block=False)
duration = 55 if paused else 60
text_obj = None
for remaining in range(duration, 0, -1):
if not paused:
break
time.sleep(1)
if text_obj is not None:
text_obj.remove() # 直接移除文本对象
text_obj = ax.text(0.5, 0.15, f"Timing: {remaining}s", transform=ax.transAxes, ha='center', va='center', color='red', fontsize=8)
fig.canvas.draw_idle()
plt.pause(0.001) # 更新图形界面
plt.close()
else:
img = plt.imread(str(path))
fig, ax = plt.subplots()
ax.imshow(img)
ax.axis('off') # 关闭坐标轴
creation_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(path.stat().st_ctime))
ax.set_title(f'File Created at: {creationTime}')
plt.show(block=False)
duration = 55 if paused else 60
text_obj = None
for remaining in range(duration, 0, -1):
time.sleep(1)
if text_obj is not None:
text_obj.remove() # 直接移除文本对象
text_obj = ax.text(0.5, 0.15, f"Timing: {remaining}s", transform=ax.transAxes, ha='center', va='center', color='red', fontsize=8)
fig.canvas.draw_idle()
plt.pause(0.001) # 更新图形界面
plt.close()
def main(input_directory, output_directory):
logging.info("-----开始运行-----")
sensor_dir = Path(input_directory)
files = list(sensor_dir.glob('*.csv'))[:210000] + list(sensor_dir.glob('*.csv'))[-210000:] # 只读取前210000笔数据和后210000笔数据
logging.info("即将进行文件读取...")
for i, f in enumerate(files, start=1):
logging.info(f"文件读取中 {i}/{len(files)}")
logging.info(f.name)
fig_dir = Path(output_directory)
if not fig_dir.exists():
try:
fig_dir.mkdir(parents=True, exist_ok=True)
except FileExististError:
logging.info("目录已存在。")
current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
logging.info(f"读取完毕,读取时间:{current_time} ^_^ 存储图片进行中...")
with ProcessPoolExecutor() as executor:
futures = []
for file_path in files:
future = executor.submit(process_file, file_path, input_directory, output_directory)
futures.append(future)
for future in as_completed(futures):
result = future.result()
if result is not None:
logging.info(f"文件 {result} 处理完成")
else:
logging.warning("某个文件处理失败")
logging.info("存储完毕")
animate_charts(output_directory)
if __name__ == '__main__':
input_directory = 'C:\\Users\\Administrator\\Desktop\\Sensor'
output_directory = 'C:\\Users\\Administrator\\Desktop\\Fig'
parser = argparse.ArgumentParser(description="Process CSV files and generate charts.")
parser.add_argument("--input", type=str, default=input_directory, help="Input directory containing CSV files.")
parser.add_argument("--output", type=str, default=output_directory, help="Output directory to save generated images.")
args = parser.parse_args()
main(args.input, args.output)