複数条件のデータハンドリング


複数条件のデータをそれぞれのディレクトリに格納するクラスを作っています。

条件の設定には、各カテゴリ毎にリストで区切ってクラスに入れます。`root_path`は作成するディレクトリ群を格納するディレクトリです。

```python
a = handle_multivalue([["aaa","bbb","ccc"],["asd","dfg"],["1","2","3","4","5"]], root_path="root")
```

パスのリストは`pathlst`に格納されています。

```python
print a.pathlst
```

出力

```
['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']
```

各条件のデータを解析したら上記パスにそれぞれデータを格納します。

カテゴリの並べ替えは下記のメソッドで可能です。`pathlst`に格納されたパスの順番が変わります。

```python
a.transpose([1,2,0])
print a.pathlst
```

出力

```
['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`で`pathlst`内の全パスを`root_path`内に作成します。

```python
a.dump_all_path()
```

`root_path`をクラスに入力すると、作成したディレクトリ群から各条件のリストと全パスを得ることができます。

```python
b = handle_multivalue("root")
print b.condlst
print b.pathlst
```

出力

```
[['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']
```


以下にスクリプトを示します。

```python
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)