public void TestConstructor () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			double[] w1 = { 1.0, 2.0, 3.0 };
			double[] w2 = { 2.0, 4.0, 6.0 };
			pwm.Add( 'a', w1 );
			pwm.Add( 'c', w2 );
			Assert.AreEqual( 3, pwm.WeightedVectorLength );
			Assert.AreEqual( 2, pwm.SymbolNumber );
		}
		/** Tests the adding and getting of weights */
		public void TestAddGet () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			double[] w1 = { 1.0, 2.0, 3.0 };
			double[] w2 = { 2.0, 4.0, 6.0 };
			pwm.Add( 'a', w1 );
			pwm.Add( alpha['c'], w2 );
			Assert.AreEqual( w1, pwm.Get( alpha['a'] ) );
			Assert.AreEqual( null, pwm.Get( alpha['z'] ) );
			Assert.AreEqual( w2, pwm.Get( 'c' ) );
			Assert.AreEqual( 3, pwm.WeightedVectorLength );
			Assert.AreEqual( 2, pwm.SymbolNumber );
			Assert.AreEqual( 1.0, pwm.Get( 'a', 0 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 'a', 1 ), 1e-3 );
			Assert.AreEqual( 3.0, pwm.Get( 'a', 2 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 'c', 0 ), 1e-3 );
			Assert.AreEqual( 4.0, pwm.Get( 'c', 1 ), 1e-3 );
			Assert.AreEqual( 6.0, pwm.Get( 'c', 2 ), 1e-3 );

			Assert.AreEqual( 1.0, pwm.Get( 't', 0 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 't', 1 ), 1e-3 );
			Assert.AreEqual( 3.0, pwm.Get( 't', 2 ), 1e-3 );
		}
Exemplo n.º 3
0
        //public static List<ErrorData> ErrorList { get; } = new List<ErrorData>();

        /*
         * /// <summary>
         * /// Gets the can talon.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * /// <returns></returns>
         * public static CanTalonData GetCanTalon(int id)
         * {
         *  CanTalonData data;
         *  bool retVal = s_canTalon.TryGetValue(id, out data);
         *  if (retVal)
         *  {
         *      //Contains key. Just return it
         *      return data;
         *  }
         *  else
         *  {
         *      //CAN Talon does not exist yet. Return null.
         *      return null;
         *  }
         * }
         *
         * /// <summary>
         * /// Initializes the can talon.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * /// <returns></returns>
         * public static bool InitializeCanTalon(int id)
         * {
         *  CanTalonData data;
         *  bool retVal = s_canTalon.TryGetValue(id, out data);
         *  if (retVal)
         *  {
         *      //Contains key. return false saying we did not initialize a new one.
         *      return false;
         *  }
         *  else
         *  {
         *      //Create a new Can Talon data and return true.
         *      data = new CanTalonData();
         *      s_canTalon.Add(id, data);
         *      OnTalonSRXAddedOrRemoved?.Invoke(id, new TalonSRXEventArgs(true));
         *      return true;
         *  }
         * }
         *
         * /// <summary>
         * /// Removes the can talon.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * public static void RemoveCanTalon(int id)
         * {
         *  s_canTalon.Remove(id);
         *  OnTalonSRXAddedOrRemoved?.Invoke(id, new TalonSRXEventArgs(false));
         * }
         *
         * /// <summary>
         * /// Gets the PCM.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * /// <returns></returns>
         * public static PCMData GetPCM(int id)
         * {
         *  PCMData data;
         *  bool retVal = s_pcm.TryGetValue(id, out data);
         *  if (retVal)
         *  {
         *      //Contains key. Just return it
         *      return data;
         *  }
         *  else
         *  {
         *      data = new PCMData();
         *      s_pcm.Add(id, data);
         *      OnPCMAdded?.Invoke(data, null);
         *      return data;
         *  }
         * }
         *
         * /// <summary>
         * /// Initializes the PCM.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * /// <returns></returns>
         * public static bool InitializePCM(int id)
         * {
         *  PCMData data;
         *  bool retVal = s_pcm.TryGetValue(id, out data);
         *  if (retVal)
         *  {
         *      //Contains key. return false saying we did not initialize a new one.
         *      return false;
         *  }
         *  else
         *  {
         *      //Create a new PCM data and return true.
         *      data = new PCMData();
         *      s_pcm.Add(id, data);
         *      OnPCMAdded?.Invoke(data, null);
         *      return true;
         *  }
         * }
         *
         * /// <summary>
         * /// Gets the PDP.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * /// <returns></returns>
         * public static PDPData GetPDP(int id)
         * {
         *  PDPData data;
         *  bool retVal = s_pdp.TryGetValue(id, out data);
         *  if (retVal)
         *  {
         *      //Contains key. Just return it
         *      return data;
         *  }
         *  else
         *  {
         *      data = new PDPData();
         *      s_pdp.Add(id, data);
         *      OnPDPAdded?.Invoke(data, null);
         *      return data;
         *  }
         * }
         *
         * /// <summary>
         * /// Initializes a new PDP.
         * /// </summary>
         * /// <param name="id">The identifier.</param>
         * /// <returns>True if new PDP was created, otherwise false</returns>
         * public static bool InitializePDP(int id)
         * {
         *  PDPData data;
         *  bool retVal = s_pdp.TryGetValue(id, out data);
         *  if (retVal)
         *  {
         *      //Contains key. return false saying we did not initialize a new one.
         *      return false;
         *  }
         *  else
         *  {
         *      //Create a new PCM data and return true.
         *      data = new PDPData();
         *      s_pdp.Add(id, data);
         *      OnPDPAdded?.Invoke(data, null);
         *      return true;
         *  }
         * }
         *
         * /// <summary>
         * /// Occurs on talon SRX added or removed.
         * /// </summary>
         * public static event EventHandler<TalonSRXEventArgs> OnTalonSRXAddedOrRemoved;
         * /// <summary>
         * /// Occurs when a PCM is added.
         * /// </summary>
         * public static event EventHandler OnPCMAdded;
         * /// <summary>
         * /// Occurs when a PDP is added.
         * /// </summary>
         * public static event EventHandler OnPDPAdded;
         *
         */
        static SimData()
        {
            for (int i = 0; i < HAL_GetNumPCMModules(); i++)
            {
                PCM.Add(new HALSimPCMData(i));
            }

            for (int i = 0; i < HAL_GetNumPDPModules(); i++)
            {
                PDP.Add(new HALSimPDPData(i));
            }

            for (int i = 0; i < HAL_GetNumAccumulators(); i++)
            {
                AnalogGyro.Add(new HALSimAnalogGyroData(i));
            }

            for (int i = 0; i < HAL_GetNumAnalogOutputs(); i++)
            {
                AnalogOut.Add(new HALSimAnalogOutData(i));
            }

            for (int i = 0; i < HAL_GetNumAnalogInputs(); i++)
            {
                AnalogIn.Add(new HALSimAnalogInData(i));
            }

            for (int i = 0; i < HAL_GetNumAnalogTriggers(); i++)
            {
                AnalogTrigger.Add(new HALSimAnalogTriggerData(i));
            }

            for (int i = 0; i < HAL_GetNumDigitalChannels(); i++)
            {
                DIO.Add(new HALSimDIOData(i));
            }

            for (int i = 0; i < HAL_GetNumDigitalPWMOutputs(); i++)
            {
                DigitalPWM.Add(new HALSimDigitalPWMData(i));
            }

            for (int i = 0; i < 4; i++)
            {
                //DigitalGlitchFilter.Add(new DigitalGlitchFilterData());
            }

            for (int i = 0; i < HAL_GetNumPWMChannels(); i++)
            {
                PWM.Add(new HALSimPWMData(i));
            }

            for (int i = 0; i < 4; i++)
            {
                Relay.Add(new HALSimRelayData(i));
            }

            for (int i = 0; i < 8; i++)
            {
                //Counter.Add(new CounterData());
            }

            for (int i = 0; i < 8; i++)
            {
                Encoder.Add(new HALSimEncoderData(i));
            }

            for (int i = 0; i < 5; i++)
            {
                SPIAccelerometer.Add(new HALSimSPIAccelerometerData(i));
            }

            for (int i = 0; i < 5; i++)
            {
                //SPIAccumulator.Add(new SPIAccumulatorData());
            }

            //InitializePDP(0);
            //ErrorList.Clear();
        }
		/** Tests the adding of weights as strings */
		public void TestAddString () {
			PWM pwm = new PWM( "test", alpha, 0.0 );

			pwm.Add( 'a', "1 2 3" );
			pwm.Add( alpha['c'], "2 4 6" );
			Assert.AreEqual( 3, pwm.WeightedVectorLength );
			Assert.AreEqual( 2, pwm.SymbolNumber );
			Assert.AreEqual( 1.0, pwm.Get( 'a', 0 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 'a', 1 ), 1e-3 );
			Assert.AreEqual( 3.0, pwm.Get( 'a', 2 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 'c', 0 ), 1e-3 );
			Assert.AreEqual( 4.0, pwm.Get( 'c', 1 ), 1e-3 );
			Assert.AreEqual( 6.0, pwm.Get( 'c', 2 ), 1e-3 );
		}
		/** test the creation of a sup PWM */
		public void TestSubPWM () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			pwm.Add( 'a', "1 2 3 4" );
			pwm.Add( 'c', "5 6 7 8" );
			PWM sub = pwm.SubPWM( "test", 1, 2 );
			Assert.AreEqual( 2, sub.Get( 'a', 0 ), 1e-3 );
			Assert.AreEqual( 3, sub.Get( 'a', 1 ), 1e-3 );
			Assert.AreEqual( 6, sub.Get( 'c', 0 ), 1e-3 );
			Assert.AreEqual( 7, sub.Get( 'c', 1 ), 1e-3 );
		}
		/** Tests the creation of the consensus and the anti-consensus. */
		public void TestConsensus () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			pwm.Add( 'a', "1 0 1" );
			pwm.Add( 'c', "0 1 0" );
			Assert.AreEqual( "aca", pwm.Consensus.Letters() );
			Assert.AreEqual( "cac", pwm.AntiConsensus.Letters() );
		}
		/** Tests the calculation of the max. score of the PWM */
		public void TestUpdateMinMax () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			pwm.Add( 'a', "2 4 3" );
			pwm.Add( 'g', "1 1 1" );
			pwm.Add( 'c', "1 3 6" );
			Assert.AreEqual( 12.0, pwm.MaxScore, 1e-3 );
			Assert.AreEqual( 3.0, pwm.MinScore, 1e-3 );
			Assert.AreEqual( 9.0, pwm.RangeScore, 1e-3 );
		}
		/** Tests the creation of a sorting index for the rows of a PWM */
		public void TestSortingIndex () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			pwm.Add( 'a', "1 3 2" );
			pwm.Add( 'g', "2 2 1" );
			pwm.Add( 'c', "3 1 3" );
			Symbol[] index = pwm.SortingIndex( 0 );
			Assert.AreEqual( 'a', index[0].Letter );
			Assert.AreEqual( 'g', index[1].Letter );
			Assert.AreEqual( 'c', index[2].Letter );
			index = pwm.SortingIndex( 1 );
			Assert.AreEqual( 'c', index[0].Letter );
			Assert.AreEqual( 'g', index[1].Letter );
			Assert.AreEqual( 'a', index[2].Letter );
			index = pwm.SortingIndex( 2 );
			Assert.AreEqual( 'g', index[0].Letter );
			Assert.AreEqual( 'a', index[1].Letter );
			Assert.AreEqual( 'c', index[2].Letter );
		}
		/** Tests the matching  */
		public void TestMatch () {
			PWM pwm = new PWM( "test", alpha, 0.5 );
			pwm.Add( 'a', "1 0 1 0" );
			pwm.Add( 'c', "0 1 0 1" );
			Sequence seq = new Sequence( AlphabetType.DNA, "aaaacac" );
			FeatureList matches = seq.Search( 0, 0, pwm );
			Assert.AreEqual( 3, matches.Count );
			Assert.AreEqual( "aaaa", matches[ 0 ].Letters() );
			Assert.AreEqual( 0.5, ( (Match) matches[ 0 ] ).Similarity, 1e-3 );
			Assert.AreEqual( "aaac", matches[ 1 ].Letters() );
			Assert.AreEqual( 0.75, ( (Match) matches[ 1 ] ).Similarity, 1e-3 );
			Assert.AreEqual( "acac", matches[ 2 ].Letters() );
			Assert.AreEqual( 1.0, ( (Match) matches[ 2 ] ).Similarity, 1e-3 );
		}
		/** Tests the setting and getting of weights */
		public void TestSetGet () {
			PWM pwm = new PWM( "test", alpha, 0.0 );
			pwm.Add( alpha['a'], "0 0" );
			pwm.Set( alpha['a'], 0, 1 );
			pwm.Set( 'a', 1, 2 );
			Assert.AreEqual( 1.0, pwm.Get( 'a', 0 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 'a', 1 ), 1e-3 );

			Assert.AreEqual( 1.0, pwm.Get( 't', 0 ), 1e-3 );
			Assert.AreEqual( 2.0, pwm.Get( 't', 1 ), 1e-3 );
		}