{
  "id": 189847,
  "title": "How to get right order?",
  "url": "/competitions/rsna-str-pulmonary-embolism-detection/discussion/189847",
  "author_name": "Youhan Lee",
  "post_date": "2020-10-09T02:23:11.594000",
  "votes": 5,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I'm making images using blow codes</p>\n<p>train = pd.read_csv(os.path.join(DATAPATH, 'train.csv'))</p>\n<p>train_unique_study_instance_UID = train['StudyInstanceUID'].unique()<br>\ngroup_df = train.groupby('StudyInstanceUID')</p>\n<p>for studyuid in tqdm(train_unique_study_instance_UID):<br>\n    tmp_df = train[train['StudyInstanceUID'] == studyuid]<br>\n    series = tmp_df['SeriesInstanceUID'].values[0]<br>\n    data_path = os.path.join(DATAPATH, 'train_images', studyuid, series)<br>\n    os.makedirs(data_path, exist_ok=True)<br>\n    for tmp_dcm_name in sorted(tmp_df['SOPInstanceUID']):<br>\n        tmp_dcm_path = os.path.join(DATAPATH, 'train', studyuid, series, f'{tmp_dcm_name}.dcm')<br>\n        dicom = pydicom.dcmread(tmp_dcm_path)<br>\n        image = dicom.pixel_array<br>\n        image = rescale_image(image, rescaledict['RescaleSlope'][tmp_dcm_name], rescaledict['RescaleIntercept'][tmp_dcm_name])<br>\n        image = apply_window_policy(image)<br>\n        image -= image.min((0,1))<br>\n        image = (255*image).astype(np.uint8)<br>\n        cv2.imwrite(data_path + f'/{tmp_dcm_name}.jpg', image)</p>\n<p>But, after visualizing using gif, I saw the ordering is weird. Do you know how to get the correct order?<br>\nYou can see gif using below link.</p>\n<p><a href=\"https://drive.google.com/file/d/1HI7eQj2aDgSIsBIN3jT1T98ZYW1_tmyC/view?usp=sharing\" target=\"_blank\">https://drive.google.com/file/d/1HI7eQj2aDgSIsBIN3jT1T98ZYW1_tmyC/view?usp=sharing</a></p>",
  "messages": [
    {
      "id": 1043462,
      "postDate": "2020-10-09T02:23:11.593Z",
      "content": "<p>I'm making images using blow codes</p>\n<p>train = pd.read_csv(os.path.join(DATAPATH, 'train.csv'))</p>\n<p>train_unique_study_instance_UID = train['StudyInstanceUID'].unique()<br>\ngroup_df = train.groupby('StudyInstanceUID')</p>\n<p>for studyuid in tqdm(train_unique_study_instance_UID):<br>\n    tmp_df = train[train['StudyInstanceUID'] == studyuid]<br>\n    series = tmp_df['SeriesInstanceUID'].values[0]<br>\n    data_path = os.path.join(DATAPATH, 'train_images', studyuid, series)<br>\n    os.makedirs(data_path, exist_ok=True)<br>\n    for tmp_dcm_name in sorted(tmp_df['SOPInstanceUID']):<br>\n        tmp_dcm_path = os.path.join(DATAPATH, 'train', studyuid, series, f'{tmp_dcm_name}.dcm')<br>\n        dicom = pydicom.dcmread(tmp_dcm_path)<br>\n        image = dicom.pixel_array<br>\n        image = rescale_image(image, rescaledict['RescaleSlope'][tmp_dcm_name], rescaledict['RescaleIntercept'][tmp_dcm_name])<br>\n        image = apply_window_policy(image)<br>\n        image -= image.min((0,1))<br>\n        image = (255*image).astype(np.uint8)<br>\n        cv2.imwrite(data_path + f'/{tmp_dcm_name}.jpg', image)</p>\n<p>But, after visualizing using gif, I saw the ordering is weird. Do you know how to get the correct order?<br>\nYou can see gif using below link.</p>\n<p><a href=\"https://drive.google.com/file/d/1HI7eQj2aDgSIsBIN3jT1T98ZYW1_tmyC/view?usp=sharing\" target=\"_blank\">https://drive.google.com/file/d/1HI7eQj2aDgSIsBIN3jT1T98ZYW1_tmyC/view?usp=sharing</a></p>",
      "rawMarkdown": "I'm making images using blow codes\n\ntrain = pd.read_csv(os.path.join(DATAPATH, 'train.csv'))\n\ntrain_unique_study_instance_UID = train['StudyInstanceUID'].unique()\ngroup_df = train.groupby('StudyInstanceUID')\n\nfor studyuid in tqdm(train_unique_study_instance_UID):\n    tmp_df = train[train['StudyInstanceUID'] == studyuid]\n    series = tmp_df['SeriesInstanceUID'].values[0]\n    data_path = os.path.join(DATAPATH, 'train_images', studyuid, series)\n    os.makedirs(data_path, exist_ok=True)\n    for tmp_dcm_name in sorted(tmp_df['SOPInstanceUID']):\n        tmp_dcm_path = os.path.join(DATAPATH, 'train', studyuid, series, f'{tmp_dcm_name}.dcm')\n        dicom = pydicom.dcmread(tmp_dcm_path)\n        image = dicom.pixel_array\n        image = rescale_image(image, rescaledict['RescaleSlope'][tmp_dcm_name], rescaledict['RescaleIntercept'][tmp_dcm_name])\n        image = apply_window_policy(image)\n        image -= image.min((0,1))\n        image = (255*image).astype(np.uint8)\n        cv2.imwrite(data_path + f'/{tmp_dcm_name}.jpg', image)\n\n\nBut, after visualizing using gif, I saw the ordering is weird. Do you know how to get the correct order?\nYou can see gif using below link.\n\nhttps://drive.google.com/file/d/1HI7eQj2aDgSIsBIN3jT1T98ZYW1_tmyC/view?usp=sharing",
      "votes": 5
    },
    {
      "id": 1043779,
      "postDate": "2020-10-09T08:37:57Z",
      "content": "<p>Try to sort them by <code>ImagePositionPatient[2]</code> which is basically the position of the slice. If that field doesn't exist (it was missing in some of the files in OSIC Pulmonary Fibrosis), sort them by <code>InstanceNumber</code>. Implement this in a try/except block.</p>",
      "rawMarkdown": "Try to sort them by `ImagePositionPatient[2]` which is basically the position of the slice. If that field doesn't exist (it was missing in some of the files in OSIC Pulmonary Fibrosis), sort them by `InstanceNumber`. Implement this in a try/except block.",
      "votes": 6,
      "replies": [
        {
          "id": 1045727,
          "postDate": "2020-10-11T00:56:16.670Z",
          "content": "<p>Thanks!! very helpful!</p>",
          "rawMarkdown": "Thanks!! very helpful!"
        }
      ]
    },
    {
      "id": 1043612,
      "postDate": "2020-10-09T05:24:44.213Z",
      "content": "<p>Hello <a href=\"https://www.kaggle.com/youhanlee\" target=\"_blank\">@youhanlee</a>, </p>\n<p>I created a notebook to show how to sort DICOM images properly. I hope it helps you get the correct ordering!</p>\n<p><a href=\"https://www.kaggle.com/aksg87/dicom-sequence-selection-and-sort\" target=\"_blank\">https://www.kaggle.com/aksg87/dicom-sequence-selection-and-sort</a></p>",
      "rawMarkdown": "Hello @youhanlee, \n\nI created a notebook to show how to sort DICOM images properly. I hope it helps you get the correct ordering!\n\nhttps://www.kaggle.com/aksg87/dicom-sequence-selection-and-sort",
      "votes": 3,
      "replies": [
        {
          "id": 1045728,
          "postDate": "2020-10-11T00:56:40.917Z",
          "content": "<p>Thanks! I solved the problem based on your kernel!</p>",
          "rawMarkdown": "Thanks! I solved the problem based on your kernel!",
          "votes": 1
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 1043779,
      "author_name": "Gunes Evitan",
      "author_url": "",
      "post_date": "2020-10-09T08:37:57",
      "content": "<p>Try to sort them by <code>ImagePositionPatient[2]</code> which is basically the position of the slice. If that field doesn't exist (it was missing in some of the files in OSIC Pulmonary Fibrosis), sort them by <code>InstanceNumber</code>. Implement this in a try/except block.</p>",
      "votes": 6,
      "replies": [
        {
          "id": 1045727,
          "author_name": "Youhan Lee",
          "author_url": "",
          "post_date": "2020-10-11T00:56:16.670000",
          "content": "<p>Thanks!! very helpful!</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1043612,
      "author_name": "aksg87",
      "author_url": "",
      "post_date": "2020-10-09T05:24:44.213000",
      "content": "<p>Hello <a href=\"https://www.kaggle.com/youhanlee\" target=\"_blank\">@youhanlee</a>, </p>\n<p>I created a notebook to show how to sort DICOM images properly. I hope it helps you get the correct ordering!</p>\n<p><a href=\"https://www.kaggle.com/aksg87/dicom-sequence-selection-and-sort\" target=\"_blank\">https://www.kaggle.com/aksg87/dicom-sequence-selection-and-sort</a></p>",
      "votes": 3,
      "replies": [
        {
          "id": 1045728,
          "author_name": "Youhan Lee",
          "author_url": "",
          "post_date": "2020-10-11T00:56:40.917000",
          "content": "<p>Thanks! I solved the problem based on your kernel!</p>",
          "votes": 1,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1043462": "I'm making images using blow codes\n\ntrain = pd.read_csv(os.path.join(DATAPATH, 'train.csv'))\n\ntrain_unique_study_instance_UID = train['StudyInstanceUID'].unique()\ngroup_df = train.groupby('StudyInstanceUID')\n\nfor studyuid in tqdm(train_unique_study_instance_UID):\n    tmp_df = train[train['StudyInstanceUID'] == studyuid]\n    series = tmp_df['SeriesInstanceUID'].values[0]\n    data_path = os.path.join(DATAPATH, 'train_images', studyuid, series)\n    os.makedirs(data_path, exist_ok=True)\n    for tmp_dcm_name in sorted(tmp_df['SOPInstanceUID']):\n        tmp_dcm_path = os.path.join(DATAPATH, 'train', studyuid, series, f'{tmp_dcm_name}.dcm')\n        dicom = pydicom.dcmread(tmp_dcm_path)\n        image = dicom.pixel_array\n        image = rescale_image(image, rescaledict['RescaleSlope'][tmp_dcm_name], rescaledict['RescaleIntercept'][tmp_dcm_name])\n        image = apply_window_policy(image)\n        image -= image.min((0,1))\n        image = (255*image).astype(np.uint8)\n        cv2.imwrite(data_path + f'/{tmp_dcm_name}.jpg', image)\n\n\nBut, after visualizing using gif, I saw the ordering is weird. Do you know how to get the correct order?\nYou can see gif using below link.\n\nhttps://drive.google.com/file/d/1HI7eQj2aDgSIsBIN3jT1T98ZYW1_tmyC/view?usp=sharing",
    "1043779": "Try to sort them by `ImagePositionPatient[2]` which is basically the position of the slice. If that field doesn't exist (it was missing in some of the files in OSIC Pulmonary Fibrosis), sort them by `InstanceNumber`. Implement this in a try/except block.",
    "1043612": "Hello @youhanlee, \n\nI created a notebook to show how to sort DICOM images properly. I hope it helps you get the correct ordering!\n\nhttps://www.kaggle.com/aksg87/dicom-sequence-selection-and-sort"
  }
}