Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change printed errors to raised exceptions #72

Merged
merged 2 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 26 additions & 32 deletions annotator/Pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,28 @@ def undo(self):
self._activeCols = backup._activeCols
print("Undo: {}".format(message))
else:
print("At last available change.")
raise IndexError("At last available change.")

def head(self):
""" Print head of `self.view` """
if hasattr(self.view, 'head'):
print(self.view.head())
else:
print("No data view set.")
raise AttributeError("No data view set.")

def tail(self):
""" Print tail of `self.view` """
if hasattr(self.view, 'tail'):
print(self.view.tail())
else:
print("No data view set")
raise AttributeError("No data view set.")

def shape(self):
""" Print shape of `self.view` """
if hasattr(self.view, 'shape'):
print(self.view.shape)
else:
print("No data view set.")

raise AttributeError("No data view set.")

def drop(self, labels, axis):
""" Delete rows or columns from a file view on Synapse.*
Expand Down Expand Up @@ -133,27 +132,26 @@ def drop(self, labels, axis):
for l in self.schema.key]]
self.view = self.view.drop(labels, axis=axis)


def metaHead(self):
""" Print head of `self._meta` """
if hasattr(self._meta, 'head'):
print(self._meta.head())
else:
print("No metadata view set.")
raise AttributeError("No metadata view set.")

def metaTail(self):
""" Print tail of `self._meta` """
if hasattr(self._meta, 'tail'):
print(self._meta.tail())
else:
print("No metadata view set.")
raise AttributeError("No metadata view set.")

def metaShape(self):
""" Print shape of `self._meta` """
if hasattr(self._meta, 'shape'):
print(self._meta.shape)
else:
print("No metadata view set.")
raise AttributeError("No metadata view set.")

def columns(self, style="numbers"):
""" Pretty print `self.view.columns`.
Expand All @@ -166,7 +164,7 @@ def columns(self, style="numbers"):
if hasattr(self.view, 'columns'):
self._prettyPrintColumns(self.view.columns, style)
else:
print("No data view set.")
raise AttributeError("No data view set.")

def metaColumns(self, style="numbers"):
""" Pretty print `self._meta.columns`.
Expand All @@ -179,7 +177,7 @@ def metaColumns(self, style="numbers"):
if hasattr(self._meta, 'columns'):
self._prettyPrintColumns(self._meta.columns, style)
else:
print("No metadata view set.")
raise AttributeError("No metadata view set.")

def activeColumns(self, style="numbers"):
""" Pretty print `self._activeCols`.
Expand Down Expand Up @@ -207,7 +205,6 @@ def metaActiveColumns(self, style="numbers"):
else:
print("No active columns.")


def addView(self, scope):
""" Add further Folders/Projects to the scope of `self.view`.

Expand All @@ -229,19 +226,19 @@ def addView(self, scope):
raise RuntimeError("Must first create a file view if the "
"view is not yet set and not all items in "
"the scope are File Views or Schemas.")
self._entityViewSchema = utils.addToScope(self.syn,
self._entityViewSchema, scope)
self._entityViewSchema = utils.addToScope(
self.syn, self._entityViewSchema, scope)
# Assuming row version/id values stay the same for the before-update
# rows, we can carry over values from the old view.
oldIndices = self._index
oldColumns = self.view.columns
newView = utils.synread(self.syn, self._entityViewSchema.id, silent=True)
newView = utils.synread(
self.syn, self._entityViewSchema.id, silent=True)
for c in oldColumns:
newView.loc[oldIndices,c] = self.view[c].values
newView.loc[oldIndices, c] = self.view[c].values
self.view = newView
self._index = self.view.index


def addActiveCols(self, activeCols, path=False, isMeta=False, backup=True):
""" Add column names to `self._activeCols` or `self._metaActiveCols`.

Expand Down Expand Up @@ -295,8 +292,7 @@ def addDefaultValues(self, colVals, backup=True):
column values. Defaults to True.
"""
if self.view is None:
print("No data view set.")
return
raise AttributeError("No data view set.")
if backup:
self.backup("addDefaultValues")
for k in colVals:
Expand All @@ -305,17 +301,16 @@ def addDefaultValues(self, colVals, backup=True):
def addKeyCol(self):
""" Add a key column to `self.view`.

A key column is a column in `self.view` whose values can be matched in a
one-to-one manner with the column values of a column in `self._meta`.
A key column is a column in `self.view` whose values can be matched in
a one-to-one manner with the column values of a column in `self._meta`.
Usually this involves applying a regular expression to one of the
columns in `self.view`. After a regular expression which satisfies the
users requirements is found, the key column is automatically added to
`self.view` with the same name as the column matched upon
in `self._meta`.
"""
if self.view is None or self._meta is None:
print("No data view set.")
return
raise AttributeError("No data view set.")
self.backup("addKeyCol")
link = self._linkCols(1)
dataKey, metaKey = link.popitem()
Expand Down Expand Up @@ -401,8 +396,7 @@ def addLinks(self, links=None, append=True, backup=True):
Links stored in `self.links`.
"""
if self.view is None or self._meta is None:
print("No data view set.")
return
raise AttributeError("No data view set.")
if backup:
self.backup("addLinks")
if links is None:
Expand Down Expand Up @@ -564,11 +558,12 @@ def _validate(self):
malformed_values = schemaModule.validateView(self.view, self.schema)
if malformed_values:
for k in malformed_values:
warnings.append("{} contains the following values which are "
"not specified in the schema: {}".format(
k, ", ".join(map(str, malformed_values[k]))) +
"\n\tPossible values are {}".format(
", ".join(self.schema.loc[k].value.values)))
warnings.append(
"{} contains the following values which are "
"not specified in the schema: {}".format(
k, ", ".join(map(str, malformed_values[k]))) +
"\n\tPossible values are {}".format(
", ".join(self.schema.loc[k].value.values)))
return warnings

def removeActiveCols(self, activeCols):
Expand Down Expand Up @@ -789,8 +784,7 @@ def inferValues(self, col, referenceCols):
Column(s) to match on.
"""
if self.view is None:
print("No data view set.")
return
raise AttributeError("No data view set.")
self.backup("inferValues")
self.view = utils.inferValues(self.view, col, referenceCols)

Expand Down
2 changes: 1 addition & 1 deletion annotator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def inferValues(df, col, referenceCols):
df.loc[df[referenceCols] == k, col] = v[0]
else:
print("Unable to infer value when {} = {}".format(
referenceCols, k))
referenceCols, k))
return df


Expand Down