Je crée une classe qui stocke les données de plusieurs conditions dans chaque répertoire.
Pour définir les conditions, séparez chaque catégorie par une liste et placez-les dans la classe. root_path
est le répertoire qui stocke les répertoires à créer.
a = handle_multivalue([["aaa","bbb","ccc"],["asd","dfg"],["1","2","3","4","5"]], root_path="root")
La liste des chemins est stockée dans pathlst
.
print a.pathlst
production
['aaa/asd/1', 'aaa/asd/2', 'aaa/asd/3', 'aaa/asd/4', 'aaa/asd/5', 'aaa/dfg/1', 'aaa/dfg/2', 'aaa/dfg/3', 'aaa/dfg/4', 'aaa/dfg/5', 'bbb/asd/1', 'bbb/asd/2', 'bbb/asd/3', 'bbb/asd/4', 'bbb/asd/5', 'bbb/dfg/1', 'bbb/dfg/2', 'bbb/dfg/3', 'bbb/dfg/4', 'bbb/dfg/5', 'ccc/asd/1', 'ccc/asd/2', 'ccc/asd/3', 'ccc/asd/4', 'ccc/asd/5', 'ccc/dfg/1', 'ccc/dfg/2', 'ccc/dfg/3', 'ccc/dfg/4', 'ccc/dfg/5']
Après avoir analysé les données de chaque condition, stockez les données dans le chemin ci-dessus.
Vous pouvez trier les catégories avec la méthode suivante. L'ordre des chemins stockés dans pathlst
change.
a.transpose([1,2,0])
print a.pathlst
production
['aaa/asd/1', 'bbb/asd/1', 'ccc/asd/1', 'aaa/dfg/1', 'bbb/dfg/1', 'ccc/dfg/1', 'aaa/asd/2', 'bbb/asd/2', 'ccc/asd/2', 'aaa/dfg/2', 'bbb/dfg/2', 'ccc/dfg/2', 'aaa/asd/3', 'bbb/asd/3', 'ccc/asd/3', 'aaa/dfg/3', 'bbb/dfg/3', 'ccc/dfg/3', 'aaa/asd/4', 'bbb/asd/4', 'ccc/asd/4', 'aaa/dfg/4', 'bbb/dfg/4', 'ccc/dfg/4', 'aaa/asd/5', 'bbb/asd/5', 'ccc/asd/5', 'aaa/dfg/5', 'bbb/dfg/5', 'ccc/dfg/5']
dump_all_path
crée tous les chemins dans pathlst
dans root_path
.
a.dump_all_path()
Si vous entrez root_path
dans la classe, vous pouvez obtenir une liste de chaque condition et le chemin complet des répertoires créés.
b = handle_multivalue("root")
print b.condlst
print b.pathlst
production
[['aaa', 'bbb', 'ccc'], ['asd', 'dfg'], ['1', '2', '3', '4', '5']]
['aaa/asd/1', 'aaa/asd/2', 'aaa/asd/3', 'aaa/asd/4', 'aaa/asd/5', 'aaa/dfg/1', 'aaa/dfg/2', 'aaa/dfg/3', 'aaa/dfg/4', 'aaa/dfg/5', 'bbb/asd/1', 'bbb/asd/2', 'bbb/asd/3', 'bbb/asd/4', 'bbb/asd/5', 'bbb/dfg/1', 'bbb/dfg/2', 'bbb/dfg/3', 'bbb/dfg/4', 'bbb/dfg/5', 'ccc/asd/1', 'ccc/asd/2', 'ccc/asd/3', 'ccc/asd/4', 'ccc/asd/5', 'ccc/dfg/1', 'ccc/dfg/2', 'ccc/dfg/3', 'ccc/dfg/4', 'ccc/dfg/5']
Le script est illustré ci-dessous.
class handle_multivalue():
def __init__(self, obj, root_path="tmpdir"):
self.idx = "ijklmnop"
self.ud = "ddddddd"
if type(obj) == str:
self.root_path = obj
self.get_all_path_from_dir()
else:
self.condlst = obj
self.root_path = root_path
self.get_all_path()
def get_all_path(self):
arrs = []
lenlst = []
for num, i in enumerate(self.condlst):
arr = arange(len(i))
lenlst += [len(i)]
arrs += [tensor(arr, idx=self.idx[num], ud="d")]
self.shape = array(lenlst)
o = tensor(ones(lenlst), self.idx[:len(lenlst)], self.ud[:len(lenlst)])
self.cond_idx = []
for arr in arrs:
ret = arr * o
ret.transpose(self.idx[:len(lenlst)])
self.cond_idx += [ret.arr.flatten()]
lst = []
for cond, idx in zip(self.condlst, self.cond_idx):
lst += [array(cond)[idx.astype(int)]]
self.pathlst = ["/".join(x).strip("/") for x in array(lst).T]
def get_all_conds(self):
idx = "ijklmnop"
ud = "ddddddd"
gen_dir_path(self.root_path, [])
self.cond_idx = self.all_conds_2d()
lst = []
for cond, idx in zip(self.condlst, self.conidx):
lst += [array(cond)[idx.astype(int)]]
self.conds2d = array(lst).T
def get_all_path_from_dir(self):
self.condlst = []
self.gen_dir_path(self.root_path, [])
self.cond_idx = self.all_conds_2d()
lst = []
for cond, idx in zip(self.condlst, self.cond_idx):
lst += [array(cond)[idx.astype(int)]]
self.pathlst = ["/".join(x).strip("/") for x in array(lst).T]
def all_conds_2d(self):
o = tensor(ones(tuple(self.shape)), self.idx[:len(self.condlst)],self.ud[:len(self.condlst)])
retlst = []
for num,i in enumerate(self.condlst):
ret = tensor(arange(len(i)), self.idx[num], self.ud[num]) * o
ret.transpose(self.idx[:len(self.shape)])
retlst += [ret.arr.flatten()]
return retlst
def gen_dir_path(self, path, condlst):
lst = glob(path+"/*")
if len(lst) == 0:
self.shape = array([len(x) for x in condlst])
self.condlst = condlst
return 0
cond = [x.split("/")[-1] for x in lst]
condlst += [cond]
self.gen_dir_path(lst[0], condlst)
def transpose(self, tlst):
import copy
tmp_condlst = copy.deepcopy(self.condlst)
self.condlst = array(self.condlst)[array(tlst)]
self.shape = array([len(x) for x in self.condlst])
self.cond_idx = self.all_conds_2d()
lst = []
for cond, idx in zip(self.condlst, self.cond_idx):
lst += [array(cond)[idx.astype(int)]]
#print lst
t_arr = array(lst)[argsort(tlst)]
self.condlst = tmp_condlst
self.pathlst = ["/".join(x).strip("/") for x in t_arr.T]
def view(self):
import matplotlib.pyplot as plt
for n,i in enumerate(self.pathlst):
if not n%a.shape[-1]:
plt.figure()
x = loadtxt(self.root_path + "/" + i)
plt.plot(x, label = i)
plt.legend()
plt.show()
def dump_all_path(self):
for i in self.pathlst:
os.makedirs(self.root_path + "/" + i)
Recommended Posts