Skip to content

Commit

Permalink
updated dakota parser closes #573
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Letter committed Feb 25, 2016
1 parent f9e964f commit 8fa9b43
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions web-server/plugins/slycat-dakota-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
import StringIO

def parse_file(file):
"""
parses out a .dat file into numpy array by column (data), the dimension meta data(dimensions),
and sets attributes (attributes)
:param file: dakota file to be parsed
:returns: attributes, dimensions, data
"""
import cherrypy
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False

cherrypy.log.error("parsing:::::::")
rows = [row.split() for row in StringIO.StringIO(file)]
if len(rows) < 2:
slycat.email.send_error("slycat-dakota-parser.py parse_file", "File must contain at least two rows.")
Expand All @@ -12,11 +27,25 @@ def parse_file(file):
attributes = []
dimensions = [{"name":"row", "type":"int64", "begin":0, "end":len(rows[1:])}]
data = []
# go through the csv by column
for column in zip(*rows):
try:
data.append(numpy.array(column[1:]).astype("float64"))
attributes.append({"name":column[0], "type":"float64"})
except:
column_has_floats = False

# start from 1 to avoid the column name
for value in column[1:]:
if isfloat(value):
column_has_floats = True
try:# note NaN's are floats
output_list = map(lambda x: 'NaN' if x=='' else x, column[1:])
data.append(numpy.array(output_list).astype("float64"))
attributes.append({"name":column[0], "type":"float64"})

# could not convert something to a float defaulting to string
except Exception as e:
column_has_floats = False
cherrypy.log.error("found floats but failed to convert, switching to string types Trace: %s" % e)
break
if not column_has_floats:
data.append(numpy.array(column[1:]))
attributes.append({"name":column[0], "type":"string"})

Expand Down

0 comments on commit 8fa9b43

Please sign in to comment.