モンテカルロ シミュレーションでは、各実行の概要をデータ ファイルに保存します。各列には、パラメーターまたは結果値のいずれかが含まれます。そのため、最終的には最大 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
結果の 1 つがパラメーターの 1 つに依存することをよく調べたいので、2 番目のパラメーターでグループ化し、1 番目のパラメーターで並べ替える必要があります。また、パラメータに応じて行を除外することもできます。
このために独自のクラスを書き始めましたが、想像以上に難しいようです。今私の質問:これを既に行っているライブラリはありますか? それとも、私は 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