Intervals

Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3623    Accepted Submission(s): 1329


Problem Description
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.

Write a program that:

> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,

> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,

> writes the answer to the standard output
 

Input
The first line of the input contains an integer n (1 <= n <= 50 000) - the number of intervals. The following n lines describe the intervals. The i+1-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi - ai + 1.

Process to the end of file.

 

Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
 

Sample Input
  
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
 

Sample Output
  
6
 

Author
1384
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1529  1531  1548  1534  1317 
 

Statistic |  Submit |  Discuss |  Note
这个题刚开始题都看不懂,完全不知道要算的是什么,后来讲过之后才理解了题意,然后也不会写,看过学长的程序后,现在才算是真的懂了这个题了,spfa中用到了先进先出的队列,最开始的时候只把源点压入队列中,并且用一个数组来存从源点到其他每个点的距离,最初设置的距离都是INF,源点到自己的距离是0,然后取出队首的点计算到其他每个点的距离是否小于当前的距离,是则更新,并判断这个点是否在队列中,如果不在则把他压入队尾,如果一个点被压入队列超过了n次则说明存在有负值的环。因为这个题没有说有环的情况,所以可以不用判环。

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#define N 51000
#define inf 1e9

using namespace std;
int num,p[N],n;
struct node
{
    int en,len,next;
}edg[N*10];
void add(int st,int en,int len)
{
    edg[num].en=en;
    edg[num].len=len;
    edg[num].next=p[st];        //该节点的前一个位置
    p[st]=num++;                //该节点当前的位置
}
queue<int>que;
bool inque[N];
int dis[N];
void spfa(int st,int en)
{
    for(int i=0;i<=en;i++)
    {
        dis[i]=-inf;
        inque[i]=false;
    }
    que.push(st);
    dis[st]=0;
    inque[st]=true;
    while(que.size())
    {
    int x=que.front();
    que.pop();
    inque[x]=false;
    for(int i=p[x];i!=-1;i=edg[i].next)
    {
        int y=edg[i].en;
        if(dis[x]+edg[i].len>dis[y])
        {
            dis[y]=dis[x]+edg[i].len;
            if(!inque[y])
            {
                que.push(y);
                inque[y]=true;
            }
        }
    }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        num=0;
        memset(p,-1,sizeof(p));
        int st=inf,en=-1;
        for(int i=1;i<=n;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add(a,b+1,c);
            st=min(st,a);
            en=max(en,b+1);
        }
        for(int i=st;i<=en;i++)
        {
            add(i,i-1,-1);
            add(i-1,i,0);
        }
        spfa(st,en);
        printf("%d\n",dis[en]);
    }
    return 0;
}


import os import numpy as np import matplotlib.pyplot as plt import re from matplotlib.ticker import MaxNLocator from scipy.stats import linregress # 解决中文显示问题 plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'WenQuanYi Micro Hei'] plt.rcParams['axes.unicode_minus'] = False def natural_sort_key(s): """自然排序算法:确保文件名按数字顺序排列""" return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)] def find_stable_intervals(counts, method='std', min_window=300, max_window=2000, threshold=0.5, merge_gap=300, min_length=500, window_step=50): """ 改进版稳定区间检测:支持三种不同指标 :param counts: 预测框数量列表(原始数据) :param method: 检测方法 ('std', 'zscore', 'slope') :param min_window: 最小窗口尺寸 :param max_window: 最大窗口尺寸 :param threshold: 阈值(基于整体统计量) :param merge_gap: 相邻区间合并的最大间隔 :param min_length: 最小有效区间长度 :param window_step: 窗口尺寸增加的步长 :return: 优化后的稳定区间列表 """ n = len(counts) if n == 0: return [] # 返回空列表 # 计算整体统计量(基于原始数据) total_mean = np.mean(counts) total_std = np.std(counts) # 1. 多窗口尺寸检测机制 base_intervals = [] # 遍历所有窗口尺寸 for window_size in range(min_window, max_window + 1, window_step): # 确保窗口大小不超过数据长度 if window_size > n: continue step_size = max(1, window_size) # 50%重叠滑动 # 使用当前窗口尺寸检测稳定区间 for i in range(0, n - window_size + 1, step_size): window = counts[i:i + window_size] if len(window) < 2: # 至少需要2个点计算 continue # 根据不同方法计算稳定性指标 if method == 'std': # 标准差方法 std_dev = np.std(window) if std_dev < threshold: base_intervals.append((i, i + window_size - 1)) elif method == 'zscore_avg': # Z-score方法:基于窗口内所有点的Z-score绝对值的平均值 mean_val = np.mean(window) std_val = np.std(window) if std_val > 0: # 避免除以0 # 计算所有点的Z-score绝对值 z_scores = np.abs((window - mean_val) / std_val) # 计算Z-score绝对值的平均值 avg_zscore = np.mean(z_scores) # 与阈值比较 if avg_zscore < threshold: # 平均Z-score绝对值低于阈值 base_intervals.append((i, i + window_size - 1)) # 处理标准差为0的特殊情况(所有值相同) elif len(window) > 0: # 所有点相同,Z-score绝对值为0,肯定小于任何正阈值 base_intervals.append((i, i + window_size - 1)) elif method == 'slope': # 趋势斜率方法 x = np.arange(len(window)) slope, _, _, _, _ = linregress(x, window) if abs(slope) < threshold: base_intervals.append((i, i + window_size - 1)) # 如果没有检测到任何区间,直接返回 if not base_intervals: return [] # 返回空列表 # 2. 合并相邻平稳段 base_intervals.sort(key=lambda x: x[0]) # 确保按起始索引排序 merged_intervals = [] if base_intervals: # 确保列表非空 current_start, current_end = base_intervals[0] for start, end in base_intervals[1:]: if start - current_end <= merge_gap: # 间隔小于合并阈值 current_end = max(current_end, end) # 扩展当前区间 else: merged_intervals.append((current_start, current_end)) current_start, current_end = start, end merged_intervals.append((current_start, current_end)) # 3. 过滤短时伪平稳段 final_intervals = [ (start, end) for start, end in merged_intervals if (end - start + 1) >= min_length # 区间长度包含两端点 ] return final_intervals def plot_box_count_trend(file_list, box_counts, stable_intervals, output_path, title_suffix="", method_name="标准差"): """ 绘制预测框数量变化趋势图并标记稳定区间 修改:根据方法名称设置对应颜色,与合并图一致 """ plt.figure(figsize=(20, 10)) # 绘制整体趋势(原始数据) plt.plot(file_list, box_counts, 'b-', linewidth=1.5, label='预测框数量') # 根据方法名称设置颜色(与合并图保持一致) method_colors = { '标准差方法': 'green', 'Z-score方法': 'purple', '趋势斜率方法': 'orange' } # 获取当前方法的颜色 fill_color = method_colors.get(method_name, 'green') # 默认绿色 # 标记稳定区间 for i, (start, end) in enumerate(stable_intervals): interval_files = file_list[start:end + 1] interval_counts = box_counts[start:end + 1] if not interval_counts: continue # 计算区间统计量 avg_count = np.mean(interval_counts) min_count = np.min(interval_counts) max_count = np.max(interval_counts) std_dev = np.std(interval_counts) # 绘制稳定区间 - 使用对应方法的颜色 plt.fill_between(interval_files, min_count, max_count, color=fill_color, alpha=0.3, zorder=0, label=f'{method_name}区间' if i == 0 else "") # 添加区间标注 mid_idx = start + (end - start) // 2 if mid_idx < len(file_list): plt.annotate(f"区间{i + 1}: {start + 1}-{end + 1}\n均值: {avg_count:.1f}±{std_dev:.1f}", (file_list[mid_idx], avg_count), xytext=(0, 20), textcoords='offset points', ha='center', fontsize=10, bbox=dict(boxstyle="round,pad=0.3", fc="yellow", alpha=0.7), zorder=10) # 设置图表属性 plt.title(f'预测框数量变化趋势 - {method_name}{title_suffix}', fontsize=18) plt.xlabel('图像文件名', fontsize=14) plt.ylabel('预测框数量', fontsize=14) plt.xticks(rotation=90, fontsize=7) plt.grid(True, linestyle='--', alpha=0.6) plt.legend(loc='upper right') plt.gca().xaxis.set_major_locator(MaxNLocator(20)) plt.tight_layout() plt.savefig(output_path, dpi=150, bbox_inches='tight') plt.close() def plot_combined_intervals(file_list, box_counts, intervals_std, intervals_zscore, intervals_slope, output_path): """ 绘制三种方法检测结果的合并图 :param file_list: 文件名列表 :param box_counts: 原始预测框数量列表 :param intervals_std: 标准差方法检测的区间 :param intervals_zscore: Z-score方法检测的区间 :param intervals_slope: 趋势斜率方法检测的区间 :param output_path: 输出图片路径 """ plt.figure(figsize=(20, 10)) # 绘制整体趋势(原始数据) plt.plot(file_list, box_counts, 'b-', linewidth=1.5, label='预测框数量') # 为每种方法定义不同的颜色和标签(与单独图表一致) method_colors = { '标准差方法': ('green', '标准差区间'), 'Z-score方法': ('purple', 'Z-score区间'), '趋势斜率方法': ('orange', '趋势斜率区间') } # 绘制标准差方法的区间 for i, (start, end) in enumerate(intervals_std): interval_files = file_list[start:end + 1] min_count = min(box_counts[start:end + 1]) max_count = max(box_counts[start:end + 1]) plt.fill_between(interval_files, min_count, max_count, color=method_colors['标准差方法'][0], alpha=0.3, zorder=0, label=method_colors['标准差方法'][1] if i == 0 else "") # 绘制Z-score方法的区间 for i, (start, end) in enumerate(intervals_zscore): interval_files = file_list[start:end + 1] min_count = min(box_counts[start:end + 1]) max_count = max(box_counts[start:end + 1]) plt.fill_between(interval_files, min_count, max_count, color=method_colors['Z-score方法'][0], alpha=0.3, zorder=0, label=method_colors['Z-score方法'][1] if i == 0 else "") # 绘制趋势斜率方法的区间 for i, (start, end) in enumerate(intervals_slope): interval_files = file_list[start:end + 1] min_count = min(box_counts[start:end + 1]) max_count = max(box_counts[start:end + 1]) plt.fill_between(interval_files, min_count, max_count, color=method_colors['趋势斜率方法'][0], alpha=0.3, zorder=0, label=method_colors['趋势斜率方法'][1] if i == 0 else "") # 设置图表属性 plt.title('预测框数量变化趋势及稳定区间分析 - 三种方法合并', fontsize=18) plt.xlabel('图像文件名', fontsize=14) plt.ylabel('预测框数量', fontsize=14) plt.xticks(rotation=90, fontsize=7) plt.grid(True, linestyle='--', alpha=0.6) plt.legend(loc='upper right') plt.gca().xaxis.set_major_locator(MaxNLocator(20)) plt.tight_layout() plt.savefig(output_path, dpi=150, bbox_inches='tight') plt.close() # 配置路径 label_dir = "E:/0718/0718-labels" # 替换为您的标签文件夹路径 output_dir = "E:/0718/0718-stable" # 输出目录 os.makedirs(output_dir, exist_ok=True) # 获取文件列表并按自然顺序排序 file_list = [f for f in os.listdir(label_dir) if f.endswith(".txt")] file_list.sort(key=natural_sort_key) # 提取文件名(不含扩展名) file_names = [os.path.splitext(f)[0] for f in file_list] # 统计每个文件的预测框数量 box_counts = [] for file in file_list: file_path = os.path.join(label_dir, file) count = 0 with open(file_path, 'r') as f: for line in f: if line.strip(): # 非空行 count += 1 box_counts.append(count) # 计算整体统计数据 total_mean = np.mean(box_counts) total_std = np.std(box_counts) # 使用三种不同方法找出稳定区间(直接使用原始数据) intervals_std = find_stable_intervals( box_counts, method='std', min_window=500, max_window=2000, threshold=1.1, # 标准差阈值 merge_gap=300, min_length=600 ) intervals_zscore = find_stable_intervals( box_counts, method='zscore_avg', min_window=500, max_window=2000, threshold=0.75, merge_gap=300, min_length=600 ) intervals_slope = find_stable_intervals( box_counts, method='slope', min_window=500, max_window=2000, threshold=0.00015, # 趋势斜率阈值 merge_gap=300, min_length=600 ) # 生成三种方法的结果图片 output_std = os.path.join(output_dir, "box_count_stable_intervals_std.png") output_zscore = os.path.join(output_dir, "box_count_stable_intervals_zscore.png") output_slope = os.path.join(output_dir, "box_count_stable_intervals_slope.png") output_combined = os.path.join(output_dir, "box_count_stable_intervals_combined.png") # 绘制最终结果图表(使用统一的方法名称) plot_box_count_trend(file_names, box_counts, intervals_std, output_std, title_suffix="", method_name="标准差方法") plot_box_count_trend(file_names, box_counts, intervals_zscore, output_zscore, title_suffix="", method_name="Z-score方法") plot_box_count_trend(file_names, box_counts, intervals_slope, output_slope, title_suffix="", method_name="趋势斜率方法") # 生成合并图 plot_combined_intervals(file_names, box_counts, intervals_std, intervals_zscore, intervals_slope, output_combined) # 输出详细结果 print(f"分析完成! 共处理 {len(file_list)} 个文件") print(f"整体平均框数: {total_mean:.2f} ± {total_std:.2f}") def print_interval_info(intervals, method_name): print(f"\n{method_name}发现 {len(intervals)} 个稳定区间:") for i, (start, end) in enumerate(intervals): interval_counts = box_counts[start:end + 1] avg_count = np.mean(interval_counts) std_dev = np.std(interval_counts) cv = std_dev / avg_count if avg_count > 0 else 0 # 计算趋势斜率(基于原始数据) x = np.arange(len(interval_counts)) slope, _, _, _, _ = linregress(x, interval_counts) print(f"区间{i + 1}:") print(f" - 文件范围: {start + 1}-{end + 1} (共{end - start + 1}个文件)") print(f" - 平均框数: {avg_count:.2f} ± {std_dev:.2f}") print(f" - 变异系数: {cv:.4f}") print(f" - 趋势斜率: {slope:.6f}") print(f" - 最小值: {min(interval_counts)}, 最大值: {max(interval_counts)}") print_interval_info(intervals_std, "标准差方法") print_interval_info(intervals_zscore, "Z-score方法") print_interval_info(intervals_slope, "趋势斜率方法") # 合并所有检测到的区间 all_intervals = intervals_std + intervals_zscore + intervals_slope def merge_intervals(intervals, merge_gap=300, min_length=500): """合并重叠或接近的区间""" if not intervals: return [] # 按起始索引排序 intervals.sort(key=lambda x: x[0]) merged = [] current_start, current_end = intervals[0] for start, end in intervals[1:]: if start - current_end <= merge_gap: # 间隔小于合并阈值 current_end = max(current_end, end) # 扩展当前区间 else: merged.append((current_start, current_end)) current_start, current_end = start, end merged.append((current_start, current_end)) # 过滤短区间 final_merged = [ (start, end) for start, end in merged if (end - start + 1) >= min_length ] return final_merged # 合并所有检测到的区间 merged_intervals = merge_intervals(all_intervals, merge_gap=300, min_length=500) # 保存区间信息到文本文件 def save_interval_report(intervals, method_name, file_path): with open(file_path, 'a') as f: f.write(f"\n{method_name}稳定区间分析报告\n") f.write(f"稳定区间数: {len(intervals)}\n") for i, (start, end) in enumerate(intervals): interval_counts = box_counts[start:end + 1] avg_count = np.mean(interval_counts) std_dev = np.std(interval_counts) cv = std_dev / avg_count if avg_count > 0 else 0 # 计算趋势斜率 x = np.arange(len(interval_counts)) slope, _, _, _, _ = linregress(x, interval_counts) f.write(f"\n区间 {i + 1}:\n") f.write(f" 起始文件索引: {start + 1} ({file_names[start]})\n") f.write(f" 结束文件索引: {end + 1} ({file_names[end]})\n") f.write(f" 文件数量: {end - start + 1}\n") f.write(f" 平均预测框数: {avg_count:.2f} ± {std_dev:.2f}\n") f.write(f" 变异系数: {cv:.4f}\n") f.write(f" 趋势斜率: {slope:.6f}\n") f.write(f" 最小值: {min(interval_counts)}, 最大值: {max(interval_counts)}\n") f.write("=" * 80 + "\n") # 创建报告文件 interval_info_path = os.path.join(output_dir, "stable_intervals_report.txt") with open(interval_info_path, 'w') as f: f.write(f"稳定区间综合分析报告\n") f.write(f"总文件数: {len(file_list)}\n") f.write(f"整体平均框数: {total_mean:.2f} ± {total_std:.2f}\n") f.write(f"数据范围: {min(box_counts)}-{max(box_counts)}\n") # 保存三种方法的区间报告 save_interval_report(intervals_std, "标准差方法", interval_info_path) save_interval_report(intervals_zscore, "Z-score方法", interval_info_path) save_interval_report(intervals_slope, "趋势斜率方法", interval_info_path) # 保存合并后的区间报告 with open(interval_info_path, 'a') as f: f.write("\n\n=== 合并区间分析报告 ===\n") f.write("此部分展示三种方法检测到的所有稳定区间合并后的结果\n") f.write(f"合并后稳定区间数: {len(merged_intervals)}\n") for i, (start, end) in enumerate(merged_intervals): interval_counts = box_counts[start:end + 1] avg_count = np.mean(interval_counts) std_dev = np.std(interval_counts) cv = std_dev / avg_count if avg_count > 0 else 0 # 计算趋势斜率 x = np.arange(len(interval_counts)) slope, _, _, _, _ = linregress(x, interval_counts) # 检测此区间被哪些方法覆盖 covered_by = [] if any(start >= s and end <= e for s, e in intervals_std): covered_by.append("标准差") if any(start >= s and end <= e for s, e in intervals_zscore): covered_by.append("Z-score") if any(start >= s and end <= e for s, e in intervals_slope): covered_by.append("趋势斜率") f.write(f"\n合并区间 {i + 1}:\n") f.write(f" 起始文件索引: {start + 1} ({file_names[start]})\n") f.write(f" 结束文件索引: {end + 1} ({file_names[end]})\n") f.write(f" 文件数量: {end - start + 1}\n") f.write(f" 平均预测框数: {avg_count:.2f} ± {std_dev:.2f}\n") f.write(f" 最小值: {min(interval_counts)}, 最大值: {max(interval_counts)}\n") f.write(f" 覆盖方法: {', '.join(covered_by) if covered_by else '无'}\n") # 添加合并区间统计 total_covered_files = sum(end - start + 1 for start, end in merged_intervals) coverage_percentage = (total_covered_files / len(file_list)) * 100 f.write("\n合并区间统计:\n") f.write(f" 总覆盖文件数: {total_covered_files}/{len(file_list)} ({coverage_percentage:.2f}%)\n") f.write(f" 平均区间长度: {np.mean([end - start + 1 for start, end in merged_intervals]):.1f} 文件\n") f.write(f" 最长区间: {max([end - start + 1 for start, end in merged_intervals])} 文件\n") f.write(f" 最短区间: {min([end - start + 1 for start, end in merged_intervals])} 文件\n") print_interval_info(merged_intervals, "合并区间") print(f"\n结果图片已保存至: {output_dir}") print(f"详细区间报告已保存至: {interval_info_path}") 把趋势斜率,z-score,标准差的稳定区间图中合并相邻相邻平稳段之前的图也生成出来,要完整代码
最新发布
07-31
<think>我们需要在现有代码的基础上,增加绘制合并前(即base_intervals)的稳定区间的图。 但注意:base_intervals是在find_stable_intervals函数内部产生的,而该函数只返回合并后的最终区间。 因此,我们需要修改find_stable_intervals函数,使其同时返回base_intervals(合并前的区间)和final_intervals(合并后的区间)。 然而,考虑到当前函数结构,我们不想破坏现有调用,所以我们可以选择: 1. 修改函数返回值为两个列表(base_intervals和final_intervals) 2. 或者单独写一个函数来绘制合并前的区间,但这需要重新计算。 由于我们希望在同一个流程中同时得到合并前和合并后的区间图,我们选择修改find_stable_intervals函数,使其返回两个结果。 但是,注意:现有代码中已经调用了find_stable_intervals,并且只使用了一个返回值(final_intervals)。因此,我们需要调整调用方式。 然而,考虑到代码的改动量,我们可以采用另一种方式:在find_stable_intervals函数内部,将base_intervals也保存下来,并通过可选参数返回。 但为了最小化改动,我们决定修改find_stable_intervals函数,使其返回两个结果:base_intervals(合并前)和final_intervals(合并后)。 修改步骤: 1. 修改find_stable_intervals函数,在最后返回两个列表:base_intervals(合并前)和final_intervals(合并后)。 2. 在调用find_stable_intervals的地方,接收两个返回值。 3. 为合并前的区间(base_intervals)生成图表。 另外,我们还需要为合并前的区间绘制图表,因此需要一个新的绘图函数,或者修改现有的绘图函数以支持绘制不同的区间列表。 由于合并前的区间数量可能非常多,我们在绘图时可能需要注意性能,并且图例标签可能会很混乱,因此我们只绘制区间,不添加标注(因为区间太多,标注会重叠)。 我们将创建一个新的绘图函数,用于绘制合并前的区间,这个函数将类似于plot_box_count_trend,但只绘制区间而不标注。 考虑到时间,我们决定创建一个新的绘图函数`plot_base_intervals`,它只绘制区间,不添加标注,并且使用半透明的颜色,不同区间使用同一种颜色。 同时,为了避免图例标签过多,我们只用一个图例项代表所有基础区间。 注意:合并前的区间可能非常多,因此我们只用一个图例项,然后一次性绘制所有区间。 具体修改如下: 1. 修改`find_stable_intervals`函数的返回,使其返回两个列表:`base_intervals`(原始检测到的区间,未合并)和`final_intervals`(合并并过滤后的区间)。 2. 在调用`find_stable_intervals`的地方,分别接收这两个返回值。 3. 为每个方法的`base_intervals`绘制一张图。 由于合并前的区间数量可能很大,我们在绘制时使用循环,但只用一个标签(避免图例中出现大量条目)。 新绘图函数`plot_base_intervals`: ```python def plot_base_intervals(file_list, box_counts, base_intervals, output_path, method_name="基础区间"): """ 绘制基础区间(合并前)的图 :param file_list: 文件名列表 :param box_counts: 框数量列表 :param base_intervals: 基础区间列表(未合并) :param output_path: 输出图片路径 :param method_name: 方法名称 """ plt.figure(figsize=(20, 10)) # 绘制整体趋势 plt.plot(file_list, box_counts, 'b-', linewidth=1.5, label='预测框数量') # 为所有基础区间使用同一种颜色,半透明 for start, end in base_intervals: interval_files = file_list[start:end + 1] interval_counts = box_counts[start:end + 1] if not interval_counts: continue min_count = min(interval_counts) max_count = max(interval_counts) # 绘制区间,颜色为浅绿色,半透明,且只用一个标签(只在第一次绘制时添加图例) if start == base_intervals[0][0]: # 第一个区间添加图例 plt.fill_between(interval_files, min_count, max_count, color='lightgreen', alpha=0.3, label=f'{method_name}') else: plt.fill_between(interval_files, min_count, max_count, color='lightgreen', alpha=0.3) plt.title(f'预测框数量变化趋势 - {method_name}(合并前)', fontsize=18) plt.xlabel('图像文件名', fontsize=14) plt.ylabel('预测框数量', fontsize=14) plt.xticks(rotation=90, fontsize=7) plt.grid(True, linestyle='--', alpha=0.6) plt.legend(loc='upper right') plt.gca().xaxis.set_major_locator(MaxNLocator(20)) plt.tight_layout() plt.savefig(output_path, dpi=150, bbox_inches='tight') plt.close() ``` 4. 在调用`find_stable_intervals`后,除了绘制最终的稳定区间图,还要绘制基础区间图。 注意:由于基础区间非常多,绘图可能会比较慢,但我们的数据量不大(文件数约2000),所以可以接受。 5. 修改函数定义和调用: 修改`find_stable_intervals`函数的返回语句: return base_intervals, final_intervals # 原来只返回final_intervals 然后,在调用时: base_intervals_std, intervals_std = find_stable_intervals(...) base_intervals_zscore, intervals_zscore = find_stable_intervals(...) base_intervals_slope, intervals_slope = find_stable_intervals(...) 6. 为每个方法的基础区间生成图片: output_base_std = os.path.join(output_dir, "box_count_base_intervals_std.png") plot_base_intervals(file_names, box_counts, base_intervals_std, output_base_std, method_name="标准差方法基础区间") ... 其他两个方法类似 由于改动较大,我们重新整理代码。 注意:由于合并前的区间可能非常密集,图片可能看起来是一片绿色,但这是为了展示算法中间结果。 考虑到时间,我们只修改预测框数量的部分(即当前代码),坐标统计量的部分类似,但根据问题要求,我们只处理预测框数量。 下面给出修改后的完整代码(只针对预测框数量分析部分):</think>以下是修改后的完整代码,增加了生成合并前基础区间的图表功能: ```python import os import numpy as np import matplotlib.pyplot as plt import re from matplotlib.ticker import MaxNLocator from scipy.stats import linregress # 解决中文显示问题 plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'WenQuanYi Micro Hei'] plt.rcParams['axes.unicode_minus'] = False def natural_sort_key(s): """自然排序算法:确保文件名按数字顺序排列""" return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)] def find_stable_intervals(counts, method='std', min_window=300, max_window=2000, threshold=0.5, merge_gap=300, min_length=500, window_step=50): """ 改进版稳定区间检测:支持三种不同指标 :param counts: 预测框数量列表(原始数据) :param method: 检测方法 ('std', 'zscore', 'slope') :param min_window: 最小窗口尺寸 :param max_window: 最大窗口尺寸 :param threshold: 阈值(基于整体统计量) :param merge_gap: 相邻区间合并的最大间隔 :param min_length: 最小有效区间长度 :param window_step: 窗口尺寸增加的步长 :return: (基础区间列表, 最终区间列表) """ n = len(counts) if n == 0: return [], [] # 返回空列表 # 计算整体统计量(基于原始数据) total_mean = np.mean(counts) total_std = np.std(counts) # 1. 多窗口尺寸检测机制 base_intervals = [] # 遍历所有窗口尺寸 for window_size in range(min_window, max_window + 1, window_step): # 确保窗口大小不超过数据长度 if window_size > n: continue step_size = max(1, window_size) # 50%重叠滑动 # 使用当前窗口尺寸检测稳定区间 for i in range(0, n - window_size + 1, step_size): window = counts[i:i + window_size] if len(window) < 2: # 至少需要2个点计算 continue # 根据不同方法计算稳定性指标 if method == 'std': # 标准差方法 std_dev = np.std(window) if std_dev < threshold: base_intervals.append((i, i + window_size - 1)) elif method == 'zscore_avg': # Z-score方法:基于窗口内所有点的Z-score绝对值的平均值 mean_val = np.mean(window) std_val = np.std(window) if std_val > 0: # 避免除以0 # 计算所有点的Z-score绝对值 z_scores = np.abs((window - mean_val) / std_val) # 计算Z-score绝对值的平均值 avg_zscore = np.mean(z_scores) # 与阈值比较 if avg_zscore < threshold: # 平均Z-score绝对值低于阈值 base_intervals.append((i, i + window_size - 1)) # 处理标准差为0的特殊情况(所有值相同) elif len(window) > 0: # 所有点相同,Z-score绝对值为0,肯定小于任何正阈值 base_intervals.append((i, i + window_size - 1)) elif method == 'slope': # 趋势斜率方法 x = np.arange(len(window)) slope, _, _, _, _ = linregress(x, window) if abs(slope) < threshold: base_intervals.append((i, i + window_size - 1)) # 如果没有检测到任何区间,直接返回 if not base_intervals: return [], [] # 返回空列表 # 2. 合并相邻平稳段 base_intervals.sort(key=lambda x: x[0]) # 确保按起始索引排序 merged_intervals = [] if base_intervals: # 确保列表非空 current_start, current_end = base_intervals[0] for start, end in base_intervals[1:]: if start - current_end <= merge_gap: # 间隔小于合并阈值 current_end = max(current_end, end) # 扩展当前区间 else: merged_intervals.append((current_start, current_end)) current_start, current_end = start, end merged_intervals.append((current_start, current_end)) # 3. 过滤短时伪平稳段 final_intervals = [ (start, end) for start, end in merged_intervals if (end - start + 1) >= min_length # 区间长度包含两端点 ] return base_intervals, final_intervals # 返回基础区间和最终区间 def plot_box_count_trend(file_list, box_counts, stable_intervals, output_path, title_suffix="", method_name="标准差", interval_type="最终"): """ 绘制预测框数量变化趋势图并标记稳定区间 :param interval_type: 区间类型 ("基础" 或 "最终") """ plt.figure(figsize=(20, 10)) # 绘制整体趋势(原始数据) plt.plot(file_list, box_counts, 'b-', linewidth=1.5, label='预测框数量') # 根据方法名称设置颜色(与合并图保持一致) method_colors = { '标准差方法': 'green', 'Z-score方法': 'purple', '趋势斜率方法': 'orange' } # 获取当前方法的颜色 fill_color = method_colors.get(method_name, 'green') # 默认绿色 # 标记稳定区间 for i, (start, end) in enumerate(stable_intervals): interval_files = file_list[start:end + 1] interval_counts = box_counts[start:end + 1] if not interval_counts: continue # 计算区间统计量 avg_count = np.mean(interval_counts) min_count = np.min(interval_counts) max_count = np.max(interval_counts) std_dev = np.std(interval_counts) # 绘制稳定区间 - 使用对应方法的颜色 plt.fill_between(interval_files, min_count, max_count, color=fill_color, alpha=0.3, zorder=0, label=f'{method_name}区间' if i == 0 else "") # 为最终区间添加标注,基础区间太多不添加 if interval_type == "最终": mid_idx = start + (end - start) // 2 if mid_idx < len(file_list): plt.annotate(f"区间{i + 1}: {start + 1}-{end + 1}\n均值: {avg_count:.1f}±{std_dev:.1f}", (file_list[mid_idx], avg_count), xytext=(0, 20), textcoords='offset points', ha='center', fontsize=10, bbox=dict(boxstyle="round,pad=0.3", fc="yellow", alpha=0.7), zorder=10) # 设置图表属性 plt.title(f'预测框数量变化趋势 - {method_name}{title_suffix} ({interval_type}区间)', fontsize=18) plt.xlabel('图像文件名', fontsize=14) plt.ylabel('预测框数量', fontsize=14) plt.xticks(rotation=90, fontsize=7) plt.grid(True, linestyle='--', alpha=0.6) plt.legend(loc='upper right') plt.gca().xaxis.set_major_locator(MaxNLocator(20)) plt.tight_layout() plt.savefig(output_path, dpi=150, bbox_inches='tight') plt.close() def plot_combined_intervals(file_list, box_counts, intervals_std, intervals_zscore, intervals_slope, output_path): """ 绘制三种方法检测结果的合并图 :param file_list: 文件名列表 :param box_counts: 原始预测框数量列表 :param intervals_std: 标准差方法检测的区间 :param intervals_zscore: Z-score方法检测的区间 :param intervals_slope: 趋势斜率方法检测的区间 :param output_path: 输出图片路径 """ plt.figure(figsize=(20, 10)) # 绘制整体趋势(原始数据) plt.plot(file_list, box_counts, 'b-', linewidth=1.5, label='预测框数量') # 为每种方法定义不同的颜色和标签(与单独图表一致) method_colors = { '标准差方法': ('green', '标准差区间'), 'Z-score方法': ('purple', 'Z-score区间'), '趋势斜率方法': ('orange', '趋势斜率区间') } # 绘制标准差方法的区间 for i, (start, end) in enumerate(intervals_std): interval_files = file_list[start:end + 1] min_count = min(box_counts[start:end + 1]) max_count = max(box_counts[start:end + 1]) plt.fill_between(interval_files, min_count, max_count, color=method_colors['标准差方法'][0], alpha=0.3, zorder=0, label=method_colors['标准差方法'][1] if i == 0 else "") # 绘制Z-score方法的区间 for i, (start, end) in enumerate(intervals_zscore): interval_files = file_list[start:end + 1] min_count = min(box_counts[start:end + 1]) max_count = max(box_counts[start:end + 1]) plt.fill_between(interval_files, min_count, max_count, color=method_colors['Z-score方法'][0], alpha=0.3, zorder=0, label=method_colors['Z-score方法'][1] if i == 0 else "") # 绘制趋势斜率方法的区间 for i, (start, end) in enumerate(intervals_slope): interval_files = file_list[start:end + 1] min_count = min(box_counts[start:end + 1]) max_count = max(box_counts[start:end + 1]) plt.fill_between(interval_files, min_count, max_count, color=method_colors['趋势斜率方法'][0], alpha=0.3, zorder=0, label=method_colors['趋势斜率方法'][1] if i == 0 else "") # 设置图表属性 plt.title('预测框数量变化趋势及稳定区间分析 - 三种方法合并', fontsize=18) plt.xlabel('图像文件名', fontsize=14) plt.ylabel('预测框数量', fontsize=14) plt.xticks(rotation=90, fontsize=7) plt.grid(True, linestyle='--', alpha=0.6) plt.legend(loc='upper right') plt.gca().xaxis.set_major_locator(MaxNLocator(20)) plt.tight_layout() plt.savefig(output_path, dpi=150, bbox_inches='tight') plt.close() # 配置路径 label_dir = "E:/0718/0718-labels" # 替换为您的标签文件夹路径 output_dir = "E:/0718/0718-stable" # 输出目录 os.makedirs(output_dir, exist_ok=True) # 获取文件列表并按自然顺序排序 file_list = [f for f in os.listdir(label_dir) if f.endswith(".txt")] file_list.sort(key=natural_sort_key) # 提取文件名(不含扩展名) file_names = [os.path.splitext(f)[0] for f in file_list] # 统计每个文件的预测框数量 box_counts = [] for file in file_list: file_path = os.path.join(label_dir, file) count = 0 with open(file_path, 'r') as f: for line in f: if line.strip(): # 非空行 count += 1 box_counts.append(count) # 计算整体统计数据 total_mean = np.mean(box_counts) total_std = np.std(box_counts) # 使用三种不同方法找出稳定区间(直接使用原始数据) base_intervals_std, intervals_std = find_stable_intervals( box_counts, method='std', min_window=500, max_window=2000, threshold=1.1, # 标准差阈值 merge_gap=300, min_length=600 ) base_intervals_zscore, intervals_zscore = find_stable_intervals( box_counts, method='zscore_avg', min_window=500, max
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值