-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathneuroj.m
338 lines (318 loc) · 13.3 KB
/
neuroj.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
function [res, restapi, jsonstring] = neuroj(cmd, varargin)
%
% [data, url, rawoutput] = neuroj(command, database, dataset, attachment, ...)
%
% NeuroJSON.io client - browsing/listing/downloading/uploading data
% provided at https://neurojson.io
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% command: a string, must be one of
% 'gui':
% - start a GUI and interactively browse datasets
% 'list':
% - if followed by nothing, list all databases
% - if database is given, list its all datasets
% - if dataset is given, list all its revisions
% 'info': return metadata associated with the specified
% database, dataset or attachment of a dataset
% 'get': must provide database and dataset name, download and
% parse the specified dataset or its attachment
% 'find':
% - if database is a string '/.../', find database by a
% regular expression pattern
% - if database is a struct, find database using
% NeuroJSON's search API
% - if dataset is a string '/.../', find datasets by a
% regular expression pattern
% - if dataset is a struct, find database using
% the _find API
%
% admin commands (require database admin credentials):
% 'put': create database, create dataset under a dataset, or
% upload an attachment under a dataset
% 'delete': delete the specified attachment, dataset or
% database
% jpath: a string in the format of JSONPath, see loadjson help
%
% output:
% data: parsed response data
% url: the URL or REST-API of the desired resource
% jsonstring: the JSON raw data from the URL
%
% example:
% neuroj('gui') % start neuroj client in the GUI mode
%
% res = neuroj('list') % list all databases under res.database
% res = neuroj('list', 'cotilab') % list all dataset under res.dataset
% res = neuroj('list', 'cotilab', 'CSF_Neurophotonics_2025') % list all versions
% res = neuroj('info') % list metadata of all datasets
% res = neuroj('info', 'cotilab') % list metadata of a given database
% res = neuroj('info', 'cotilab', 'CSF_Neurophotonics_2025') % list dataset header
% res = neuroj('info', 'cotilab', 'CSF_Neurophotonics_2025') % list dataset header
% [res, url, rawstr] = neuroj('get', 'cotilab', 'CSF_Neurophotonics_2025')
% res = neuroj('get', 'cotilab', 'CSF_Neurophotonics_2025')
% userinfo = inputdlg({'Username:', 'Password:'});
% options = {'UserName', userinfo{1}, 'Password', userinfo{2});
% res = neuroj('put', 'sandbox1d', 'newdoc', struct('Author', 'QF') 'weboptions', options);
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%
if (nargin == 0)
disp('NeuroJSON.io Client (https://neurojson.io)');
fprintf('Format:\n\t[data, restapi] = neuroj(command, database, dataset, attachment, ...)\n\n');
return
end
if (nargin == 1 && strcmp(cmd, 'gui'))
handles.fmMain = figure('numbertitle', 'off', 'name', 'NeuroJSON.io Dataset Browser');
tbTool = uitoolbar(handles.fmMain);
handles.lsDb = uicontrol(handles.fmMain, 'tooltipstring', 'Database', 'style', 'listbox', 'units', 'normalized', 'position', [0 0 1 / 5 1]);
handles.lsDs = uicontrol(handles.fmMain, 'tooltipstring', 'Dataset', 'style', 'listbox', 'units', 'normalized', 'position', [1 / 5 0 1 / 4 1]);
handles.lsJSON = uicontrol(handles.fmMain, 'tooltipstring', 'Data', 'style', 'listbox', 'units', 'normalized', 'position', [9 / 20 1 / 4 1 - 9 / 20 3 / 4]);
handles.txValue = uicontrol(handles.fmMain, 'tooltipstring', 'Value', 'style', 'edit', 'max', 50, 'HorizontalAlignment', 'left', 'units', 'normalized', 'position', [9 / 20 0 1 - 9 / 20 1 / 4]);
handles.t0 = cputime;
handles.hbox = msgbox('Loading data', 'modal');
set(handles.hbox, 'visible', 'off');
set(handles.fmMain, 'userdata', handles);
set(handles.lsDb, 'Callback', @(src, events) loadds(src, events, handles.fmMain));
set(handles.lsDs, 'Callback', @(src, events) loaddsdata(src, events, handles.fmMain));
set(handles.lsJSON, 'Callback', @(src, events) expandjsontree(src, events, handles.fmMain));
btLoadDb = uipushtool(tbTool, 'tooltipstring', 'List databases', 'ClickedCallback', @(src, events) loaddb(src, events, handles.fmMain));
if (~isoctavemesh)
btLoadDb.CData = zeros(40, 40, 3);
end
return
end
dbname = '';
if (~isempty(varargin))
dbname = varargin{1};
end
dsname = '';
if (length(varargin) > 1)
dsname = varargin{2};
end
attachment = '';
if (length(varargin) > 2)
attachment = varargin{3};
end
opt = struct;
if (length(varargin) > 3)
opt = varargin2struct(varargin{4:end});
end
serverurl = getenv('NEUROJSON_IO');
if (isempty(serverurl))
serverurl = 'https://neurojson.io:7777/';
end
serverurl = jsonopt('server', serverurl, opt);
options = jsonopt('weboptions', {}, opt);
opt.weboptions = options;
rev = jsonopt('rev', '', opt);
cmd = lower(cmd);
restapi = serverurl;
if (strcmp(cmd, 'list'))
restapi = [serverurl, 'sys/registry'];
if (~isempty(dbname))
restapi = [serverurl, dbname, '/', '_all_docs'];
if (~isempty(dsname))
restapi = [serverurl, dbname, '/', dsname, '?revs_info=true'];
end
end
jsonstring = loadjson(restapi, opt, 'raw', 1);
res = loadjson(jsonstring, opt);
if (~isempty(dsname))
res = res.(encodevarname('_revs_info'));
elseif (~isempty(dbname))
res.dataset = res.rows;
res = rmfield(res, 'rows');
end
elseif (strcmp(cmd, 'info'))
restapi = [serverurl, '_dbs_info'];
if (~isempty(dbname))
restapi = [serverurl, dbname, '/'];
if (~isempty(dsname))
restapi = [serverurl, dbname, '/', dsname];
if (~isempty(attachment))
restapi = [serverurl, dbname, '/', dsname, '/', attachment];
end
end
end
if (~isempty(dsname) || ~isempty(attachment))
res = loadjson(restapi, opt, 'header', 1);
jsonstring = savejson('', res);
else
jsonstring = loadjson(restapi, opt, 'raw', 1);
res = loadjson(jsonstring);
end
elseif (strcmp(cmd, 'get'))
if (isempty(dsname))
error('get requires a dataset, i.e. document, name');
end
if (isempty(attachment))
restapi = [serverurl, dbname, '/', dsname];
if (~isempty(rev))
restapi = [serverurl, dbname, '/', dsname, '?rev=' rev];
end
jsonstring = loadjson(restapi, opt, 'raw', 1);
res = loadjson(jsonstring, opt);
else
restapi = [serverurl, dbname, '/', dsname, '/', attachment];
[res, jsonstring] = jdlink(restapi);
end
elseif (strcmp(cmd, 'put'))
if (isempty(dbname))
error('put requires at least a database name');
end
restapi = [serverurl, dbname];
putoption = weboptions(opt.weboptions{:});
putoption.RequestMethod = 'post';
putoption.MediaType = 'application/json';
if (~isempty(dsname))
if (isempty(attachment))
error('must provide JSON input to upload');
end
if (ischar(attachment) && exist(attachment, 'file'))
[afpath, afname, afext] = fileparts(attachment);
attname = jsonopt('filename', [afname, afext], opt);
restapi = [serverurl, dbname, '/' dsname '/' attname];
res = websave(attname, restapi, weboptions('RequestMethod', 'put'));
else
restapi = [serverurl, dbname, '/_design/qq/_update/timestamp/' dsname];
jsonstring = savejson('', attachment, 'compact', 1);
res = webwrite(restapi, jsonstring, putoption);
end
else
putoption.RequestMethod = 'put';
res = webwrite(restapi, [], putoption);
end
elseif (strcmp(cmd, 'delete'))
if (isempty(dbname))
error('put requires at least a database name');
end
deloption = weboptions(opt.weboptions{:});
deloption.RequestMethod = 'delete';
restapi = [serverurl, dbname];
if (~isempty(dsname))
restapi = [serverurl, dbname, '/', dsname];
if (~isempty(attachment))
restapi = [serverurl, dbname, '/', dsname, '/', attachment];
end
end
res = webwrite(restapi, [], deloption);
elseif (strcmp(cmd, 'find'))
if (isempty(dbname))
error('find requires at least a search regular expression pattern');
end
if (~isempty(dbname))
if (ischar(dsname) && dbname(1) == '/' && dbname(end) == '/')
[dblist, restapi, jsonstring] = neuroj('list');
res = {};
for i = 1:length(dblist.database)
if (~isempty(regexpi(savejson('', dblist.database{i}, 'compact', 1), dbname(2:end - 1), 'once')))
res{end + 1} = dblist.database{i};
end
end
elseif (isstruct(dbname))
param = join(cellfun(@(x) [x '=' dbname.(x)], fieldnames(dbname), 'UniformOutput', false));
restapi = ['https://neurojson.org/io/search.cgi?' param{:}];
jsonstring = loadjson(restapi, opt, 'raw', 1);
res = loadjson(jsonstrong, opt);
elseif (~isempty(dsname) && ischar(dsname) && dsname(1) == '/' && dsname(end) == '/')
[dslist, restapi, jsonstring] = neuroj('list', dbname);
res = {};
for i = 1:length(dslist.dataset)
if (~isempty(regexpi(dslist.dataset(i).id, dsname(2:end - 1), 'once')))
res{end + 1} = dslist.dataset(i).id;
end
end
elseif (~isempty(dsname) && (isstruct(dsname) || (ischar(dsname) && dsname(1) == '{' && dsname(end) == '}')))
findoption = weboptions(opt.weboptions{:});
findoption.RequestMethod = 'post';
findoption.MediaType = 'application/json';
restapi = [serverurl, dbname, '/_find'];
if (isstruct(dsname))
if (~isfield(dsname, 'selector'))
dsname.selector = {};
end
res = webwrite(restapi, savejson('', dsname, 'compact', 1), findoption);
else
res = webwrite(restapi, dsname, findoption);
end
end
end
end
function loaddb(src, event, hwin)
handles = get(hwin, 'userdata');
set(handles.hbox, 'visible', 'on');
dbs = neuroj('list');
set(handles.lsDb, 'String', (cellfun(@(x) x.id, dbs.database, 'UniformOutput', false)));
set(handles.hbox, 'visible', 'off');
function loadds(src, event, hwin)
handles = get(hwin, 'userdata');
set(handles.hbox, 'visible', 'on');
if (isfield(event, 'Key') && strcmp(event.Key, 'enter')) || strcmp(get(handles.fmMain, 'SelectionType'), 'open') || (cputime - handles.t0) < 0.01
idx = get(src, 'value');
dbs = get(src, 'string');
dslist = neuroj('list', dbs{idx});
dslist.dataset = dslist.dataset(arrayfun(@(x) x.id(1) ~= '_', dslist.dataset));
set(handles.lsDs, 'string', {dslist.dataset.id}, 'value', 1);
set(handles.lsDb, 'tag', dbs{idx});
end
handles.t0 = cputime;
set(hwin, 'userdata', handles);
set(handles.hbox, 'visible', 'off');
function loaddsdata(src, event, hwin)
handles = get(hwin, 'userdata');
set(handles.hbox, 'visible', 'on');
if isfield(event, 'Key') && strcmp(event.Key, 'enter') || strcmp(get(handles.fmMain, 'SelectionType'), 'open') || (cputime - handles.t0) < 0.01
idx = get(src, 'value');
dbs = get(src, 'string');
dbid = get(handles.lsDb, 'tag');
datasets = jdict(neuroj('get', dbid, dbs{idx}));
set(handles.lsJSON, 'string', cellfun(@(x) decodevarname(x), datasets.keys(), 'UniformOutput', false), 'value', 1);
set(handles.lsJSON, 'userdata', datasets);
set(handles.lsJSON, 'tag', '');
end
handles.t0 = cputime;
set(hwin, 'userdata', handles);
set(handles.hbox, 'visible', 'off');
function expandjsontree(src, event, hwin)
handles = get(hwin, 'userdata');
if (~isa(get(handles.lsJSON, 'userdata'), 'jdict'))
return
end
set(handles.hbox, 'visible', 'on');
if isfield(event, 'Key') && strcmp(event.Key, 'enter') || strcmp(get(handles.fmMain, 'SelectionType'), 'open') || (cputime - handles.t0) < 0.01
idx = get(src, 'value');
dbs = get(src, 'string');
rootpath = get(handles.lsJSON, 'tag');
datasets = get(handles.lsJSON, 'userdata');
if (isempty(rootpath))
rootpath = '$';
end
if (strcmp(dbs{idx}, '..'))
rootpath = regexprep(rootpath, '\[[^\]]+\]$', '');
else
rootpath = [rootpath '["' dbs{idx} '"]'];
end
datasets = datasets.(rootpath);
try
if (iscell(datasets.keys()))
subitem = cellfun(@(x) decodevarname(x), datasets.keys(), 'UniformOutput', false);
if (~strcmp(rootpath, '$'))
subitem = {'..', subitem{:}};
end
set(handles.lsJSON, 'string', subitem, 'value', 1);
set(handles.lsJSON, 'tag', rootpath);
else
set(handles.txValue, 'string', datasets.v());
end
catch
end
end
handles.t0 = cputime;
set(hwin, 'userdata', handles);
set(handles.hbox, 'visible', 'off');