Repository navigation
Expand file tree
/
Copy pathplot_dates.py
More file actions
165 lines (132 loc) · 4.87 KB
/
Copy pathplot_dates.py
File metadata and controls
165 lines (132 loc) · 4.87 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
"""
绘制 papers 表中 updated_at 列的时间分布直方图。
对数据库的日期格式要求:
- ISO 8601 (无微秒): "2025-06-19T10:23:50+00:00"
用法:
python plot_dates.py [--mode relative|absolute] [--bins N] [--days N] [--output FILE]
选项:
--mode 显示模式:
- relative: X 轴显示 "距今 -N 天",标题显示截止日期 (默认)
- absolute: X 轴显示绝对日期
--bins 直方图柱数,默认 50
--days 仅绘制最近 N 天内的论文,过滤长尾数据。默认 0 (不过滤)
--output 输出文件路径,默认 plot_dates.png
"""
import argparse
import sqlite3
from datetime import datetime, timezone, timedelta
from typing import List
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil import parser as dtparser
from app.config import CONFIG
def load_dates(days: int = 0) -> List[datetime]:
"""
从数据库加载所有 updated_at 日期。
Args:
days: 仅加载最近 N 天内的日期。0 表示不过滤。
Returns:
datetime 对象列表 (UTC 时区)
"""
conn = sqlite3.connect(CONFIG.db_path)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
rows = cur.execute("SELECT updated_at FROM papers WHERE updated_at IS NOT NULL").fetchall()
cur.close()
conn.close()
now = datetime.now(timezone.utc)
cutoff = now - timedelta(days=days) if days > 0 else None
dates = []
for row in rows:
try:
dt = dtparser.parse(row["updated_at"])
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
if cutoff is None or dt >= cutoff:
dates.append(dt)
except Exception:
continue
return dates
def plot_relative(dates: List[datetime], bins: int, output: str) -> None:
"""
绘制相对日期直方图。
X 轴: 距今天数 (负数,如 -200, -100, 0)
标题: "论文更新时间分布 (截止 YYYY-MM-DD)"
"""
now = datetime.now(timezone.utc)
days_ago = [(dt - now).total_seconds() / 86400 for dt in dates]
fig, ax = plt.subplots(figsize=(12, 6))
ax.hist(days_ago, bins=bins, edgecolor="white", alpha=0.8, color="#2d7ff9")
ax.set_xlabel("Days ago", fontsize=12)
ax.set_ylabel("Number of papers", fontsize=12)
ax.set_title(f"Paper update time distribution (as of {now.strftime('%Y-%m-%d')})", fontsize=14)
ax.grid(axis="y", alpha=0.3)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
plt.savefig(output, dpi=150)
print(f"图表已保存至: {output}")
def plot_absolute(dates: List[datetime], bins: int, output: str) -> None:
"""
绘制绝对日期直方图。
X 轴: 绝对日期 (如 2024-01, 2024-07, 2025-01)
标题: "论文更新时间分布"
"""
fig, ax = plt.subplots(figsize=(12, 6))
ax.hist(dates, bins=bins, edgecolor="white", alpha=0.8, color="#ff6b4a")
ax.set_xlabel("Date", fontsize=12)
ax.set_ylabel("Number of papers", fontsize=12)
ax.set_title("Paper update time distribution", fontsize=14)
# 格式化 X 轴日期
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m"))
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
fig.autofmt_xdate(rotation=45)
ax.grid(axis="y", alpha=0.3)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
plt.savefig(output, dpi=150)
print(f"图表已保存至: {output}")
def main() -> None:
parser = argparse.ArgumentParser(
description="绘制 papers 表中 updated_at 列的时间分布直方图。"
)
parser.add_argument(
"--mode",
type=str,
choices=["relative", "absolute"],
default="relative",
help="显示模式: relative (距今天数) 或 absolute (绝对日期)。默认: relative",
)
parser.add_argument(
"--bins",
type=int,
default=50,
help="直方图柱数。默认: 50",
)
parser.add_argument(
"--days",
type=int,
default=0,
help="仅绘制最近 N 天内的论文,过滤长尾数据。默认: 0 (不过滤)",
)
parser.add_argument(
"--output",
type=str,
default="plot_dates.png",
help="输出文件路径。默认: plot_dates.png",
)
args = parser.parse_args()
dates = load_dates(days=args.days)
if not dates:
print("没有找到有效的日期数据。")
return
print(f"加载了 {len(dates)} 条日期记录。")
print(f"日期范围: {min(dates).strftime('%Y-%m-%d')} ~ {max(dates).strftime('%Y-%m-%d')}")
if args.mode == "relative":
plot_relative(dates, args.bins, args.output)
else:
plot_absolute(dates, args.bins, args.output)
if __name__ == "__main__":
main()