File size: 1,576 Bytes
3ca5af2
7ca6ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e63bc3
7ca6ce4
 
 
 
 
3ca5af2
7ca6ce4
 
 
 
3ca5af2
 
7ca6ce4
 
 
 
 
 
 
3ca5af2
7ca6ce4
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
import gradio as gr
import os
import codecs
import numpy as np
import pandas as pd
from scipy.signal import find_peaks


def process_text(file):

    row = []  # 行号
    x = []    # x值
    y = []    # y值

    filename = file.split(".")[0]
    target_path = f"{filename}.csv"  # 目标路径
    
    # 读取文件内容
    with codecs.open(file, 'r', 'utf-8') as f:
        lines = f.readlines()
        data = []
        for line in lines:
            temp = line.split()
            temp1 = [int(float(x)) for x in temp]
            data.append(temp1)
        data = np.array(data)
        peaks, _ = find_peaks(data[:, 1], distance=2500)  # 根据第2列找到所有峰值, 前后比较阈值为distance=2500

        temp_row = []
        temp_x = []
        temp_y = []
        for peak in peaks:
            temp_row.append(peak)
            temp_x.append(data[peak, 0])
            temp_y.append(data[peak, 1])

        temp_y = sorted(enumerate(temp_y), key=lambda z: z[1], reverse=True)

        for index, value in temp_y:
            row.append(temp_row[index])
            x.append(temp_x[index])
            y.append(value)

        datas = {
                'row_num': row,
                'x': x,
                'y': y,
            }

        df = pd.DataFrame(datas)
        df.to_csv(target_path, index=False)
    
    return df


iface = gr.Interface(
    fn=process_text,
    inputs="file",
    outputs="text",
    title="Upload and Process Text File",
    description="Upload a txt file and it will be processed by this app.",
)

iface.launch()