Example #1
0
		createTree: function(dataset, features) {
			var targets = _.unique(_.pluck(dataset, 'output'));
			if (targets.length == 1){
                // console.log("end node! "+targets[0]);
                return {type:"result", val: targets[0], name: targets[0], alias:targets[0]+this.randomTag() }; 
        	}
        	 if(features.length == 0){
                // console.log("returning the most dominate feature!!!");
                var topTarget = this.mostCommon(targets);
                return {type:"result", val: topTarget, name: topTarget, alias: topTarget+this.randomTag()};
        	}
        	var bestFeature = this.maxGain(dataset, features);
			var remainingFeatures = _.without(features,bestFeature);
        	var possibleValues = _.unique(_.pluck(_.pluck(dataset, 'input'), bestFeature));
        	var node = {name: bestFeature,alias: bestFeature+this.randomTag()};
        	node.type = "feature";
        	node.vals = _.map(possibleValues,function(v){
                var _newS = dataset.filter(function(x) {return x['input'][bestFeature] == v});
                var child_node = {name:v,alias:v+this.randomTag(),type: "feature_value"};
                child_node.child =  this.createTree(_newS,remainingFeatures);
                return child_node;
        }, this);
        
        return node;
		},
Example #2
0
            engine.exec(script, function(err, results) {
                if(err) {
                    console.log(err.stack || err);
                    test.ok(false, 'Grrr');
                    test.done();
                }
                else {
                    results = results.body;
                    test.ok(results.i1);
                    test.ok(results.i2);
                    test.ok(results.ids);
                    var i1 = _.sortBy(results.i1, function(id) {
                        return id;
                    });
                    i1 = _.unique(i1);
                    var i2 = _.sortBy(results.i2, function(id) {
                        return id;
                    });
                    i2 = _.unique(i2);
                    var ids = _.sortBy(results.ids, function(id) {
                        return id;
                    });
                    ids = _.unique(ids);
                    test.equals(i1.length + i2.length, ids.length);

                    test.equals(results.txb.length, 2);
                    test.equals(results.txs.length, 2);
                    test.ok(results.be === undefined);
                    test.ok(results.se === undefined);
                    test.done();
                }
                server.close();

            });
function createTree(_s, target, features) {
	var targets = _.unique(_.pluck(_s, target));
	if (targets.length == 1){
		// console.log("end node! "+targets[0]);
		return {type:"result", val: targets[0], name: targets[0],alias:targets[0]+randomTag() }; 
	}
	if(features.length == 0){
		// console.log("returning the most dominate feature!!!");
		var topTarget = mostCommon(targets);
		return {type:"result", val: topTarget, name: topTarget, alias: topTarget+randomTag()};
	}
	var bestFeature = maxGain(_s,target,features);
	var remainingFeatures = _.without(features,bestFeature);
	var possibleValues = _.unique(_.pluck(_s, bestFeature));
	var node = {name: bestFeature,alias: bestFeature+randomTag()};
	node.type = "feature";
	node.vals = _.map(possibleValues,function(v){
		// console.log("creating a branch for "+v);
		var _newS = _s.filter(function(x) {return x[bestFeature] == v});
		var child_node = {name:v,alias:v+randomTag(),type: "feature_value"};
		child_node.child =  createTree(_newS,target,remainingFeatures);
		return child_node;
	});
	
	return node;
}
Example #4
0
var ID3 = function(data, features, y){
    var y_values = _.unique(_.pluck(data, y));

    // last leaf
    if (y_values.length == 1){
        return {
            type: "result",
            val: y_values[0],
            name: y_values[0],
            alias: y_values[0] + RID()
        };
    }

    if (features === true || y_values.length == 0){
        // end of branch
        // returning the most dominate feature
        var dominate_y = GetDominate(_.pluck(data, y));
        return {
            type:"result",
            val: dominate_y,
            name: dominate_y,
            alias: dominate_y + RID()
        };
    }

    if (!features || features.length == 0){
        // get all the features that are not y
        features = _.reject(_.keys(data[0]), function(f){ return f == y; });
    }

    var best_feature = _.max(features, function(f){return Gain(data, f, y).gain; });
    var feature_remains = _.without(features, best_feature);
    var possibilities = _.unique(_.pluck(data, best_feature));
    var tree = {
        name: best_feature,
        alias: best_feature + RID(),
        type: "feature"
    };

    // create the branch of the tree
    tree.vals = _.map(possibilities, function(v){
        var data_modified = data.filter(function(x) { return x[best_feature] == v; });

        var branch = {
            name: v,
            alias: v + RID(),
            type: "feature_value"
        };

        if (feature_remains.length == 0){
            feature_remains = true;
        }
        branch.child = ID3(data_modified, feature_remains, y);

        return branch;
    });

    return tree;
};
Example #5
0
DBSelect.prototype.assemble = DBSelect.prototype.toString = function()
{
  
  var completeQuery = (this._what.length > 0) ? true : false ;
  
  var sql = ( completeQuery ) ? 'SELECT ' : '';
  
  // Requested fields : this._what
  if( completeQuery )
  {
    if( this._distinct )
      sql += 'DISTINCT(' + this._what.join(', ') +') ';
    else
      sql += this._what.join(', ');
  }

  // "FROM" clause : this._from
  if( this._from.length > 0 )
  {
    this._from = _.unique( this._from );
    sql += ' FROM ' + this._from.join(', ');
  }
  
  // "JOIN" clause : this._join
  if( this._join.length > 0 )
  {
    this._join = _.unique( this._join );
    sql += '  ' + this._join.join(' ');
  }
    
  // "WHERE" clause : this._where
  if( this._where.length > 0 )
  {
    this._where = _.unique( this._where );
    sql += ( (completeQuery) ? ' WHERE ' : '' ) + this._where.join(' AND ');
  }
  
  // "GROUP BY" clause : this._group
  if( this._group && this._group.length>0 )
    sql += ' GROUP BY ' + this._group;
  
 
  //"ORDER BY" clause : this._order
  if( this._order.length > 0 )
    sql += ' ORDER BY ' + this._order.join(', ');
  // "LIMIT" clause : this._limit
  if( this._limit.length > 0 )
    sql += ' LIMIT ' + this._limit.join(', ');
 
  return sql;

};
Example #6
0
var getPartials = function(src) {
  var nodes = handlebars.parse(src);
  
  function recursiveNodeSearch( statements, res ) {
    statements.forEach(function ( statement ) {
      if ( statement && statement.type === 'partial' ) {
        res.push(statement.id.string);
      }
      if ( statement && statement.program && statement.program.statements ) {
        recursiveNodeSearch( statement.program.statements, res );
      }
      if ( statement && statement.program && statement.program.inverse && statement.program.inverse.statements ) {
        recursiveNodeSearch( statement.program.inverse.statements, res );
      }
    });
    return res;
  }
  
  var res = [];
  
  if ( nodes && nodes.statements ) {
    res = recursiveNodeSearch( nodes.statements, [] );
  }
  
  return _.unique(res);
};
    var createQrCodes = function (done) {
      //
      // Figure out what images we need to fetch, if any
      //
      var allElements = _.flatten(_.union(reportDefinition.headerElements,
          reportDefinition.detailElements, reportDefinition.footerElements)),
        allQrElements = _.unique(_.pluck(_.where(allElements, {element: "image", imageType: "qr"}), "definition")),
        marriedQrElements = _.map(allQrElements, function (el) {
          return {
            source: el[0].attr || el[0].text,
            target: marryData(el, reportData[0])[0].data
          };
        });
        // thanks Jeremy

      if (allQrElements.length === 0) {
        // no need to try to fetch no images
        done();
        return;
      }

      async.eachSeries(marriedQrElements, function (element, next) {
        var targetFilename = element.target.replace(/\W+/g, "") + ".png",
        qr_svg = qr.image(element.target, { type: 'png' }),
        writeStream = fs.createWriteStream(path.join(workingDir, targetFilename));

        qr_svg.pipe(writeStream);
        writeStream.on("finish", function () {
          imageFilenameMap[element.source] = targetFilename;
          next();
        });

      }, done);
    };
Example #8
0
    var fetchImages = function (done) {
      //
      // Figure out what images we need to fetch, if any
      //
      var allElements = _.flatten(_.union(reportDefinition.headerElements,
          reportDefinition.detailElements, reportDefinition.footerElements)),
        allImages = _.unique(_.pluck(_.where(allElements, {element: "image"}), "definition"));
        // thanks Jeremy

      if (allImages.length === 0) {
        // no need to try to fetch no images
        done();
        return;
      }

      //
      // TODO: use the working dir as a cache
      //

      //
      // Get the images
      //
      queryDatabaseForImages(allImages, function (err, fileCollection) {
        //
        // Write the images to the filesystem
        //
        async.map(fileCollection.models, writeImageToFilesystem, done);
      });
    };
Example #9
0
  _getPeople: function () {
    var types = this._acl.pluck('type');
    if (types.indexOf('org') > -1) {
      return this._userModel.usersOrganization.total_user_entries;
    } else {
      var users = [];

      // Grab all ids from every group
      _.each(this._acl.where({type: 'group'}), function (group) {
        var entity = group.get('entity');
        Array.prototype.push.apply(users, entity.users.pluck('id'));
      }, this);

      // Grab all ids from every user
      _.each(this._acl.where({type: 'user'}), function (group) {
        var entity = group.get('entity');
        users.push(entity.get('id'));
      }, this);

      // remove current user id because it can be part of a group
      users = _.without(users, this._userModel.id);

      // counts unique ids
      return _.unique(users).length;
    }
  },
Example #10
0
var Entropy = function(vals){
    var unique = _.unique(vals),
        probs = unique.map(function(x){ return Probability(x, vals); }),
        logs = probs.map(function(p){ return -p*Log2(p); });

    return logs.reduce(function(a, b){ return a+b; }, 0);
};
 Object.keys(externalMessages).forEach(function (locale) {
     var localeFile = dest.replace(that.options.localePlaceholder, locale),
             localFileExists = grunt.file.exists(localeFile),
             externalLocaleData = externalMessages[locale],
             dataId;
     if (localFileExists) {
         messages = grunt.file.readJSON(localeFile);
         grunt.log.writeln('Parsed locale messages from ' + localeFile.cyan + '.');
     }
     for (dataId in externalLocaleData) {
         if (messages.hasOwnProperty(dataId)) {
             messages[dataId].value = externalLocaleData[dataId].value;
             var msgFiles = messages[dataId].files || [];
             if (externalLocaleData[dataId].files.length > 0) {
                 msgFiles = msgFiles.concat(externalLocaleData[dataId].files);
             }
             msgFiles.sort();
             messages[dataId].files = _s.unique(msgFiles, true);
         }
         else {
             messages[dataId] = externalLocaleData[dataId];
         }
     }
     grunt.file.write(localeFile, JSON.stringify(
             messages,
             that.options.jsonReplacer,
             that.options.jsonSpace
     ));
 });
Example #12
0
            fn: function () {
                var self = this;
                var result = [];
                var urls = htmlify.collectLinks(this.body);
                var oobURIs = _.pluck(this.oobURIs || [], 'url');
                var uniqueURIs = _.unique(result.concat(urls).concat(oobURIs));

                _.each(uniqueURIs, function (url) {
                    var oidx = oobURIs.indexOf(url);
                    if (oidx >= 0) {
                        result.push({
                            href: url,
                            desc: self.oobURIs[oidx].desc,
                            source: 'oob'
                        });
                    } else {
                        result.push({
                            href: url,
                            desc: url,
                            source: 'body'
                        });
                    }
                });

                return result;
            }
Example #13
0
  _generateTabPaneItems: function () {
    var availableTypes = _.unique(_.keys(this._analysisOptions));

    this._tabPaneItems = _.map(analysesTypes(this._analysisOptions), function (d) {
      if (_.contains(availableTypes, d.type)) {
        return d.createTabPaneItem(this._analysisOptionsCollection, {
          modalModel: this._modalModel,
          analysisOptionsCollection: this._analysisOptionsCollection,
          queryGeometryModel: this._queryGeometryModel
        });
      }
    }.bind(this));

    this._tabPaneItems.unshift({
      label: _t('analysis-category.all'),
      name: 'all',
      createContentView: function () {
        return new AnalysisCategoryView({
          analysisType: 'all',
          modalModel: this._modalModel,
          analysisOptions: this._analysisOptions,
          analysisOptionsCollection: this._analysisOptionsCollection,
          queryGeometryModel: this._queryGeometryModel
        });
      }.bind(this)
    });
  },
Example #14
0
        var mergeModules = function() {
            var files = [],
                quiet = false,
                start = 0,
                args = Array.prototype.slice.call(arguments, start);
            if (typeof args[0] === "boolean") {
                quiet = true;
                start = 1;
            }

            Array.prototype.slice.call(arguments, start).forEach(function(filegroup) {
                fineUploaderModules[filegroup].forEach(function(file) {
                    // replace @ref
                    var match = file.match(/^\@(.*)/);
                    if (match) {
                        //if (!quiet) { console.log("Adding module to build: @" + match[1]); }
                        files = files.concat(mergeModules(quiet, match[1]));
                        //files = files.concat(fineUploaderModules[match[1]]);
                    } else {
                        //if (!quiet) { console.log("    Adding file to build: " + file); }
                        files.push(file);
                    }
                });
            });

            return _.unique(files);
        };
Example #15
0
var cacheItems = function (items, options) {
  var key = getQueryKey(options);
  var cached = (app.cache.get(key) || []).slice();
  if (options.overwrite) cached = [];
  _.each(items, function (item) { app.cache.set(getTerm(item), item); });
  app.cache.set(key, _.unique(cached.concat(_.map(items, getTerm))));
};
 onRecodesUpdated: function(){
     var toRecordsNeeded = this.recodes.pluck('toRecordId');
     toRecordsNeeded.push(this.currentRRS.get('defaultRecordId'));
     $.when( this.metafields.fetch(), this.records.fetch({
             data: { recordIDs: _.compact(_.unique(toRecordsNeeded)).join() }
         }) ).then(_.bind(this.onListReady, this));
 },
Example #17
0
 getRawValue: function () {
     var value = this.getValueRepeatableItems();
     if (this.unique) {
         return u.unique(value);
     }
     return value;
 },
Example #18
0
	__init__: function(app, options){
		var self = this;
		self.app = app;
		self.app.controllers = [];
		self.eventer = app.eventer;
		self.eventer.emit("state_manager.before_init", self);
		self.initial = options.initial_state;
		self.event_pattern = StateManager.unique4() + ".{0}.{1}";//0 - state name, 1 - event
		self.states = [];
		for(var state in options.states){
			var events_handler = options.states[state];
			if(_.isArray(events_handler))
				_.each(events_handler, function(eh){
					self.add_events_handler(state, eh);
				});
			else
				self.add_events_handler(state, events_handler);
			self.states.push(state);
		}
		self.shared = {};
		for(var sc_name in options.shared){
			var sc = options.shared[sc_name];
			if(_.isFunction(sc))
				sc = new sc();
			self.shared[sc_name] = sc;
			self.add_event_controller_handler(sc_name, sc, "", "shared.{0}.{1}");
		}
		self.app.controllers = _.unique(self.app.controllers);
		self.eventer.emit("state_manager.after_init", self);
	},
Example #19
0
			function (results, next) {
				var uids = _.unique(_.flatten(results).concat(socket.uid.toString()));
				uids.forEach(function (uid) {
					websockets.in('uid_' + uid).emit('event:post_edited', editResult);
				});
				next(null, editResult.post);
			}
Example #20
0
		db.getSortedSetsMembers(sets, function(err, members) {
			if (err) {
				return callback(err);
			}

			var uniqueGroups = _.unique(_.flatten(members));
			uniqueGroups = Groups.internals.removeEphemeralGroups(uniqueGroups);

			Groups.isMemberOfGroups(uid, uniqueGroups, function(err, isMembers) {
				if (err) {
					return callback(err);
				}

				var map = {};

				uniqueGroups.forEach(function(groupName, index) {
					map[groupName] = isMembers[index];
				});

				var result = members.map(function(groupNames) {
					for (var i=0; i<groupNames.length; ++i) {
						if (map[groupNames[i]]) {
							return true;
						}
					}
					return false;
				});

				callback(null, result);
			});
		});
Example #21
0
      .then((cardsArrays) => {
        const cards = _.unique(_.flatten(cardsArrays));
        // Re-fetch each Issue
        return Promise.all(cards.map((card) => card.fetchIssue(true/*skipSavingToDb*/)))
        .then((newIssues) => {
          return Database.putCards(cards)
          .then(() => {
            // Update the list of labels now that all the Issues have been updated
            return Database.putRepoLabels(repoOwner, repoName, newLabels)
            .then(() => {


              // FINALLY, actually fetch the updates
              return Database.getRepoOrNull(repoOwner, repoName).then((repo) => {
                let lastSeenAt;
                if (repo && repo.lastSeenAt) {
                  lastSeenAt = repo.lastSeenAt;
                }
                return this._fetchLastSeenUpdatesForRepo(repoOwner, repoName, progress, lastSeenAt, isPrivate, this._getDidLabelsChange(newLabels, oldLabels));
              });

            })
          })
        })
      })
Example #22
0
            }, function (error, query) {
                if (error) {
                    console.log(error); // an error occurred
                } else {

                    // Grab a list of all the hosts' private IP addresses, public IP addresses, and tags. 
                    // This can be easily expanded to support multiple IP output, searching by multiple tags and/or multiple tag values, etc.
                    var hosts = _.unique(
                        _.flatten(
                            _.map(query.Reservations, function (reservation) {
                                return _.map(reservation.Instances, function (instance) {
                                    return { 
                                        PublicIpAddress: instance.PublicIpAddress, 
                                        PrivateIpAddress: instance.PrivateIpAddress, 
                                        Tags: instance.Tags
                                    };
                                });
                            })
                        )
                    );

                    // For now, just output the IP address of each result on a line.
                    _.each(hosts, function (host) { 
                        // Handle simple lookup request: 
                        if (options.request.name != null) {
                            console.log(host.PublicIpAddress);
                            return;
                        }
                        // Otherwise this is a list request - show more information
                        var name = _.findWhere(host.Tags, { Key: "Name" }) || null;
                        name = name === null ? "[unnamed instance]" : name.Value;
                        console.log("Name: " + name + " | " + "Public IP: " + host.PublicIpAddress); 
                    });
                }
            });
 it("all bar for facet chart should have equal width", function () {
     var svg = context.chart.getSVG();
     var width = _.map(svg.querySelectorAll('.i-role-element'), function (item) {
         return item.getAttribute('width');
     });
     expect(_.unique(width).length).to.equals(1);
 });
Example #24
0
 return this.cheerioRequest(lbaRootLink).then(function ($) {
   var categoriesList = $('a', $('ul.level1')).map(function (i, el) {
     return $(this).attr('href');
   }).get();
   categoriesList = _.unique(categoriesList);
   console.log(categoriesList.join("\n"));
   //categoriesList = _.take(categoriesList, 10); //TODO
   return Q.all(categoriesList.map(function (categoryLink) {
     var categoryUrl = lbaRootLink + categoryLink + '/results,0-1000';
     return self.cheerioRequest(categoryUrl).then(function ($) {
       console.log('Scraping: ' + categoryUrl);
       return _.filter($('.row > .product').map(function () {
         var link = $('a', $(this));
         if (!link.text()) {
           return;
         }
         var priceBlock = $('.product-price__base', $(this));
         if (!priceBlock || !priceBlock.text()) {
           return;
         }
         var priceMatch = priceBlock.text().match(/(\d+)\s+руб/);
         if (!priceMatch) {
           return;
         }
         return {
           name: link.text().trim(),
           siteUrl: lbaRootLink + link.attr('href'),
           price: parseInt(priceMatch[1]) * 100
         };
       }).get(), _.identity);
     })
   })).then(_.flatten);
 })
Example #25
0
    _u.unique(plugins).forEach(function (pdir) {
        if (_u.isFunction(pdir)) {

            var plugin = isPluginApi(pdir) ? new pdir(options, express, null, null, this) : pdir(options, express, null, null, this);

            ret.push(plugin);
            loaded[plugin.name || pdir] = true;

        } else {

            if (loaded[pdir]) {
                console.warn('plugin already loaded [' + pdir + ']');
                return;
            }

            _u.unique(dirs).forEach(function (dir) {
                if (!path.existsSync(dir)) {
                    return;
                }
                var fpath = path.join(dir, pdir, pdir + '.js');
                if (path.existsSync(fpath)) {
                    console.log('loading ', fpath);
                    try {
                        var Plugin = require(fpath);
                        var plugin = new Plugin(options, express, pdir, path.join(dir, pdir), this);

                        ret.push(plugin);
                        loaded[pdir] = plugin;
                    } catch (e) {
                        console.warn('error loading plugin [' + fpath + ']', e);
                    }
                }
            }, this);
        }
    }, this);
Example #26
0
  this.findById(id, function (err, doc) {

    if (!doc.canChangeOrder()) {
      return cb();
    }

    // only update the parent if the child is new
    if (doc._items.indexOf(itemId) == -1) {

      log.debug("Adding item to order: " + itemId);
      doc._items.push(itemId);
      // KLUDGE: there is something funky creating a duplicate - so let's clean up
      doc._items = _.unique(doc._items);

      doc.placeOrder(function (err) {
        if (err) {
          log.warn(err + ' on id: ' + doc._id + ' - current state is: ' + doc.state);
        }
      })

      doc.save(function (err, doc, numAffected) {
        cb(err);
      });

    }

    cb();

  });
Example #27
0
					function(memberSets, next) {

						memberSets = memberSets.map(function(set) {
							return set.map(function(uid) {
								return parseInt(uid, 10);
							});
						});

						var members = _.unique(_.flatten(memberSets));

						user.getUsersFields(members, ['picture', 'username'], function(err, memberData) {
							if (err) {
								return next(err);
							}

							memberData.forEach(function(member) {
								member.privileges = {};
								for(var x=0,numPrivs=privileges.length;x<numPrivs;x++) {
									member.privileges[privileges[x]] = memberSets[x].indexOf(parseInt(member.uid, 10)) !== -1;
								}
							});

							next(null, memberData);
						});
					}
Example #28
0
 return function ($el, root) {
   return _.unique(_.filter($el.map(function (i, el) {
     if (root && $(this).attr('href').indexOf('/') !== 0) {
       return;
     }
     return (root || '') + $(this).attr('href');
   }).get(), _.identity));
 }
Example #29
0
 setMethods: function(parent, child){
   child.methods = _.unique(
     _.difference(
       _.methods(parent).concat(_.methods(child)),
       this.baseMethods
     )
   );
 },
Example #30
0
File: utils.js Project: dijs/poet
function getCategories (posts) {
  var categories = posts.reduce(function (categories, post) {
    if (!post.category) return categories;
    return categories.concat(post.category);
  }, []);

  return _.unique(categories).sort();
}