Skip to content

Commit abca3f2

Browse files
Add option for showing sleep stage buttons and retrieve and save annotations through Response model
1 parent 0774976 commit abca3f2

File tree

3 files changed

+259
-99
lines changed

3 files changed

+259
-99
lines changed

bundle.js

Lines changed: 145 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
550550
idleTimeThresholdSeconds: 300,
551551
experiment: {},
552552
showArtifactButtons: false,
553+
showSleepStageButtons: false,
553554
showNavigationButtons: true,
554555
showBackToLastActiveWindowButton: true,
555556
showFastBackwardButton: true,
@@ -1061,6 +1062,13 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
10611062
<button type="button" class="btn btn-default artifact" data-annotation-type="artifacts_medium">Medium Artifacts</button> \
10621063
<button type="button" class="btn btn-default artifact" data-annotation-type="artifacts_strong">Strong Artifacts</button> \
10631064
</div> \
1065+
<div class="sleep_stage_panel btn-group" role="group"> \
1066+
<button type="button" class="btn btn-default grey sleep_stage" data-annotation-type="sleep_stage_wake">Wake</button> \
1067+
<button type="button" class="btn btn-default grey sleep_stage" data-annotation-type="sleep_stage_n1">N1</button> \
1068+
<button type="button" class="btn btn-default grey sleep_stage" data-annotation-type="sleep_stage_n2">N2</button> \
1069+
<button type="button" class="btn btn-default grey sleep_stage" data-annotation-type="sleep_stage_n3">N3</button> \
1070+
<button type="button" class="btn btn-default grey sleep_stage" data-annotation-type="sleep_stage_rem">REM</button> \
1071+
</div> \
10641072
<div class="navigation_panel"> \
10651073
<button type="button" class="btn btn-default backToLastActiveWindow" aria-label="Back to Last Active Window"> \
10661074
<span class="fa fa-repeat" aria-hidden="true"></span> \
@@ -1128,6 +1136,9 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
11281136
if (!that.options.showArtifactButtons) {
11291137
$(that.element).find('.artifact_panel').hide();
11301138
}
1139+
if (!that.options.showSleepStageButtons) {
1140+
$(that.element).find('.sleep_stage_panel').hide();
1141+
}
11311142
if (!that.options.showNavigationButtons) {
11321143
$(that.element).find('.navigation_panel').hide();
11331144
}
@@ -1584,6 +1595,7 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
15841595
that._setupFeaturePanel();
15851596
that._setupNavigationPanel();
15861597
that._setupArtifactPanel();
1598+
that._setupSleepStagePanel();
15871599
that._setupTrainingPhase();
15881600
that._getUserStatus();
15891601
},
@@ -1762,6 +1774,23 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
17621774
});
17631775
},
17641776

1777+
_setupSleepStagePanel: function() {
1778+
var activeClass = 'teal';
1779+
var inactiveClass = 'grey';
1780+
var that = this;
1781+
$(that.element).find('.sleep_stage_panel button.sleep_stage').click(function() {
1782+
var button = $(this);
1783+
var type = button.data('annotation-type');
1784+
that._saveSleepStageAnnotation(type);
1785+
button
1786+
.addClass(activeClass)
1787+
.removeClass(inactiveClass)
1788+
.siblings()
1789+
.removeClass(activeClass)
1790+
.addClass(inactiveClass);
1791+
});
1792+
},
1793+
17651794
_setupNavigationPanel: function() {
17661795
var that = this;
17671796
that._setForwardEnabledStatus(false);
@@ -2745,6 +2774,11 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
27452774
that._saveFullWindowLabel('ARTIFACT', type);
27462775
},
27472776

2777+
_saveSleepStageAnnotation: function(type) {
2778+
var that = this;
2779+
that._saveFullWindowLabel('SLEEP_STAGE', type);
2780+
},
2781+
27482782
_getAnnotationXMinFixed: function(annotation) {
27492783
return parseFloat(annotation.options.xValue).toFixed(2);
27502784
},
@@ -3165,35 +3199,38 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
31653199
that._incrementNumberOfAnnotationsInCurrentWindow(that._getVisibleAnnotations(data.annotations).length);
31663200
}
31673201
that._displayArtifactsSelection(data.annotations);
3202+
that._displaySleepStageSelection(data.annotations);
31683203
return;
31693204
}
31703205
that.vars.annotationsCache[cacheKey] = {
31713206
annotations: [],
31723207
};
31733208
that.vars.annotationsLoaded = true;
3174-
// that._getApiClient().annotation().getForContext(function(annotations, error) {
3175-
// if (error) {
3176-
// that.vars.annotationsLoaded = false;
3177-
// console.log(error.message);
3178-
// return;
3179-
// }
3180-
// annotations.forEach(function(annotation) {
3181-
// var annotationWindowStart = Math.floor(annotation.position.start / that.options.windowSizeInSeconds) * that.options.windowSizeInSeconds;
3182-
// var annotationWindowEnd = annotationWindowStart + that.options.windowSizeInSeconds;
3183-
// var annotationCacheKey = that._getAnnotationsCacheKey(recording_name, annotationWindowStart, annotationWindowEnd, correctAnswers);
3184-
// if (!that.vars.annotationsCache[annotationCacheKey]) {
3185-
// that.vars.annotationsCache[annotationCacheKey] = {
3186-
// annotations: [],
3187-
// };
3188-
// }
3189-
// that.vars.annotationsCache[annotationCacheKey].annotations.push(annotation);
3190-
// });
3191-
// var data = that.vars.annotationsCache[cacheKey] || {
3192-
// annotations: [],
3193-
// };
3194-
// that._displayArtifactsSelection(annotations);
3195-
// that._displayAnnotations(annotations);
3196-
// });
3209+
that._getApiClient().list('response', {}, function(response) {
3210+
if (!response.results) {
3211+
return;
3212+
}
3213+
var annotations = response.results.map(function(annotation) {
3214+
return annotation.content;
3215+
});
3216+
annotations.forEach(function(annotation) {
3217+
var annotationWindowStart = Math.floor(annotation.position.start / that.options.windowSizeInSeconds) * that.options.windowSizeInSeconds;
3218+
var annotationWindowEnd = annotationWindowStart + that.options.windowSizeInSeconds;
3219+
var annotationCacheKey = that._getAnnotationsCacheKey(recording_name, annotationWindowStart, annotationWindowEnd, correctAnswers);
3220+
if (!that.vars.annotationsCache[annotationCacheKey]) {
3221+
that.vars.annotationsCache[annotationCacheKey] = {
3222+
annotations: [],
3223+
};
3224+
}
3225+
that.vars.annotationsCache[annotationCacheKey].annotations.push(annotation);
3226+
});
3227+
var data = that.vars.annotationsCache[cacheKey] || {
3228+
annotations: [],
3229+
};
3230+
that._displayArtifactsSelection(annotations);
3231+
that._displaySleepStageSelection(annotations);
3232+
that._displayAnnotations(annotations);
3233+
});
31973234
},
31983235

31993236
_getVisibleAnnotations: function(annotations) {
@@ -3245,7 +3282,7 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
32453282
var cacheKey = that._getAnnotationsCacheKey(that.vars.currentWindowRecording, annotation.position.start, annotation.position.end, false, 'ARTIFACT');
32463283
that.vars.annotationsCache[cacheKey] = annotation.id;
32473284
$(that.element).find('.artifact_panel button.artifact[data-annotation-type="' + annotation.label + '"]').addClass(activeClass);
3248-
var noArtifactAnnotation = false;
3285+
noArtifactAnnotation = false;
32493286
break;
32503287
}
32513288
}
@@ -3254,6 +3291,31 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
32543291
}
32553292
},
32563293

3294+
_displaySleepStageSelection: function(annotations) {
3295+
var that = this;
3296+
var activeClass = 'teal';
3297+
var inactiveClass = 'grey';
3298+
var validKeys = [
3299+
'sleep_stage_wake',
3300+
'sleep_stage_n1',
3301+
'sleep_stage_n2',
3302+
'sleep_stage_n3',
3303+
'sleep_stage_rem',
3304+
];
3305+
var noSleepStageAnnotation = true;
3306+
$(that.element).find('.sleep_stage_panel button.sleep_stage').removeClass(activeClass).addClass(inactiveClass);
3307+
for (var i = 0; i < annotations.length; ++i) {
3308+
var annotation = annotations[i];
3309+
if (validKeys.indexOf(annotation.label) > -1) {
3310+
var cacheKey = that._getAnnotationsCacheKey(that.vars.currentWindowRecording, annotation.position.start, annotation.position.end, false, 'SLEEP_STAGE');
3311+
that.vars.annotationsCache[cacheKey] = annotation.id;
3312+
$(that.element).find('.sleep_stage_panel button.sleep_stage[data-annotation-type="' + annotation.label + '"]').addClass(activeClass).removeClass(inactiveClass);
3313+
noSleepStageAnnotation = false;
3314+
break;
3315+
}
3316+
}
3317+
},
3318+
32573319
_turnExampleIntoAnnotation: function(example) {
32583320
var that = this;
32593321
var annotation = {
@@ -3339,26 +3401,32 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
33393401
channels = [ channels ];
33403402
}
33413403
var apiClient = that._getApiClient();
3342-
var annotation = apiClient.annotation().init({
3343-
id: annotationId,
3344-
label: type,
3345-
confidence: confidence,
3346-
position: {
3347-
channels: channels,
3348-
start: parseFloat(start),
3349-
end: parseFloat(end),
3350-
},
3351-
metadata: {
3352-
channels_displayed: that.options.channelsDisplayed,
3353-
comment: comment,
3354-
metadata: metadata,
3355-
},
3356-
}).save(function(annotation, error) {
3357-
callback && callback(annotation, error);
3358-
if (error) {
3359-
alert(error.message);
3360-
return;
3404+
var params = {
3405+
content: {
3406+
label: type,
3407+
confidence: confidence,
3408+
position: {
3409+
channels: channels,
3410+
start: parseFloat(start),
3411+
end: parseFloat(end),
3412+
},
3413+
metadata: {
3414+
channels_displayed: that.options.channelsDisplayed,
3415+
comment: comment,
3416+
metadata: metadata,
3417+
},
33613418
}
3419+
}
3420+
if (!annotationId) {
3421+
apiClient.create('response', params, updateCache);
3422+
}
3423+
else {
3424+
params.id = annotationId;
3425+
console.log(params);
3426+
apiClient.update('response', params, updateCache);
3427+
}
3428+
function updateCache(annotation) {
3429+
callback && callback(annotation, null);
33623430
var window_start = that.vars.currentWindowStart;
33633431
var window_end = window_start + that.options.windowSizeInSeconds;
33643432
var key = that._getAnnotationsCacheKey(recording_name, window_start, window_end);
@@ -3368,9 +3436,9 @@ $.widget('crowdcurio.TimeSeriesAnnotator', {
33683436
annotations: []
33693437
}
33703438
}
3371-
cacheEntry.annotations.unshift(annotation);
3439+
cacheEntry.annotations.unshift(annotation.content);
33723440
that.vars.annotationsCache[key] = cacheEntry;
3373-
});
3441+
}
33743442
},
33753443

33763444
_deleteAnnotation: function(annotationId, recording_name, start, end, channels, channelsDisplayed) {
@@ -6917,6 +6985,7 @@ CrowdCurioClient.prototype.getNextTask = function(queue_type, callback){
69176985
// note: create is supported for two models (event/response)
69186986
// update is supported for all models
69196987
// list is supported for two models (response/data)
6988+
// delete is supported for all models
69206989
CrowdCurioClient.prototype.create = function(model, params, callback){
69216990
var that = this;
69226991
// extend params by adding relations
@@ -6953,15 +7022,31 @@ CrowdCurioClient.prototype.list = function(model, params, callback){
69537022
var that = this;
69547023
// extend params by adding relations
69557024
if(model === 'response'){
6956-
params = jQuery.extend({
6957-
owner: that.user,
6958-
data: that.data,
6959-
task: that.task,
6960-
}, params);
7025+
if (that.user) {
7026+
params.owner = that.user.id;
7027+
}
7028+
if (that.data) {
7029+
params.data = that.data.id;
7030+
}
7031+
if (that.task) {
7032+
params.task = that.task.id;
7033+
}
7034+
if (that.experiment) {
7035+
params.experiment = that.experiment.id;
7036+
}
7037+
if (that.condition) {
7038+
params.condition = that.condition.id;
7039+
}
69617040
} else if (model === 'data'){
6962-
params = jQuery.extend({
6963-
task: that.task !== null ? that.task.id : null,
6964-
}, params);
7041+
if (that.task) {
7042+
params.task = that.task.id;
7043+
}
7044+
if (that.experiment) {
7045+
params.experiment = that.experiment.id;
7046+
}
7047+
if (that.condition) {
7048+
params.condition = that.condition.id;
7049+
}
69657050
}
69667051

69677052
let action = [model, "list"];
@@ -6970,6 +7055,13 @@ CrowdCurioClient.prototype.list = function(model, params, callback){
69707055
});
69717056
}
69727057

7058+
CrowdCurioClient.prototype.delete = function(model, params, callback){
7059+
let action = [model, "delete"];
7060+
this.client.action(schema, action, params).then(function(result) {
7061+
callback(result);
7062+
});
7063+
}
7064+
69737065

69747066
module.exports = CrowdCurioClient;
69757067
},{"./task-router":19,"./task-session":20}],19:[function(require,module,exports){

0 commit comments

Comments
 (0)