public MusicAnalyzer(List <string> contexts)
        {
            // _storedSongs = new double[MaxSongsAtOnce][];
            // _storedTypes = new int[MaxSongsAtOnce][];
            _allContexts = contexts;
            // _currentIndex = 0;

            // _network = new ActivationNetwork(new SigmoidFunction(), InputsSize, (int) ((InputsSize * (2.0/3.0)) + contexts.Count), (int)((InputsSize * (1.0 / 3.0)) + contexts.Count), contexts.Count);
            int sum = (InputsSize + contexts.Count);

            _network = new ActivationNetwork(new SigmoidFunction(), InputsSize, contexts.Count, contexts.Count);
            // _network = new ActivationNetwork(new SigmoidFunction(), InputsSize, contexts.Count * 3, contexts.Count, contexts.Count);
            _network.Randomize();

            _learning = new BackPropagationLearning(_network)
            {
                LearningRate = 0.2,
                Momentum     = 0.5
            };


            _standard = new Standardizer();

            // _learning = new ParallelResilientBackpropagationLearning(_network);

            // learning.RunEpoch()

            //_teacher = new MultilabelSupportVectorLearning<Linear>()
            //{
            //    Learner = (p) => new SequentialMinimalOptimization<Linear>()
            //    {
            //        Complexity = 100.0
            //    }
            //};
        }
        public unsafe double ProcessData(char *data, int size, string context)
        {
            if (!_allContexts.Contains(context))
            {
                throw new Exception(context + " context does not exist");
            }

            var arr = ByteArrToDoubleArr(data, size);

            Standardizer.ProcessCalculate(arr);

            double avg = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                avg += arr[i];
            }
            avg /= arr.Length;
            Debug.WriteLine("Average: " + avg);

            double[] types = TypesValues(context);


            // Fourier.FFT(arr, FourierDirection.Forward);

            //double[] input = new double[arr.Length];
            //for(int i = 0; i < arr.Length; i++)
            //{
            //    input[i] = (double) arr[i].Re;
            //}

            double error;

            // int t;
            // do
            // {
            error = _learning.Run(arr, types);

            // t = TestData(data, size).Select(a => a).Where(a => a.Value >= 1 && a.Key.Equals(context)).Count();
            // } while (t < 1 && confirmGood);

            Debug.WriteLine("Data Error: " + error);
            return(error);
        }
 public MusicAnalyzer(string loadLocation)
 {
     using (Stream loader = new FileStream(loadLocation, FileMode.Open))
     {
         var binaryFormatter = new BinaryFormatter();
         // todo deseserialize standarizer
         Version v = (Version)binaryFormatter.Deserialize(loader);
         if (v.Major == Ver.Major && v.Minor == Ver.Minor)
         {
             _allContexts = (List <string>)binaryFormatter.Deserialize(loader);
             _standard    = (Standardizer)binaryFormatter.Deserialize(loader);
             _network     = (ActivationNetwork)Network.Load(loader);
         }
         else
         {
             throw new Exception(string.Format("{0}.{1}.{2} version does not match current {3}.{4}.{5}", v.Major, v.Minor, v.SubMinor, Ver.Major, Ver.Minor, Ver.SubMinor));
         }
     }
 }
        private double[] TypesValues(string context)
        {
            double[] types = new double[_allContexts.Count];
            for (int i = 0; i < types.Length; i++)
            {
                if (_allContexts[i] == context)
                {
                    types[i] = 1.0;
                }
                else
                {
                    types[i] = -1.0;
                }
            }

            Standardizer.ProcessCalculate(types);

            // types = NormalizeDouble(types);

            return(types);
        }
        public unsafe Dictionary <string, double> TestData(char *data, int size)
        {
            // var model = _teacher.Model;
            // var arr = ByteArrToComplexArr(data, size);
            // Fourier.FFT(arr, FourierDirection.Forward);

            //double[] input = new double[arr.Length];
            //for (int i = 0; i < arr.Length; i++)
            //{
            //    input[i] = (double)arr[i].Re;
            //}

            var input = ByteArrToDoubleArr(data, size);

            Standardizer.ProcessCalculate(input);

            double[] probabilities = _network.Compute(input);


            double total = 0;

            foreach (double probability in probabilities)
            {
                total += probability;
            }

            for (int i = 0; i < probabilities.Length; i++)
            {
                probabilities[i] = probabilities[i] / total;
            }



            return(_allContexts.Zip(probabilities, (k, v) => new { s = k, d = v })
                   .OrderBy((k) => k.d).Reverse().ToDictionary(key => key.s, value => value.d));
        }