diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..fc3ef48 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,343 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Populating the interactive namespace from numpy and matplotlib\n" + ] + } + ], + "source": [ + "%pylab inline\n", + "import mfield" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "dc = 60\n", + "sus = dc*mfield.chiFerritin\n", + "susMap = sus*ones([4,4,4])\n", + "B0 = 7.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "db = mean(mfield.genDBz(susMap,B0,0)*3)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "23.247303490320004" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db*mfield.gamma2pi" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def g(x):\n", + " def f(y):\n", + " return x+y\n", + " return f\n", + "def f(x,y):\n", + " return x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "45" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "reduce(f,range(10),0)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def map(fn, coll):\n", + " def _inner(out_coll, item):\n", + " out_coll.append(fn(item))\n", + " return out_coll\n", + " return reduce(_inner, coll, ())" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "map() takes exactly 2 arguments (3 given)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: map() takes exactly 2 arguments (3 given)" + ] + } + ], + "source": [ + "map(f,range(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Calculation(object):\n", + " def apply(self, x, y):\n", + " raise NotImplementedError(\"\")\n", + "\n", + " def calculate(self, coll):\n", + " total = 0\n", + " for item in coll:\n", + " total = self.apply(total, item)\n", + " return total\n", + "\n", + "\n", + "class Addition(Calculation):\n", + " def apply(self, x, y):\n", + " return x + y\n", + "\n", + "\n", + "class Multiplication(Calculation):\n", + " def apply(self, x, y):\n", + " return x * y\n", + "\n", + "\n", + "adder = Addition().calculate\n", + "multiplier = Multiplication().calculate" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Addition.calculate" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.69146246127401301" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mfield.phi(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.30853753872598694" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mfield.phi(-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "42554.88910737372" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "6*4/3.*3.14156*(10)**3/.839**3" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3500000.0" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "14e3*250" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0432432432432433e-06" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0.7*0.6*0.3*0.3/18.5e3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.12" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/__pycache__/mfield.cpython-35.pyc b/__pycache__/mfield.cpython-35.pyc new file mode 100644 index 0000000..2e990d8 Binary files /dev/null and b/__pycache__/mfield.cpython-35.pyc differ diff --git a/mfield.py b/mfield.py index 767b1c1..b50526c 100644 --- a/mfield.py +++ b/mfield.py @@ -66,31 +66,37 @@ def phi(x): #needed for smoothing return 1/2.*(1 + erf(x/(2*np.sqrt(2)))) -def genSusMap(cMap, sliceHeight, drytissuecorrection = False, smooth = False): +def genSusMap(cMap, sliceHeight,stackedaxis, drytissuecorrection = False, smooth = False): # Creates a correctly spaced volume of the samples susceptibility assuming that it is proportional to the # iron density (like Ferritin). drytissueprop = 0.2 # Proportion of dry tissue in physiological conditions - chiFerritin = 1.30e-9 # Ferritin susceptibility following Schenck 96 - # (*e3 because we hace [c] in \mu g / g and not mg / g ) - if drytissuecorrection: chiFerritin = chiFerritin*drytissueprop - suscep = cMap * chiFerritin + suscep = cMap * mfield.chiFerritin # To real scaled cube: sliceHeight high slices with area of cMap.shape[1]*cMap.shape[2] px - suscep3dz = np.zeros([sliceHeight * cMap.shape[0],cMap.shape[1],cMap.shape[2]]) - for i in range(cMap.shape[0]): - suscep3dz[(i*sliceHeight):((i+1)*sliceHeight),:,:] = suscep[i,:,:] - - susMap = np.swapaxes(np.swapaxes(suscep3dz,0,1),1,2) + if stackedaxis==0: + suscep3dz = np.zeros([sliceHeight * cMap.shape[0],cMap.shape[1],cMap.shape[2]]) + for i in range(cMap.shape[0]): + suscep3dz[(i*sliceHeight):((i+1)*sliceHeight),:,:] = suscep[i,:,:] + + elif stackedaxis==1: + suscep3dz = np.zeros([cMap.shape[0],sliceHeight * cMap.shape[1],cMap.shape[2]]) + for i in range(cMap.shape[1]): + suscep3dz[:,(i*sliceHeight):((i+1)*sliceHeight),:] = suscep[:,i,:] + + elif stackedaxis==2: + suscep3dz = np.zeros([cMap.shape[0],cMap.shape[1],sliceHeight * cMap.shape[2]]) + for i in range(cMap.shape[2]): + suscep3dz[:,:,(i*sliceHeight):((i+1)*sliceHeight)] = suscep[:,:,i][:,:,None] - if smooth and sliceHeight==4 and cMap.shape[0]==10: # For "original" 10 mu slices. + if smooth and sliceHeight==4 and cMap.shape[0]==10 and stackedaxis==2: # For "original" 10 mu slices. for i in range(10): susMap[:,:,4*i-1]= phi(1)*susMap[:,:,4*i-1] + phi(-1)*susMap[:,:,4*i] susMap[:,:,4*i] = phi(-1)*susMap[:,:,4*i-1] + phi(1)*susMap[:,:,4*i] print('sucessfully smoothed') else: - return 'error: smoothing could not be done, wrong dimensions?' - return susMap + print('smoothing was not done') + return suscep3dz diff --git a/mfield.pyc b/mfield.pyc new file mode 100644 index 0000000..2a74360 Binary files /dev/null and b/mfield.pyc differ