public override ClassificationModel Train(BaseVector[] x, int[][] y, int ngroups, Parameters param, int nthreads,
                                                  Action <double> reportProgress)
        {
            string err = CheckInput(x, y, ngroups);

            if (err != null)
            {
                throw new Exception(err);
            }
            ParameterWithSubParams <int> kernelParam = param.GetParamWithSubParams <int>("Kernel");
            SvmParameter sp = new SvmParameter {
                kernelFunction = KernelFunctions.GetKernelFunction(kernelParam.Value, kernelParam.GetSubParameters()),
                svmType        = SvmType.CSvc,
                c = param.GetParam <double>("C").Value
            };

            bool[]            invert;
            SvmProblem[]      problems = CreateProblems(x, y, ngroups, out invert);
            SvmModel[]        models   = new SvmModel[problems.Length];
            ThreadDistributor td       = new ThreadDistributor(nthreads, models.Length,
                                                               i => { models[i] = SvmMain.SvmTrain(problems[i], sp); }, fractionDone => { reportProgress?.Invoke(fractionDone); });

            td.Start();
            return(new SvmClassificationModel(models, invert));
        }
Esempio n. 2
0
        public ClassificationModel TrainGroupWise(BaseVector[] x, int[][] y, int ngroups, IGroupDataProvider data,
                                                  int nthreads)
        {
            ClassificationModel[] c  = new ClassificationModel[ngroups];
            ThreadDistributor     td = new ThreadDistributor(nthreads, ngroups, i => { c[i] = TrainGroupWise(i, y, x, data); });

            td.Start();
            return(new GroupWiseClassifier(c));
        }
        private IMatrixData ProcessDbFiles(ProcessInfo processInfo, int nThreads, IList<Database> databases)
        {
            string tempFile = Path.Combine(FileUtils.GetTempFolder(), "databaseref.txt");
            IMatrixData matrix;
            StreamWriter writer = null;

            try{
                processInfo.Progress(0);
                processInfo.Status(string.Format("Read database files [{0}|{1}]", 0, "?"));

                Enum[] enums = new Enum[] { databaseRef.file, databaseRef.source, databaseRef.specie, databaseRef.taxid, databaseRef.version, databaseRef.identifier };
                IList<string> header = enums.Select(Constants.GetPattern).ToList();

                if (databases == null || databases.Count == 0){
                    return null;
                }

                writer = new StreamWriter(tempFile);

                int nTasks = databases.Count;
                writer.WriteLine(StringUtils.Concat("\t", header));
                writer.WriteLine("#!{Type}" + StringUtils.Concat("\t", header.Select(x => "T")));

                ThreadDistributor distr = new ThreadDistributor(nThreads, nTasks,
                                                                x =>
                                                                ParseDatabase(writer, databases[x],
                                                                              string.Format(
                                                                                  "Read database files [{0}|{1}]",
                                                                                  x + 1, nTasks), (x + 1)*100/nTasks,
                                                                              processInfo));
                distr.Start();

                processInfo.Status("Close all files");

                writer.Close();
                writer.Dispose();
                writer = null;

                processInfo.Progress(0);
                processInfo.Status("Create DatabaseRef Matrix");

                matrix = new MatrixData();
                LoadData(matrix, tempFile, processInfo);
            }
            catch (Exception ex){
                throw ex;
            }
            finally{
                if (writer != null){
                    writer.Close();
                }

                if (File.Exists(tempFile)){
                    File.Delete(tempFile);
                }
            }

            return matrix;
        }
        private IMatrixData ProcessAplFiles(ProcessInfo processInfo, int nThreads, IList<MsRunImpl> aplfiles)
        {
            string tempFile = Path.Combine(FileUtils.GetTempFolder(), "spectraref.txt");
            if (File.Exists(tempFile)){
                File.Delete(tempFile);
            }
            IMatrixData matrix;
            StreamWriter writer = null;

            try{
                Enum[] enums = new Enum[]{spectraRef.raw_file, spectraRef.charge, spectraRef.scan_number, spectraRef.location, spectraRef.format, spectraRef.id_format, spectraRef.fragmentation, spectraRef.mz, spectraRef.index};
                IList<string> header = enums.Select(Constants.GetPattern).ToList();

                if (aplfiles == null || aplfiles.Count == 0){
                    return null;
                }

                int nTasks = aplfiles.Count;

                processInfo.Progress(0);
                processInfo.Status(string.Format("Read Andromeda peaklist files [{0}|{1}]", 0, nTasks));

                writer = new StreamWriter(tempFile);
                writer.WriteLine(StringUtils.Concat("\t", header));
                writer.WriteLine("#!{Type}" + StringUtils.Concat("\t", header.Select(x => "T")));

                ThreadDistributor distr = new ThreadDistributor(nThreads, nTasks,
                                                                x =>
                                                                ParseAplFile(aplfiles[x], writer,
                                                                             string.Format(
                                                                                 "Read Andromeda peaklist files [{0}|{1}]",
                                                                                 x + 1, nTasks), (x + 1)*100/nTasks,
                                                                             processInfo));
                distr.Start();

                processInfo.Status("Close all files");

                writer.Close();
                writer.Dispose();
                writer = null;

                processInfo.Progress(0);
                processInfo.Status("Create SpectraRef matrix");

                matrix = new MatrixData();
                LoadData(matrix, tempFile, processInfo);
            }
            catch (Exception ex){
                throw ex;
            }
            finally{
                if (writer != null){
                    writer.Close();
                }

                if (File.Exists(tempFile)){
                    File.Delete(tempFile);
                }
            }

            return matrix;
        }