{"cells":[{"metadata":{},"cell_type":"markdown","source":"# RANZCR - Simple X-Ray viewer\n\nYou can view any train image searching it by UID or view any random image based on criteria."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":false,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport json\n\ndf_train = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/train.csv')\ndf_train.columns = ['UID','ETTA','ETTB','ETTN','NGTA','NGTB','NGTI','NGTN','CVCA','CVCB','CVCN','SGCP','PID']\ndf_annotations = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/train_annotations.csv')\ndf_annotations.columns = ['UID','label','data']\n\ndef plot(r, annotations):\n    img = cv2.imread('../input/ranzcr-clip-catheter-line-classification/train/' + r.UID + '.jpg')\n    fig = px.imshow(img, height=800)\n    if annotations.isnull().values.any() == False:\n        for i, ann in annotations.iterrows():\n            df = pd.DataFrame(json.loads(ann.data),columns=['x', 'y'])\n            fig.add_trace(go.Scatter(x=df.x, y=df.y, name=ann.label))\n    \n    title = 'Labels: '\n    for name, value in r.iteritems():\n        if len(name) == 4 and value == 1:\n            title += ' ' + name\n\n    fig.update_layout(title_text=title, xaxis_title=r.UID,) \n    fig.show()\n\ndef xray(query):\n    df = df_train.query(query).sample(1)\n    an = df.join(df_annotations.set_index('UID'),how='left',on='UID')\n    plot(df.iloc[0], an[['label','data']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# examples:\n# xray('CVCA == 1') # -- view a random X-Ray with CVC abnormal\n# xray(\"UID == '1.2.826.0.1.3680043.8.498.59757398491099579448057921213132792160'\") # -- view a specific X-Ray by UID\n# xray('SGCP == 1') # -- view a random X-Ray with Swan Ganz catheter\n# xray('SGCP == 1 and CVCA == 1') # -- view a random X-Ray with Swan Ganz catheter and CVC abnormal\n\n\nxray(\"UID == '1.2.826.0.1.3680043.8.498.59757398491099579448057921213132792160'\")","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}