{"action":"created","comment":{"url":"https://api.github.com/repos/inanhkn/analysis/pulls/comments/37605219","id":37605219,"diff_hunk":"@@ -0,0 +1,326 @@\n+function inv_quality = extract_cells(movie_source,pca_source,max_num)\n+\n+%   Cell extraction method that is based on an algorithm that essentially\n+%   solves the sparse source separation problem to find cell filters.\n+%   Traces are then found by using least squares.\n+%\n+%   Inputs:\n+%\n+%       movie_source: path to the movie matrix (height x width x frames)\n+%\n+%       pca_sources: path to the pca .mat file. It is assumed that there is\n+%       a variable named pca_filters that has size [# of PCs x # of pixels]\n+%       in the .mat file.\n+%       \n+%       max_num = Maximum estimated number of cells. Set to inf to get all\n+%       that the algorithm can find in the movie.\n+%\n+%   Outputs:\n+%\n+%       Script saves the traces and filters in a .mat file.\n+%\n+%       inv_quality: Inverse quality metric that indicates how good each\n+%       found potential is as far as the extraction algorithm is concerned.\n+%       It is a [1 x # of extracted cells] array whose entries are\n+%       normalized to [(1/# of pixels),1] range. This array is also plotted\n+%       on the screen at the end of execution.\n+%\n+%   Hakan Inan (Aug 2015)\n+%\n+\n+% Some parameters\n+maxsize_chunk_GPU = 500;\n+maxsize_chunk_CPU = 3000;\n+corr_thresh = 0.1;\n+filter_thresh = 0.25;\n+\n+max_num = min(max_num,1000); % Internal control for max_num\n+\n+fprintf('%s : Loading movie singular vectors...\\n',datestr(now));\n+\n+load(pca_source,'pca_info','pca_filters');\n+is_trimmed = pca_info.trim.enabled;\n+h = pca_info.movie_height;\n+w = pca_info.movie_width;\n+t = pca_info.movie_frames;\n+\n+if is_trimmed\n+    load(pca_source,'idx_kept');\n+end\n+\n+fprintf('%s : Extracting filters...\\n',datestr(now));\n+\n+U = (pca_filters');\n+[N,num_pcs] = size(U);\n+norms_U = sqrt(sum(U.^2,2));\n+U_norm = bsxfun(@times,U,1./norms_U);\n+\n+% Brute-force approach for 1-norm computation, use GPU if available\n+use_gpu = 0;\n+gpu_exists = gpuDeviceCount>0;\n+if gpu_exists\n+    D = gpuDevice;\n+    % Handle 2 different versions of gpuDevice\n+    if isprop(D,'AvailableMemory')\n+        avail_mem = D.AvailableMemory;\n+    elseif isprop(D,'FreeMemory')\n+        avail_mem = D.FreeMemory;\n+    else \n+        avail_mem = 0;\n+    end\n+    \n+    f = max(avail_mem/4 - N*(num_pcs+2),0); % available data size assuming single type\n+    f = f-f/10; % Leave some buffer space in GPU\n+    chunk_size = floor(f/ (2*N+num_pcs));\n+    use_gpu = chunk_size>=1;\n+end\n+\n+if use_gpu %GPU\n+    fprintf('\\t \\t \\t Using GPU to accelarate processing\\n');\n+    U = gpuArray(U);\n+    one_norms = gpuArray(zeros(1,N,'single'));\n+    norms_U = gpuArray(norms_U);\n+    chunk_size = min(chunk_size,maxsize_chunk_GPU); % Set upper limit\n+    \n+    for i = 1:chunk_size:N\n+        fin = min(chunk_size-1,N-i);\n+        Q = bsxfun(@times,U(i:i+fin,:),1./norms_U(i:i+fin))';\n+        one_norms(i:i+fin) = sum(abs(U*Q),1);\n+        clear Q;\n+    end\n+\n+    % Transfer variables back to CPU\n+    U = gather(U);\n+    one_norms = gather(one_norms);\n+    norms_U = gather(norms_U);\n+    \n+else % CPU\n+    chunk_size = maxsize_chunk_CPU;\n+    one_norms = zeros(1,N);\n+    for i = 1:chunk_size:N\n+        fin = min(chunk_size-1,N-i);\n+        Q = U_norm(i:i+fin,:)';\n+        one_norms(i:i+fin) = sum(abs(U*Q),1);\n+        clear Q;\n+    end\n+\n+end\n+\n+% Extract filters recursively using the 1-norm criterion\n+idx_possible = 1:N;\n+F = zeros(N,max_num);\n+inv_quality = zeros(1,max_num);\n+acc=0;\n+\n+while true\n+    acc = acc+1;\n+    [val,idx_this] = min(one_norms(idx_possible));\n+    idx_this = idx_possible(idx_this);\n+    sig_this = U*U_norm(idx_this,:)';    \n+    correl = sig_this.*(1./norms_U);\n+    idx_possible = intersect(find(abs(correl)<corr_thresh),idx_possible);\n+    F(:,acc) = sig_this;\n+    inv_quality(:,acc) = val;\n+    % Termination condition\n+    if isempty(idx_possible) || acc==max_num\n+        break;\n+    end\n+    \n+end\n+\n+% Truncate F in case there are less components than max_num\n+F = F(:,1:acc);\n+inv_quality = inv_quality(1:acc)/sqrt(N);\n+\n+if is_trimmed % untrim the pixels\n+    F_temp = zeros(h*w,acc);\n+    F_temp(idx_kept,:) = F;\n+    F = F_temp;\n+end\n+\n+fprintf('\\t \\t \\t Extracted %d potential cells \\n',acc);\n+fprintf('%s : Cleaning filters and removing duplicates...\\n',datestr(now));\n+[F,idx] = modify_filters(F,filter_thresh);\n+inv_quality = inv_quality(idx);\n+\n+figure,\n+plot(inv_quality,'LineWidth',1.5);\n+title('Inverse quality metric for the extracted potential cells','Fontsize',16);\n+xlabel('Cell index','Fontsize',16)\n+ylabel('Inverse quality(normalized to 1)','Fontsize',16)\n+\n+fprintf('%s : Loading movie for trace extraction...\\n',datestr(now));\n+M = load_movie(movie_source);\n+\n+% Extract time traces\n+fprintf('%s : Extracting traces...\\n',datestr(now));\n+M = reshape(M,h*w,t);\n+idx_nonzero = find(sum(F,2)>0);\n+M_small = M(idx_nonzero,:);\n+F_small = F(idx_nonzero,:);\n+T = (F_small'*F_small+0*eye(size(F,2))) \\  (F_small'*M_small);\n+clear M;\n+\n+% Reconstruction settings\n+info.type = 'simple';","path":"pca_ica/extract_cells.m","position":165,"original_position":165,"commit_id":"0e51cbffb26c4c17cde4d2b5125737c4e1e21573","original_commit_id":"0e51cbffb26c4c17cde4d2b5125737c4e1e21573","user":{"login":"kimtonyhyun","id":2081503,"avatar_url":"https://avatars.githubusercontent.com/u/2081503?v=3","gravatar_id":"","url":"https://api.github.com/users/kimtonyhyun","html_url":"https://github.com/kimtonyhyun","followers_url":"https://api.github.com/users/kimtonyhyun/followers","following_url":"https://api.github.com/users/kimtonyhyun/following{/other_user}","gists_url":"https://api.github.com/users/kimtonyhyun/gists{/gist_id}","starred_url":"https://api.github.com/users/kimtonyhyun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kimtonyhyun/subscriptions","organizations_url":"https://api.github.com/users/kimtonyhyun/orgs","repos_url":"https://api.github.com/users/kimtonyhyun/repos","events_url":"https://api.github.com/users/kimtonyhyun/events{/privacy}","received_events_url":"https://api.github.com/users/kimtonyhyun/received_events","type":"User","site_admin":false},"body":"We need to come up with a different name here.","created_at":"2015-08-21T04:07:22Z","updated_at":"2015-08-21T04:07:22Z","html_url":"https://github.com/inanhkn/analysis/pull/101#discussion_r37605219","pull_request_url":"https://api.github.com/repos/inanhkn/analysis/pulls/101","_links":{"self":{"href":"https://api.github.com/repos/inanhkn/analysis/pulls/comments/37605219"},"html":{"href":"https://github.com/inanhkn/analysis/pull/101#discussion_r37605219"},"pull_request":{"href":"https://api.github.com/repos/inanhkn/analysis/pulls/101"}}},"pull_request":{"url":"https://api.github.com/repos/inanhkn/analysis/pulls/101","id":42405928,"html_url":"https://github.com/inanhkn/analysis/pull/101","diff_url":"https://github.com/inanhkn/analysis/pull/101.diff","patch_url":"https://github.com/inanhkn/analysis/pull/101.patch","issue_url":"https://api.github.com/repos/inanhkn/analysis/issues/101","number":101,"state":"open","locked":false,"title":"Inanhkn/extract cells","user":{"login":"inanhkn","id":5778377,"avatar_url":"https://avatars.githubusercontent.com/u/5778377?v=3","gravatar_id":"","url":"https://api.github.com/users/inanhkn","html_url":"https://github.com/inanhkn","followers_url":"https://api.github.com/users/inanhkn/followers","following_url":"https://api.github.com/users/inanhkn/following{/other_user}","gists_url":"https://api.github.com/users/inanhkn/gists{/gist_id}","starred_url":"https://api.github.com/users/inanhkn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inanhkn/subscriptions","organizations_url":"https://api.github.com/users/inanhkn/orgs","repos_url":"https://api.github.com/users/inanhkn/repos","events_url":"https://api.github.com/users/inanhkn/events{/privacy}","received_events_url":"https://api.github.com/users/inanhkn/received_events","type":"User","site_admin":false},"body":"The cell extraction routine that first finds filters solving the source separation problem and then finds traces with Least Squares.  Call it as:\r\n\r\n`extract_cells('movie file path','pca*.mat file path',[maximum estimated number of cells])`\r\n\r\nThe script outputs a `rec*.mat` file that contains filters and traces. ","created_at":"2015-08-13T22:17:17Z","updated_at":"2015-08-21T04:07:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"80a20191030edac38a6e435cee3204632265f13c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/inanhkn/analysis/pulls/101/commits","review_comments_url":"https://api.github.com/repos/inanhkn/analysis/pulls/101/comments","review_comment_url":"https://api.github.com/repos/inanhkn/analysis/pulls/comments{/number}","comments_url":"https://api.github.com/repos/inanhkn/analysis/issues/101/comments","statuses_url":"https://api.github.com/repos/inanhkn/analysis/statuses/0e51cbffb26c4c17cde4d2b5125737c4e1e21573","head":{"label":"inanhkn:inanhkn/extract_cells","ref":"inanhkn/extract_cells","sha":"0e51cbffb26c4c17cde4d2b5125737c4e1e21573","user":{"login":"inanhkn","id":5778377,"avatar_url":"https://avatars.githubusercontent.com/u/5778377?v=3","gravatar_id":"","url":"https://api.github.com/users/inanhkn","html_url":"https://github.com/inanhkn","followers_url":"https://api.github.com/users/inanhkn/followers","following_url":"https://api.github.com/users/inanhkn/following{/other_user}","gists_url":"https://api.github.com/users/inanhkn/gists{/gist_id}","starred_url":"https://api.github.com/users/inanhkn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inanhkn/subscriptions","organizations_url":"https://api.github.com/users/inanhkn/orgs","repos_url":"https://api.github.com/users/inanhkn/repos","events_url":"https://api.github.com/users/inanhkn/events{/privacy}","received_events_url":"https://api.github.com/users/inanhkn/received_events","type":"User","site_admin":false},"repo":{"id":27896702,"name":"analysis","full_name":"inanhkn/analysis","owner":{"login":"inanhkn","id":5778377,"avatar_url":"https://avatars.githubusercontent.com/u/5778377?v=3","gravatar_id":"","url":"https://api.github.com/users/inanhkn","html_url":"https://github.com/inanhkn","followers_url":"https://api.github.com/users/inanhkn/followers","following_url":"https://api.github.com/users/inanhkn/following{/other_user}","gists_url":"https://api.github.com/users/inanhkn/gists{/gist_id}","starred_url":"https://api.github.com/users/inanhkn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inanhkn/subscriptions","organizations_url":"https://api.github.com/users/inanhkn/orgs","repos_url":"https://api.github.com/users/inanhkn/repos","events_url":"https://api.github.com/users/inanhkn/events{/privacy}","received_events_url":"https://api.github.com/users/inanhkn/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/inanhkn/analysis","description":"","fork":false,"url":"https://api.github.com/repos/inanhkn/analysis","forks_url":"https://api.github.com/repos/inanhkn/analysis/forks","keys_url":"https://api.github.com/repos/inanhkn/analysis/keys{/key_id}","collaborators_url":"https://api.github.com/repos/inanhkn/analysis/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/inanhkn/analysis/teams","hooks_url":"https://api.github.com/repos/inanhkn/analysis/hooks","issue_events_url":"https://api.github.com/repos/inanhkn/analysis/issues/events{/number}","events_url":"https://api.github.com/repos/inanhkn/analysis/events","assignees_url":"https://api.github.com/repos/inanhkn/analysis/assignees{/user}","branches_url":"https://api.github.com/repos/inanhkn/analysis/branches{/branch}","tags_url":"https://api.github.com/repos/inanhkn/analysis/tags","blobs_url":"https://api.github.com/repos/inanhkn/analysis/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/inanhkn/analysis/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/inanhkn/analysis/git/refs{/sha}","trees_url":"https://api.github.com/repos/inanhkn/analysis/git/trees{/sha}","statuses_url":"https://api.github.com/repos/inanhkn/analysis/statuses/{sha}","languages_url":"https://api.github.com/repos/inanhkn/analysis/languages","stargazers_url":"https://api.github.com/repos/inanhkn/analysis/stargazers","contributors_url":"https://api.github.com/repos/inanhkn/analysis/contributors","subscribers_url":"https://api.github.com/repos/inanhkn/analysis/subscribers","subscription_url":"https://api.github.com/repos/inanhkn/analysis/subscription","commits_url":"https://api.github.com/repos/inanhkn/analysis/commits{/sha}","git_commits_url":"https://api.github.com/repos/inanhkn/analysis/git/commits{/sha}","comments_url":"https://api.github.com/repos/inanhkn/analysis/comments{/number}","issue_comment_url":"https://api.github.com/repos/inanhkn/analysis/issues/comments{/number}","contents_url":"https://api.github.com/repos/inanhkn/analysis/contents/{+path}","compare_url":"https://api.github.com/repos/inanhkn/analysis/compare/{base}...{head}","merges_url":"https://api.github.com/repos/inanhkn/analysis/merges","archive_url":"https://api.github.com/repos/inanhkn/analysis/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/inanhkn/analysis/downloads","issues_url":"https://api.github.com/repos/inanhkn/analysis/issues{/number}","pulls_url":"https://api.github.com/repos/inanhkn/analysis/pulls{/number}","milestones_url":"https://api.github.com/repos/inanhkn/analysis/milestones{/number}","notifications_url":"https://api.github.com/repos/inanhkn/analysis/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/inanhkn/analysis/labels{/name}","releases_url":"https://api.github.com/repos/inanhkn/analysis/releases{/id}","created_at":"2014-12-11T23:21:49Z","updated_at":"2015-04-28T16:46:49Z","pushed_at":"2015-08-21T00:46:16Z","git_url":"git://github.com/inanhkn/analysis.git","ssh_url":"git@github.com:inanhkn/analysis.git","clone_url":"https://github.com/inanhkn/analysis.git","svn_url":"https://github.com/inanhkn/analysis","homepage":null,"size":1944,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":6,"forks":0,"open_issues":6,"watchers":0,"default_branch":"master"}},"base":{"label":"inanhkn:master","ref":"master","sha":"3decbe51f3d6818026c0cf3741fa00142c3a99f3","user":{"login":"inanhkn","id":5778377,"avatar_url":"https://avatars.githubusercontent.com/u/5778377?v=3","gravatar_id":"","url":"https://api.github.com/users/inanhkn","html_url":"https://github.com/inanhkn","followers_url":"https://api.github.com/users/inanhkn/followers","following_url":"https://api.github.com/users/inanhkn/following{/other_user}","gists_url":"https://api.github.com/users/inanhkn/gists{/gist_id}","starred_url":"https://api.github.com/users/inanhkn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inanhkn/subscriptions","organizations_url":"https://api.github.com/users/inanhkn/orgs","repos_url":"https://api.github.com/users/inanhkn/repos","events_url":"https://api.github.com/users/inanhkn/events{/privacy}","received_events_url":"https://api.github.com/users/inanhkn/received_events","type":"User","site_admin":false},"repo":{"id":27896702,"name":"analysis","full_name":"inanhkn/analysis","owner":{"login":"inanhkn","id":5778377,"avatar_url":"https://avatars.githubusercontent.com/u/5778377?v=3","gravatar_id":"","url":"https://api.github.com/users/inanhkn","html_url":"https://github.com/inanhkn","followers_url":"https://api.github.com/users/inanhkn/followers","following_url":"https://api.github.com/users/inanhkn/following{/other_user}","gists_url":"https://api.github.com/users/inanhkn/gists{/gist_id}","starred_url":"https://api.github.com/users/inanhkn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inanhkn/subscriptions","organizations_url":"https://api.github.com/users/inanhkn/orgs","repos_url":"https://api.github.com/users/inanhkn/repos","events_url":"https://api.github.com/users/inanhkn/events{/privacy}","received_events_url":"https://api.github.com/users/inanhkn/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/inanhkn/analysis","description":"","fork":false,"url":"https://api.github.com/repos/inanhkn/analysis","forks_url":"https://api.github.com/repos/inanhkn/analysis/forks","keys_url":"https://api.github.com/repos/inanhkn/analysis/keys{/key_id}","collaborators_url":"https://api.github.com/repos/inanhkn/analysis/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/inanhkn/analysis/teams","hooks_url":"https://api.github.com/repos/inanhkn/analysis/hooks","issue_events_url":"https://api.github.com/repos/inanhkn/analysis/issues/events{/number}","events_url":"https://api.github.com/repos/inanhkn/analysis/events","assignees_url":"https://api.github.com/repos/inanhkn/analysis/assignees{/user}","branches_url":"https://api.github.com/repos/inanhkn/analysis/branches{/branch}","tags_url":"https://api.github.com/repos/inanhkn/analysis/tags","blobs_url":"https://api.github.com/repos/inanhkn/analysis/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/inanhkn/analysis/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/inanhkn/analysis/git/refs{/sha}","trees_url":"https://api.github.com/repos/inanhkn/analysis/git/trees{/sha}","statuses_url":"https://api.github.com/repos/inanhkn/analysis/statuses/{sha}","languages_url":"https://api.github.com/repos/inanhkn/analysis/languages","stargazers_url":"https://api.github.com/repos/inanhkn/analysis/stargazers","contributors_url":"https://api.github.com/repos/inanhkn/analysis/contributors","subscribers_url":"https://api.github.com/repos/inanhkn/analysis/subscribers","subscription_url":"https://api.github.com/repos/inanhkn/analysis/subscription","commits_url":"https://api.github.com/repos/inanhkn/analysis/commits{/sha}","git_commits_url":"https://api.github.com/repos/inanhkn/analysis/git/commits{/sha}","comments_url":"https://api.github.com/repos/inanhkn/analysis/comments{/number}","issue_comment_url":"https://api.github.com/repos/inanhkn/analysis/issues/comments{/number}","contents_url":"https://api.github.com/repos/inanhkn/analysis/contents/{+path}","compare_url":"https://api.github.com/repos/inanhkn/analysis/compare/{base}...{head}","merges_url":"https://api.github.com/repos/inanhkn/analysis/merges","archive_url":"https://api.github.com/repos/inanhkn/analysis/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/inanhkn/analysis/downloads","issues_url":"https://api.github.com/repos/inanhkn/analysis/issues{/number}","pulls_url":"https://api.github.com/repos/inanhkn/analysis/pulls{/number}","milestones_url":"https://api.github.com/repos/inanhkn/analysis/milestones{/number}","notifications_url":"https://api.github.com/repos/inanhkn/analysis/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/inanhkn/analysis/labels{/name}","releases_url":"https://api.github.com/repos/inanhkn/analysis/releases{/id}","created_at":"2014-12-11T23:21:49Z","updated_at":"2015-04-28T16:46:49Z","pushed_at":"2015-08-21T00:46:16Z","git_url":"git://github.com/inanhkn/analysis.git","ssh_url":"git@github.com:inanhkn/analysis.git","clone_url":"https://github.com/inanhkn/analysis.git","svn_url":"https://github.com/inanhkn/analysis","homepage":null,"size":1944,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":6,"forks":0,"open_issues":6,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/inanhkn/analysis/pulls/101"},"html":{"href":"https://github.com/inanhkn/analysis/pull/101"},"issue":{"href":"https://api.github.com/repos/inanhkn/analysis/issues/101"},"comments":{"href":"https://api.github.com/repos/inanhkn/analysis/issues/101/comments"},"review_comments":{"href":"https://api.github.com/repos/inanhkn/analysis/pulls/101/comments"},"review_comment":{"href":"https://api.github.com/repos/inanhkn/analysis/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/inanhkn/analysis/pulls/101/commits"},"statuses":{"href":"https://api.github.com/repos/inanhkn/analysis/statuses/0e51cbffb26c4c17cde4d2b5125737c4e1e21573"}}}}