|
a |
|
b/tool/Code/utilities/misc.py |
|
|
1 |
# Copyright 2019 Population Health Sciences and Image Analysis, German Center for Neurodegenerative Diseases(DZNE) |
|
|
2 |
# |
|
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
4 |
# you may not use this file except in compliance with the License. |
|
|
5 |
# You may obtain a copy of the License at |
|
|
6 |
# |
|
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
|
8 |
# |
|
|
9 |
# Unless required by applicable law or agreed to in writing, software |
|
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
12 |
# See the License for the specific language governing permissions and |
|
|
13 |
# limitations under the License. |
|
|
14 |
|
|
|
15 |
import os |
|
|
16 |
import fnmatch |
|
|
17 |
import h5py |
|
|
18 |
import numpy as np |
|
|
19 |
from shutil import copy2 |
|
|
20 |
|
|
|
21 |
def locate_file(pattern, root): |
|
|
22 |
'''Locate all files matching supplied filename pattern in and below |
|
|
23 |
supplied root directory.''' |
|
|
24 |
file_path=[] |
|
|
25 |
for path, dirs, files in os.walk(os.path.abspath(root)): |
|
|
26 |
for filename in fnmatch.filter(files, pattern): |
|
|
27 |
file_path.append(os.path.join(path, filename)) |
|
|
28 |
return file_path |
|
|
29 |
|
|
|
30 |
def locate_dir(pattern, root): |
|
|
31 |
'''Locate all files matching supplied filename pattern in and below |
|
|
32 |
supplied root directory.''' |
|
|
33 |
dir_path=[] |
|
|
34 |
for path, dirs, files in os.walk(os.path.abspath(root)): |
|
|
35 |
for dirname in fnmatch.filter(dirs, pattern): |
|
|
36 |
dir_path.append(os.path.join(path, dirname)) |
|
|
37 |
return dir_path |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
def save_dict_to_hdf5(dic, filename): |
|
|
41 |
""" |
|
|
42 |
.... |
|
|
43 |
""" |
|
|
44 |
with h5py.File(filename, 'w') as h5file: |
|
|
45 |
recursively_save_dict_contents_to_group(h5file, '/', dic) |
|
|
46 |
|
|
|
47 |
def recursively_save_dict_contents_to_group(h5file, path, dic): |
|
|
48 |
""" |
|
|
49 |
.... |
|
|
50 |
""" |
|
|
51 |
for key, item in dic.items(): |
|
|
52 |
if isinstance(item, (np.ndarray, np.int64, np.dtype(float).type, str, bytes)): |
|
|
53 |
h5file[path + key] = item |
|
|
54 |
elif isinstance(item, dict): |
|
|
55 |
recursively_save_dict_contents_to_group(h5file, path + key + '/', item) |
|
|
56 |
else: |
|
|
57 |
raise ValueError('Cannot save %s type'%type(item)) |
|
|
58 |
|
|
|
59 |
def load_dict_from_hdf5(filename): |
|
|
60 |
""" |
|
|
61 |
.... |
|
|
62 |
""" |
|
|
63 |
with h5py.File(filename, 'r') as h5file: |
|
|
64 |
return recursively_load_dict_contents_from_group(h5file, '/') |
|
|
65 |
|
|
|
66 |
def recursively_load_dict_contents_from_group(h5file, path): |
|
|
67 |
""" |
|
|
68 |
.... |
|
|
69 |
""" |
|
|
70 |
ans = {} |
|
|
71 |
for key, item in h5file[path].items(): |
|
|
72 |
if isinstance(item, h5py._hl.dataset.Dataset): |
|
|
73 |
ans[key] = item.value |
|
|
74 |
elif isinstance(item, h5py._hl.group.Group): |
|
|
75 |
ans[key] = recursively_load_dict_contents_from_group(h5file, path + key + '/') |
|
|
76 |
return ans |
|
|
77 |
|
|
|
78 |
def move_file(source,destination): |
|
|
79 |
copy2(source,destination) |
|
|
80 |
return None |
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|