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

Unittests for Pipeline #71

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
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
24 changes: 17 additions & 7 deletions annotator/Pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def __init__(self, syn, view=None, meta=None, activeCols=[],
Optional. Whether to sort the columns lexicographically in
`view` and/or `meta`. Defaults to True.
"""
self.syn = syn
if isinstance(syn, sc.Synapse):
self.syn = syn
else:
raise TypeError("syn must be a synapseclient.Synapse object")
self.view = view if view is None else self._parseView(view, sortCols)
self._entityViewSchema = (self.syn.get(view)
if isinstance(view, str) else None)
Expand All @@ -62,11 +65,13 @@ def __init__(self, syn, view=None, meta=None, activeCols=[],
self.links = links if isinstance(links, dict) else None
self._backup = []

def backup(self, message):
def backup(self, message=None):
""" Backup the state of `self` and store in `self._backup` """
self._backup.append((Pipeline(
self.syn, self.view, self._meta, self._activeCols,
self._metaActiveCols, self.links, self._sortCols), message))
syn=self.syn, view=self.view, meta=self._meta,
activeCols=self._activeCols, metaActiveCols=self._metaActiveCols,
links=self.links, sortCols=self._sortCols, schema=self.schema),
message))
if len(self._backup) > self.BACKUP_LENGTH:
self._backup = self._backup[1:]

Expand Down Expand Up @@ -470,7 +475,7 @@ def _parseView(self, view, sortCols, isMeta=False):
"""
if isinstance(view, str):
return utils.synread(self.syn, view, sortCols=sortCols)
elif isinstance(view, list) and meta:
elif isinstance(view, list) and isMeta:
return utils.combineSynapseTabulars(self.syn, view, axis=1)
elif isinstance(view, pd.DataFrame):
if sortCols:
Expand Down Expand Up @@ -524,7 +529,11 @@ def _getUserConfirmation(self, message="Proceed anyways? (y) or (n): "):
Otherwise asks user to input confirmation again.
"""
proceed = ''
counter = 0
while not proceed:
counter += 1
if counter > 3:
return False # after three attempts return False
proceed = input(message)
if len(proceed) and not proceed[0] in ['Y', 'y', 'N', 'n']:
proceed = ''
Expand Down Expand Up @@ -562,15 +571,16 @@ def _validate(self):
", ".join(self.schema.loc[k].value.values)))
return warnings

def removeActiveCols(self, activeCols):
def removeActiveCols(self, activeCols, backup=True):
""" Remove a column name from `self._activeCols`

Parameters
----------
activeCols : str or list-like
Column name(s) to remove.
"""
self.backup("removeActiveCols")
if backup:
self.backup("removeActiveCols")
if isinstance(activeCols, str):
self._activeCols.remove(activeCols)
else: # is list-like
Expand Down
44 changes: 26 additions & 18 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas
import synapseclient
import uuid
import annotator


SAMPLE_FILE = "https://raw.githubusercontent.com/Sage-Bionetworks/annotator/master/tests/sampleFile.csv"
Expand Down Expand Up @@ -41,17 +42,16 @@ def project(syn):
syn.delete(project)


def file_(syn, parent, path, annotations=None, **kwargs):
def file_(syn, parent, path, **kwargs):
if 'name' not in kwargs:
name = str(uuid.uuid4())
else:
name = kwargs.pop('name')
file_ = synapseclient.File(path=path, name=name,
parent=parent, **kwargs)
if annotations:
for a in annotations.items():
key, value = a
file_[key] = value
file_ = synapseclient.File(
path=path,
name=name,
parent=parent,
**kwargs)
file_ = syn.store(file_)
return file_

Expand Down Expand Up @@ -106,21 +106,29 @@ def genericTable(syn, project, sampleFile):
def entities(syn, sampleFile, project):
# store a folder for our entities
sample_folder = folder(syn, project)
sample_folder_two = folder(syn, project)
# store sample files
_file = file_(syn, sample_folder, SAMPLE_FILE, name="file1.csv")
_file2 = file_(syn, sample_folder, SAMPLE_FILE, name="file2.csv")
_file3 = file_(syn, sample_folder, SAMPLE_FILE, name="file3.csv")
_file = file_(syn, sample_folder, SAMPLE_FILE, name="file1.csv",
synapseStore=False)
_file2 = file_(syn, sample_folder, SAMPLE_FILE, name="file2.csv",
synapseStore=False)
_file3 = file_(syn, sample_folder_two, SAMPLE_FILE, name="file3.csv",
synapseStore=False)
# store a sample metadata file
meta = synapseclient.File(path=SAMPLE_META, name='meta',
parent=sample_folder)
meta = syn.store(meta)
meta = file_(syn, project, SAMPLE_META, name='meta', synapseStore=False)
# store a sample table (same values as sample file)
schema = table(syn, project, sampleFile)
# store a sample file view
entity_view_ = entity_view(syn, project, scopes=sample_folder)
ents = {'files': [_file, _file2, _file3],
'folder': sample_folder,
entity_view_ = entity_view(syn, project,
scopes=[sample_folder, sample_folder_two])
entities = {'files': [_file, _file2, _file3],
'folders': [sample_folder, sample_folder_two],
'meta': meta,
'table_schema': schema,
'entity_view': entity_view_}
return ents
'entity_view': entity_view_,
'project': project}
return entities

@pytest.fixture(scope='session')
def genericPipeline(syn):
return annotator.Pipeline(syn)
Loading