Last active
December 29, 2018 13:56
-
-
Save mcdlee/23802e19923a223d8efd to your computer and use it in GitHub Desktop.
實踐 staging 利用16進位排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
AJCC7 = { | |
'lung': { | |
'T': {'x', '0', 'is', '1', '1a', '1b', '2', '2a', '2b', '3', '4'}, | |
'N': {'x', '0', '1', '2', '3'}, | |
'M': {'x', '0', '1', '1a', '1b'} | |
}, | |
'colon': { | |
'T': {'x', '0', 'is', '1', '2', '3', '4', '4a', '4b'}, | |
'N': {'x', '0', '1', '1a', '1b', '1c', '2', '2a', '2b'}, | |
'M': {'x', '0', '1', '1a', '1b'} | |
} | |
} | |
def stringProcess(string): #去掉空白、()及其內容、去掉 T, N, M, p, y, r... | |
if string ==string: | |
ans = re.sub("\s", "", string) | |
ans = re.sub("\(.*\)|T|N|M|\?|^(p|y|r)*","" , ans) #TNM不管行首了 | |
ans = re.sub("X", "x", ans) #把大寫 X 轉成小寫 x | |
if string != string: #把 NaN 轉成空白 | |
ans = "" | |
if string == "無": | |
ans = "" | |
return ans | |
def lowerStage(s): #需往後補0 共3位,輸入已經分割好的點,也作為 definite digitalized | |
if s=="is": #in situ 定義為 stage 010 | |
ans=16 | |
elif s =="x": #x 定義為 stage 500 | |
ans=1280 | |
elif len(s) ==3: | |
ans=int(s, 16) | |
elif len(s) ==2: | |
ans=int(s, 16)*16 | |
elif len(s) ==1: | |
ans=int(s, 16)*256 | |
return ans | |
def higherStage(s): #需往後補f 共3位 | |
if s=="is": #in situ 定義為 stage 010 | |
ans=16 | |
elif len(s) ==3: | |
ans=int(s, 16) | |
elif len(s) ==2: | |
ans=int(s, 16)*16 +15 | |
elif len(s) ==1: | |
ans=int(s, 16)*256 + 255 | |
return ans | |
def rangeStage(s): #輸入口語回傳16位數範圍 | |
s = set(s.split("or")) | |
ans = set() | |
for item in s: | |
if "-" in item: | |
sp = item.split("-") | |
sub = set(range(lowerStage(sp[0]), higherStage(sp[1])+1)) | |
ans = ans.union(sub) | |
else: | |
sub = set(range(lowerStage(item), higherStage(item)+1)) | |
ans = ans.union(sub) | |
return ans | |
def matchingStage(est, ref): #est 是估計的範圍, ref 是病理診斷。輸入口語,回傳是否 match | |
if est == "": #未打staging | |
ans = "no staging" | |
elif est == "x": #無法打 staging | |
ans = "x" | |
elif ref == "" or ref=="x": #無病理診斷或病理無法判定 | |
ans = "no patho" | |
else: | |
est = rangeStage(est) | |
ref = rangeStage(ref) | |
if ref.issubset(est): | |
ans = "match" | |
else: | |
ans = "mismatch" | |
return ans | |
def revertStage(numb): #輸入10進位數字,回傳 staging 字串 | |
if numb==16: #in situ 定義為 stage 010 | |
ans = 'is' | |
elif numb==1280: #x 定義為 stage 500 | |
ans = 'x' | |
elif numb==0: | |
ans = '0' | |
else: | |
st = format(numb, "x") | |
if len(st)==1: | |
st = '00' +st | |
elif len(st)==2: | |
st = '0' + st | |
ans = re.sub("0+$", "", st) | |
return(ans) | |
def specificStage(stage, cancer, category): #輸入口語化,回傳特定癌症精確描述 | |
ans = set(map(lambda x:revertStage(x), rangeStage(stage))) & AJCC7[cancer][category] | |
return(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment