CORE modules¶
Contents
Challenge¶
Common utility to all challenges
-
class
Challenge
(challenge_name, verbose=False, download=True, **kargs)[source]¶ Common class to all challenges
If you have not setup a .synapseConfig in your HOME, you must provide a synapse client
from dreamtools import * s = Challenge('D2C1') client = Login(username=username, password=pwd).client s.client = client
constructor
Parameters: challenge_name (str) – Must be formatted as DXCY where X and Y are numbers. Intermediate challenges from e.g. D9.5 should be encoded as D9dot5CY -
debug
= None¶ alias of the challenge as DXCY form with X, Y being 2 numbers
-
directory
¶ Gets directory where data will be stored.
-
get_pathname
(filename)[source]¶ Return pathname of a file to be found on ./config/dreamtools if available
-
import_scoring_class
()[source]¶ Dynamic import of a challenge class
c = Challenge('D7C1') inst_class = c.import_scoring_class()
-
mainpath
= None¶ directory where is stored the configuration file and data files.
-
Rocs¶
Provides tools related to Receiver Operating Characteristic (ROC).
Those codes were directly translated from Perl or matlab codes. We should be using scikit-learn in the future.
-
class
ROC
(scores=None, classes=None)[source]¶ A class to compute ROC, AUC and AUPRs for a binary problem
>>> r = ROC() # could provide scores and labels as arguments >>> r.scores = [0.9,0.8,0.7,.6,.6,.3] >>> r.classes = [1,0,1,0,1,1] >>> r.compute_auc() 0.4375
Constructor
Parameters: -
classes
¶ Read/Write the classes
-
get_roc
()[source]¶ See
get_statistics()
-
get_statistics
()[source]¶ Compute the ROC curve X/Y vectors and some other metrics
Returns: a dictionary with different metrics such as FPR (false positive rate), PTR (true positive rate).
-
plot_roc
(roc=None)[source]¶ Plot ROC curves
from dreamtools.core.rocs import ROC r = ROC() r.scores = [.9,.5,.6,.7,.1,.2,.6,.4,.7,.9, .2] r.classes = [1,0,1,0,0,1,1,0,0,1,1] r.plot_roc()
-
scores
¶ Read/Write the scores
-
Synapse utilities¶
A module dedicated to synapse
The class SynapseClient
is a specialised class built upon
synapseclient package, which source code is on GitHub:
git clone git://github.com/Sage-Bionetworks/synapsePythonClient.git
cd synapsePythonClient
python setup.py install
This class may be removed but for now it is used in D8C1 challenge.
>>> from dreamtools.core import sageutils
>>> s = sageutils.SynapseClient()
-
class
SynapseClient
(username=None, password=None)[source]¶ This class inherits all methods from synapseClient.
Be aware that most of the functionalities are now available in synapseclient itself. So, most of the methods that were written are hidden (double underscore) and may be removed in the future.
The only remaining feature is the automatic login, and simple version of the downloadSubmission method. There is also a
json()
method used throughout the dream8hpn code.Constructor
Parameters: - username – your synapse usename
- password – your synapse password
You can create create a file called .synapseConfig (note the dot) in your home directory and add something like:
[authentication] username: yourlogin password: yourpassword
-
downloadSubmissionAndFilename
(sub, downloadFile=True, **kargs)[source]¶ Return filename of a submission downloaded from synapse.
Parameters: - sub – A submission (as a dictionary).
- version – The specific version to get. Defaults to the most recent version.
- downloadFile – Whether associated files(s) should be downloaded. Defaults to True. If set to False, downloadLocation and ifcollision are ignored
- downloadLocation – Directory where to download the Synapse File Entity. Defaults to the local cache.
- ifcollision – Determines how to handle file collisions. May be “overwrite.local”, “keep.local”, or “keep.both”. Defaults to “keep.both”.
Warning
ifcollision does not seem to work (0.5.1)
-
class
Login
(client=None, username=None, password=None)[source]¶ A simple class to login to synapse
Parameters: client – Connection to synapse takes a couple of seconds. This may be too much if in a debugging mode or accessing to synapse from different places. The login can be instantiate with an existing instance of SynapseClient, if which case, the instance creation is fast. Otherwise, the default behaviour is to create a new connection. >>> from dreamtools.core.sageutils import Login >>> l = Login() This is a SynapseClient built on top of Synapse class. Trying to login automatically. Welcome, ***************** You're logged in Synapse Welcome, XXX In [10]: l = sageutils.Login(l)
Ziptools¶
-
class
ZIP
[source]¶ Simple utility to load a ZIP file
Note
could be moved to easydev package
-
loadZIPFile
(filename)[source]¶ Loads a ZIP file
This method uses the zipfile module and stores the data into
zip_data
. The filenames contained within this archive can be found inzip_filenames
. To read the data contained in the first filename, type:self.zip_data.open(self.filenames[0].read()
Parameters: filename (str) – the ZIP filename to load
-
Downloader¶
Utility to download a synapse project in the dreamtools directory
-
class
Downloader
(challenge, client=None, username=None, password=None)[source]¶ Factory to download gold standard files
Download a synpase file once for all in the dreamtools directory.
constructor
Parameters: challenge (str) – alias of a challenge (e.g., D5C1) To automatically connect to synapse, create a file called .synapseConfig with this content:
[authentication] username: email password: password
Layout¶
Layout to create a new challenge from scratch
Concordance Index¶
Concordance index computation (exact version)
Based on R code provided by Ben Sauerwine and Erhan Bilal double checked with concordance.index from survcomp R package.
-
cindex
(prediction, survtime, survevent)[source]¶ Function to compute the concordance index for a risk prediction, i.e., the probability that, for a pair of randomly chosen comparable samples, the sample with the higher risk prediction will experience an event before the other sample or belongs to a higher binary class.
Parameters: - prediction – a vector of risk predictions.
- survtime – a vector of event times.
- survevent – a vector of event occurence indicators (True and False).
>>> from dreamtools.core.cindex import cindex >>> print(cindex([0, 1, 3,4], [0, 4, 3, 1], [True]*4)) 0.5 >>> print(cindex([0, 1, 3,4], [0, 1, 3, 4], [True]*4)) 0.0