Permalink
Browse files

Added Normal Distribution Data Type

  • Loading branch information...
1 parent eae595d commit c8f8edbf61e8027fa68a7af12bc318dc5d273161 @benkeen committed Mar 22, 2013
View
@@ -1,6 +1,9 @@
settings.php
cache/*
.DS_Store
+.project
+.buildpath
+.settings/*
resources/themes/classic/.sass-cache/*
resources/themes/default/.sass-cache/*
resources/themes/.sass-cache/*
@@ -0,0 +1,85 @@
+<?php
+
+/**
+ * @author Ben Keen <ben.keen@gmail.com>
+ * @package DataTypes
+ */
+class DataType_NormalDistribution extends DataTypePlugin {
+
+ /**#@+
+ * @access protected
+ */
+ protected $isEnabled = true;
+ protected $dataTypeName = "Normal Distribution";
+ protected $dataTypeFieldGroup = "math";
+ protected $dataTypeFieldGroupOrder = 10;
+ protected $jsModules = array("NormalDistribution.js");
+ protected $randMax = null;
+
+
+ public function generate($generator, $generationContextData) {
+ $mean = $generationContextData["mean"];
+ $sigma = $generationContextData["sigma"];
+
+ return array(
+ "display" => $this->gauss($mean, $sigma)
+ );
+ }
+
+ public function getRowGenerationOptions($generator, $postdata, $colNum, $numCols) {
+ if (!isset($postdata["dtOptionMean_$colNum"]) || empty($postdata["dtOptionMean_$colNum"]) ||
+ !isset($postdata["dtOptionSigma_$colNum"]) || empty($postdata["dtOptionSigma_$colNum"])) {
+ return false;
+ }
+
+ $this->randMax = (float) getrandmax();
+
+ return array(
+ "mean" => $postdata["dtOptionMean_$colNum"],
+ "sigma" => $postdata["dtOptionSigma_$colNum"]
+ );
+ }
+
+ public function getOptionsColumnHTML() {
+ $options =<<< END
+ <label for="dtOptionMean_%ROW%">Mean</label>
+ <input type="text" name="dtOptionMean_%ROW%" id="dtOptionMean_%ROW%" style="width: 30px" value="45" />
+ <label for="dtOptionSigma_%ROW%">Sigma</label>
+ <input type="text" name="dtOptionSigma_%ROW%" id="dtOptionSigma_%ROW%" style="width: 30px" value="10" />
+END;
+
+ return $options;
+ }
+
+ // N(m,s)
+ // returns random number with normal distribution:
+ // mean=m
+ // std dev=s
+ public function gauss_ms($m = 0.0, $s = 1.0) {
+ return $this->gauss() * $s + $m;
+ }
+
+ public function gauss() {
+ // N(0,1)
+ // returns random number with normal distribution:
+ // mean = 0
+ // std dev = 1
+
+ // auxilliary vars
+ $x = $this->random_0_1();
+ $y = $this->random_0_1();
+
+ // two independent variables with normal distribution N(0,1)
+ $u = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
+// $v = sqrt(-2 * log($x)) * sin(2 * pi() * $y);
+
+ // i will return only one, couse only one needed
+ return $u;
+ }
+
+ // auxilliary function
+ // returns random number with flat distribution from 0 to 1
+ public function random_0_1() {
+ return (float) rand() / $this->randMax;
+ }
+}
@@ -0,0 +1,76 @@
+/*global $:false*/
+define([
+ "manager",
+ "constants",
+ "lang",
+ "generator"
+], function(manager, C, L, generator) {
+
+ "use strict";
+
+ /**
+ * @name NormalDistribution
+ * @description JS code for the NormalDistribution Data Type.
+ * @see DataType
+ * @namespace
+ */
+
+ var MODULE_ID = "data-type-NormalDistribution";
+ var LANG = L.dataTypePlugins.NormalDistribution;
+ var subscriptions = {};
+
+ var _init = function() {
+ subscriptions[C.EVENT.DATA_TABLE.ROW.EXAMPLE_CHANGE + "__" + MODULE_ID] = _exampleChange;
+ manager.subscribe(MODULE_ID, subscriptions);
+ };
+
+ var _saveRow = function(rowNum) {
+ return {
+ "mean": $("#dtOptionMean_" + rowNum).val(),
+ "sigma": $("#dtOptionSigma_" + rowNum).val()
+ };
+ };
+
+ var _loadRow = function(rowNum, data) {
+ return {
+ execute: function() {
+ $("#dtOptionMean_" + rowNum).val(data.mean);
+ $("#dtOptionSigma_" + rowNum).val(data.sigma);
+ },
+ isComplete: function() { return $("#dtOptionSigma_" + rowNum).length > 0; }
+ };
+ };
+
+ var _validate = function(rows) {
+ var visibleProblemRows = [];
+ var problemFields = [];
+ for (var i=0; i<rows.length; i++) {
+ var currMean = $("#dtOptionMean_" + rows[i]);
+ var currSigma = $("#dtOptionSigma_" + rows[i]);
+
+ var visibleRowNum = generator.getVisibleRowOrderByRowNum(rows[i]);
+ if ($.trim(currMean.val()) === "" || $.trim(currSigma.val()) === "") {
+ visibleProblemRows.push(visibleRowNum);
+
+ if ($.trim(currMean.val()) === "") {
+ problemFields.push(currMean);
+ }
+ if ($.trim(currSigma.val()) === "") {
+ problemFields.push(currSigma);
+ }
+ }
+ }
+ var errors = [];
+ if (visibleProblemRows.length) {
+ errors.push({ els: problemFields, error: LANG.incomplete_fields + " <b>" + visibleProblemRows.join(", ") + "</b>"});
+ }
+ return errors;
+ };
+
+ manager.registerDataType(MODULE_ID, {
+ init: _init,
+ validate: _validate,
+ saveRow: _saveRow,
+ loadRow: _loadRow
+ });
+});
@@ -0,0 +1,3 @@
+<?php
+
+$L["incomplete_fields"] = "The Mean and Sigma fields are required for all Normal Distribution rows. Please fix the following rows:";
@@ -31,7 +31,7 @@ class Core {
private static $version = "3.0.0 alpha 2";
private static $minimumPHPVersion = "5.2.0";
private static $settingsFileExists = false;
- private static $dataTypeGroups = array("human_data", "geo", "text", "numeric", "other");
+ private static $dataTypeGroups = array("human_data", "geo", "text", "numeric", "math", "other");
private static $isLoggedIn = false;
// left as public, because they're often modified / accessed, and it's just too fussy otherwise
View
@@ -41,6 +41,7 @@
$L["help"] = "Help";
$L["hide_error"] = "Hide error";
$L["human_data"] = "Human Data";
+$L["math"] = "Math";
$L["include_create_table_query"] = "Include CREATE TABLE query";
$L["include_drop_table_query"] = "Include DROP TABLE query";
$L["invalid_custom_xml"] = "There&#39;s a problem with the custom XML markup you entered. Please consult the user documentation for more information.";

0 comments on commit c8f8edb

Please sign in to comment.