Skip to content

Commit dcb4369

Browse files
authored
Add pl test (#7)
* parse documentation also some refactoring * pl test * Update test_PL.py
1 parent 1eb951f commit dcb4369

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

makeitwright/core/parsers/__init__.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def typeID(*fpaths):
4040
if "LabRAM HR" in txt:
4141
if (htype := horiba_typeID(fpath)) is not None:
4242
types[fpath] = htype
43-
if "Goniometer" in txt:
43+
elif "Goniometer" in txt:
4444
types[fpath] = 'Bruker_XRD'
45-
if "[m]" in txt:
45+
elif "[m]" in txt:
4646
types[fpath] = 'Gwyddion_traces'
4747

4848
if fpath.suffix == '.asc':
@@ -56,7 +56,7 @@ def typeID(*fpaths):
5656
if fpath.suffix == '.wt5':
5757
types[fpath] = 'wt5'
5858

59-
print(f"{len(types)} of {len(fpaths)} files identified as valid data types")
59+
print(f"{len(types)} of {len(fpaths)} files designated valid")
6060
return types
6161

6262

@@ -78,30 +78,43 @@ def listfiles(fdir:str|pathlib.Path, pattern:str="*") -> list[pathlib.Path]:
7878
]
7979

8080

81-
def parse(fdir, objective, select_types=None, keywords:list|str=[], exclude=[]):
81+
def parse(fdir, objective, select_types=None, keywords:list|str=[], exclude:list|str=[]):
8282
"""
83-
DOCUMENTATION NEEDED
83+
import all files in a directory matching name rules.
84+
A file must match all provided keywords.
85+
Any file that matches any exclude word is ignored.
86+
87+
Parameters
88+
----------
89+
fdir: path-like
90+
directory to search for files
91+
objective: identifier
92+
the objective used. for images, this is used to convert camera indices into spatial coordinates
93+
select_types: list of strings (optional)
94+
types of data to keep (e.g. "TRPL"). other data types are ignored.
95+
keywords: list of strings
96+
files are only parsed if their names contain all keywords
97+
exclude: string or list of strings
98+
files are only parsed if their names contain no exclude words
99+
100+
see also
101+
--------
102+
103+
WrightTools.collection.from_directory
84104
"""
85-
files = listfiles(fdir)
86-
87-
include = [1 for i in range(len(files))]
88-
if keywords:
89-
if type(keywords) is not list:
90-
keywords = [keywords]
91-
for kw in keywords:
92-
for i, f in enumerate(files):
93-
if kw not in str(f):
94-
include[i]=0
95-
if exclude:
96-
if type(exclude) is not list:
97-
exclude = [exclude]
98-
for x in exclude:
99-
for i, f in enumerate(files):
100-
if x in str(f):
101-
include[i]=0
102-
103-
files = [file for i, file in zip(include, files) if i]
104-
print(f'found {sum(include)} files matching keyword specifications')
105+
if not isinstance(keywords, list):
106+
keywords = [keywords]
107+
if not isinstance(exclude, list):
108+
exclude = [exclude]
109+
110+
files = [
111+
file for file in filter(
112+
lambda f: all([kw in str(f) for kw in keywords])
113+
and all(x not in str(f) for x in exclude),
114+
listfiles(fdir)
115+
)
116+
]
117+
print(f'found {len(files)} files matching keyword specifications')
105118

106119
ftypes = typeID(*files)
107120
if select_types:

makeitwright/core/parsers/andor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def fromAndorNeo(fpath, name=None, px_per_um=None):
2626
data
2727
New data object.
2828
"""
29-
# parse filepath
3029
data:wt.Data = wt.data.from_Solis(fpath, name=name, verbose=True)
3130
data.rename_variables(xindex="x", yindex="y", wm="wl")
3231
data.rename_channels(signal="sig")
@@ -47,5 +46,4 @@ def fromAndorNeo(fpath, name=None, px_per_um=None):
4746
else:
4847
data.sig.label = "counts"
4948

50-
5149
return data

tests/test_PL.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import makeitwright as mw
2+
from makeitwright import datasets
3+
4+
andor = mw.andor
5+
parse = mw.parsers.parse
6+
7+
8+
def test_import_andor():
9+
"""smokescreen to see if importing fails"""
10+
p = datasets.PL
11+
filepath = p.parent
12+
filename = p.stem
13+
14+
data1 = parse(filepath, objective="10", keywords=filename + ".asc")
15+
data2 = mw.parsers.fromAndorNeo(p)
16+
assert data1.variable_names == data2.variable_names == ("wl", "y")
17+
18+
19+
if __name__ == "__main__":
20+
test_import_andor()

0 commit comments

Comments
 (0)