在monte-carlo模拟中,我将每个运行的摘要存储在一个数据文件中,其中每列包含一个参数或一个结果值。所以我最终得到一个大型数据文件,其中存储了多达40列数据,其中许多行与其他行没有任何关系。
比如说,这个文件看起来像这样:
#param1 param2 result1 result2
1.0 1.0 3.14 6.28
1.0 2.0 6.28 12.56
...
2.0 1.0 1.14 2.28
2.0 2.0 2.28 4.56
由于我经常想要研究其中一个结果对其中一个参数的依赖性,我需要按第二个参数分组并按第一个参数排序。此外,我可能希望根据任何参数筛选出行。
我开始为此编写自己的课程,但似乎比人们猜测的更难。现在我的问题是:有没有图书馆,已经这样做了?或者,既然我熟悉SQL,那么编写SQLAlchemy的SQL后端是否很难,这允许我对我的数据进行简单的SQL查询?据我所知,这将提供我需要的一切。
基于cravoori的答案(或者至少是他/她发布的链接中的答案),这是我的问题的一个很好的简短解决方案:
#!/usr/bin/python2
import numpy as np
import sqlite3 as sql
# number of columns to read in
COLUMNS = 31
# read the file. My columns are always 18chars long. the first line are the names
data = np.genfromtxt('compare.dat',dtype=None,delimiter=18, autostrip=True,
names=True, usecols=range(COLUMNS), comments=None)
# connect to the database in memory
con = sql.connect(":memory:")
# create the table 'data' according to the column names
con.execute("create table data({0})".format(",".join(data.dtype.names)))
# insert the data into the table
con.executemany("insert into data values (%s)" % ",".join(['?']*COLUMNS),
data.tolist())
# make some query and create a numpy array from the result
res = np.array(con.execute("select DOS_Exponent,Temperature,Mobility from data ORDER \
BY DOS_Exponent,Temperature ASC").fetchall())
print res
答案 0 :(得分:2)
看到数据是分隔的,一个选项是通过csv模块将文件导入到内存中的SQLite数据库,示例链接如下。 Sqlite支持大多数SQL子句
答案 1 :(得分:0)
假设只需要简单的计算,代码内的方法可以是以下几行:
file = open('list_filter_data.txt', mode='r')
lines = file.read().splitlines()
row_sets = [[float(c) for c in line.split()] for line in lines[1:]] # read and split the lines in the columns
# get only rows whose param1 = 1.0
subset = [row for row in row_sets if row[0] == 1.0]
print subset
# get only rows whose param1 = 2.0
subset = [row for row in row_sets if row[0] == 2.0]
print subset
# average result1 where param2 = 2.0
avg = sum([row[2] for row in row_sets if row[1] == 2.0]) / len([row[2] for row in row_sets if row[1] == 2.0])
print avg
答案 2 :(得分:0)
如果您的文件大小是几MB的数量级,您可以轻松地在内存中读取此内容并使用其他答案进行解决。
如果文件大小是几百MB或几GB,那么最好使用像这里描述的延迟加载方法 - Lazy Method for Reading Big File in Python?
如果您打算进行的计算可以按行进行,那么这些小块应该足以让您做任何您需要的事情。
另外,只需创建一个包含C1,C2,... CN列的SQL表,参数和结果假设它与params和结果之间的一对一关系。只需使用python数据库访问apis来编写SQL语句并分析您需要的任何内容。
另一方面,Excel电子表格也可以解决您的问题