Gitlab CSE Unil

Commit d23dbcba authored by Julien Furrer's avatar Julien Furrer
Browse files

Ajouté module analyse pour env CIMAF

parent f2e1f9ab
/**
* Created by jfurrer on 02.12.13.
*/
/*
requirejs.config({
paths: {
'jquery': "lib/jquery.min",
'paper': "lib/paper-latest",
'signals': "lib/signals",
'jquery.mousewheel': "lib/jquery.mousewheel",
'jquery.autoGrowInput': "lib/jquery.autoGrowInput",
'jquery.bootstrap': "../bootstrap/js/bootstrap.min",
'bootstrap.slider': "../bootstrap/js/bootstrap-slider.min"
},
shim: {
'bootstrap.slider': { deps: ["jquery.bootstrap"] }
}
});
*/
requirejs([
"jquery",
"paper",
"signals",
"adim/config",
"adim/io",
"adim/view",
"env/ui-analyse"
],
function ($, paper, Signal, config, io, view, ui) {
// ----- Ajax init ------------------------------------
$.ajaxSetup({
headers: {
"Authorization": "Token " + config.user.token
}
});
// ----- Functions -----------------------------------
var imageViewWidth = 200;
var _imageBox;
var graphMargin = {top:20, bottom: 10};
var _groupSize = 3;
var _cutPos;
function loadImage(imageConfig) {
// Set default and sanitize imageConfig
if (typeof imageConfig === 'string') {
imageConfig = {url:imageConfig};
} else {
imageConfig = $.extend({url:""},imageConfig);
}
_cutPos = imageConfig.cutPos;
// Get image layer and keep track of current active layer
// for later reactivation
var imageRaster = new paper.Raster();
var clippingMask = new paper.Path.Rectangle({
from: [0, 0],
to: [imageViewWidth*2, 34600],
strokeWidth: 0
});
_imageBox = new paper.Group(clippingMask, imageRaster);
_imageBox.position = [0, 0];
_imageBox.clipped = true;
imageRaster.onLoad = function() {
imageRaster.position = [0, 0];
imageRaster.translate([(imageRaster.width / 2) - imageConfig.cutPos + (imageViewWidth / 2), imageRaster.height / 2]);
_imageBox.translate(0, graphMargin.top);
view.resize(null, imageRaster.height + graphMargin.top + graphMargin.bottom);
scaleImage(_graphScale);
drawGraph(_groupSize, 200, imageRaster.height);
};
imageRaster.source = imageConfig.url;
window.imageBox = _imageBox;
}
function scaleImage(scale) {
var clipping = _imageBox.children[0];
clipping.scale(1, scale, clipping.bounds.topCenter);
var imageRaster = _imageBox.children[1];
imageRaster.scale(scale / imageRaster.scaling.x, imageRaster.bounds.topLeft.add([_cutPos, 0]));
view.resize(null, imageRaster.bounds.height + graphMargin.top + graphMargin.bottom);
}
var graph;
function groupedData(annotationsData, groupSize) {
var data = {
'concordantstr': {},
'discordantstr': {}
};
var semiGroupSize = Math.round(groupSize/2*100)/100;
for (var kind in data) {
if (!data.hasOwnProperty(kind)) continue;
// Count the number of occurrences of each value
for (var ai=0, a; a=annotationsData[kind][ai]; ai++) {
//var y = (Math.round(a[2] / groupSize) * groupSize) + semiGroupSize;
var y = Math.round(a[2] / groupSize) * groupSize;
if (!data[kind][y]) {
data[kind][y] = {total: 0, 1:0, 2:0, 3:0};
}
data[kind][y].total += 1;
data[kind][y][a[4]] += 1; // confidence
}
}
return data;
}
function updateGraph(graphParam) {
var needToDraw = false;
var testingVal;
if (graphParam.groupSize) {
testingVal = parseInt(graphParam.groupSize, 10) || 1;
if (testingVal > 0 && testingVal !== _groupSize) {
_groupSize = testingVal;
needToDraw = true;
}
}
if (graphParam.hasOwnProperty('showConfidence')) {
testingVal = !!graphParam.showConfidence;
if (testingVal !== _graphShowConfidence) {
_graphShowConfidence = testingVal;
needToDraw = true;
}
}
if (graphParam.hasOwnProperty('showDiscordance')) {
testingVal = !!graphParam.showDiscordance;
if (testingVal !== _graphShowDiscordance) {
_graphShowDiscordance = testingVal;
needToDraw = true;
}
}
if (graphParam.hasOwnProperty('showConcordance')) {
testingVal = !!graphParam.showConcordance;
if (testingVal !== _graphShowConcordance) {
_graphShowConcordance = testingVal;
needToDraw = true;
}
}
if (graphParam.hasOwnProperty('setScale')) {
testingVal = parseFloat(graphParam.setScale) || 1;
if (testingVal > 0 && testingVal !== _graphScale) {
_graphScale = testingVal;
scaleImage(_graphScale);
needToDraw = true;
}
}
if (graphParam.hasOwnProperty('viewWidthChanged')) {
needToDraw = !!graphParam['viewWidthChanged'];
}
if (needToDraw)
drawGraph();
}
var _graphScale = 1;
var _graphShowConfidence = false;
var _graphShowDiscordance = false;
var _graphShowConcordance = true;
var _graphWidth = 200;
var _graphHeight = 400;
function drawGraph(groupSize, width, height) {
groupSize = _groupSize;
_graphWidth = parseInt(width, 10) || _graphWidth;
_graphHeight = parseInt(height, 10) || _graphHeight;
if (graph) {
try { graph.remove(); } catch(e) {}
}
var data = groupedData(config.annotationsData, groupSize);
//var maxY = Math.max.apply(Math, data.map(function(d){return d[1]}));
//var minY = Math.min.apply(Math, data.map(function(d){return d[1]}))
//var scaleY = width / maxY;
var seriesFillColor = {
concordantstr : {
3: new paper.Color("#FF0000"),
2: new paper.Color("#FF7420"),
1: new paper.Color("#FFCB30")
},
discordantstr : {
3: new paper.Color("#0000FF"),
2: new paper.Color("#406FFF"),
1: new paper.Color("#60C3FF")
}
};
var seriesStrokeColor = {
concordantstr : new paper.Color(0.5, 0, 0, 1),
discordantstr : new paper.Color(0, 0, 0.5, 1)
};
var seriesStrokeWidth = groupSize > 8 ? 0.2 : 0;
var series = { };
var grid = new paper.Group();
graph = new paper.Group(grid);
if (_graphShowConcordance) {
series.concordantstr = new paper.Group();
graph.addChild(series.concordantstr);
}
if (_graphShowDiscordance) {
series.discordantstr = new paper.Group();
graph.addChild(series.discordantstr);
}
var maxH = 0; // The max number of entry for a category
var y, // y position loop counter
rec, // A y position data record {total: 1, 1:0, 2:0, 3:1}
kind, // The kind of record (concordant / discordant)
xScale = 90; // the width in pixel of one unite of a category, computed after maxH is found
for (y=0; y<=_graphHeight; y+=groupSize) {
for (kind in series) {
if (!series.hasOwnProperty(kind)) continue;
rec = data[kind][y];
if (rec) {
// we have some data at this y position
maxH = Math.max(maxH, rec.total);
}
}
}
xScale = (paper.view.viewSize.width - imageViewWidth) / (maxH + 2);
for (y=0; y<=_graphHeight; y+=groupSize) {
var s = 0; // start x pos, increment after each box
var c, cStr; // confidence, confidence as a String
for (kind in series) {
if (!series.hasOwnProperty(kind)) continue;
rec = data[kind][y];
if (rec) {
// we have some data at this y position
maxH = Math.max(maxH, rec.total);
if (_graphShowConfidence) {
// Iterate over confidence's values, plot a box for each one found
for (c=3; c>0; c--){
cStr = ""+c;
if (rec[cStr]) {
series[kind].addChild(new paper.Path.Rectangle({
from: [s, y * _graphScale],
to: [s+=(rec[cStr] * xScale), (y + groupSize) * _graphScale],
strokeWidth: seriesStrokeWidth,
strokeColor: seriesStrokeColor[kind],
fillColor: seriesFillColor[kind][cStr]
}));
}
}
} else {
// Do not differentiate based on confidence
series[kind].addChild(new paper.Path.Rectangle({
from: [s, y * _graphScale],
to: [s+=(rec.total * xScale), (y + groupSize) * _graphScale],
strokeWidth: seriesStrokeWidth,
strokeColor: seriesStrokeColor[kind],
fillColor: seriesFillColor[kind]['3']
}));
}
}
}
}
// Draw Grid
var gridColor = new paper.Color(0, 0.33);
var labelColor = new paper.Color(0, 0.50);
for (var x = 1; x <= maxH; x++) {
grid.addChild(new paper.Path.Line({
from: [x*xScale, 0],
to: [x*xScale, _graphHeight],
strokeColor: gridColor,
strokeWidth: 0.5
}));
var label = grid.addChild(new paper.PointText({
point: [x*xScale, 0],
strokeWidth: 0,
fillColor: labelColor,
fontSize: 10,
content: x
}));
label.translate([-label.bounds.width/2, -4]);
}
graph.translate([imageViewWidth, graphMargin.top]);
paper.view.draw();
}
// ----- Events Binding -------------------------------
io.events.annotableLoaded.add(function(annotableData){
// Arera width
var cutMargin = parseInt(annotableData.envparam.cimaf_cut_margin, 10) || 25;
// Cut Position
var cutPos = parseFloat(annotableData.envparam.cimaf_cut_pos);
if (isNaN(cutPos))
cutPos = 0;
// Load the image
loadImage({
url: config.annotable.image,
cutPos: cutPos
});
});
ui.events.graphParamChanged.add(updateGraph);
// ----- DOM ready ------------------------------------
$(function(){
if (!view.init("my-canvas")) {
console && console.error && console.error("View not initialized. Abort.");
return;
}
ui.init();
io.loadData(config.api.annotables + config.annotable.id + '/');
});
// ----- Global Scopevar ------------------------------
var ADIM = {
view: view
};
window[config.adim_global_varname] = ADIM;
// call ready callback if defined in the config
if (config.ready && typeof config.ready === 'function') {
config.ready(ADIM);
}
});
\ No newline at end of file
define(
[
"jquery",
"signals",
"adim/config",
"adim/view",
"jquery.bootstrap",
"jquery.mousewheel",
"bootstrap.slider"
],
function($, Signal, config, view){
// ----- Locale variables -----------------------------
var _$w = $(window);
var _reservedWidth = 0;
var _reservedHeight = 0;
var _events = {
graphParamChanged: new Signal()
};
// ----- Initialisations ------------------------------
/**
* Main UI initialization, called by main.js during initialization.
* When this function is called, we can assume following:
* - document is ready
*/
function init() {
// ----- Store jQuery objects for some layout elements
_$w = $(window);
$("#mode-selector button[data-href]").click(function(event){
event.preventDefault();
document.location.href = $(this).data('href');
});
var analyseGraphCtrl = $("#analyse-graph-ctrl");
var graphCol = $("#graph-col");
var _curGraphColWidth = 0;
// Initialize reserved width and height
_reservedWidth = 0;
_reservedHeight = 0;
_$w
.on('scroll', function(){
analyseGraphCtrl.toggleClass("pinned", (window.scrollY > 50));
})
.on('resize', function(){
var w = graphCol.width();
if (w !== _curGraphColWidth) {
_curGraphColWidth = w;
view.resize(w);
_events.graphParamChanged.dispatch({viewWidthChanged:true});
}
})
.resize()
;
$("#page-loader").remove();
var inputGroupSize = $("#inputGroupSize");
inputGroupSize
.keydown(function(event) {
var d = (event.which === 38) ? 1 :
(event.which === 40) ? -1 : 0;
if (d) {
event.preventDefault();
event.stopPropagation();
var groupSize = parseInt(inputGroupSize.val(),10) + d;
if (groupSize > 0) {
inputGroupSize.val(groupSize);
_events.graphParamChanged.dispatch({groupSize: groupSize});
}
}
})
.change(function(){
var groupSize = parseInt(inputGroupSize.val(), 10);
if (groupSize > 0) {
_events.graphParamChanged.dispatch({groupSize: groupSize});
}
});
var checkShowConfidence = $("#checkShowConfidence");
checkShowConfidence.change(function(event){
_events.graphParamChanged.dispatch({showConfidence: checkShowConfidence.is(":checked")});
});
var checkShowDiscordance = $("#checkShowDiscordance");
checkShowDiscordance.change(function(){
_events.graphParamChanged.dispatch({showDiscordance: checkShowDiscordance.is(":checked")});
});
var checkShowConcordance = $("#checkShowConcordance");
checkShowConcordance.change(function(){
_events.graphParamChanged.dispatch({showConcordance: checkShowConcordance.is(":checked")});
});
var inputScale = $("#inputScale").slider({
selection: 'none',
value: 100,
formater: function(v) { return v+"%"; }
})
.on('slide', function(evt){
var scale = (parseFloat(evt.value) || 100) / 100;
_events.graphParamChanged.dispatch({setScale: scale});
});
}
// ----- Returned Module ------------------------------
return {
init: init,
events: _events
};
}
);
\ No newline at end of file
......@@ -18,6 +18,7 @@ function(_, config){
);
function _proxy(fname) {
console.log("_proxy");
return function() {
_.invoke(_envs, fname, arguments);
}
......
......@@ -182,6 +182,7 @@ function ($, paper, Signal, Config, view) {
},
loadSharedAnnotations: function(url) {
console.log("loadSharedAnnotations");
var d = $.Deferred();
$.ajax({
url: url,
......
......@@ -99,7 +99,7 @@ define(["paper", "helper/utils"], function (paper, utils) {
* Create a new zone
* @param {object} o the parameters of the new zone
* @param {boolean} [noEventListening] Set to true to disable event listening on the item
* @returns {paper.Path}
* @returns {paper.Path|u}
*/
function createNewZone(o, noEventListening) {
var newArea = new paper.Path({
......@@ -136,7 +136,6 @@ define(["paper", "helper/utils"], function (paper, utils) {
newArea.onKeyUp = itemKeyUp;
}
_events.annotationAdded.dispatch(newArea);
return newArea;
}
......@@ -236,7 +235,8 @@ define(["paper", "helper/utils"], function (paper, utils) {
_drawingArea = null;
}
createNewZone({from:_startPoint.y, to:dstPoint.y});
var newArea = createNewZone({from:_startPoint.y, to:dstPoint.y});
_events.annotationAdded.dispatch(newArea);
_startPoint = null;
create = false;
......
......@@ -149,7 +149,6 @@ define(["paper", "helper/utils"], function (paper, utils) {
newStria.onPropertyChange = itemPropertyChange;
_events.annotationAdded.dispatch(newStria);
return newStria;
}
......@@ -244,7 +243,6 @@ define(["paper", "helper/utils"], function (paper, utils) {
function itemPropertyChange(property, value) {
if (property === 'confidence') {
console.log(value, confidenceStyle[value]);
this.style = confidenceStyle[value];
paper.view.draw();
}
......@@ -275,7 +273,8 @@ define(["paper", "helper/utils"], function (paper, utils) {
_drawingLine = null;
}
createNewStria({from: _startPoint, to: dstPoint});
var newStria = createNewStria({from: _startPoint, to: dstPoint});
_events.annotationAdded.dispatch(newStria);
_startPoint = null;
create = false;
}
......
......@@ -130,7 +130,6 @@ define(["paper", "helper/utils"], function (paper, utils) {
newStria.onKeyUp = itemKeyUp;
newStria.onPropertyChange = itemPropertyChange;
_events.annotationAdded.dispatch(newStria);
return newStria;
}
......@@ -260,7 +259,9 @@ define(["paper", "helper/utils"], function (paper, utils) {
if (_drawingLine)
_drawingLine.remove();
createNewStria({from: _startPoint, to: dstPoint});
var newStria = createNewStria({from: _startPoint, to: dstPoint});
_events.annotationAdded.dispatch(newStria);
_startPoint = null;
create = false;
}
......
......@@ -129,7 +129,7 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
updateUserLayers();
_autoSave = true;
io.setAutoSave(_autoSave);
updateSaveButState();
//updateSaveButState();
});