Skip to content

Commit

Permalink
adding generalized float data extraction method and updating tests an…
Browse files Browse the repository at this point in the history
…d code to use it
  • Loading branch information
dsavransky committed Nov 12, 2018
1 parent 4efc427 commit 4df6ca7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
40 changes: 32 additions & 8 deletions MeanStars/MeanStars.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,21 @@ def __init__(self, datapath=None):
colorgraph[r[0]].append(r[1])
colorgraph[r[1]].append(r[0])

#attributes
self.colors = colors
self.bands = bands
self.colorgraph = colorgraph
self.colorstr = np.array(["-".join(c) for c in self.colors])
self.noncolors = np.array(noncolors)
self.Teff = self.getFloatData('Teff')

#storage dicts
self.Teffinterps = {}
self.SpTinterps = {}

#useful regexs
self.specregex = re.compile('([OBAFGKMLTY])(\d*\.\d+|\d+).*')

self.nondec = re.compile('[^\d.-]+')


def searchgraph(self, start, end, path=[]):
Expand Down Expand Up @@ -132,7 +138,27 @@ def translatepath(self,path):
raise LookupError
res[j] = np.array([tmp[0],-1])
return res


def getFloatData(self,key):
""""Grab a numeric data column from the table and strip any non-numeric
characters as needed.
Args:
key (str):
Name of column to grab
Returns:
vals (float ndarray):
Numerical values from columns
"""
assert key in self.data.keys(), "%s not found in data table."%key

if np.issubdtype(self.data[key].dtype,np.number):
return self.data[key].data.data.astype(float)
else:
tmp = self.data[key].data.data
return np.array([self.nondec.sub('',v) if v != 'nan' else v for v in tmp]).astype(float)

def interpTeff(self, start, end):
"""Create an interpolant as a function of effective temprature for the
Expand All @@ -152,9 +178,8 @@ def interpTeff(self, start, end):
return

vals = self.getDataForColorInterp(start,end)
Teff = self.data['Teff'].data.data.astype(float)

self.Teffinterps[name] = scipy.interpolate.interp1d(Teff[~np.isnan(vals)],\
self.Teffinterps[name] = scipy.interpolate.interp1d(self.Teff[~np.isnan(vals)],\
vals[~np.isnan(vals)],bounds_error=False)


Expand Down Expand Up @@ -184,7 +209,7 @@ def getDataForColorInterp(self,start,end):

vals = np.zeros(len(self.data))
for r in res:
vals += r[1]*self.data[self.colorstr[r[0].astype(int)]].data.data.astype(float)
vals += r[1]*self.getFloatData(self.colorstr[r[0].astype(int)])

return vals

Expand Down Expand Up @@ -277,7 +302,7 @@ def getDataForOtherInterp(self,key):

assert key in self.noncolors, "%s is not a known property"%end

vals = self.data[key].data.data.astype(float)
vals = self.getFloatData(key)

return vals

Expand All @@ -296,9 +321,8 @@ def interpOtherTeff(self, key):
return

vals = self.getDataForOtherInterp(key)
Teff = self.data['Teff'].data.data.astype(float)

self.Teffinterps[key] = scipy.interpolate.interp1d(Teff[~np.isnan(vals)],\
self.Teffinterps[key] = scipy.interpolate.interp1d(self.Teff[~np.isnan(vals)],\
vals[~np.isnan(vals)],bounds_error=False)

def TeffOther(self,key,Teff):
Expand Down
18 changes: 7 additions & 11 deletions tests/test_MeanStars.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ def test_searchgraph(self):

def test_TeffColor(self):

Teffs = self.ms.data['Teff'].data.data.astype(float)

#grab color at random
cind = np.random.randint(0,high=len(self.ms.colors))

vals = self.ms.data[self.ms.colorstr[cind]].data.data.astype(float)
vals = self.ms.getFloatData(self.ms.colorstr[cind])

goodinds = np.isfinite(vals)

self.assertTrue(np.all(self.ms.TeffColor(self.ms.colors[cind][0],self.ms.colors[cind][1],Teffs[goodinds]) == vals[goodinds]),"Cannot reproduce colors from interpolant for %s"%self.ms.colorstr[cind])
self.assertTrue(np.all(self.ms.TeffColor(self.ms.colors[cind][0],self.ms.colors[cind][1],self.ms.Teff[goodinds]) == vals[goodinds]),"Cannot reproduce colors from interpolant for %s"%self.ms.colorstr[cind])

def test_SpTColor(self):

Expand All @@ -73,7 +71,7 @@ def test_SpTColor(self):
#grab color at random
cind = np.random.randint(0,high=len(self.ms.colors))

vals = self.ms.data[self.ms.colorstr[cind]].data.data.astype(float)
vals = self.ms.getFloatData(self.ms.colorstr[cind])

goodinds = np.isfinite(vals)

Expand All @@ -85,16 +83,14 @@ def test_SpTColor(self):

def test_TeffOther(self):

Teffs = self.ms.data['Teff'].data.data.astype(float)

#grab property at random
key = self.ms.noncolors[np.random.randint(0,high=len(self.ms.noncolors))]

vals = self.ms.data[key].data.data.astype(float)
vals = self.ms.getFloatData(key)

goodinds = np.isfinite(vals)

self.assertTrue(np.all(self.ms.TeffOther(key,Teffs[goodinds]) == vals[goodinds]),"Cannot reproduce values from interpolant for %s"%key)
self.assertTrue(np.all(self.ms.TeffOther(key,self.ms.Teff[goodinds]) == vals[goodinds]),"Cannot reproduce values from interpolant for %s"%key)

def test_SpTOther(self):

Expand All @@ -103,7 +99,7 @@ def test_SpTOther(self):
#grab property at random
key = self.ms.noncolors[np.random.randint(0,high=len(self.ms.noncolors))]

vals = self.ms.data[key].data.data.astype(float)
vals = self.ms.getFloatData(key)

goodinds = np.isfinite(vals)

Expand Down

0 comments on commit 4df6ca7

Please sign in to comment.