直方图中的命令行参数

时间:2017-07-08 19:54:55

标签: python excel command-line-arguments argparse

我正在编写一些代码,这些代码从目录中导出excel文件中的直方图,并根据某些参数对数据进行分类,并相应地导出新的excel文件,并将其分类到各自的bin中,例如。如果有一个数字5.16那么bin(5,10)的bin计数将增加一个,依此类推。但是,我想写一些东西,我可以输入一个特定的值,相应地改变bin,就像我想要3的分档,我会选择import pandas as pd import numpy as np import matplotlib.pyplot as plt import openpyxl from pandas import ExcelWriter import os datadir = '/Users/user/Desktop/Newfolder/' for file in os.listdir(datadir): if file.endswith('.xlsx'): data = pd.read_excel(os.path.join(datadir, file)) counts, bins, patches = plt.hist(data.values, bins=range(0, int(max(data.values)+5), 5)) df = pd.DataFrame({'bin_leftedge': bins[:-1], 'count': counts}) plt.title('Data') plt.xlabel('Neuron') plt.ylabel('# of Spikes') plt.show() outfile = os.path.join(datadir, file.replace('.xlsx', '_bins.xlsx')) writer = pd.ExcelWriter(outfile) df.to_excel(writer) writer.save() ,代码现在会相应地分区,这样它就会产生bin(0,3),(3,6)等等,并且应用与之前相同的规则。原始代码是:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import openpyxl
from pandas import ExcelWriter
import os
import argparse

datadir = '/Users/user/Desktop/Newfolder/'

parser = argparse.ArgumentParser(description = 'Calculating the bin width')
parser.add_argument('n', type=int, help='Changing of the width')
args = parser.parse_args()

def vary(n):
    wid = n
     return wid

if __name__ == '__main__':
    print(vary(args.n))

for file in os.listdir(datadir):
    if file.endswith('.xlsx'):
       data = pd.read_excel(os.path.join(datadir, file))
       counts, bins, patches = plt.hist(data.values, bins=range(0, 
       int(max(data.values)+vary(n)), vary(n)))
       df = pd.DataFrame({'bin_leftedge': bins[:-1], 'count': counts})
       plt.title('Data')
       plt.xlabel('Neuron')
       plt.ylabel('# of Spikes')
       plt.show()

       outfile = os.path.join(datadir, file.replace('.xlsx', '_bins.xlsx'))
       writer = pd.ExcelWriter(outfile)
       df.to_excel(writer)
       writer.save()

我的想法是使用argparse作为命令行参数,我可以在bin中输入更改,所以我写道:

Traceback (most recent call last):
File "\Users\user\Desktop\Bins.py", line 25, in <module>
        counts, bins, patches = plt.hist(data.values, bins=range(0, int(max(data.values)+vary(n)), vary(n)))
NameError: name 'n' is not defined

如果这看起来很惯用,我会事先道歉,因为我对编码很新,但对任何事情都不太了解。无论如何,我得到一个错误说

argparse

我可以得到一些帮助吗,我如何在直方图中实现这个命令行参数("autoload": { "psr-4": { "": "src/" }, "classmap": [ "app/AppKernel.php", "app/AppCache.php" ] } ),这样我每次需要更改它时都可以输入相应的bin。非常感谢任何帮助,谢谢

2 个答案:

答案 0 :(得分:0)

不会过多地使用代码,命令行参数很容易使用sys模块进行处理:

import sys
print sys.argv # first element is the script name, then follow the parameters as strings

这个脚本,如果我将它命名为sysArgs.py并在控制台中使用某些参数调用它,则打印

python sysArgs.py lala 5
['sysArgs.py', 'lala', '5']

如果您只想传递一个参数n,请将其更改为

import sys
n = int(sys.argv[1])
# do stuff with n other than printing it
print n

答案 1 :(得分:0)

以下是我如何组织您的代码。可以重新组织main,但我的重点是命令行解析的位置。

datadir = '/Users/user/Desktop/Newfolder/'
n = 3  # a default if not used with argparse

def main(datadir, n):
    # might split the load and the plot functions
    # or put the action for one file in a separate function

    for file in os.listdir(datadir):
        if file.endswith('.xlsx'):
           data = pd.read_excel(os.path.join(datadir, file))
           counts, bins, patches = plt.hist(data.values, bins=range(0, 
           int(max(data.values)+n), n))
           df = pd.DataFrame({'bin_leftedge': bins[:-1], 'count': counts})
           plt.title('Data')
           plt.xlabel('Neuron')
           plt.ylabel('# of Spikes')
           plt.show()

           outfile = os.path.join(datadir, file.replace('.xlsx', '_bins.xlsx'))
           writer = pd.ExcelWriter(outfile)
           df.to_excel(writer)
           writer.save()

if __name__ == '__main__':
    # run only as a script; not on import
    # if more complicated define this parser in a function

    parser = argparse.ArgumentParser(description = 'Calculating the bin width')
    parser.add_argument('n', type=int, help='Changing of the width')
    args = parser.parse_args()

    print(args)  # to see what argparse does
    main(datadir, args.n)
    # main(args.datadir, args.n) # if parser has a datadir  argument

(我还没有测试过这个。)

相关问题