Skip to content

Commit

Permalink
Merge pull request #1 from nexusformat/master
Browse files Browse the repository at this point in the history
updating fork
  • Loading branch information
rerpha authored Oct 25, 2017
2 parents 5284dde + 64576d6 commit 4770d6e
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 16 deletions.
62 changes: 46 additions & 16 deletions src/nxfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,57 @@ def entries(self):
ent = []
for entry in self.file.keys():
try:
features = [int(feat, 16) for feat in os.listdir(RECIPIE_DIR)]
features = []
for feat in os.listdir(RECIPIE_DIR):
try:
features.append(int(feat, 16))
except:
print("Could not parse feature with name %s" % (feat))
ent.append(InsaneEntryWithFeatures(self.file, entry, features))
except:
print "no recipes in " + RECIPIE_DIR
pass
return ent

class SingleFeatureDiscoverer:
def __init__(self, nxsfile, feature):
self.file = h5py.File(nxsfile, 'r')
self.feature = feature

if __name__ == '__main__':
import optparse

usage = "%prog [options] nxs_file"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-t", "--test", dest="test", help="Test file against all recipes", action="store_true",
default=False)
def entries(self):
ent = []
for entry in self.file.keys():
try:
ent.append(InsaneEntryWithFeatures(self.file, entry, [self.feature]))
except:
print("Issues with parsing feature %i"% self.feature)
pass
return ent

(options, args) = parser.parse_args()

if options.test:
disco = AllFeatureDiscoverer(args[0])
if __name__ == '__main__':
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-t", "--test", dest="test", help="Test file against all recipes", action="store_true",
default=False)
parser.add_argument("-f", "--feature", dest="feature", help="Test file against a defined feature",
default=None)
parser.add_argument("nexusfile", help="Nexus file to test")

args = parser.parse_args()

if args.feature:
try:
disco = SingleFeatureDiscoverer(args.nexusfile, int(args.feature, 16))
except:
print("The feature '%s' has not parsed correctly, exiting" %(args.feature))
sys.exit()
else:
disco = InsaneFeatureDiscoverer(args[0])
if args.test:
disco = AllFeatureDiscoverer(args.nexusfile)
else:
disco = InsaneFeatureDiscoverer(args.nexusfile)

for entry in disco.entries():
fail_list = []
Expand All @@ -102,11 +131,12 @@ def entries(self):
try:
print("\t%s (%d) is invalid with the following errors:" % (entry.feature_title(feat), feat))
print("\t\t" + message.replace('\n', '\n\t\t'))
except:
error_list.append(feat)
except :
e = sys.exc_info()[0]
error_list.append((feat, str(e)))

if len(error_list) > 0:
print("\nThe following features had unexpected errors (Are you running windows?):")
for feat in error_list:
print(" (%d)" % (feat))
for error in error_list:
print(" (%d) %s" % error)
print("\n")
Empty file.
62 changes: 62 additions & 0 deletions src/recipes/000000005A403F80/recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@


def find_class(nx_file, nx_class):
"""
Find a given NXclass
"""
hits = []
if not isinstance(nx_class, list):
nx_class = [nx_class]
def visitor(name, obj):
if "NX_class" in obj.attrs.keys():
if obj.attrs["NX_class"] in nx_class:
hits.append((name, obj))

nx_file.visititems(visitor)
return hits

REQUIRED_FIELDS = ['photoelectrons_energy', 'detector_sensitivity', 'energy_direction', 'energy_dispersion']

def check_detector(d):
hit = []
for f in REQUIRED_FIELDS:
if f in d.keys():
hit.append(f)
return hit

class recipe(object):
"""
Recipe to validate files with the NXrixs feature
WIP: just detector parameters at the moment
"""

def __init__(self, filedesc, entrypath):
self.file = filedesc
self.entry = entrypath
self.title = "NXrixs"

def process(self):
hits = dict()
for en, e in find_class(self.file, 'NXentry'):
for dn, d in find_class(e, 'NXdetector'):
h = check_detector(d)
if h:
hits[en + '/' + dn] = h

if len(hits) == 0:
raise Exception('No detectors found with any required fields')
nh = len(REQUIRED_FIELDS)
msg = ''
for d in hits:
h = hits[d]
if len(h) != nh:
msg += 'Detector %s missing fields: only %s\n' % d, h
if msg:
raise Exception(msg)

return hits


Empty file.
27 changes: 27 additions & 0 deletions src/recipes/3930676423686820/recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class recipe:
"""
Has title.
Proposed by: [email protected]
"""

def __init__(self, filedesc, entrypath):
"""
:param filedesc: h5py file object of the NeXus/HDF5 file
:param entrypath: path of the entry containing this feature
"""
self.file = filedesc
self.entry = entrypath
self.title = "Has title"

def process(self):
"""
Finds the title.
:return: the title
:raises: AssertionError if field not found
"""
try:
return self.file[self.entry]['title'][0]
except:
raise AssertionError("This file does not contain a title field")
Empty file.
27 changes: 27 additions & 0 deletions src/recipes/8801154206180708/recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class recipe:
"""
Has experiment_identifier.
Proposed by: [email protected]
"""

def __init__(self, filedesc, entrypath):
"""
:param filedesc: h5py file object of the NeXus/HDF5 file
:param entrypath: path of the entry containing this feature
"""
self.file = filedesc
self.entry = entrypath
self.title = "Has experiment_identifier"

def process(self):
"""
Finds the experiment identifier.
:return: the experiment identifier
:raises: AssertionError if field not found
"""
try:
return self.file[self.entry]['experiment_identifier'][0]
except:
raise AssertionError("This file does not contain an experiment_identfier field")

0 comments on commit 4770d6e

Please sign in to comment.