From f1721625f31d9073298c536b0de20f237810f889 Mon Sep 17 00:00:00 2001 From: Ankur Sinha (Ankur Sinha Gmail) Date: Nov 19 2018 15:58:49 +0000 Subject: Initial commit --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8469f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/pygiftiio-1.0.4.tar.gz diff --git a/gifti_read_example.py b/gifti_read_example.py new file mode 100644 index 0000000..e167e08 --- /dev/null +++ b/gifti_read_example.py @@ -0,0 +1,66 @@ +from pygiftiio import * +import numpy + +# Read Gifti Image +filename = '/path/to/file' +image = gifti_read_image(fn, 1) + +# image is now of type GiftiImage +# The _fields_ variable is populated with the same structure +# as the C-based struct +# Now we query the image to get data arrays, meta data, etc. +print "Number of Data Arrays in Image = " + str(image.numDA) + +# Extract a dataarray from the Gifti Image based on its index. +def get_da_from_index(im, ind): + if ind = im.numDA: + print "Index exceeds number of DA's in gifti image" + + # Most things are handled automatically by the ctypes wrapping. + # However, in the case of pointers, you need to access the + # contents of the pointer to get the right result + return im.darray[ind].contents + +# Extract a dataarray from the Gifti Image based on its IntentCode +def get_da_from_intent(im, intent): + for i in xrange(im.numDA): + # Intent code is stored in the DataArray struct + da = im.darray[i].contents + + # The wrapping allows the intent codes to be represented as + # integers as defined by the C-Macros or as the strings that + # are replaced. + if type(intent) == type(1): + if da.intent == intent: + # Grab the first DataArray that is the correct intent + return da + else: + # If it's not an int, we have to look up the integer value in + # the Intent Code dictionary + if da.intent == GiftiIntentCode.intents[intent]: + return da + +# Extract metadata from the GiftiImage OR a GiftiDataArray +def get_metadata_from_image(im): + metadata = im.meta + # metadata is now of type GiftiMetaData. It has length, name, and value arrays. + return metadata + +# Print all the metadata in the GiftiMetaData object +def print_metadata(md): + size = md.length + for i in xrange(size): + name = md.name[i] + val = md.value[i] + print str(name) + ": " + str(val) + +# Extract the Coordinate Transform(s) from a dataarray +def get_coord_xform(da): + dspace = da.coordsys.contents.dataspace + xformspace = da.coordsys.contents.xformspace + xform = da.coordsys.contents.xform + xform_ar = numpy.array(xform) # make the list of values into an array + xform_ar.shape = 4,4 # reshape the array into a valid transform matrix + return xform_ar # Return a numpy array representing the transform + + diff --git a/gifti_write_example.py b/gifti_write_example.py new file mode 100644 index 0000000..9605374 --- /dev/null +++ b/gifti_write_example.py @@ -0,0 +1,75 @@ +from pygiftiio import * +from ctypes import * +import numpy + +# setup +image = GiftiImage() +# CTypes provides constructors for pointer-based types. This is important +# to the initialization of the various members of the structs we need +# to write via the library. +image.version = c_char_p("version_string") +image.darray = POINTER(POINTER(GiftiDataArray))() +image.data = c_void_p(None) +image.numDA = c_int(0) +image.swapped = c_int(0) +image.compressed = c_int(0) + +# type(im) = GiftiImage +# type(ver) = str +def set_version(im, ver): + image.version = c_char_p(ver) + +# type(im) = GiftiImage +# type(numDA) = int +def set_numDA(im, numDA): + im.numDA = c_int(numDA) + +# type(im) = GiftiImage +# type(md) = GiftiMetaData +def set_meta_data(im, md): + im.meta = md + +# type(im) = GiftiImage +# type(da) = GiftiDataArray +def add_data_array(im, da): + cur_numda = im.numDA + + # Create a pointer to the new dataarray + da_ptr = pointer(da.data) + # Grab the pointer to the image's dataarrays + ptr = image.darray + + # Create a new dataarray array pointer + ar = (POINTER(GiftiDataArray)*(cur_numda+1))() + # We need to cast the resulting pointer for use by C + ar = cast(ar, POINTER(POINTER(GiftiDataArray))) + + # Copy all of the current da's to the new array. This just copies the pointers! + for i in xrange(num_da): + ar[i] = im.darray[i] + + # Add the new data array to the image's data + ar[num_da] = da_ptr + + # Reassign the pointer + im.darray = ar + + # Tell the image it has an extra DA now + cur_numda += 1 + im.numDA = c_int(cur_numda) + +# type(da) = GiftiDataArray +# type(axis) = int +# type(val) = int +def set_da_dim(da, axis, val): + # Simple setter. However, the axis variable here is a + # python array index (as the da dims is an array) + # To properly assign the value, the val variable must + # be usable by C, so we must form a c_int type. + # This is true for all Setters. + da.dims[axis] = c_int(val) + +# type(filename) = string +# type(im) = GiftiImage +def write_image(filename, im): + return gifti_write_image, im, filename, 1) diff --git a/python-pygiftiio.spec b/python-pygiftiio.spec new file mode 100644 index 0000000..1b8d658 --- /dev/null +++ b/python-pygiftiio.spec @@ -0,0 +1,92 @@ +# https://fedoraproject.org/wiki/Packaging:DistTag?rd=Packaging/DistTag#Conditionals +# http://rpm.org/user_doc/conditional_builds.html +%if 0%{?fedora} >= 30 +# disabled by default +%bcond_with py2 +%else +%bcond_without py2 0 +%endif + +%global srcname pygiftiio + +%global desc %{expand: \ +GIFTI is an XML-based file format for cortical surface data. This reference IO +implementation is developed by the Neuroimaging Informatics Technology +Initiative (NIfTI).} + +Name: python-%{srcname} +Version: 1.0.4 +Release: 1%{?dist} +Summary: Python bindings for Gifti + +License: GPLv2 +URL: https://www.nitrc.org/frs/?group_id=75 +Source0: https://www.nitrc.org/frs/download.php/1285/%{srcname}-%{version}.tar.gz +Source1: https://www.nitrc.org/frs/download.php/261/gifti_write_example.py +Source2: https://www.nitrc.org/frs/download.php/260/gifti_read_example.py + +BuildArch: noarch + +%description +%{desc} + +%if %{with py2} +%package -n python2-%{srcname} +Summary: %{summary} +BuildRequires: python2-devel +Requires: gifticlib-devel +%{?python_provide:%python_provide python2-%{srcname}} + +%description -n python2-%{srcname} +%{desc} +%endif + +%package -n python3-%{srcname} +Summary: %{summary} +BuildRequires: python3-devel +Requires: gifticlib-devel +%{?python_provide:%python_provide python3-%{srcname}} + +%description -n python3-%{srcname} +%{desc} + + +%prep +%autosetup -n %{srcname} +cp -v %{SOURCE1} . +cp -v %{SOURCE2} . + +%build +# Nothing to do + +%install +# Put things where they belong +install -D -m 0644 %{srcname}.py -t %{buildroot}/%{python3_sitelib}/ +%if %{with py2} +install -D -m 0644 %{srcname}.py -t %{buildroot}/%{python2_sitelib}/ +%endif + +%check +# No tests + +%if %{with py2} +%files -n python2-%{srcname} +%license LICENSE.GPL +%doc gifti_write_example.py gifti_read_example.py +%{python2_sitelib}/%{srcname}.py +%{python2_sitelib}/%{srcname}.pyc +%{python2_sitelib}/%{srcname}.pyo +%endif + +%files -n python3-%{srcname} +%license LICENSE.GPL +%doc gifti_write_example.py gifti_read_example.py +%{python3_sitelib}/%{srcname}.py +%{python3_sitelib}/__pycache__/%{srcname}.cpython-37.opt-1.pyc +%{python3_sitelib}/__pycache__/%{srcname}.cpython-37.pyc + +%changelog +* Sun Nov 18 2018 Ankur Sinha - 1.0.4-1 +- Only install py2 files conditionally +- Initial build +- Correct license diff --git a/sources b/sources new file mode 100644 index 0000000..4fa7b2f --- /dev/null +++ b/sources @@ -0,0 +1 @@ +SHA512 (pygiftiio-1.0.4.tar.gz) = cf5397ab971d89f1caac8f51d515ede47ad289f4ee49c5e9bedb647ce1d334b1bbe1992816607859dde11c14c40e5322d69f2e1f71c2d5efe30bf2ee9951c83e