{"action":"created","comment":{"url":"https://api.github.com/repos/nistats/nistats/pulls/comments/80953500","pull_request_review_id":1970035,"id":80953500,"diff_hunk":"@@ -446,3 +446,47 @@ def plot_design_matrix(design_matrix, rescale=True, ax=None):\n     ax.set_xticks(range(len(names)))\n     ax.set_xticklabels(names, rotation=60, ha='right')\n     return ax\n+\n+\n+def create_second_level_design(maps_table, confounds=None):\n+    \"\"\"Sets up a second level design from a maps table.\n+\n+    Parameters\n+    ----------\n+    maps_table: pandas DataFrame\n+        Contains at least columns 'map_name' and 'subject_id'\n+    confounds: pandas DataFrame, optional\n+        If given, contains at least two columns, 'subject_id' and one confound.\n+        confounds and maps_table do not need to agree on their shape,\n+        information between them is matched based on the 'subject_id' column\n+        that both must have.\n+\n+    Returns\n+    -------\n+    design_matrix: pandas DataFrame\n+        The second level design matrix\n+    \"\"\"\n+    maps_name = maps_table['map_name'].tolist()\n+    subjects_id = maps_table['subject_id'].tolist()\n+    confounds_name = []\n+    if confounds is not None:\n+        confounds_name = confounds.columns.tolist()\n+        confounds_name.remove('subject_id')\n+    design_columns = (np.unique(maps_name).tolist() +\n+                      np.unique(subjects_id).tolist() +\n+                      confounds_name)\n+    design_matrix = pd.DataFrame(columns=design_columns)\n+    for ridx, row in maps_table.iterrows():\n+        design_matrix.loc[ridx] = [0] * len(design_columns)\n+        design_matrix.loc[ridx, row['map_name']] = 1\n+        design_matrix.loc[ridx, row['subject_id']] = 1\n+        if confounds is not None:\n+            conrow = confounds['subject_id'] == row['subject_id']\n+            for conf_name in confounds_name:\n+                design_matrix.loc[ridx, conf_name] = confounds[conrow][conf_name].values\n+\n+    # check column names are unique\n+    if len(np.unique(design_columns)) != len(design_columns):\n+        raise ValueError('Design matrix columns do not have unique names')\n+\n+    return design_matrix","path":"nistats/design_matrix.py","position":47,"original_position":47,"commit_id":"a26f019a2b39979012f10e213a02c1d0ff508ab4","original_commit_id":"a26f019a2b39979012f10e213a02c1d0ff508ab4","user":{"login":"MartinPerez","id":8766915,"avatar_url":"https://avatars.githubusercontent.com/u/8766915?v=3","gravatar_id":"","url":"https://api.github.com/users/MartinPerez","html_url":"https://github.com/MartinPerez","followers_url":"https://api.github.com/users/MartinPerez/followers","following_url":"https://api.github.com/users/MartinPerez/following{/other_user}","gists_url":"https://api.github.com/users/MartinPerez/gists{/gist_id}","starred_url":"https://api.github.com/users/MartinPerez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MartinPerez/subscriptions","organizations_url":"https://api.github.com/users/MartinPerez/orgs","repos_url":"https://api.github.com/users/MartinPerez/repos","events_url":"https://api.github.com/users/MartinPerez/events{/privacy}","received_events_url":"https://api.github.com/users/MartinPerez/received_events","type":"User","site_admin":false},"body":"so should I add a check like this here?:\r\n\r\nlinalg.cond(x) < 1/sys.float_info.epsilon","created_at":"2016-09-28T16:01:14Z","updated_at":"2016-09-28T16:01:14Z","html_url":"https://github.com/nistats/nistats/pull/78#discussion_r80953500","pull_request_url":"https://api.github.com/repos/nistats/nistats/pulls/78","_links":{"self":{"href":"https://api.github.com/repos/nistats/nistats/pulls/comments/80953500"},"html":{"href":"https://github.com/nistats/nistats/pull/78#discussion_r80953500"},"pull_request":{"href":"https://api.github.com/repos/nistats/nistats/pulls/78"}}},"pull_request":{"url":"https://api.github.com/repos/nistats/nistats/pulls/78","id":73512037,"html_url":"https://github.com/nistats/nistats/pull/78","diff_url":"https://github.com/nistats/nistats/pull/78.diff","patch_url":"https://github.com/nistats/nistats/pull/78.patch","issue_url":"https://api.github.com/repos/nistats/nistats/issues/78","number":78,"state":"open","locked":false,"title":"Second level model","user":{"login":"MartinPerez","id":8766915,"avatar_url":"https://avatars.githubusercontent.com/u/8766915?v=3","gravatar_id":"","url":"https://api.github.com/users/MartinPerez","html_url":"https://github.com/MartinPerez","followers_url":"https://api.github.com/users/MartinPerez/followers","following_url":"https://api.github.com/users/MartinPerez/following{/other_user}","gists_url":"https://api.github.com/users/MartinPerez/gists{/gist_id}","starred_url":"https://api.github.com/users/MartinPerez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MartinPerez/subscriptions","organizations_url":"https://api.github.com/users/MartinPerez/orgs","repos_url":"https://api.github.com/users/MartinPerez/repos","events_url":"https://api.github.com/users/MartinPerez/events{/privacy}","received_events_url":"https://api.github.com/users/MartinPerez/received_events","type":"User","site_admin":false},"body":"Here is the second level model with one simple example (second level analysis only) on the mixed gambles dataset (which is more a sanity check than an actual example due to the lack of a nice contrast to picture the results). I will create a new PR for a complete first and second level example on a bids dataset. The second level model object is quite flexible at the moment, it accepts list of first level models, the pandas dataframe discussed with @bthirion  and a simple list of images, then the other arguments must be matched accordingly. I hope the documentation is clear in this respect.\r\n\r\nAt the moment the second level only includes a simple OLS regression that only takes into account effect maps (RFX). Future PRs would be done for more complex models.\r\n\r\nI included the patsy formulas functionality for the contrasts, it was a very small adaptation of the code. So we already have formulas for contrasts in both first and second level objects.\r\n\r\nIs built on top of the first level model, so it should be compared with #72 instead of master until #72 is merged.\r\n\r\nAll tests included too.","created_at":"2016-06-13T00:21:10Z","updated_at":"2016-09-28T16:01:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1493beb72e3860c1ec03e0cd14d4f9af8695739a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/nistats/nistats/pulls/78/commits","review_comments_url":"https://api.github.com/repos/nistats/nistats/pulls/78/comments","review_comment_url":"https://api.github.com/repos/nistats/nistats/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nistats/nistats/issues/78/comments","statuses_url":"https://api.github.com/repos/nistats/nistats/statuses/a26f019a2b39979012f10e213a02c1d0ff508ab4","head":{"label":"MartinPerez:second_level_model","ref":"second_level_model","sha":"a26f019a2b39979012f10e213a02c1d0ff508ab4","user":{"login":"MartinPerez","id":8766915,"avatar_url":"https://avatars.githubusercontent.com/u/8766915?v=3","gravatar_id":"","url":"https://api.github.com/users/MartinPerez","html_url":"https://github.com/MartinPerez","followers_url":"https://api.github.com/users/MartinPerez/followers","following_url":"https://api.github.com/users/MartinPerez/following{/other_user}","gists_url":"https://api.github.com/users/MartinPerez/gists{/gist_id}","starred_url":"https://api.github.com/users/MartinPerez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MartinPerez/subscriptions","organizations_url":"https://api.github.com/users/MartinPerez/orgs","repos_url":"https://api.github.com/users/MartinPerez/repos","events_url":"https://api.github.com/users/MartinPerez/events{/privacy}","received_events_url":"https://api.github.com/users/MartinPerez/received_events","type":"User","site_admin":false},"repo":{"id":41736457,"name":"nistats","full_name":"MartinPerez/nistats","owner":{"login":"MartinPerez","id":8766915,"avatar_url":"https://avatars.githubusercontent.com/u/8766915?v=3","gravatar_id":"","url":"https://api.github.com/users/MartinPerez","html_url":"https://github.com/MartinPerez","followers_url":"https://api.github.com/users/MartinPerez/followers","following_url":"https://api.github.com/users/MartinPerez/following{/other_user}","gists_url":"https://api.github.com/users/MartinPerez/gists{/gist_id}","starred_url":"https://api.github.com/users/MartinPerez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MartinPerez/subscriptions","organizations_url":"https://api.github.com/users/MartinPerez/orgs","repos_url":"https://api.github.com/users/MartinPerez/repos","events_url":"https://api.github.com/users/MartinPerez/events{/privacy}","received_events_url":"https://api.github.com/users/MartinPerez/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/MartinPerez/nistats","description":"Modeling and statistical inference on fMRI data in Python ","fork":true,"url":"https://api.github.com/repos/MartinPerez/nistats","forks_url":"https://api.github.com/repos/MartinPerez/nistats/forks","keys_url":"https://api.github.com/repos/MartinPerez/nistats/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MartinPerez/nistats/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MartinPerez/nistats/teams","hooks_url":"https://api.github.com/repos/MartinPerez/nistats/hooks","issue_events_url":"https://api.github.com/repos/MartinPerez/nistats/issues/events{/number}","events_url":"https://api.github.com/repos/MartinPerez/nistats/events","assignees_url":"https://api.github.com/repos/MartinPerez/nistats/assignees{/user}","branches_url":"https://api.github.com/repos/MartinPerez/nistats/branches{/branch}","tags_url":"https://api.github.com/repos/MartinPerez/nistats/tags","blobs_url":"https://api.github.com/repos/MartinPerez/nistats/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MartinPerez/nistats/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MartinPerez/nistats/git/refs{/sha}","trees_url":"https://api.github.com/repos/MartinPerez/nistats/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MartinPerez/nistats/statuses/{sha}","languages_url":"https://api.github.com/repos/MartinPerez/nistats/languages","stargazers_url":"https://api.github.com/repos/MartinPerez/nistats/stargazers","contributors_url":"https://api.github.com/repos/MartinPerez/nistats/contributors","subscribers_url":"https://api.github.com/repos/MartinPerez/nistats/subscribers","subscription_url":"https://api.github.com/repos/MartinPerez/nistats/subscription","commits_url":"https://api.github.com/repos/MartinPerez/nistats/commits{/sha}","git_commits_url":"https://api.github.com/repos/MartinPerez/nistats/git/commits{/sha}","comments_url":"https://api.github.com/repos/MartinPerez/nistats/comments{/number}","issue_comment_url":"https://api.github.com/repos/MartinPerez/nistats/issues/comments{/number}","contents_url":"https://api.github.com/repos/MartinPerez/nistats/contents/{+path}","compare_url":"https://api.github.com/repos/MartinPerez/nistats/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MartinPerez/nistats/merges","archive_url":"https://api.github.com/repos/MartinPerez/nistats/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MartinPerez/nistats/downloads","issues_url":"https://api.github.com/repos/MartinPerez/nistats/issues{/number}","pulls_url":"https://api.github.com/repos/MartinPerez/nistats/pulls{/number}","milestones_url":"https://api.github.com/repos/MartinPerez/nistats/milestones{/number}","notifications_url":"https://api.github.com/repos/MartinPerez/nistats/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MartinPerez/nistats/labels{/name}","releases_url":"https://api.github.com/repos/MartinPerez/nistats/releases{/id}","deployments_url":"https://api.github.com/repos/MartinPerez/nistats/deployments","created_at":"2015-09-01T12:12:28Z","updated_at":"2016-01-31T13:27:28Z","pushed_at":"2016-09-19T17:55:06Z","git_url":"git://github.com/MartinPerez/nistats.git","ssh_url":"git@github.com:MartinPerez/nistats.git","clone_url":"https://github.com/MartinPerez/nistats.git","svn_url":"https://github.com/MartinPerez/nistats","homepage":null,"size":591,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"nistats:master","ref":"master","sha":"fcf0cafdd5a4ab876226a23ce6e0e5964eaedd67","user":{"login":"nistats","id":12428142,"avatar_url":"https://avatars.githubusercontent.com/u/12428142?v=3","gravatar_id":"","url":"https://api.github.com/users/nistats","html_url":"https://github.com/nistats","followers_url":"https://api.github.com/users/nistats/followers","following_url":"https://api.github.com/users/nistats/following{/other_user}","gists_url":"https://api.github.com/users/nistats/gists{/gist_id}","starred_url":"https://api.github.com/users/nistats/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nistats/subscriptions","organizations_url":"https://api.github.com/users/nistats/orgs","repos_url":"https://api.github.com/users/nistats/repos","events_url":"https://api.github.com/users/nistats/events{/privacy}","received_events_url":"https://api.github.com/users/nistats/received_events","type":"Organization","site_admin":false},"repo":{"id":35545854,"name":"nistats","full_name":"nistats/nistats","owner":{"login":"nistats","id":12428142,"avatar_url":"https://avatars.githubusercontent.com/u/12428142?v=3","gravatar_id":"","url":"https://api.github.com/users/nistats","html_url":"https://github.com/nistats","followers_url":"https://api.github.com/users/nistats/followers","following_url":"https://api.github.com/users/nistats/following{/other_user}","gists_url":"https://api.github.com/users/nistats/gists{/gist_id}","starred_url":"https://api.github.com/users/nistats/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nistats/subscriptions","organizations_url":"https://api.github.com/users/nistats/orgs","repos_url":"https://api.github.com/users/nistats/repos","events_url":"https://api.github.com/users/nistats/events{/privacy}","received_events_url":"https://api.github.com/users/nistats/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nistats/nistats","description":"Modeling and statistical inference on fMRI data in Python ","fork":false,"url":"https://api.github.com/repos/nistats/nistats","forks_url":"https://api.github.com/repos/nistats/nistats/forks","keys_url":"https://api.github.com/repos/nistats/nistats/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nistats/nistats/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nistats/nistats/teams","hooks_url":"https://api.github.com/repos/nistats/nistats/hooks","issue_events_url":"https://api.github.com/repos/nistats/nistats/issues/events{/number}","events_url":"https://api.github.com/repos/nistats/nistats/events","assignees_url":"https://api.github.com/repos/nistats/nistats/assignees{/user}","branches_url":"https://api.github.com/repos/nistats/nistats/branches{/branch}","tags_url":"https://api.github.com/repos/nistats/nistats/tags","blobs_url":"https://api.github.com/repos/nistats/nistats/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nistats/nistats/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nistats/nistats/git/refs{/sha}","trees_url":"https://api.github.com/repos/nistats/nistats/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nistats/nistats/statuses/{sha}","languages_url":"https://api.github.com/repos/nistats/nistats/languages","stargazers_url":"https://api.github.com/repos/nistats/nistats/stargazers","contributors_url":"https://api.github.com/repos/nistats/nistats/contributors","subscribers_url":"https://api.github.com/repos/nistats/nistats/subscribers","subscription_url":"https://api.github.com/repos/nistats/nistats/subscription","commits_url":"https://api.github.com/repos/nistats/nistats/commits{/sha}","git_commits_url":"https://api.github.com/repos/nistats/nistats/git/commits{/sha}","comments_url":"https://api.github.com/repos/nistats/nistats/comments{/number}","issue_comment_url":"https://api.github.com/repos/nistats/nistats/issues/comments{/number}","contents_url":"https://api.github.com/repos/nistats/nistats/contents/{+path}","compare_url":"https://api.github.com/repos/nistats/nistats/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nistats/nistats/merges","archive_url":"https://api.github.com/repos/nistats/nistats/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nistats/nistats/downloads","issues_url":"https://api.github.com/repos/nistats/nistats/issues{/number}","pulls_url":"https://api.github.com/repos/nistats/nistats/pulls{/number}","milestones_url":"https://api.github.com/repos/nistats/nistats/milestones{/number}","notifications_url":"https://api.github.com/repos/nistats/nistats/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nistats/nistats/labels{/name}","releases_url":"https://api.github.com/repos/nistats/nistats/releases{/id}","deployments_url":"https://api.github.com/repos/nistats/nistats/deployments","created_at":"2015-05-13T11:44:31Z","updated_at":"2016-08-19T18:36:23Z","pushed_at":"2016-09-20T14:58:14Z","git_url":"git://github.com/nistats/nistats.git","ssh_url":"git@github.com:nistats/nistats.git","clone_url":"https://github.com/nistats/nistats.git","svn_url":"https://github.com/nistats/nistats","homepage":null,"size":447,"stargazers_count":17,"watchers_count":17,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":39,"forks":19,"open_issues":39,"watchers":17,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nistats/nistats/pulls/78"},"html":{"href":"https://github.com/nistats/nistats/pull/78"},"issue":{"href":"https://api.github.com/repos/nistats/nistats/issues/78"},"comments":{"href":"https://api.github.com/repos/nistats/nistats/issues/78/comments"},"review_comments":{"href":"https://api.github.com/repos/nistats/nistats/pulls/78/comments"},"review_comment":{"href":"https://api.github.com/repos/nistats/nistats/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nistats/nistats/pulls/78/commits"},"statuses":{"href":"https://api.github.com/repos/nistats/nistats/statuses/a26f019a2b39979012f10e213a02c1d0ff508ab4"}}}}
{"actor":{"display_login":"MartinPerez"}}