-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
98 lines (83 loc) · 2.71 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
import h5py, os, sys
from contextlib import contextmanager
from pathlib import Path
def is_valid_h5_file(path):
"""
Checks to ensure that the file path is a valid hdf5 file
"""
try:
file = h5py.File(path, 'r')
except IOError:
raise argparse.ArgumentTypeError("File is not valid h5 format")
return path
def get_h5_file(path):
try:
file = h5py.File(path, 'r')
except Exception:
raise IOError("Not a valid h5 file")
return file
def getNumberOfFiles(path):
"""Return number of data files linked to a master file. Much
slower than listing the number of files in a master file but
also checks for correct type."""
f = get_h5_file(path)
group = f['/entry/data']
num = 0
for value in group:
if isinstance(group.get(value, getlink=True), h5py.ExternalLink):
num += 1
return num
def getNumberOfFiles_fast(path):
"""return number of data files by simply
finding the length of the H5 group. No checks."""
f = get_h5_file(path)
return len(f['/entry/data'])
def getNumberOfFilesToProcess(path,num=None):
if num == None:
return getNumberOfFiles_fast(path)
else:
return num
def getNumberOfDataWells(masterfile,oscillation,framesperdegree):
"""returns the number of data wells in the master file."""
filenum = getNumberOfFiles_fast(masterfile)
frames = framesperdegree / oscillation
return int(filenum / frames)
def getMasterPrefix(arg):
""""Returns the name of the master file without the "_master.h5"
suffix.
"""
if arg.endswith('_master.h5'):
stripped = arg.replace('_master.h5','')
head, tail = os.path.split(stripped)
return tail
else:
return
def setupMasterDirectory(input, dir):
"Creates a master directory."
master = getMasterPrefix(input)
masterdir = os.path.join(dir,master)
try:
os.mkdir(masterdir)
except FileExistsError:
pass
def getMasterDirectory(input,dir):
master = getMasterPrefix(input)
return os.path.join(dir,master)
def setupMasterDirectories(argslist):
"""Sets up the master directories into which data sets
will be processed. Input is parsed arguments.
"""
for master in argslist.input:
masterdir = setupMasterDirectory(master,argslist.output)
def getMasterDirectoryPaths(argslist):
"""Returns paths of master directories. Input is parsed arguments.
"""
masterlist = []
for masterfile in argslist.input:
master = getMasterPrefix(masterfile)
masterdir = os.path.join(argslist.output, master)
masterlist.append(masterdir)
return masterlist
def setup(argslist):
setupMasterDirectories(argslist)