Skip to content

Commit

Permalink
Merge pull request #15 from neuro-ml/dev
Browse files Browse the repository at this point in the history
added to_color_space to stack images
  • Loading branch information
maxme1 authored Dec 9, 2021
2 parents 2637776 + eb745ac commit 14d0ee9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Python package

on:
release:
types: [ released ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- id: get_version
name: Get the release version
uses: battila7/get-version-action@v2

- name: Check the version and build the package
run: |
RELEASE=${{ steps.get_version.outputs.version-without-v }}
VERSION=$(python -c "from pathlib import Path; import runpy; folder, = {d.parent for d in Path().resolve().glob('*/__init__.py') if d.parent.is_dir() and (d.parent / '__version__.py').exists()}; print(runpy.run_path(folder / '__version__.py')['__version__'])")
MATCH=$(pip index versions dicom-csv | grep "Available versions:" | grep $VERSION) || echo
echo $MATCH
if [ "$GITHUB_BASE_REF" = "master" ] && [ "$MATCH" != "" ]; then echo "Version $VERSION already present" && exit 1; fi
if [ "$VERSION" != "$RELEASE" ]; then echo "$VERSION vs $RELEASE" && exit 1; fi
python setup.py sdist
- name: Publish to PyPi
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.PYPI_API_TOKEN }}
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Python package

on: [ push, pull_request ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.6, 3.7, 3.8, 3.9 ]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Check the version and build the package
run: |
VERSION=$(python -c "from pathlib import Path; import runpy; folder, = {d.parent for d in Path().resolve().glob('*/__init__.py') if d.parent.is_dir() and (d.parent / '__version__.py').exists()}; print(runpy.run_path(folder / '__version__.py')['__version__'])")
MATCH=$(pip index versions dicom-csv | grep "Available versions:" | grep $VERSION) || echo
echo $MATCH
if [ "$GITHUB_BASE_REF" = "master" ] && [ "$MATCH" != "" ]; then exit 1; fi
python setup.py sdist
- name: Install
run: |
pip install dist/*
2 changes: 1 addition & 1 deletion dicom_csv/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.1'
__version__ = '0.2.2'
13 changes: 10 additions & 3 deletions dicom_csv/misc.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import os
import warnings
from functools import partial
from operator import itemgetter
from os.path import join as jp
from typing import Optional

import numpy as np
from pydicom.pixel_data_handlers import convert_color_space

from .utils import *

__all__ = 'get_image', 'stack_images'


def get_image(instance: Dataset):
def get_image(instance: Dataset, to_color_space: Optional[str] = None):
def _to_int(x):
# this little trick helps to avoid unneeded type casting
if x == int(x):
x = int(x)
return x

array = instance.pixel_array
if to_color_space is not None:
array = convert_color_space(array, instance.PhotometricInterpretation, to_color_space)

slope, intercept = instance.get('RescaleSlope'), instance.get('RescaleIntercept')
if slope is not None and slope != 1:
array = array * _to_int(slope)
Expand All @@ -26,8 +33,8 @@ def _to_int(x):
return array


def stack_images(series: Series, axis: int = -1):
return np.stack(list(map(get_image, series)), axis)
def stack_images(series: Series, axis: int = -1, to_color_space: Optional[str] = None):
return np.stack(list(map(partial(get_image, to_color_space=to_color_space), series)), axis)


# TODO: legacy support
Expand Down

0 comments on commit 14d0ee9

Please sign in to comment.