-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifyCascade.py
116 lines (95 loc) · 4.2 KB
/
classifyCascade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: utf-8 -*-
"""
Create class for passing a new case's features to the cascade classifier and obtain a Prediction
Created on Tue May 13 12:33:11 2014
@ author (C) Cristina Gallego, University of Toronto, 2014
"""
import os, os.path
import sys
import string
from sys import argv, stderr, exit
import vtk
from vtk.util.numpy_support import vtk_to_numpy
import numpy as np
import pandas as pd
import pylab
# convertion packages
import pandas.rpy.common as com
from rpy2.robjects.numpy2ri import numpy2ri
from rpy2.robjects.packages import importr
import rpy2.robjects as R
from rpy2.robjects import globalenv
#!/usr/bin/env python
class classifyCascade(object):
"""
USAGE:
=============
classifier = classifyCascade()
"""
def __init__(self):
""" initialize """
# use cell picker for interacting with the image orthogonal views.
self.rpycasesFrame = []
self.RFcascade_probs = []
self.veredict = []
def __call__(self):
""" Turn Class into a callable object """
classifyCascade()
def parse_classes(self, cascadeprobs):
""" Take prob outputs of cascade classifiers and contrast it with labels to produce output accuracy"""
caselabel = cascadeprobs['labels'].iloc[0]
stage1label = caselabel[:-1] # mass vs. nonmass
stage2label = caselabel[-1] # B vs. M
if stage2label == 'B': stage2label = 'NC'
if stage2label == 'M': stage2label = 'C'
hit = []
miss = []
# proccess 2 possible correct classifications (e.g hit)
if (cascadeprobs['pred1'].iloc[0] == stage1label):
hit.append('stage1')
else:
miss.append('stage1')
# proccess 2 possible correct classifications (e.g hit)
if (cascadeprobs['pred2'].iloc[0] == stage2label):
hit.append('stage2')
else:
miss.append('stage2')
#procees correct results
if( hit == ['stage1', 'stage2']):
self.veredict = True
self.caseoutcome = "P_stage1_P_stage2"
if( hit == ['stage2'] and miss == ['stage1'] ):
self.veredict = True
self.caseoutcome = "N_stage1_P_stage2"
#procees incorrect results
if( miss == ['stage1', 'stage2']):
self.veredict = False
self.caseoutcome = "N_stage1_N_stage2"
if( hit == ['stage1'] and miss == ['stage2'] ):
self.veredict = False
self.caseoutcome = "P_stage1_N_stage2"
return(self.veredict, self.caseoutcome)
def case_classifyCascade(self):
""" A individual case classification function"""
########### To R for classification
os.chdir("Z:\Cristina\MassNonmass\codeProject\codeBase\extractFeatures\casesDatabase")
cF = pd.read_csv('casesFrames_toclasify.csv')
cF['finding.mri_mass_yn'] = cF['finding.mri_mass_yn'].astype('int32')
cF['finding.mri_nonmass_yn'] = cF['finding.mri_nonmass_yn'].astype('int32')
cF['finding.mri_foci_yn'] = cF['finding.mri_foci_yn'].astype('int32')
cF['finding.mri_foci_yn'] = cF['finding.mri_foci_yn'].astype('int32')
cF['is_insitu'] = cF['is_insitu'].astype('int32')
cF['is_invasive'] = cF['is_invasive'].astype('int32')
self.rpycasesFrame = com.convert_to_r_dataframe(cF)
base = importr('base')
base.source('Z:/Cristina/MassNonmass/codeProject/codeBase/finalClassifier/finalClassifier_classifyCascade.R')
RFcascade = globalenv['finalClassifier_classifyCascade'](self.rpycasesFrame)
self.RFcascade_probs = com.convert_robj(RFcascade)
print "\n========================"
print self.RFcascade_probs
# proccess possible outcome
[veredict, caseoutcome] = self.parse_classes(self.RFcascade_probs)
print "\n========================\nCascade classification result:"
print veredict
print caseoutcome
return