用python帮博士师兄解决流态化专业问题

2021-05-14 10:17:35 浏览数 (1)

今天博士师兄让我帮忙实现一个画图的代码,虽然研究背景比较专业,但是需求就是在某两个大表中找到相同的数据并画柱状图,下面就直接贴代码了,主要用的就是numpy包,注释也比较详细:

代码语言:javascript复制
#!/usr/bin/env python

import numpy as np
import xlsxwriter


# Step 1: Read data from flux plane raw particle files
def caculate():
    # Change the file names to match your project settings
    f1 = 'FLUX_upstream_raw_particle'
    f2 = 'FLUX_downstream_raw_particle'

    # Check column numbers, because they might be different for your project

    # Fluxplane
    #
    # @   1  "Time"                                                        "s"
    # @   2  "Unique particle ID"                                          ""

    tCol = 1
    pidCol = 2

    t1, pid1 = np.genfromtxt(f1, usecols=(tCol - 1, pidCol - 1), unpack=True)
    t2, pid2 = np.genfromtxt(f2, usecols=(tCol - 1, pidCol - 1), unpack=True)

    # Step 2: Calculate travel time between the two flux planes
    # 用numpy包创建一个array数组
    travelTimeArray = np.array([])

    # 将第一个文件里的第一列和第二列的每两个元素组装成一个元组 比如第一个文件的第一行是1,3;第二行是2,9
    # 现在的zip(t1, pid1)就变成了[(1,3),(2,9)]
    for myTime, myPID in zip(t1, pid1):
        # 判断第一个表格的Id是否在第二个文件的id列里出现
        if np.isin(myPID, pid2):
            # 遍历出第二个文件里面的第二列的id和第一个文件里的id相等的一个array数组,每一个
            # arrayIndex就是第二个文件里相应的pid的行数
            for arrayIndex in np.where(pid2 == myPID)[0]:
                # if t2[arrayIndex] > myTime:
                # 把得到的数据保存到之前创建的travelTimeArray数组中
                travelTimeArray = np.append(travelTimeArray, t2[arrayIndex] - myTime)
                # 跳出内层循环
                break
    # --------------------------保存到excel------------------------------------------
    #先定义一个workbook就是excel表格,参数填写想要的名称,每次执行前一定要保证没有此文件
    workbook = xlsxwriter.Workbook('hello.xlsx')  # 建立文件
    # 添加sheet的名字
    worksheet = workbook.add_worksheet(
        "time")
    # 遍历刚才保存的数组,遍历上限是数组的长度,也就是结果的个数
    for i in range(len(travelTimeArray)):
        # 向excel中写入数据,i 1的原因是excel里的第一行是1开始,但是range函数是从0开始
        worksheet.write('A'   str(i   1), travelTimeArray[i])  # 向A1写入
    # 执行完后关闭表格IO操作
    workbook.close()

    # Step 3: Print some general information about the calculated travel times
    print("========= General information about calculated travel times =========")
    print("Length of travelTimeArray =", len(travelTimeArray))
    print("Average travel time =", "{0:.4f}".format(np.mean(travelTimeArray)), "s")
    print("Standard dev. of travel time =", "{0:.4f}".format(np.std(travelTimeArray)), "s")
    print("Minimum travel time =", "{0:.4f}".format(np.amin(travelTimeArray)), "s")
    print("Maximum travel time =", "{0:.4f}".format(np.amax(travelTimeArray)), "s")

    # Step 4: Calculate a histogram of the travel times, to summarize the data
    hist, bin_edges = np.histogram(travelTimeArray)
    print()
    print("========= Histogram of calculated travel times =========")
    print("bin edges:", bin_edges)
    print("histogram:", hist)


if __name__ == '__main__':
    caculate()

0 人点赞