prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Import error DICOM file
  **Describe the bug**
  When uploading a DICOM file in which the Window Center and Window Width attributes are set to floating values (e.g. 2047.0), the file importer raises an error (see screenshot). The issue can be solved by converting the floating values to integers and writing a new DICOM file. 
  
  **To Reproduce**
  Steps to reproduce the behavior:
  1. Go to 'Reader Studies'.
  2. Select a reader study. 
  3. Go to 'Cases'.
  4. Click 'Add Cases'.
  5. Click 'Choose Files'. 
  6. Select a DICOM image with Window Center/Width set to a floating value. 
  7. Click 'Submit'.
  8. An error will be raised.
  
  **Screenshots**
  <img width="828" alt="import_error" src="https://user-images.githubusercontent.com/47858231/83017756-817de600-a024-11ea-9094-d7e4d60cb01f.PNG">
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `app/grandchallenge/cases/image_builders/utils.py`
  Content:
  ```
  1 from pathlib import Path
  2 from tempfile import TemporaryDirectory, TemporaryFile
  3 from typing import AnyStr, Optional, Sequence, Tuple
  4 from uuid import uuid4
  5 
  6 import SimpleITK
  7 from django.conf import settings
  8 from django.core.files import File
  9 
  10 from grandchallenge.cases.models import Image, ImageFile
  11 
  12 
  13 def convert_itk_to_internal(
  14     simple_itk_image: SimpleITK.Image,
  15     name: Optional[AnyStr] = None,
  16     use_spacing: Optional[bool] = True,
  17 ) -> Tuple[Image, Sequence[ImageFile]]:
  18     color_space = simple_itk_image.GetNumberOfComponentsPerPixel()
  19     color_space = {
  20         1: Image.COLOR_SPACE_GRAY,
  21         3: Image.COLOR_SPACE_RGB,
  22         4: Image.COLOR_SPACE_RGBA,
  23     }.get(color_space, None)
  24     if color_space is None:
  25         raise ValueError("Unknown color space for MetaIO image.")
  26 
  27     with TemporaryDirectory() as work_dir:
  28         work_dir = Path(work_dir)
  29 
  30         pk = uuid4()
  31         if not name:
  32             name = str(pk)
  33         SimpleITK.WriteImage(
  34             simple_itk_image,
  35             str(work_dir / f"{pk}.{settings.ITK_INTERNAL_FILE_FORMAT}"),
  36             True,
  37         )
  38 
  39         if simple_itk_image.GetDimension() == 4:
  40             timepoints = simple_itk_image.GetSize()[-1]
  41         else:
  42             timepoints = None
  43         depth = simple_itk_image.GetDepth()
  44         metadata_keys = simple_itk_image.GetMetaDataKeys()
  45         window_center = (
  46             float(simple_itk_image.GetMetaData("WindowCenter"))
  47             if "WindowCenter" in metadata_keys
  48             else None
  49         )
  50         window_width = (
  51             float(simple_itk_image.GetMetaData("WindowWidth"))
  52             if "WindowWidth" in metadata_keys
  53             else None
  54         )
  55         db_image = Image(
  56             pk=pk,
  57             name=name,
  58             width=simple_itk_image.GetWidth(),
  59             height=simple_itk_image.GetHeight(),
  60             depth=depth if depth else None,
  61             window_center=window_center,
  62             window_width=window_width,
  63             timepoints=timepoints,
  64             resolution_levels=None,
  65             color_space=color_space,
  66             voxel_width_mm=simple_itk_image.GetSpacing()[0]
  67             if use_spacing
  68             else None,
  69             voxel_height_mm=simple_itk_image.GetSpacing()[1]
  70             if use_spacing
  71             else None,
  72             voxel_depth_mm=simple_itk_image.GetSpacing()[2] if depth else None,
  73         )
  74         db_image_files = []
  75         for _file in work_dir.iterdir():
  76             temp_file = TemporaryFile()
  77             with open(str(_file), "rb") as open_file:
  78                 buffer = True
  79                 while buffer:
  80                     buffer = open_file.read(1024)
  81                     temp_file.write(buffer)
  82             db_image_file = ImageFile(
  83                 image=db_image,
  84                 image_type=ImageFile.IMAGE_TYPE_MHD,
  85                 file=File(temp_file, name=_file.name),
  86             )
  87             db_image_files.append(db_image_file)
  88 
  89     return db_image, db_image_files
  90
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/app/grandchallenge/cases/image_builders/utils.py b/app/grandchallenge/cases/image_builders/utils.py
  --- a/app/grandchallenge/cases/image_builders/utils.py
  +++ b/app/grandchallenge/cases/image_builders/utils.py
  @@ -41,17 +41,16 @@
           else:
               timepoints = None
           depth = simple_itk_image.GetDepth()
  -        metadata_keys = simple_itk_image.GetMetaDataKeys()
  -        window_center = (
  -            float(simple_itk_image.GetMetaData("WindowCenter"))
  -            if "WindowCenter" in metadata_keys
  -            else None
  -        )
  -        window_width = (
  -            float(simple_itk_image.GetMetaData("WindowWidth"))
  -            if "WindowWidth" in metadata_keys
  -            else None
  -        )
  +
  +        try:
  +            window_center = float(simple_itk_image.GetMetaData("WindowCenter"))
  +        except (RuntimeError, ValueError):
  +            window_center = None
  +        try:
  +            window_width = float(simple_itk_image.GetMetaData("WindowWidth"))
  +        except (RuntimeError, ValueError):
  +            window_width = None
  +
           db_image = Image(
               pk=pk,
               name=name,
  
verification_info: {"golden_diff": "diff --git a/app/grandchallenge/cases/image_builders/utils.py b/app/grandchallenge/cases/image_builders/utils.py\n--- a/app/grandchallenge/cases/image_builders/utils.py\n+++ b/app/grandchallenge/cases/image_builders/utils.py\n@@ -41,17 +41,16 @@\n         else:\n             timepoints = None\n         depth = simple_itk_image.GetDepth()\n-        metadata_keys = simple_itk_image.GetMetaDataKeys()\n-        window_center = (\n-            float(simple_itk_image.GetMetaData(\"WindowCenter\"))\n-            if \"WindowCenter\" in metadata_keys\n-            else None\n-        )\n-        window_width = (\n-            float(simple_itk_image.GetMetaData(\"WindowWidth\"))\n-            if \"WindowWidth\" in metadata_keys\n-            else None\n-        )\n+\n+        try:\n+            window_center = float(simple_itk_image.GetMetaData(\"WindowCenter\"))\n+        except (RuntimeError, ValueError):\n+            window_center = None\n+        try:\n+            window_width = float(simple_itk_image.GetMetaData(\"WindowWidth\"))\n+        except (RuntimeError, ValueError):\n+            window_width = None\n+\n         db_image = Image(\n             pk=pk,\n             name=name,\n", "issue": "Import error DICOM file\n**Describe the bug**\r\nWhen uploading a DICOM file in which the Window Center and Window Width attributes are set to floating values (e.g. 2047.0), the file importer raises an error (see screenshot). The issue can be solved by converting the floating values to integers and writing a new DICOM file. \r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. Go to 'Reader Studies'.\r\n2. Select a reader study. \r\n3. Go to 'Cases'.\r\n4. Click 'Add Cases'.\r\n5. Click 'Choose Files'. \r\n6. Select a DICOM image with Window Center/Width set to a floating value. \r\n7. Click 'Submit'.\r\n8. An error will be raised.\r\n\r\n**Screenshots**\r\n<img width=\"828\" alt=\"import_error\" src=\"https://user-images.githubusercontent.com/47858231/83017756-817de600-a024-11ea-9094-d7e4d60cb01f.PNG\">\r\n\n", "before_files": [{"content": "from pathlib import Path\nfrom tempfile import TemporaryDirectory, TemporaryFile\nfrom typing import AnyStr, Optional, Sequence, Tuple\nfrom uuid import uuid4\n\nimport SimpleITK\nfrom django.conf import settings\nfrom django.core.files import File\n\nfrom grandchallenge.cases.models import Image, ImageFile\n\n\ndef convert_itk_to_internal(\n    simple_itk_image: SimpleITK.Image,\n    name: Optional[AnyStr] = None,\n    use_spacing: Optional[bool] = True,\n) -> Tuple[Image, Sequence[ImageFile]]:\n    color_space = simple_itk_image.GetNumberOfComponentsPerPixel()\n    color_space = {\n        1: Image.COLOR_SPACE_GRAY,\n        3: Image.COLOR_SPACE_RGB,\n        4: Image.COLOR_SPACE_RGBA,\n    }.get(color_space, None)\n    if color_space is None:\n        raise ValueError(\"Unknown color space for MetaIO image.\")\n\n    with TemporaryDirectory() as work_dir:\n        work_dir = Path(work_dir)\n\n        pk = uuid4()\n        if not name:\n            name = str(pk)\n        SimpleITK.WriteImage(\n            simple_itk_image,\n            str(work_dir / f\"{pk}.{settings.ITK_INTERNAL_FILE_FORMAT}\"),\n            True,\n        )\n\n        if simple_itk_image.GetDimension() == 4:\n            timepoints = simple_itk_image.GetSize()[-1]\n        else:\n            timepoints = None\n        depth = simple_itk_image.GetDepth()\n        metadata_keys = simple_itk_image.GetMetaDataKeys()\n        window_center = (\n            float(simple_itk_image.GetMetaData(\"WindowCenter\"))\n            if \"WindowCenter\" in metadata_keys\n            else None\n        )\n        window_width = (\n            float(simple_itk_image.GetMetaData(\"WindowWidth\"))\n            if \"WindowWidth\" in metadata_keys\n            else None\n        )\n        db_image = Image(\n            pk=pk,\n            name=name,\n            width=simple_itk_image.GetWidth(),\n            height=simple_itk_image.GetHeight(),\n            depth=depth if depth else None,\n            window_center=window_center,\n            window_width=window_width,\n            timepoints=timepoints,\n            resolution_levels=None,\n            color_space=color_space,\n            voxel_width_mm=simple_itk_image.GetSpacing()[0]\n            if use_spacing\n            else None,\n            voxel_height_mm=simple_itk_image.GetSpacing()[1]\n            if use_spacing\n            else None,\n            voxel_depth_mm=simple_itk_image.GetSpacing()[2] if depth else None,\n        )\n        db_image_files = []\n        for _file in work_dir.iterdir():\n            temp_file = TemporaryFile()\n            with open(str(_file), \"rb\") as open_file:\n                buffer = True\n                while buffer:\n                    buffer = open_file.read(1024)\n                    temp_file.write(buffer)\n            db_image_file = ImageFile(\n                image=db_image,\n                image_type=ImageFile.IMAGE_TYPE_MHD,\n                file=File(temp_file, name=_file.name),\n            )\n            db_image_files.append(db_image_file)\n\n    return db_image, db_image_files\n", "path": "app/grandchallenge/cases/image_builders/utils.py"}], "after_files": [{"content": "from pathlib import Path\nfrom tempfile import TemporaryDirectory, TemporaryFile\nfrom typing import AnyStr, Optional, Sequence, Tuple\nfrom uuid import uuid4\n\nimport SimpleITK\nfrom django.conf import settings\nfrom django.core.files import File\n\nfrom grandchallenge.cases.models import Image, ImageFile\n\n\ndef convert_itk_to_internal(\n    simple_itk_image: SimpleITK.Image,\n    name: Optional[AnyStr] = None,\n    use_spacing: Optional[bool] = True,\n) -> Tuple[Image, Sequence[ImageFile]]:\n    color_space = simple_itk_image.GetNumberOfComponentsPerPixel()\n    color_space = {\n        1: Image.COLOR_SPACE_GRAY,\n        3: Image.COLOR_SPACE_RGB,\n        4: Image.COLOR_SPACE_RGBA,\n    }.get(color_space, None)\n    if color_space is None:\n        raise ValueError(\"Unknown color space for MetaIO image.\")\n\n    with TemporaryDirectory() as work_dir:\n        work_dir = Path(work_dir)\n\n        pk = uuid4()\n        if not name:\n            name = str(pk)\n        SimpleITK.WriteImage(\n            simple_itk_image,\n            str(work_dir / f\"{pk}.{settings.ITK_INTERNAL_FILE_FORMAT}\"),\n            True,\n        )\n\n        if simple_itk_image.GetDimension() == 4:\n            timepoints = simple_itk_image.GetSize()[-1]\n        else:\n            timepoints = None\n        depth = simple_itk_image.GetDepth()\n\n        try:\n            window_center = float(simple_itk_image.GetMetaData(\"WindowCenter\"))\n        except (RuntimeError, ValueError):\n            window_center = None\n        try:\n            window_width = float(simple_itk_image.GetMetaData(\"WindowWidth\"))\n        except (RuntimeError, ValueError):\n            window_width = None\n\n        db_image = Image(\n            pk=pk,\n            name=name,\n            width=simple_itk_image.GetWidth(),\n            height=simple_itk_image.GetHeight(),\n            depth=depth if depth else None,\n            window_center=window_center,\n            window_width=window_width,\n            timepoints=timepoints,\n            resolution_levels=None,\n            color_space=color_space,\n            voxel_width_mm=simple_itk_image.GetSpacing()[0]\n            if use_spacing\n            else None,\n            voxel_height_mm=simple_itk_image.GetSpacing()[1]\n            if use_spacing\n            else None,\n            voxel_depth_mm=simple_itk_image.GetSpacing()[2] if depth else None,\n        )\n        db_image_files = []\n        for _file in work_dir.iterdir():\n            temp_file = TemporaryFile()\n            with open(str(_file), \"rb\") as open_file:\n                buffer = True\n                while buffer:\n                    buffer = open_file.read(1024)\n                    temp_file.write(buffer)\n            db_image_file = ImageFile(\n                image=db_image,\n                image_type=ImageFile.IMAGE_TYPE_MHD,\n                file=File(temp_file, name=_file.name),\n            )\n            db_image_files.append(db_image_file)\n\n    return db_image, db_image_files\n", "path": "app/grandchallenge/cases/image_builders/utils.py"}]}