Skip to content

Commit

Permalink
notebook with functions for Q1-Q3
Browse files Browse the repository at this point in the history
  • Loading branch information
Shee-Ra committed Aug 29, 2020
0 parents commit 9d74f7b
Show file tree
Hide file tree
Showing 4 changed files with 531 additions and 0 deletions.
Binary file added 1_1D_spatial_cross_correlation /.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using basic data structures"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"# Question one: using basic python structures\n",
"\n",
"def CrossCorr(template=[1, 2, 2, 1], pattern=[0, 1, 1, 0, 1, 2, 2, 1, 0, 0, 0]): \n",
" '''Calculates the cross correlation of two lists'''\n",
" # get length of inputs\n",
" lt=len(template)\n",
" lp=len(pattern)\n",
"\n",
" # handles edge case if pattern is shorter than template: left pads pattern with zeros\n",
" if lp < lt:\n",
" zeros = [0]*(lt-lp) # zero matrix to left pad pattern\n",
" pattern=zeros+pattern # concatinate lists\n",
" lp=len(pattern) # update pattern\n",
"\n",
" # inititalise output\n",
" cc=[]\n",
" for i in range(lp-lt+1):\n",
" # extract four elements from pattern\n",
" sub_pattern=[pattern[j] for j in range(i,i+lt)]\n",
"\n",
" # element wise multiplication\n",
" eltmult =[sub_pattern[j]*template[j] for j in range(lt)]\n",
"\n",
" # sum to get cross corrlation\n",
" cc.append(sum(eltmult))\n",
"\n",
" return(cc)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Function passes for input: [4, 4, 5, 8, 10, 8, 4, 1]\n"
]
}
],
"source": [
"# test\n",
"expected=[4, 4, 5, 8, 10, 8, 4, 1]\n",
"\n",
"if CrossCorr() == expected:\n",
" print(f'Function passes for input: {expected}')\n",
"else:\n",
" print(f'Function fails. Expected {expected}, actual: {test}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's give numpy a go"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"def CrossCorr_np(template=[1, 2, 2, 1], pattern=[0, 1, 1, 0, 1, 2, 2, 1, 0, 0, 0]): \n",
" \n",
" # get length of inputs\n",
" lt=len(template)\n",
" lp=len(pattern)\n",
" \n",
" # handles edge case if pattern is shorter than template: left pads pattern with zeros (np.correlate treates this edgecase differently that how I want, so I use pad with zeros)\n",
" if lp < lt:\n",
" zeros = [0]*(lt-lp) # zero matrix to left pad pattern\n",
" pattern=zeros+pattern # concatinate lists\n",
" lp=len(pattern) # update pattern\n",
" \n",
" cc=np.correlate(np.array(pattern),np.array(template)) # convert template and pattern to array and take cc\n",
" return(list(cc))\n"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test passes for input: [4, 4, 5, 8, 10, 8, 4, 1]\n"
]
}
],
"source": [
"# test\n",
"expected=[4, 4, 5, 8, 10, 8, 4, 1]\n",
"\n",
"if CrossCorr_np() == expected:\n",
" print(f'Function passes for input: {expected}')\n",
"else:\n",
" print(f'Function fails. Expected {expected}, actual: {CrossCorr}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part B"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"# using data structures\n",
"def GetIndex(score_array=[4, 4, 5, 8, 10, 8, 4, 1]):\n",
" smax=max(score_array) # find max\n",
" si=score_array.index(smax) # get location\n",
" return(si,smax)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test fails. Expected (4, 10), actual: [4, 4, 5, 8, 10, 8, 4, 1]\n"
]
}
],
"source": [
"# test\n",
"expected=(4, 10)\n",
"\n",
"if CrossCorr_np() == expected:\n",
" print(f'test passes for input: {expected}')\n",
"else:\n",
" print(f'test fails. Expected {expected}, actual: {CrossCorr_np()}')"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'numpy.ndarray' object has no attribute 'amax'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-94-f9f4304ed120>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# numpy\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mamax\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;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'amax'"
]
}
],
"source": [
"# numpy\n",
"\n",
"np.array([4, 4, 5, 8, 10, 8, 4, 1]).amax()\n",
"\n",
"result = numpy.where(arr == np.amax(arr))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 9d74f7b

Please sign in to comment.