<commit_before>RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
DC = 'http://purl.org/dc/terms/'
CE = 'http://ibm.com/ce/ns#'
SUS = 'http://setupshop.me/ns#'
ALREADY_IN_HISTORY = true

app = new function () { // make a global called app. It's useful for debugging
    var self = this
    self.jwt = misc_util.get_jwt_claims()
    self.cpanel = APPLICATION_ENVIRON.initial_simple_jso._subject;

    self.last_message = ko.observable()
    self.last_error = ko.observable()
    self.host_controller = ko.observable()
    self.site_controller = ko.observable()
    self.edit_site_controller = ko.observable()
    self.create_site_controller = ko.observable()
    self.create_improvement_controller = ko.observable()

    var dispatcher = new misc_util.Dispatcher(
        function(element) { // function is called to decide if this single-page-app claims a click on an element
            var segments = element.pathname.split('/')
            return element.host == window.location.host && segments.length > 1 && segments[1] == 'mt'
            },
        get_resource_and_show_view) // function is called a) if a user click is claimed (already_in_history will be false) b) if a history event happens (already_in_history will be true)
    dispatcher.hook_history_and_links()

    function show_view(model) {
            /* This is the central dispatcher for the application. It is called every time we change URL within the app. This can happen when:
                1) The user types a URL of one of our resources into the address bar and we enter the app
                2) The user hits a back or forward button or selects from history
                3) The code of our application chooses to navigate to a new URL
                4) The user clicks on an anchor element that referenced the URL. This also has two cases:
                    a) The anchor element was created by another application. In that case we are entering this app
                    b) The anchor element was create by this app. In that case we will stay within the app and navigate to the right place
                In all cases, by the time we get here, we have already done a GET to fetch the resource from the server and converted the response to a simple Javascript object.
                We are strict about URLs - we do not support the technique where you navigate to a 'fake' URL that does not exist on the server. 'Friends don't let friends fake URLs'
            */
        //if (model.rdf_type == CE+'Saas_host') {
        if (model.rdf_type == LDP+'DirectContainer') {
            self.host_controller(new host_controller(model, self))
            self.site_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(null)
            self.create_improvement_controller(null)
            }
        else if (model.rdf_type == CE+'Site') {
            self.site_controller(new site_controller(model, self))
            self.host_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(null)
            self.create_improvement_controller(null)
            }
        else if (model.rdf_type == CE+'NewMemberInstructions' &&  model.ce_newMemberContainer.ldp_hasMemberRelation == CE+'sites') {
            self.site_controller(null)
            self.host_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(new create_site_controller(model, self))
            self.create_improvement_controller(null)
            }
        else if (model.rdf_type == CE+'NewMemberInstructions' &&  model.ce_newMemberContainer.ldp_hasMemberRelation == CE+'has_improvement') {
            self.site_controller(null)
            self.host_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(null)
            self.create_improvement_controller(new create_improvement_controller(model, self))
            }
        else {
            return false
            }
        return true
        }

    self.enter_edit_mode = function (arg) {
        self.edit_site_controller(self.site_controller())
        self.site_controller(null)
        }

    self.enter_create_improvement_mode = function (arg) {
        self.create_improvement_controller(self.site_controller())
        self.site_controller(null)
        }

    function get_resource_and_show_view(resource_url, history_tracker) {
        ld_util.get(resource_url, function(request){
            if (request.status==200) {
                var resource_json = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                if (show_view(resource_json)) { // resource accepted
                    history_tracker.accept_url()
                    }
                else { // we cannot handle this resource
                    history_tracker.decline_url()
                    }
                }
            else if (request.status==401) {
                window.name = resource_url
                var resource_json = JSON.parse(request.responseText)
                window.location.href = resource_json['http://ibm.com/ce/ns#login-page']
                }
            else {
                console.log( request.status )
                }
            })
        }

    show_view(APPLICATION_ENVIRON.initial_simple_jso)

    function site_controller (model, parent) {
        var self = this
        self.parent = parent
        self.site = model
        self.improvements = get_improvements(self.site.ce_improvements)
        self.capabilities = get_capabilities()

        function get_capabilities() {
            var result = ko.observableArray()
            var ss_session_id = misc_util.getSSSessionId()
            ld_util.get(self.site.ce_site_capabilities, function(request){
                if (request.status==200) {
                    var capabilities_jso = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                    result(capabilities_jso.ce_contains)
                    }
                else {
                    console.log( request.status )
                    }
                }, ss_session_id ? {'SSSESSIONID': ss_session_id}: null) // going cross-origin. Pass SSSESSIONID header to avoid login challenge
            return result
            }

        function get_improvements(improvement_urls) {
            var improvements = ko.observableArray()
            if (improvement_urls) {
                var ss_session_id = misc_util.getSSSessionId()
                for (var i = 0; i<improvement_urls.length; i++) {
                    var improvement_url = improvement_urls[i] //calculate the tenant's domain
                    ld_util.get(improvement_url, function(request) {
                        if (request.status==200) {
                            var improvement = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                            improvements.push(improvement)
                            improvements.sort(function(a,b){
                                return (b.dc_created > a.dc_created) ? -1 : (b.dc_created < a.dc_created) ? 1 : 0
                                })
                            }
                        else {
                            console.log( request.status )
                            }
                        }, ss_session_id ? {'SSSESSIONID': ss_session_id}: null) // going cross-origin. Pass SSSESSIONID header to avoid login challenge
                    }
                }
            return improvements
            }

        self.submit_edit = function() {
            self.update_site({dc_title: self.site.dc_title})
            }

        self.update_site = function(patch) {
            patch['_subject'] = self.site._subject
            var rdf_jso = APPLICATION_ENVIRON.rdf_converter.convert_to_rdf_jso(patch)
            ld_util.send_patch(self.site._subject, self.site.ce_modificationCount, rdf_jso, function(http) {
                if (http.status==200) { // update ok
                    parent.last_error('Update OK')
                    }
                else if (http.status==400) {
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                else {
                    alert('Update failed unexpectedly: ' + http.status + '/' + http.responseText)
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                self.cancel_site_mode()
                })
            }

        self.create_improvement = function(capability) {
            var ss_session_id = misc_util.getSSSessionId()
            var new_improvement = {
                _subject: '',
                rdf_type: capability.ce_improvement_type,
                ce_capability: new rdf_util.URI(capability._subject),
                dc_title: ''
                }
            ld_util.send_create(capability.ce_improvement_container, APPLICATION_ENVIRON.rdf_converter.convert_to_rdf_jso(new_improvement), function(http) {
                if (http.status==201) { // create ok
                    var new_improvement_url = new rdf_util.URI(http.getResponseHeader('Location'))
                    if (!self.site.ce_improvements) {
                        self.site.ce_improvements = [new_improvement_url]
                        }
                    else {
                        self.site.ce_improvements.push(new_improvement_url)
                        }
                    self.update_site({ce_improvements: self.site.ce_improvements})
                    }
                else if (http.status==400) {
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                else {
                    alert('Update failed unexpectedly: ' + http.status + '/' + http.responseText)
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                }, ss_session_id ? {'SSSESSIONID': ss_session_id}: null ) // going cross-origin. Pass ss_session_id header to avoid login challenge
            }

        self.cancel_site_mode = function (arg) {
            dispatcher.go_to(self.site._subject, ALREADY_IN_HISTORY)
            }

        }

    function create_site_controller (model, parent) {
        var self = this
        self.parent = parent
        self.site = {
            _subject: '',
            rdf_type: new rdf_util.URI(CE+'Site')
            }
        self.newMemberInstructions = model
        self.create_site = function() {
            var rdf_jso = APPLICATION_ENVIRON.rdf_converter.convert_to_rdf_jso(self.site)
            ld_util.send_create('/mt/sites', rdf_jso, function(http) {
                if (http.status==201) { // create ok
                    parent.last_error('Update OK')
                    self.cancel_create_site()
                    }
                else if (http.status==400) {
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                else {
                    alert('Update failed unexpectedly: ' + http.status + '/' + http.responseText)
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                })
            }

        self.cancel_create_site = function (arg) {
            window.history.back()
            }
        }

    function create_improvement_controller (model, parent) {
        var self = this
        self.parent = parent
        self.newMemberInstructions = model
        self.new_improvement = null
        self.capabilities = get_capabilities()

        self.cancel_create_improvement = function (arg) {
            window.history.back()
            }
        }

    function host_controller (model, parent) {
        var self = this
        self.parent  = parent
        self.saas_host = model
        self.sites_container = ko.observable()
        self.sites_members = ko.observableArray()

        var sites_json = APPLICATION_ENVIRON.initial_simple_jso;
        self.sites_container(sites_json);
        if (sites_json.ldp_contains.length === undefined) {
            self.sites_members.push(sites_json.ldp_contains);
        }
        else
            self.sites_members(sites_json.ce_contains);

/*
        get_sites(model.ce_sites)
        function get_sites(sites_url) {
            ld_util.get(sites_url, function(request){
                if (request.status==200) {
                    var sites_json = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                    self.sites_members(sites_json.ce_contains)
                    self.sites_container(sites_json)
                    }
                else {
                    console.log( request.status )
                    }
                })
            }
*/

        self.new_site = function () {
            console.log('DEBUG THIS');
            window.location = self.sites_container().ce_newMemberInstructions._subject;
            // dispatcher.go_to(self.sites_container().ce_newMemberInstructions._subject, false)
            }
        }

    } ()

ko.applyBindings(app)

<commit_msg>use standard LDP containers<commit_after>RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
DC = 'http://purl.org/dc/terms/'
CE = 'http://ibm.com/ce/ns#'
SUS = 'http://setupshop.me/ns#'
ALREADY_IN_HISTORY = true

app = new function () { // make a global called app. It's useful for debugging
    var self = this
    self.jwt = misc_util.get_jwt_claims()
    self.cpanel = APPLICATION_ENVIRON.initial_simple_jso._subject;

    self.last_message = ko.observable()
    self.last_error = ko.observable()
    self.host_controller = ko.observable()
    self.site_controller = ko.observable()
    self.edit_site_controller = ko.observable()
    self.create_site_controller = ko.observable()
    self.create_improvement_controller = ko.observable()

    var dispatcher = new misc_util.Dispatcher(
        function(element) { // function is called to decide if this single-page-app claims a click on an element
            var segments = element.pathname.split('/')
            return element.host == window.location.host && segments.length > 1 && segments[1] == 'mt'
            },
        get_resource_and_show_view) // function is called a) if a user click is claimed (already_in_history will be false) b) if a history event happens (already_in_history will be true)
    dispatcher.hook_history_and_links()

    function show_view(model) {
            /* This is the central dispatcher for the application. It is called every time we change URL within the app. This can happen when:
                1) The user types a URL of one of our resources into the address bar and we enter the app
                2) The user hits a back or forward button or selects from history
                3) The code of our application chooses to navigate to a new URL
                4) The user clicks on an anchor element that referenced the URL. This also has two cases:
                    a) The anchor element was created by another application. In that case we are entering this app
                    b) The anchor element was create by this app. In that case we will stay within the app and navigate to the right place
                In all cases, by the time we get here, we have already done a GET to fetch the resource from the server and converted the response to a simple Javascript object.
                We are strict about URLs - we do not support the technique where you navigate to a 'fake' URL that does not exist on the server. 'Friends don't let friends fake URLs'
            */
        //if (model.rdf_type == CE+'Saas_host') {
        if (model.rdf_type == LDP+'DirectContainer') {
            self.host_controller(new host_controller(model, self))
            self.site_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(null)
            self.create_improvement_controller(null)
            }
        else if (model.rdf_type == CE+'Site') {
            self.site_controller(new site_controller(model, self))
            self.host_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(null)
            self.create_improvement_controller(null)
            }
        else if (model.rdf_type == CE+'NewMemberInstructions' &&  model.ce_newMemberContainer.ldp_hasMemberRelation == CE+'sites') {
            self.site_controller(null)
            self.host_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(new create_site_controller(model, self))
            self.create_improvement_controller(null)
            }
        else if (model.rdf_type == CE+'NewMemberInstructions' &&  model.ce_newMemberContainer.ldp_hasMemberRelation == CE+'has_improvement') {
            self.site_controller(null)
            self.host_controller(null)
            self.edit_site_controller(null)
            self.create_site_controller(null)
            self.create_improvement_controller(new create_improvement_controller(model, self))
            }
        else {
            return false
            }
        return true
        }

    self.enter_edit_mode = function (arg) {
        self.edit_site_controller(self.site_controller())
        self.site_controller(null)
        }

    self.enter_create_improvement_mode = function (arg) {
        self.create_improvement_controller(self.site_controller())
        self.site_controller(null)
        }

    function get_resource_and_show_view(resource_url, history_tracker) {
        ld_util.get(resource_url, function(request){
            if (request.status==200) {
                var resource_json = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                if (show_view(resource_json)) { // resource accepted
                    history_tracker.accept_url()
                    }
                else { // we cannot handle this resource
                    history_tracker.decline_url()
                    }
                }
            else if (request.status==401) {
                window.name = resource_url
                var resource_json = JSON.parse(request.responseText)
                window.location.href = resource_json['http://ibm.com/ce/ns#login-page']
                }
            else {
                console.log( request.status )
                }
            })
        }

    show_view(APPLICATION_ENVIRON.initial_simple_jso)

    function site_controller (model, parent) {
        var self = this
        self.parent = parent
        self.site = model
        self.improvements = get_improvements(self.site.ce_improvements)
        self.capabilities = get_capabilities()

        function get_capabilities() {
            var result = ko.observableArray()
            var ss_session_id = misc_util.getSSSessionId()
            ld_util.get(self.site.ce_site_capabilities, function(request){
                if (request.status==200) {
                    var capabilities_jso = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                    result(capabilities_jso.ldp_contains)
                    }
                else {
                    console.log( request.status )
                    }
                }, ss_session_id ? {'SSSESSIONID': ss_session_id}: null) // going cross-origin. Pass SSSESSIONID header to avoid login challenge
            return result
            }

        function get_improvements(improvement_urls) {
            var improvements = ko.observableArray()
            if (improvement_urls) {
                var ss_session_id = misc_util.getSSSessionId()
                for (var i = 0; i<improvement_urls.length; i++) {
                    var improvement_url = improvement_urls[i] //calculate the tenant's domain
                    ld_util.get(improvement_url, function(request) {
                        if (request.status==200) {
                            var improvement = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                            improvements.push(improvement)
                            improvements.sort(function(a,b){
                                return (b.dc_created > a.dc_created) ? -1 : (b.dc_created < a.dc_created) ? 1 : 0
                                })
                            }
                        else {
                            console.log( request.status )
                            }
                        }, ss_session_id ? {'SSSESSIONID': ss_session_id}: null) // going cross-origin. Pass SSSESSIONID header to avoid login challenge
                    }
                }
            return improvements
            }

        self.submit_edit = function() {
            self.update_site({dc_title: self.site.dc_title})
            }

        self.update_site = function(patch) {
            patch['_subject'] = self.site._subject
            var rdf_jso = APPLICATION_ENVIRON.rdf_converter.convert_to_rdf_jso(patch)
            ld_util.send_patch(self.site._subject, self.site.ce_modificationCount, rdf_jso, function(http) {
                if (http.status==200) { // update ok
                    parent.last_error('Update OK')
                    }
                else if (http.status==400) {
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                else {
                    alert('Update failed unexpectedly: ' + http.status + '/' + http.responseText)
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                self.cancel_site_mode()
                })
            }

        self.create_improvement = function(capability) {
            var ss_session_id = misc_util.getSSSessionId()
            var new_improvement = {
                _subject: '',
                rdf_type: capability.ce_improvement_type,
                ce_capability: new rdf_util.URI(capability._subject),
                dc_title: ''
                }
            ld_util.send_create(capability.ce_improvement_container, APPLICATION_ENVIRON.rdf_converter.convert_to_rdf_jso(new_improvement), function(http) {
                if (http.status==201) { // create ok
                    var new_improvement_url = new rdf_util.URI(http.getResponseHeader('Location'))
                    if (!self.site.ce_improvements) {
                        self.site.ce_improvements = [new_improvement_url]
                        }
                    else {
                        self.site.ce_improvements.push(new_improvement_url)
                        }
                    self.update_site({ce_improvements: self.site.ce_improvements})
                    }
                else if (http.status==400) {
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                else {
                    alert('Update failed unexpectedly: ' + http.status + '/' + http.responseText)
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                }, ss_session_id ? {'SSSESSIONID': ss_session_id}: null ) // going cross-origin. Pass ss_session_id header to avoid login challenge
            }

        self.cancel_site_mode = function (arg) {
            dispatcher.go_to(self.site._subject, ALREADY_IN_HISTORY)
            }

        }

    function create_site_controller (model, parent) {
        var self = this
        self.parent = parent
        self.site = {
            _subject: '',
            rdf_type: new rdf_util.URI(CE+'Site')
            }
        self.newMemberInstructions = model
        self.create_site = function() {
            var rdf_jso = APPLICATION_ENVIRON.rdf_converter.convert_to_rdf_jso(self.site)
            ld_util.send_create('/mt/sites', rdf_jso, function(http) {
                if (http.status==201) { // create ok
                    parent.last_error('Update OK')
                    self.cancel_create_site()
                    }
                else if (http.status==400) {
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                else {
                    alert('Update failed unexpectedly: ' + http.status + '/' + http.responseText)
                    parent.last_error(http.responseText) //todo - parse this out nicely
                    }
                })
            }

        self.cancel_create_site = function (arg) {
            window.history.back()
            }
        }

    function create_improvement_controller (model, parent) {
        var self = this
        self.parent = parent
        self.newMemberInstructions = model
        self.new_improvement = null
        self.capabilities = get_capabilities()

        self.cancel_create_improvement = function (arg) {
            window.history.back()
            }
        }

    function host_controller (model, parent) {
        var self = this
        self.parent  = parent
        self.saas_host = model
        self.sites_container = ko.observable()
        self.sites_members = ko.observableArray()

        var sites_json = APPLICATION_ENVIRON.initial_simple_jso;
        self.sites_container(sites_json);
        if (sites_json.ldp_contains.length === undefined) {
            self.sites_members.push(sites_json.ldp_contains);
        }
        else
            self.sites_members(sites_json.ldp_contains);

/*
        get_sites(model.ce_sites)
        function get_sites(sites_url) {
            ld_util.get(sites_url, function(request){
                if (request.status==200) {
                    var sites_json = APPLICATION_ENVIRON.rdf_converter.make_simple_jso(request)
                    self.sites_members(sites_json.ldp_contains)
                    self.sites_container(sites_json)
                    }
                else {
                    console.log( request.status )
                    }
                })
            }
*/

        self.new_site = function () {
            console.log('DEBUG THIS');
            window.location = self.sites_container().ce_newMemberInstructions._subject;
            // dispatcher.go_to(self.sites_container().ce_newMemberInstructions._subject, false)
            }
        }

    } ()

ko.applyBindings(app)

<|endoftext|>


<commit_before>mapkob = Object.create(null);

/**
 * Generic function to prepare input for consumption
 *
 * Type can be one of:
 *  - "newline delim": Take a string where sentences are delimited by newlines
 *  - "sentence array": Takes an array of sentences.
 *
 * @param {Object} data The input data
 * @param {String} type The type of input
 * @return {Array} An array of strings, sentences delimited by undefined
 */
mapkob.prepareInput = function(data, type) {
  function splitSentenceArray(a) {
    var delimited = [];
    a.map(function(sentence, i) {
      delimited.push(sentence.trim());
      if (i < sentences.length - 1) {
        delimited.push(undefined);
      }
    });
    return delimited;
  }

  function flattenSentenceArray(a) {
    var wordSplitRe = new RegExp("[ .]+");
    return a.map(function(sentence) {
      return sentence === undefined ? [undefined] : sentence.split(wordSplitRe);
    }).reduce(function(x,y) {return x.concat(y);});
  }

  // newline delim
  var splitRe = new RegExp("[\n\r.!?\t]+");
  if (type === "newline delim") {
    var sentences = data.split(splitRe);
    var delimited = splitSentenceArray(sentences);
    return flattenSentenceArray(delimited);

  }

  // sentence array
  if (type === "sentence array") {
    var delimited = splitSentenceArray(sentences);
    return flattenSentenceArray(delimited);
  }
  return;
};

/**
 * Creates and empty Transition matrix
 *
 * @param {Number} n An integer specifying which type of n-gram to use
 * @return {Object} A Transition Matrix object
 */
mapkob.TransitionMatrix = function(n) {
  if (n % 1 !== 0 || n < 2) {throw "n needs to be an integer >= 2";}
  var uniqueWords = [];
  var matrix = {};
  var starts = {};

  this.stateSpace = [];
  this.matrix = {};
  this.initialStates = new mapkob.Row();
  this.n = n;
  return this;
};

/**
 * functional wrapper around constructor
 * Can train supplied data implicitly or load a JSONified Transition matrix.
 *
 * @param {Number} n An integer specifying which type of n-gram to use
 * @param {Object} input transition matrix input or a JSONified Matrix
 * @return {Object} A Transition Matrix
 */
mapkob.transitionMatrix = function(n, input) {
  n = n || 2;
  var output =  new mapkob.TransitionMatrix(n);

  try {
    data = JSON.parse(input);
    if (data.hasOwnProperty("type") && data.type === "mapkob Transition Matrix") {
      output.statceSpace = data.initialStates;
      output.matrix = data.matrix;
      output.initialStates = new mapkob.Row(data.initialStates.row);
    }
  } catch(err) {
    if (input !== undefined) {
      output.train(input);
    }
  }
  return output;
};

/**
 * Adds training data to an existing model
 *
 * @param {Array} words An array of consecutive words. delimited by undefined
 * @return {this} The trained model
 */
mapkob.TransitionMatrix.prototype.train = function(words) {
  function wordJoin(words, i, n) {
    var output = [];
    while (n > 0) {
      output.push(words[i + n]);
      n -= 1;
    }

    return output.join(" ").replace(/undefined\w+/);
  }

  // State space
  words.map(function(word, i) {
    if (this.stateSpace.indexOf(word) === -1) {
      this.stateSpace.push(word);
    }

    // Initial states
    if (words[i - 1] === undefined &&
        this.initialStates.row[word] === undefined) {
      this.initialStates.row[word] = 1;
      this.initialStates.stateSpace.push(word);
    } else if (words[i - 1] === undefined){
      this.initialStates.row[word] += 1;
    }

    // Rows
    if (this.matrix[word] === undefined) {
      this.matrix[word] = {};
    }
    var nextWord = wordJoin(words, i, this.n - 1);
    // Columns
    if (this.matrix[word][nextWord] === undefined) {
      this.matrix[word][nextWord] = 1;
    } else {
      this.matrix[word][nextWord] += 1;
    }
  }, this);
  return this;
};

/**
 * return the row of a matrix
 *
 * @param {String} row The row by name to look up
 * @return {Object} The row of the matrix
 */
mapkob.TransitionMatrix.prototype.getRow = function(row) {
  var output = new mapkob.Row(this.matrix[row]);
  return output;
};

/**
 * Generates a Markov chain
 *
 * @return {String} A Computer generated string
 */
mapkob.TransitionMatrix.prototype.generateChain = function() {
  var currentState = this.
    initialStates.
    probabilities().
    cumSum().
    pickState(Math.random());

  var output = [];
  console.log(currentState);
  while (currentState !== "undefined" && currentState !== undefined) {
    output.push(currentState);
    currentState = currentState.split(" ")[this.n - 1];
    currentState = this.
      getRow(currentState).
      probabilities().
      cumSum().
      pickState(Math.random());
      console.log(currentState);
  }
  return output.join(" ") + ".";
};

/**
 * Transforms a Transitionmatrix into an identifiable object
 *
 * @return {Object} A JSON file
 */
mapkob.TransitionMatrix.prototype.toJSON = function() {
  return JSON.stringify({
    type: "mapkob Transition Matrix",
    matrix: this.matrix,
    stateSpace: this.stateSpace,
    initialStates: this.initialStates
  });
};

/**
 * A Row mapkob row constructor
 *
 * @param {Object} input A simple object
 * @return {Object} A maokob row
 */
mapkob.Row = function(input) {
  if (input === undefined) {
    this.row = {};
    this.stateSpace = [];
  } else {
    this.row = input;
    this.stateSpace = Object.keys(input);
  }
  return this;
};

/**
 * Calculates the sum of the rows
 *
 * @return {Number} The sum of the values
 */
mapkob.Row.prototype.sum = function() {
  return this.stateSpace.map(function(state) {
    return this.row[state];
  }, this).reduce(function(x, y) {
    return x + y;
  });
};

/**
 * Calculates relative frequencies of a row
 *
 * @return {Object} A row
 */
mapkob.Row.prototype.probabilities = function() {
  var sum = this.sum();
  var probs = {};
  this.stateSpace.map(function(state) {
    probs[state] = this.row[state] / sum;
  }, this);
  return new mapkob.Row(probs);
};

/**
 * A row with the cumulative sums
 *
 * @return {Object} A row
 */
mapkob.Row.prototype.cumSum = function() {
  var sums = {};
  var sum = 0;
  this.stateSpace.map(function(state) {
    sum += this.row[state];
    sums[state] = sum;
  }, this);
  return new mapkob.Row(sums);
};

/**
 * return an array with the row's values
 *
 * @return {Array} The rows values
 */
mapkob.Row.prototype.values = function() {
  return this.stateSpace.map(function(state) {
    return this.row[state];
  }, this);
};

/**
 * Picks the state with the largest CDF that is smaller or equal to p
 * Technically the first one where the next is larger or the array ends
 *
 * @return {String} A state string
 */
mapkob.Row.prototype.pickState = function(p) {
  cdf = this.values();
  for (var col in cdf) {
    if (p <= cdf[col] && (cdf[col + 1] == undefined || cdf[col + 1] > p)) {
      return this.stateSpace[col];
    }
  }
};
<commit_msg>New text prep type plain text and fixed bugs<commit_after>mapkob = Object.create(null);

/**
 * Generic function to prepare input for consumption
 *
 * Type can be one of:
 *  - "plain text": A regular text, sentences are delimited by periods
 *  - "newline delim": Take a string where sentences are delimited by newlines
 *  - "sentence array": Takes an array of sentences.
 *
 * @param {Object} data The input data
 * @param {String} type The type of input
 * @return {Array} An array of strings, sentences delimited by undefined
 */
mapkob.prepareInput = function(data, type) {
  function splitSentenceArray(a) {
    var delimited = [];
    a.map(function(sentence, i) {
      delimited.push(sentence.trim());
      if (i < sentences.length - 1) {
        delimited.push(undefined);
      }
    });
    return delimited;
  }

  function flattenSentenceArray(a) {
    var wordSplitRe = new RegExp("[ .]+");
    return a.map(function(sentence) {
      return sentence === undefined ? [undefined] : sentence.split(wordSplitRe);
    }).reduce(function(x,y) {return x.concat(y);});
  }
  // plain text
  var plainTextSplitter = new RegExp("([\.!?]+)", "gm")
  //var plainTextSplitter = new RegExp("(\.)")
  if (type === "plain text") {
    data = data.replace(plainTextSplitter, "\n")
    type = "newline delim"
  }

  // newline delim
  var splitRe = new RegExp("[\n\r]+", "gm");
  if (type === "newline delim") {
    var sentences = data.split(splitRe);
    var delimited = splitSentenceArray(sentences);
    return flattenSentenceArray(delimited);

  }

  // sentence array
  if (type === "sentence array") {
    var delimited = splitSentenceArray(sentences);
    return flattenSentenceArray(delimited);
  }
  return;
};

/**
 * Creates and empty Transition matrix
 *
 * @param {Number} n An integer specifying which type of n-gram to use
 * @return {Object} A Transition Matrix object
 */
mapkob.TransitionMatrix = function(n) {
  if (n % 1 !== 0 || n < 2) {throw "n needs to be an integer >= 2";}
  var uniqueWords = [];
  var matrix = {};
  var starts = {};

  this.stateSpace = [];
  this.matrix = {};
  this.initialStates = new mapkob.Row();
  this.n = n;
  return this;
};

/**
 * functional wrapper around constructor
 * Can train supplied data implicitly or load a JSONified Transition matrix.
 *
 * @param {Number} n An integer specifying which type of n-gram to use
 * @param {Object} input transition matrix input or a JSONified Matrix
 * @return {Object} A Transition Matrix
 */
mapkob.transitionMatrix = function(n, input) {
  n = n || 2;
  var output =  new mapkob.TransitionMatrix(n);

  try {
    data = JSON.parse(input);
    if (data.hasOwnProperty("type") && data.type === "mapkob Transition Matrix") {
      output.statceSpace = data.initialStates;
      output.matrix = data.matrix;
      output.initialStates = new mapkob.Row(data.initialStates.row);
    }
  } catch(err) {
    if (input !== undefined) {
      output.train(input);
    }
  }
  return output;
};

/**
 * Adds training data to an existing model
 *
 * @param {Array} words An array of consecutive words. delimited by undefined
 * @return {this} The trained model
 */
mapkob.TransitionMatrix.prototype.train = function(words) {
  function wordJoin(words, i, n) {
    var output = [];
    var offset = 1;
    while (n >= offset) {
      output.push(words[i + offset]);
      offset += 1;
    }

    return output
        .reduce(function(x,y) {return x + " " + y;})
        .replace(/undefined[\w, ]+/, "undefined");
  }

  // State space
  words.map(function(word, i) {
    if (this.stateSpace.indexOf(word) === -1) {
      this.stateSpace.push(word);
    }

    // Initial states
    if (words[i - 1] === undefined &&
        this.initialStates.row[word] === undefined) {
      this.initialStates.row[word] = 1;
      this.initialStates.stateSpace.push(word);
    } else if (words[i - 1] === undefined){
      this.initialStates.row[word] += 1;
    }

    // Rows
    if (this.matrix[word] === undefined) {
      this.matrix[word] = {};
    }
    var nextWord = wordJoin(words, i, this.n - 1);
    // Columns
    if (this.matrix[word][nextWord] === undefined) {
      this.matrix[word][nextWord] = 1;
    } else {
      this.matrix[word][nextWord] += 1;
    }
  }, this);
  return this;
};

/**
 * return the row of a matrix
 *
 * @param {String} row The row by name to look up
 * @return {Object} The row of the matrix
 */
mapkob.TransitionMatrix.prototype.getRow = function(row) {
  var output = new mapkob.Row(this.matrix[row]);
  return output;
};

/**
 * Generates a Markov chain
 *
 * @return {String} A Computer generated string
 */
mapkob.TransitionMatrix.prototype.generateChain = function() {
  var currentState = this.
    initialStates.
    probabilities().
    cumSum().
    pickState(Math.random());

  var output = [];
  while (currentState !== "undefined" && currentState !== undefined) {
    output.push(currentState);
    currentNGram = currentState.split(" ")
    currentState = currentNGram[currentNGram.length - 1];
    currentState = this.
      getRow(currentState).
      probabilities().
      cumSum().
      pickState(Math.random());
  }
  return output.join(" ") + ".";
};

/**
 * Transforms a Transitionmatrix into an identifiable object
 *
 * @return {Object} A JSON file
 */
mapkob.TransitionMatrix.prototype.toJSON = function() {
  return JSON.stringify({
    type: "mapkob Transition Matrix",
    matrix: this.matrix,
    stateSpace: this.stateSpace,
    initialStates: this.initialStates
  });
};

/**
 * A Row mapkob row constructor
 *
 * @param {Object} input A simple object
 * @return {Object} A maokob row
 */
mapkob.Row = function(input) {
  if (input === undefined) {
    this.row = {};
    this.stateSpace = [];
  } else {
    this.row = input;
    this.stateSpace = Object.keys(input);
  }
  return this;
};

/**
 * Calculates the sum of the rows
 *
 * @return {Number} The sum of the values
 */
mapkob.Row.prototype.sum = function() {
  return this.stateSpace.map(function(state) {
    return this.row[state];
  }, this).reduce(function(x, y) {
    return x + y;
  });
};

/**
 * Calculates relative frequencies of a row
 *
 * @return {Object} A row
 */
mapkob.Row.prototype.probabilities = function() {
  var sum = this.sum();
  var probs = {};
  this.stateSpace.map(function(state) {
    probs[state] = this.row[state] / sum;
  }, this);
  return new mapkob.Row(probs);
};

/**
 * A row with the cumulative sums
 *
 * @return {Object} A row
 */
mapkob.Row.prototype.cumSum = function() {
  var sums = {};
  var sum = 0;
  this.stateSpace.map(function(state) {
    sum += this.row[state];
    sums[state] = sum;
  }, this);
  return new mapkob.Row(sums);
};

/**
 * return an array with the row's values
 *
 * @return {Array} The rows values
 */
mapkob.Row.prototype.values = function() {
  return this.stateSpace.map(function(state) {
    return this.row[state];
  }, this);
};

/**
 * Picks the state with the largest CDF that is smaller or equal to p
 * Technically the first one where the next is larger or the array ends
 *
 * @return {String} A state string
 */
mapkob.Row.prototype.pickState = function(p) {
  cdf = this.values();
  for (var col in cdf) {
    if (p <= cdf[col] && (cdf[col + 1] == undefined || cdf[col + 1] > p)) {
      return this.stateSpace[col];
    }
  }
};
<|endoftext|>


<commit_before>'use strict'

let logger = require('tracer').colorConsole()
let PipelineExecutor = require('../../core/pipelines/pipeline-executor')

module.exports = (msg, next, msgid) => {

  // process your message
  logger.debug('message received. ID: ' + msgid)
  logger.debug(msg)

  // Parse the JSON in the message
  msg = JSON.parse(msg)

  // Validate the incoming job data
  if (!msg.pipeline_execution_id) {
    logger.error('a queue job was submitted to pipeline_executions, but with no pipeline_execution_id')
  }

  new PipelineExecutor(msg.pipeline_execution_id, next)

}
<commit_msg>Remove message id not being used for logic<commit_after>'use strict'

let logger = require('tracer').colorConsole()
let PipelineExecutor = require('../../core/pipelines/pipeline-executor')

module.exports = (msg, next) => {
  logger.debug(msg)

  // Parse the JSON in the message
  msg = JSON.parse(msg)

  // Validate the incoming job data
  if (!msg.pipeline_execution_id) {
    logger.error('a queue job was submitted to pipeline_executions, but with no pipeline_execution_id')
  }

  new PipelineExecutor(msg.pipeline_execution_id, next)
}
<|endoftext|>


<commit_before>function gForcesDebug(){}

gForcesDebug.prototype.initialize = function(){
	this.canvas = $('<canvas></canvas>').appendTo(this.rootElement);
	this.textElement = $('

').appendTo(this.rootElement);

	this.resize();
};

gForcesDebug.prototype.update = function(streams){
	//convert to G
	gForces = {};
	$.each(streams.sensors, function(index, val) {
		gForces[index] = val / 9.81;
	});

	c = this.canvas[0];
	ctx = c.getContext('2d');
	ctx.font = '16px "Lucida Console", Monaco, monospace';
	ctx.textAlign = "right";
	ctx.textBaseline = "middle";

	ctx.clearRect(-100,-100,200,200);

	// Circles
	ctx.fillStyle = "RGBA(128,128,128,0.2)";
	for (var i = 1; i < 3; i++) {
		ctx.beginPath();
		ctx.arc(0,0,50*i,0,2*Math.PI,false);
		ctx.fill();
	}

	// Min/Max-g's
	ctx.strokeStyle = "RGBA(0,0,255,0.7)";
	ctx.lineWidth = 2;
	ctx.lineCap = "round";

	ctx.beginPath();
	ctx.moveTo(gForces.gxMin*50,0);
	ctx.lineTo(gForces.gxMax*50,0);
	ctx.stroke();

	ctx.beginPath();
	ctx.moveTo(0,gForces.gyMin*-50);
	ctx.lineTo(0,gForces.gyMax*-50);
	ctx.stroke();

	// Labels
	ctx.fillStyle = "black";
	for (var i = 1; i < 3; i++) {
		ctx.fillText(i+"g",50*i-5,0);
	}

	ctx.fillStyle = "red";
	ctx.beginPath();
	ctx.arc(gForces.gx*50,gForces.gy*50,5,0,2*Math.PI,false);
	ctx.fill();

	ctx.fillStyle = "green";

	ctx.beginPath();
	ctx.arc(gForces.gx2*50,gForces.gy2*-50,5,0,2*Math.PI,false);
	ctx.fill();

//	this.textElement.html(htmlstr);
};

gForcesDebug.prototype.resize = function(){
	size = Math.min(this.rootElement.height(),this.rootElement.width());
	this.canvas.height(size);
	this.canvas.width(size);

	c = this.canvas[0];
	c.width = size;
	c.height = size;

	ctx = c.getContext('2d');
	ctx.setTransform(c.width/200, 0, 0, c.height/200, c.width/2,c.height/2);
};
<commit_msg>[fixed] gForce visualization<commit_after>function gForcesDebug(){}

gForcesDebug.prototype.initialize = function(){
	this.canvas = $('<canvas></canvas>').appendTo(this.rootElement);
	this.textElement = $('

').appendTo(this.rootElement);

	this.resize();
};

gForcesDebug.prototype.update = function(streams){
	//convert to G
	gForces = {};
	$.each(streams.sensors, function(index, val) {
		gForces[index] = val / 9.81;
	});

	c = this.canvas[0];
	ctx = c.getContext('2d');
	ctx.font = '16px "Lucida Console", Monaco, monospace';
	ctx.textAlign = "right";
	ctx.textBaseline = "middle";

	ctx.clearRect(-100,-100,200,200);

	// Circles
	ctx.fillStyle = "RGBA(128,128,128,0.2)";
	for (var i = 1; i < 3; i++) {
		ctx.beginPath();
		ctx.arc(0,0,50*i,0,2*Math.PI,false);
		ctx.fill();
	}

	// Min/Max-g's
	ctx.strokeStyle = "RGBA(0,0,255,0.7)";
	ctx.lineWidth = 2;
	ctx.lineCap = "round";

	ctx.beginPath();
	ctx.moveTo(gForces.gxMin*50,0);
	ctx.lineTo(gForces.gxMax*50,0);
	ctx.stroke();

	ctx.beginPath();
	ctx.moveTo(0,gForces.gyMin*-50);
	ctx.lineTo(0,gForces.gyMax*-50);
	ctx.stroke();

	// Labels
	ctx.fillStyle = "black";
	for (var i = 1; i < 3; i++) {
		ctx.fillText(i+"g",50*i-5,0);
	}

	ctx.fillStyle = "red";
	ctx.beginPath();
	ctx.arc(gForces.gx*50,gForces.gy*-50,5,0,2*Math.PI,false);
	ctx.fill();

	ctx.fillStyle = "green";

	ctx.beginPath();
	ctx.arc(gForces.gx2*50,gForces.gy2*-50,5,0,2*Math.PI,false);
	ctx.fill();

//	this.textElement.html(htmlstr);
};

gForcesDebug.prototype.resize = function(){
	size = Math.min(this.rootElement.height(),this.rootElement.width());
	this.canvas.height(size);
	this.canvas.width(size);

	c = this.canvas[0];
	c.width = size;
	c.height = size;

	ctx = c.getContext('2d');
	ctx.setTransform(c.width/200, 0, 0, c.height/200, c.width/2,c.height/2);
};
<|endoftext|>


<commit_before>var scatterplot_ready = (function() {
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    svg, prevBounds = null, transform = d3.zoomIdentity;

var x = d3.scaleLinear().range([0, width]),
    x0;

var y = d3.scaleLinear().range([height, 0]),
    y0;

var color = d3.scaleOrdinal(d3.schemeCategory10);

var xAxis = d3.axisBottom(x)
        .tickSize(height)
        .tickPadding(8 - height);

var yAxis = d3.axisRight(y)
        .tickSize(width)
        .tickPadding(8 - width);

var zoom = d3.zoom()
        //.scaleExtent([1, 32])
        .on("zoom", scatterplot_zoomed);

var view, gX, gY, zoomable;

const DEFAULT_SIGMA = 1;
const DEFAULT_FILTER = "default";
const MAX_PREV_IMAGES = 3;
var imageHistory = new History(MAX_PREV_IMAGES);

function scatterplot_update(data) {
    module_update(data);
    scatterplot_update_vis(data);
}

function _db(b) { return b[1]-b[0]; }

function scatterplot_dragstart(d, i) {
    d3.event.sourceEvent.stopPropagation();
    d3.select(this).classed("dragging", true);
}

function scatterplot_dragmove(d, i) {
    d[0] = x0.invert(d3.event.x);
    d[1] = y0.invert(d3.event.y);
    d3.select(this)
        .attr("cx", d3.event.x)
        .attr("cy", d3.event.y);
}

function scatterplot_dragend(d, i) {
    var msg = {};
    d3.select(this).classed("dragging", false);
    msg[i] = d;
    module_input(msg, ignore, progressivis_error, module_id+"/move_point");
}

var node_drag = d3.drag()
        .on("start", scatterplot_dragstart)
        .on("drag", scatterplot_dragmove)
        .on("end", scatterplot_dragend);
/*
 We use a transform that is a bit tricky.
 All the coordinates are translated in pixels by the x0/y0 scales.
 The x/y scales are the zoomed versions managed by the zoom tool.
 However, the zoom changes the transform of the group that contains
 the heatmap and the points in screen coordinates.
 We change the radius of the points to be constant, so we divide them by the
 zoom scale.
 We position everything on the group according to x0/y0 and not x/y, which are
 only used by the axes.

 When a new scatterplot arrives, the bounds can have changed. When
 progressing, the bounds can grow, but through filtering, the bounds
 can also shrink.  So when a new scatterplot arrives, we recompute the
 scale/translation of the zoom component to show the same viewport as
 before.
*/
function scatterplot_update_vis(rawdata) {
    var data = rawdata['scatterplot'],
        bounds = rawdata['bounds'],
        ix, iy, iw, ih;

    if (!data || !bounds) return;
    var index = data['index'];

    if (prevBounds == null) { // first display, not refresh
        prevBounds = bounds;
        x.domain([bounds['xmin'], bounds['xmax']]).nice();
        y.domain([bounds['ymin'], bounds['ymax']]).nice();
        x0 = x.copy();
        y0 = y.copy();
        // zoom.x(x)
        //     .y(y);

        // svg.append("rect")
        //     .attr("x", 0)
        //     .attr("y", 0)
        //     .attr("width", width)
        //     .attr("height", height)
        //     .style("fill", "none")
        //     .style("pointer-events", "all");

        zoomable = svg.append("g")
            .attr('id', 'zoomable')
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        ix = x(bounds['xmin']),
        iy = y(bounds['ymax']),
        iw = x(bounds['xmax'])-ix,
        ih = y(bounds['ymin'])-iy;

        zoomable.append("image")
            .attr("class", "heatmap")
            .style("pointer-events",  "none")
            .attr("xlink:href", rawdata['image'])
            .attr("preserveAspectRatio", "none")
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih)
            .attr("filter", "url(#gaussianBlur)");

        svg.append("image")
            .attr("class", "heatmapCompare")
            .style("pointer-events",  "none")
            .attr("preserveAspectRatio", "none")
            .attr("opacity", 0.5)
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih);

        gX = svg.append("g")
            .attr("class", "x axis axis--x")
            .call(xAxis);

        gY = svg.append("g")
            .attr("class", "y axis axis--y")
            .call(yAxis);

        svg.call(zoom);
        firstTime = false;
    }
    else { // prevBounds != null
        var changed = false;
        for (v in prevBounds) {
            if (prevBounds[v] != bounds[v]) {
                changed = true;
                break;
            }
        }
        if (changed) {
            var x_bounds = [prevBounds.xmin, prevBounds.xmax],
                y_bounds = [prevBounds.ymin, prevBounds.ymax];

            prevBounds = bounds;
            x.domain([bounds['xmin'], bounds['xmax']]).nice();
            y.domain([bounds['ymin'], bounds['ymax']]).nice();
            x0 = x.copy();
            y0 = y.copy();
            // zoom.x(x)
            //     .y(y)
            //     .translate(translate)
            //     .scale(scale);
            // x.domain(x0.range()
            //          .map(function(x) {
            //              return (x - translate[0]) / scale; })
            //          .map(x0.invert));
        }

        ix = x0(bounds['xmin']);
        iy = y0(bounds['ymax']);
        iw = x0(bounds['xmax'])-ix;
        ih = y0(bounds['ymin'])-iy;
        svg.select(".heatmap")
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih)
            .attr("xlink:href", rawdata['image']);

        svg.select(".heatmapCompare")
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih);

        //zoom.event(svg); // propagate
    }
    var imgSrc = rawdata['image'];
    imageHistory.enqueueUnique(imgSrc);

    var prevImgElements = d3.select("#prevImages")
            .selectAll("img")
            .data(imageHistory.getItems(), function(d){ return d; });

    prevImgElements.enter()
        .append("img")
        .attr("width", 50)
        .attr("height", 50)
        .on("mouseover", function(d){
          d3.select(".heatmapCompare")
            .attr("xlink:href", d)
            .attr("visibility", "inherit");
        })
        .on("mouseout", function(d){
          d3.select(".heatmapCompare")
            .attr("visibility", "hidden");
        });

    prevImgElements.transition().duration(500)
        .attr("src", function(d){ return d; })
        .attr("width", 100)
        .attr("height", 100);

    prevImgElements.exit().transition().duration(500)
        .attr("width", 5)
        .attr("height", 5)
        .remove();

    var dots = zoomable
            .selectAll(".dot")
             .data(data['data'], function(d, i) { return index[i]; });

    dots.enter().append("circle")
         .attr("class", "dot")
         .attr("r", 3.5/transform.k)
         .attr("cx", function(d) { return x0(d[0]); }) // use untransformed x0/y0
         .attr("cy", function(d) { return y0(d[1]); })
         .style("fill", "blue") //function(d) { return color(d.species); });
         .call(node_drag)
        .append("title")
        .text(function(d, i) { return index[i]; });
    dots//.transition()  // Transition from old to new
        //.duration(500)  // Length of animation
         .attr("cx", function(d) { return x0(d[0]); })
         .attr("cy", function(d) { return y0(d[1]); });
    dots.exit().remove();
    dots.order();
}

function scatterplot_zoomed(transform) {
    if (transform === undefined)
        transform = d3.event.transform;
    gX.call(xAxis.scale(transform.rescaleX(x)));
    gY.call(yAxis.scale(transform.rescaleY(y)));
    zoomable.attr("transform", transform);
    svg.selectAll(".dot").attr("r", 3.5/transform.k);
}

function scatterplot_refresh() {
  module_get(scatterplot_update, error);
}

/**
 * @param select - a select element that will be mutated
 * @param names - list of option names (the value of an option is set to its name)
 */
function makeOptions(select, names){
  if(!select){
    console.warn("makeOptions requires an existing select element");
    return;
  }
  names.forEach(function(name){
    var option = document.createElement("option");
    option.setAttribute("value", name);
    option.innerHTML = name;
    select.appendChild(option);
  });
}

function ignore(data) {}

function scatterplot_filter() {
    var xmin    = x.invert(0),
        xmax    = x.invert(width),
        ymin    = y.invert(height),
        ymax    = y.invert(0),
        bounds  = progressivis_data['bounds'],
        columns = progressivis_data['columns'],
        min     = {},
        max     = {};

    if (xmin > bounds['xmin'])
        min[columns[0]] = xmin;
    else
        min[columns[0]] = null; // NaN means bump to min
    if (xmax < bounds['xmax'])
        max[columns[0]] = xmax;
    else
        max[columns[0]] = null;

    if (ymin > bounds['ymin'])
        min[columns[1]] = ymin;
    else
        min[columns[1]] = null;

    if (ymax < bounds['ymax'])
        max[columns[1]] = ymax;
    else
        max[columns[1]] = null;

    module_input(min, ignore, progressivis_error, module_id+"/min_value");
    module_input(max, ignore, progressivis_error, module_id+"/max_value");
}

function scatterplot_ready() {
    svg = d3.select("#scatterplot svg")
         .attr("width", width + margin.left + margin.right)
         .attr("height", height + margin.top + margin.bottom);
        //.append("g")
        //.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        //.call(zoom);

    $('#nav-tabs a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    });

    const gaussianBlur = document.getElementById("gaussianBlurElement");
    const filterSlider = $("#filterSlider");
    filterSlider.change(function(){
      gaussianBlur.setStdDeviation(this.value, this.value);
    });
    filterSlider.get(0).value = DEFAULT_SIGMA;
    gaussianBlur.setStdDeviation(DEFAULT_SIGMA, DEFAULT_SIGMA);

    const colorMap = document.getElementById("colorMap");
    const colorMapSelect = $("#colorMapSelect");
    colorMapSelect.change(function(){
      colormaps.makeTableFilter(colorMap, this.value);
    });
    colorMapSelect.get(0).value = DEFAULT_FILTER;
    makeOptions(colorMapSelect.get(0), colormaps.getTableNames());

    $('#filter').click(function() { scatterplot_filter(); });

    refresh = scatterplot_refresh; // function to call to refresh
    module_ready();
}
return scatterplot_ready;
})();
<commit_msg>Fix filter callback<commit_after>var scatterplot_ready = (function() {
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    svg, prevBounds = null, transform = d3.zoomIdentity;

var x = d3.scaleLinear().range([0, width]),
    x0;

var y = d3.scaleLinear().range([height, 0]),
    y0;

var color = d3.scaleOrdinal(d3.schemeCategory10);

var xAxis = d3.axisBottom(x)
        .tickSize(height)
        .tickPadding(8 - height);

var yAxis = d3.axisRight(y)
        .tickSize(width)
        .tickPadding(8 - width);

var zoom = d3.zoom()
        //.scaleExtent([1, 32])
        .on("zoom", scatterplot_zoomed);

var view, gX, gY, zoomable;

const DEFAULT_SIGMA = 1;
const DEFAULT_FILTER = "default";
const MAX_PREV_IMAGES = 3;
var imageHistory = new History(MAX_PREV_IMAGES);

function scatterplot_update(data) {
    module_update(data);
    scatterplot_update_vis(data);
}

function _db(b) { return b[1]-b[0]; }

function scatterplot_dragstart(d, i) {
    d3.event.sourceEvent.stopPropagation();
    d3.select(this).classed("dragging", true);
}

function scatterplot_dragmove(d, i) {
    d[0] = x0.invert(d3.event.x);
    d[1] = y0.invert(d3.event.y);
    d3.select(this)
        .attr("cx", d3.event.x)
        .attr("cy", d3.event.y);
}

function scatterplot_dragend(d, i) {
    var msg = {};
    d3.select(this).classed("dragging", false);
    msg[i] = d;
    module_input(msg, ignore, progressivis_error, module_id+"/move_point");
}

var node_drag = d3.drag()
        .on("start", scatterplot_dragstart)
        .on("drag", scatterplot_dragmove)
        .on("end", scatterplot_dragend);
/*
 We use a transform that is a bit tricky.
 All the coordinates are translated in pixels by the x0/y0 scales.
 The x/y scales are the zoomed versions managed by the zoom tool.
 However, the zoom changes the transform of the group that contains
 the heatmap and the points in screen coordinates.
 We change the radius of the points to be constant, so we divide them by the
 zoom scale.
 We position everything on the group according to x0/y0 and not x/y, which are
 only used by the axes.

 When a new scatterplot arrives, the bounds can have changed. When
 progressing, the bounds can grow, but through filtering, the bounds
 can also shrink.  So when a new scatterplot arrives, we recompute the
 scale/translation of the zoom component to show the same viewport as
 before.
*/
function scatterplot_update_vis(rawdata) {
    var data = rawdata['scatterplot'],
        bounds = rawdata['bounds'],
        ix, iy, iw, ih;

    if (!data || !bounds) return;
    var index = data['index'];

    if (prevBounds == null) { // first display, not refresh
        prevBounds = bounds;
        x.domain([bounds['xmin'], bounds['xmax']]).nice();
        y.domain([bounds['ymin'], bounds['ymax']]).nice();
        x0 = x.copy();
        y0 = y.copy();

        // svg.append("rect")
        //     .attr("x", 0)
        //     .attr("y", 0)
        //     .attr("width", width)
        //     .attr("height", height)
        //     .style("fill", "none")
        //     .style("pointer-events", "all");

        zoomable = svg.append("g")
            .attr('id', 'zoomable')
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        ix = x(bounds['xmin']),
        iy = y(bounds['ymax']),
        iw = x(bounds['xmax'])-ix,
        ih = y(bounds['ymin'])-iy;

        zoomable.append("image")
            .attr("class", "heatmap")
            .style("pointer-events",  "none")
            .attr("xlink:href", rawdata['image'])
            .attr("preserveAspectRatio", "none")
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih)
            .attr("filter", "url(#gaussianBlur)");

        svg.append("image")
            .attr("class", "heatmapCompare")
            .style("pointer-events",  "none")
            .attr("preserveAspectRatio", "none")
            .attr("opacity", 0.5)
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih);

        gX = svg.append("g")
            .attr("class", "x axis axis--x")
            .call(xAxis);

        gY = svg.append("g")
            .attr("class", "y axis axis--y")
            .call(yAxis);

        svg.call(zoom);
        firstTime = false;
    }
    else { // prevBounds != null
        var changed = false;
        for (v in prevBounds) {
            if (prevBounds[v] != bounds[v]) {
                changed = true;
                break;
            }
        }
        if (changed) {
            var x_bounds = [prevBounds.xmin, prevBounds.xmax],
                y_bounds = [prevBounds.ymin, prevBounds.ymax];

            console.log('Bounds have changed from '+prevBounds+' to '+bounds);
            prevBounds = bounds;
            x.domain([bounds['xmin'], bounds['xmax']]).nice();
            y.domain([bounds['ymin'], bounds['ymax']]).nice();
            x0 = x.copy();
            y0 = y.copy();
        }

        ix = x0(bounds['xmin']);
        iy = y0(bounds['ymax']);
        iw = x0(bounds['xmax'])-ix;
        ih = y0(bounds['ymin'])-iy;
        svg.select(".heatmap")
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih)
            .attr("xlink:href", rawdata['image']);

        svg.select(".heatmapCompare")
            .attr("x", ix)
            .attr("y", iy)
            .attr("width",  iw)
            .attr("height", ih);

        //zoom.event(svg); // propagate
    }
    var imgSrc = rawdata['image'];
    imageHistory.enqueueUnique(imgSrc);

    var prevImgElements = d3.select("#prevImages")
            .selectAll("img")
            .data(imageHistory.getItems(), function(d){ return d; });

    prevImgElements.enter()
        .append("img")
        .attr("width", 50)
        .attr("height", 50)
        .on("mouseover", function(d){
          d3.select(".heatmapCompare")
            .attr("xlink:href", d)
            .attr("visibility", "inherit");
        })
        .on("mouseout", function(d){
          d3.select(".heatmapCompare")
            .attr("visibility", "hidden");
        });

    prevImgElements.transition().duration(500)
        .attr("src", function(d){ return d; })
        .attr("width", 100)
        .attr("height", 100);

    prevImgElements.exit().transition().duration(500)
        .attr("width", 5)
        .attr("height", 5)
        .remove();

    var dots = zoomable
            .selectAll(".dot")
             .data(data['data'], function(d, i) { return index[i]; });

    dots.enter().append("circle")
         .attr("class", "dot")
         .attr("r", 3.5/transform.k)
         .attr("cx", function(d) { return x0(d[0]); }) // use untransformed x0/y0
         .attr("cy", function(d) { return y0(d[1]); })
         .style("fill", "blue") //function(d) { return color(d.species); });
         .call(node_drag)
        .append("title")
        .text(function(d, i) { return index[i]; });
    dots .attr("cx", function(d) { return x0(d[0]); })
         .attr("cy", function(d) { return y0(d[1]); });
    dots.exit().remove();
    dots.order();
}

function scatterplot_zoomed(t) {
    if (t === undefined)
        t = d3.event.transform;
    transform = t;
    gX.call(xAxis.scale(transform.rescaleX(x)));
    gY.call(yAxis.scale(transform.rescaleY(y)));
    zoomable.attr("transform", transform);
    svg.selectAll(".dot").attr("r", 3.5/transform.k);
}

function scatterplot_refresh() {
  module_get(scatterplot_update, error);
}

/**
 * @param select - a select element that will be mutated
 * @param names - list of option names (the value of an option is set to its name)
 */
function makeOptions(select, names){
  if(!select){
    console.warn("makeOptions requires an existing select element");
    return;
  }
  names.forEach(function(name){
    var option = document.createElement("option");
    option.setAttribute("value", name);
    option.innerHTML = name;
    select.appendChild(option);
  });
}

function ignore(data) {}

function scatterplot_filter() {
    var xmin = xAxis.scale().invert(0),
        xmax = xAxis.scale().invert(width),
        ymin = yAxis.scale().invert(height),
        ymax = yAxis.scale().invert(0),
        bounds  = prevBounds,
        columns = progressivis_data['columns'],
        min     = {},
        max     = {};

    if (xmin > bounds['xmin'])
        min[columns[0]] = xmin;
    else
        min[columns[0]] = null; // NaN means bump to min
    if (xmax < bounds['xmax'])
        max[columns[0]] = xmax;
    else
        max[columns[0]] = null;

    if (ymin > bounds['ymin'])
        min[columns[1]] = ymin;
    else
        min[columns[1]] = null;

    if (ymax < bounds['ymax'])
        max[columns[1]] = ymax;
    else
        max[columns[1]] = null;

    module_input(min, ignore, progressivis_error, module_id+"/min_value");
    module_input(max, ignore, progressivis_error, module_id+"/max_value");
}

function scatterplot_ready() {
    svg = d3.select("#scatterplot svg")
         .attr("width", width + margin.left + margin.right)
         .attr("height", height + margin.top + margin.bottom);
        //.append("g")
        //.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        //.call(zoom);

    $('#nav-tabs a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    });

    const gaussianBlur = document.getElementById("gaussianBlurElement");
    const filterSlider = $("#filterSlider");
    filterSlider.change(function(){
      gaussianBlur.setStdDeviation(this.value, this.value);
    });
    filterSlider.get(0).value = DEFAULT_SIGMA;
    gaussianBlur.setStdDeviation(DEFAULT_SIGMA, DEFAULT_SIGMA);

    const colorMap = document.getElementById("colorMap");
    const colorMapSelect = $("#colorMapSelect");
    colorMapSelect.change(function(){
      colormaps.makeTableFilter(colorMap, this.value);
    });
    colorMapSelect.get(0).value = DEFAULT_FILTER;
    makeOptions(colorMapSelect.get(0), colormaps.getTableNames());

    $('#filter').click(function() { scatterplot_filter(); });

    refresh = scatterplot_refresh; // function to call to refresh
    module_ready();
}
return scatterplot_ready;
})();
<|endoftext|>


<commit_before>import React, { Component } from "react"

import styles from "./index.css"
import logo from "./evilmartians.svg"

export default class Footer extends Component {

  render() {
    return (

              { "Distributed under the MIT License." }

{ "Found an issue?" }

                { "Report it!" }

    )
  }
}
<commit_msg>Add Evil Martians link to footer<commit_after>import React, { Component } from "react"

import styles from "./index.css"
import logo from "./evilmartians.svg"

export default class Footer extends Component {

  render() {
    return (

              { "Distributed under the MIT License." }

{ "Found an issue?" }

                { "Report it!" }

    )
  }
}
<|endoftext|>


<commit_before>#!/usr/bin/env node

import webpack from 'webpack';
import program from 'commander';
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
import shelljs from 'shelljs';
import packageJson from '../../package.json';
import getBaseConfig from './config/webpack.config.prod';
import loadConfig from './config';
import getIndexHtml from './index.html';
import getIframeHtml from './iframe.html';
import { getPreviewHeadHtml, getManagerHeadHtml, parseList, getEnvConfig } from './utils';

process.env.NODE_ENV = process.env.NODE_ENV || 'production';

// avoid ESLint errors
const logger = console;

program
  .version(packageJson.version)
  .option('-s, --static-dir <dir-names>', 'Directory where to load static files from', parseList)
  .option('-o, --output-dir [dir-name]', 'Directory where to store built files')
  .option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from')
  .option('-d, --db-path [db-file]', 'DEPRECATED!')
  .option('--enable-db', 'DEPRECATED!')
  .parse(process.argv);

logger.info(chalk.bold(`${packageJson.name} v${packageJson.version}\n`));

if (program.enableDb || program.dbPath) {
  logger.error(
    [
      'Error: the experimental local database addon is no longer bundled with',
      'react-storybook. Please remove these flags (-d,--db-path,--enable-db)',
      'from the command or npm script and try again.',
    ].join(' ')
  );
  process.exit(1);
}

// The key is the field created in `program` variable for
// each command line argument. Value is the env variable.
getEnvConfig(program, {
  staticDir: 'SBCONFIG_STATIC_DIR',
  outputDir: 'SBCONFIG_OUTPUT_DIR',
  configDir: 'SBCONFIG_CONFIG_DIR',
});

const configDir = program.configDir || './.storybook';
const outputDir = program.outputDir || './storybook-static';

// create output directory (and the static dir) if not exists
shelljs.rm('-rf', outputDir);
shelljs.mkdir('-p', path.resolve(outputDir));
shelljs.cp(path.resolve(__dirname, 'public/favicon.ico'), outputDir);

// Build the webpack configuration using the `baseConfig`
// custom `.babelrc` file and `webpack.config.js` files
// NOTE changes to env should be done before calling `getBaseConfig`
const config = loadConfig('PRODUCTION', getBaseConfig(), configDir);
config.output.path = path.resolve(outputDir);

// copy all static files
if (program.staticDir) {
  program.staticDir.forEach(dir => {
    if (!fs.existsSync(dir)) {
      logger.error(`Error: no such directory to load static files: ${dir}`);
      process.exit(-1);
    }
    logger.log(`=> Copying static files from: ${dir}`);
    shelljs.cp('-r', `${dir}/*`, outputDir);
  });
}

// compile all resources with webpack and write them to the disk.
logger.log('Building storybook ...');
webpack(config).run((err, stats) => {
  if (err) {
    logger.error('Failed to build the storybook');
    logger.error(err.message);
    process.exit(1);
  }

  const data = {
    publicPath: config.output.publicPath,
    assets: stats.toJson().assetsByChunkName,
  };

  // Write both the storybook UI and IFRAME HTML files to destination path.
  fs.writeFileSync(
    path.resolve(outputDir, 'index.html'),
    getIndexHtml({ ...data, headHtml: getManagerHeadHtml(configDir) })
  );
  fs.writeFileSync(
    path.resolve(outputDir, 'iframe.html'),
    getIframeHtml({ ...data, headHtml: getPreviewHeadHtml(configDir) })
  );
});
<commit_msg>FIX build-storybook for angular<commit_after>import webpack from 'webpack';
import program from 'commander';
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
import shelljs from 'shelljs';
import packageJson from '../../package.json';
import getBaseConfig from './config/webpack.config.prod';
import loadConfig from './config';
import { parseList, getEnvConfig } from './utils';

process.env.NODE_ENV = process.env.NODE_ENV || 'production';

// avoid ESLint errors
const logger = console;

program
  .version(packageJson.version)
  .option('-s, --static-dir <dir-names>', 'Directory where to load static files from', parseList)
  .option('-o, --output-dir [dir-name]', 'Directory where to store built files')
  .option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from')
  .option('-d, --db-path [db-file]', 'DEPRECATED!')
  .option('--enable-db', 'DEPRECATED!')
  .parse(process.argv);

logger.info(chalk.bold(`${packageJson.name} v${packageJson.version}\n`));

if (program.enableDb || program.dbPath) {
  logger.error(
    [
      'Error: the experimental local database addon is no longer bundled with',
      'react-storybook. Please remove these flags (-d,--db-path,--enable-db)',
      'from the command or npm script and try again.',
    ].join(' ')
  );
  process.exit(1);
}

// The key is the field created in `program` variable for
// each command line argument. Value is the env variable.
getEnvConfig(program, {
  staticDir: 'SBCONFIG_STATIC_DIR',
  outputDir: 'SBCONFIG_OUTPUT_DIR',
  configDir: 'SBCONFIG_CONFIG_DIR',
});

const configDir = program.configDir || './.storybook';
const outputDir = program.outputDir || './storybook-static';

// create output directory if not exists
shelljs.mkdir('-p', path.resolve(outputDir));
// clear the static dir
shelljs.rm('-rf', path.resolve(outputDir, 'static'));
shelljs.cp(path.resolve(__dirname, 'public/favicon.ico'), outputDir);

// Build the webpack configuration using the `baseConfig`
// custom `.babelrc` file and `webpack.config.js` files
// NOTE changes to env should be done before calling `getBaseConfig`
const config = loadConfig('PRODUCTION', getBaseConfig(), configDir);
config.output.path = path.resolve(outputDir);

// copy all static files
if (program.staticDir) {
  program.staticDir.forEach(dir => {
    if (!fs.existsSync(dir)) {
      logger.error(`Error: no such directory to load static files: ${dir}`);
      process.exit(-1);
    }
    logger.log(`=> Copying static files from: ${dir}`);
    shelljs.cp('-r', `${dir}/*`, outputDir);
  });
}

// compile all resources with webpack and write them to the disk.
logger.log('Building storybook ...');
webpack(config).run((err, stats) => {
  if (err || stats.hasErrors()) {
    logger.error('Failed to build the storybook');
    // eslint-disable-next-line no-unused-expressions
    err && logger.error(err.message);
    // eslint-disable-next-line no-unused-expressions
    stats.hasErrors() && stats.toJson().errors.forEach(e => logger.error(e));
    process.exit(1);
  }
});
<|endoftext|>


<commit_before>/* eslint-disable */

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Route, Redirect, withRouter } from 'react-router-dom'

const PrivateRouteComponent = props => {
  const { component: Component, loggedIn, customRedirectPath, ...rest } = props
  return (
    <Route
      {...rest}
      render={values =>
        loggedIn
          ? <Component {...values} />
          : <Redirect
              to={{
                pathname: customRedirectPath ? customRedirectPath : '/2/login',
                state: { from: values.location }
              }}
            />}
    />
  )
}

PrivateRouteComponent.propTypes = {
  component: PropTypes.func.isRequired,
  loggedIn: PropTypes.bool.isRequired
}

const mapStateToProps = state => ({
  loggedIn: state.app.loggedIn
})

const PrivateRoute = withRouter(connect(mapStateToProps)(PrivateRouteComponent))

export default PrivateRoute
<commit_msg>Fix private route flashing login<commit_after>/* eslint-disable */
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Route, Redirect, withRouter } from 'react-router-dom'

const PrivateRouteComponent = props => {
  const { component: Component, loggedIn, customRedirectPath, currentlySending, ...rest } = props
  return (
    <Route
      {...rest}
      render={values =>
        loggedIn || currentlySending
          ? <Component {...values} />
          : <Redirect
              to={{
                pathname: customRedirectPath ? customRedirectPath : '/2/login',
                state: { from: values.location }
              }}
            />}
    />
  )
}

PrivateRouteComponent.propTypes = {
  component: PropTypes.func.isRequired,
  loggedIn: PropTypes.bool.isRequired
}

const mapStateToProps = state => ({
  loggedIn: state.app.loggedIn,
  currentlySending: state.app.currentlySending
})

const PrivateRoute = withRouter(connect(mapStateToProps)(PrivateRouteComponent))

export default PrivateRoute
<|endoftext|>


<commit_before>jQuery.fn.badge = function(config) {
  var elem = this;
  var sort_random = function() {
    return (Math.round(Math.random())-0.5);
  };

  jQuery.ajaxSetup({cache: true});

  var show_photos = function(data) {
    var photo_array = data.photos.photo;
    var link_text = config.link_text || "See more of my photos on 23";
    photo_array.sort(sort_random);
    $.each(photo_array.slice(0, config.show || 10), function(index, photo) {
      $("").attr("src",
        'http://www.23hq.com/{{server}}/{{filename}}'
          .replace(/{{server}}/, photo.server)
          .replace(/{{filename}}/, [photo.id, photo.secret, 's'].join('_')+'.jpg'))
        .attr("alt", photo.title)
        .attr("title", photo.title)
        .attr("class", "badge-photo")
        .attr("width", config.size || 100)
        .attr("height", config.size || 100)
        .appendTo(elem.selector)
        .wrap(''
          .replace(/{{user}}/, config.user_name)
          .replace(/{{photo}}/, photo.id)
        );
    });
    $(elem).append('
'+ link_text +'
');
  };

  jQuery.getJSON('http://www.23hq.com/services/rest?user_id='+config.user_id+'&method=flickr.people.getPublicPhotos&format=json&jsoncallback=?', show_photos);
};
<commit_msg>Limit number of photos requested via API to 30<commit_after>jQuery.fn.badge = function(config) {
  var elem = this;
  var sort_random = function() {
    return (Math.round(Math.random())-0.5);
  };

  jQuery.ajaxSetup({cache: true});

  var show_photos = function(data) {
    var photo_array = data.photos.photo;
    var link_text = config.link_text || "See more of my photos on 23";
    photo_array.sort(sort_random);
    $.each(photo_array.slice(0, config.show || 10), function(index, photo) {
      $("").attr("src",
        'http://www.23hq.com/{{server}}/{{filename}}'
          .replace(/{{server}}/, photo.server)
          .replace(/{{filename}}/, [photo.id, photo.secret, 's'].join('_')+'.jpg'))
        .attr("alt", photo.title)
        .attr("title", photo.title)
        .attr("class", "badge-photo")
        .attr("width", config.size || 100)
        .attr("height", config.size || 100)
        .appendTo(elem.selector)
        .wrap(''
          .replace(/{{user}}/, config.user_name)
          .replace(/{{photo}}/, photo.id)
        );
    });
    $(elem).append('
'+ link_text +'
');
  };

  jQuery.getJSON('http://www.23hq.com/services/rest?user_id='+config.user_id+'&method=flickr.people.getPublicPhotos&per_page=30&format=json&jsoncallback=?', show_photos);
};
<|endoftext|>