Skip to content

Commit 1b1801f

Browse files
committed
added code for downloading the murdock 1962 dataset
1 parent c2382af commit 1b1801f

File tree

1 file changed

+74
-16
lines changed

1 file changed

+74
-16
lines changed

content/assignments/Assignment_2:Search_of_Associative_Memory_Model/sam_assignment_template.ipynb

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,97 @@
4141
"import pandas as pd\n",
4242
"import numpy as np\n",
4343
"import matplotlib.pyplot as plt\n",
44-
"import seaborn as sns"
44+
"import seaborn as sns\n",
45+
"\n",
46+
"import requests\n",
47+
"import os\n",
48+
"from tempfile import NamedTemporaryFile"
4549
]
4650
},
4751
{
4852
"cell_type": "markdown",
49-
"metadata": {
50-
"id": "atpXuzOFKDSb"
51-
},
53+
"metadata": {},
5254
"source": [
53-
"Basic skeleton for the SAM model"
55+
"Download dataset and store as `sequence` objects containing the `presented` and `recalled` items for each trial. The sequences are stored in nested dictionaries in the form\n",
56+
"```\n",
57+
"dict[list length][presentation rate]\n",
58+
"```"
5459
]
5560
},
5661
{
5762
"cell_type": "code",
5863
"execution_count": null,
59-
"metadata": {
60-
"id": "PUXuEy_3-q3E"
61-
},
64+
"metadata": {},
6265
"outputs": [],
6366
"source": [
6467
"class item(object):\n",
65-
" count = 0\n",
68+
" idx = 1\n",
6669
"\n",
67-
" def __init__(self):\n",
68-
" self.id = count\n",
69-
" count += 1\n",
70+
" def __init__(self, val=None):\n",
71+
" if val is None:\n",
72+
" self.id = item.idx\n",
73+
" item.idx += 1\n",
74+
" else:\n",
75+
" self.id = val\n",
7076
"\n",
7177
"\n",
7278
"class sequence(object):\n",
73-
" def __init__(self, items):\n",
74-
" self.items = items\n",
79+
" def __init__(self, items):\n",
80+
" self.items = items\n",
81+
"\n",
82+
"\n",
83+
"def load_recall_data():\n",
84+
" base_url = \"https://raw.githubusercontent.com/ContextLab/memory-models-course/refs/heads/main/content/assignments/Assignment_2%3ASearch_of_Associative_Memory_Model/Murd62%20data/\"\n",
85+
" filenames = [\"fr10-2.txt\", \"fr15-2.txt\", \"fr20-1.txt\", \"fr20-2.txt\", \"fr30-1.txt\", \"fr40-1.txt\"]\n",
86+
"\n",
87+
" presented = {}\n",
88+
" recalled = {}\n",
89+
"\n",
90+
" for filename in filenames:\n",
91+
" list_len, pres_rate = map(int, filename.replace(\".txt\", \"\").replace(\"fr\", \"\").split(\"-\"))\n",
92+
" if list_len not in presented:\n",
93+
" presented[list_len] = {}\n",
94+
" recalled[list_len] = {}\n",
95+
" if pres_rate not in presented[list_len]:\n",
96+
" presented[list_len][pres_rate] = []\n",
97+
" recalled[list_len][pres_rate] = []\n",
7598
"\n",
99+
" # Download the file\n",
100+
" url = base_url + filename\n",
101+
" response = requests.get(url)\n",
102+
" response.raise_for_status()\n",
103+
" lines = response.text.strip().split(\"\\n\")\n",
76104
"\n",
105+
" for line in lines:\n",
106+
" recall_ids = [int(x) for x in line.strip().split() if int(x) != 88]\n",
107+
" recall_seq = sequence([item(val) for val in recall_ids])\n",
108+
" presented_seq = sequence([item(val) for val in range(1, list_len + 1)])\n",
109+
"\n",
110+
" presented[list_len][pres_rate].append(presented_seq)\n",
111+
" recalled[list_len][pres_rate].append(recall_seq)\n",
112+
"\n",
113+
" return presented, recalled\n",
114+
"\n",
115+
"presented, recalled = load_recall_data()"
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {
121+
"id": "atpXuzOFKDSb"
122+
},
123+
"source": [
124+
"Basic skeleton for the SAM model"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"metadata": {
131+
"id": "PUXuEy_3-q3E"
132+
},
133+
"outputs": [],
134+
"source": [
77135
"class STS(object):\n",
78136
" def __init__(self, r, q, s_f, s_b, max_items=None, lts=None):\n",
79137
" self.r = r\n",
@@ -142,8 +200,8 @@
142200
},
143201
"source": [
144202
"Other tasks:\n",
145-
" - Fit params to [Murdock (1962) dataset](https://github.com/ContextLab/memory-models-course/tree/main/content/assignments/Assignment_2%3ASearch_of_Associative_Memory_Model/Murd62%20data)\n",
146-
" - Need to define a \"loss\" function. I suggest computing MSE for one or more behavioral curves, computed for a subset of the Murdock (1962) participants/lists\n",
203+
" - Fit params to [Murdock (1962) dataset](https://github.com/ContextLab/memory-models-course/tree/main/content/assignments/Assignment_2%3ASearch_of_Associative_Memory_Model/Murd62%20data) that you downloaded with the `load_data` function.\n",
204+
" - You'll need to define a \"loss\" function. I suggest computing MSE for one or more behavioral curves, computed for a subset of the Murdock (1962) participants/lists\n",
147205
" - I suggest using [scipy.optimize.minimize](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) to estimate the model parameters.\n",
148206
" - Create observed/predicted plots for held-out data:\n",
149207
" - p(first recall)\n",

0 commit comments

Comments
 (0)