// End PowerIterate() // Solve for Distributed Answer = (Matrix-1) First // Note RealMatrixSize takes account of forced zero parameters which does not crop up in explicit // algorithm as implemented in Matrix Vector Multiplier public static bool ConjugateGradientSolver(double[][] Answer, Desertwind Solution, bool useexact, double[][] GlobalxVector, double[][] DistributedRHS, ref int NumberofIterations, int RealMatrixSize, double LimitonNormofR) { bool matrixsuccess = true; // Initialize SALSABLAS.zrword(Answer); // Zero Solution x called xshift in Manxcat and stored in Answer SALSABLAS.CopyVector(DistributedCGVector_R, DistributedRHS, 0, SALSAUtility.PointCount_Process); // Set R(0) = RHS double InitialRNorm = SALSABLAS.VectorScalarProduct(DistributedCGVector_R, DistributedCGVector_R); double RNormTest = InitialRNorm * LimitonNormofR; // Limit for Test on Iteration of Norm of R var SaveRNorm = new double[RealMatrixSize + 1]; SaveRNorm[0] = InitialRNorm; int CountSteps = 0; double lastrho = 1.0; double currentrho = 1.0; // Loop over Conjugate Gradient Steps while (true) { // Set value of rho lastrho = currentrho; currentrho = SALSABLAS.VectorScalarProduct(DistributedCGVector_R, DistributedCGVector_R); // Set Vector P ++CountSteps; if (CountSteps == 1) { SALSABLAS.CopyVector(DistributedCGVector_P, DistributedCGVector_R, 0, SALSAUtility.PointCount_Process); } else { SALSABLAS.LinearCombineVector(DistributedCGVector_P, currentrho / lastrho, DistributedCGVector_P, 1.0, DistributedCGVector_R); } // Make a Global Vector of DistributedCGVector_P ManxcatCentral.MakeVectorGlobal(DistributedCGVector_P, GlobalCGVector_P); // Distributed Q = Matrix . Global P UserRoutines.GlobalMatrixVectorProduct(DistributedCGVector_Q, Solution, useexact, Hotsun.GlobalParameter, GlobalCGVector_P); // New Answer is Old answer + (Current Rho / (P dot Q)) Vector P double PdotQ = SALSABLAS.VectorScalarProduct(DistributedCGVector_P, DistributedCGVector_Q); double alpha = currentrho / PdotQ; SALSABLAS.LinearCombineVector(Answer, alpha, DistributedCGVector_P, 1.0, Answer); // New residual R = Old Residual - (Current Rho / (P dot Q)) Vector Q SALSABLAS.LinearCombineVector(DistributedCGVector_R, -alpha, DistributedCGVector_Q, 1.0, DistributedCGVector_R); // See if we can or should End double CurrentRNorm = SALSABLAS.VectorScalarProduct(DistributedCGVector_R, DistributedCGVector_R); SaveRNorm[CountSteps] = CurrentRNorm; bool TestRNorm = CurrentRNorm <= RNormTest; SALSAUtility.SynchronizeMPIvariable(ref TestRNorm); if (TestRNorm) { matrixsuccess = true; break; } // Singular Matrix if (CountSteps >= RealMatrixSize) { matrixsuccess = false; ManxcatCentral.MakeVectorGlobal(DistributedCGVector_R, GlobalCGVector_R); if (SALSAUtility.MPI_Rank == 0) { SALSAUtility.SALSAPrint(0, " CG Failure after " + RealMatrixSize.ToString() + " Steps"); string ListofNorms = ""; int Normindex = CountSteps; for (int inorm = 0; inorm < 10; inorm++) { ListofNorms += " " + SaveRNorm[Normindex].ToString("E4"); --Normindex; if (Normindex < 0) { break; } } SALSAUtility.SALSAPrint(0, "Last 10 Norms " + ListofNorms); string fname = ManxcatCentral.ResultDirectoryName + "\\BadCGVector" + Hotsun.TotalCGFailures.ToString(); var sw = new StreamWriter(fname, false, Encoding.UTF8); double fractionnorm = 1.0 / Math.Sqrt(CurrentRNorm); try { for (int GlobalPointIndex = 0; GlobalPointIndex < SALSAUtility.PointCount_Global; GlobalPointIndex++) { string Coordinates = ""; int UsedPointIndex = SALSAUtility.NaivetoActualUsedOrder[GlobalPointIndex]; double pointsize = 0.0; for (int LocalVectorIndex = 0; LocalVectorIndex < Hotsun.ParameterVectorDimension; LocalVectorIndex++) { Coordinates += GlobalCGVector_R[UsedPointIndex][LocalVectorIndex].ToString("E4") + "\t"; pointsize += GlobalCGVector_R[UsedPointIndex][LocalVectorIndex] * GlobalCGVector_R[UsedPointIndex][LocalVectorIndex]; } pointsize = Math.Sqrt(pointsize) * fractionnorm; sw.WriteLine( String.Format((GlobalPointIndex).ToString() + "\t" + Coordinates + " " + pointsize.ToString("E4"))); } sw.Flush(); sw.Close(); } catch (Exception e) { Console.WriteLine("Failed writing data in CG Solver " + e); throw (e); } } break; } } // End Loop over Conjugate Gradient Steps NumberofIterations = CountSteps; return(matrixsuccess); }
// End SetupHotsunforMDS public static void SetupRotateMDS() { if (Hotsun.InitializationLoops < 2) { Hotsun.InitializationLoops = 2; Hotsun.InitLoopChisq = new double[Hotsun.InitializationLoops]; ManxcatCentral.Configuration.InitializationLoops = Hotsun.InitializationLoops; } Hotsun.bnderrLimit = 10; if (SALSAUtility.NumberFixedPoints > 0) { Exception e = SALSAUtility.SALSAError(" Fixed Points not Allowed in Rotations"); throw (e); } RotationOption = ManxcatCentral.Configuration.RotationOption; // set Rotation Option RotationOption_GenerateTestInput = RotationOption / 100; // Control special test inputs = 0 normal -- don't generate points to rotate; // RotationOption_GenerateTestInput = 1 full inversion; // RotationOption_GenerateTestInput = 2 invert x and y only; // RotationOption_GenerateTestInput = 3 invert x only // RotationOption_GenerateTestInput = 4 set new file = old file RotationOption = RotationOption - 100 * RotationOption_GenerateTestInput; RotationOption_invert = RotationOption / 10; // Control how to control scaling in fit. = 0 force scaling positive so when cosmic scaling improper, one cannot fit proper rotation // RotationOption_invert = 1 allow scaling to be negative which can allow fit to be trapped in wring proper/improper domain for all starts. // If RotationOption_invert=0 then every other initialization loop is proper (0 1 ..) or improper (1 3 ...) // Note initial angle values are 0 for initialization loops 0 1nd 1, they are random for later loops // Angles are forced to lie between -PI and PI RotationOption = RotationOption - 10 * RotationOption_invert; // RotationOption = 0 usual Chisq; = 1 Each term divided by magnitude of original point so one minimizes realative errors squared PointVectorDimension = SALSAUtility.GlobalFileProperties.LocalVectorDimension; if (PointVectorDimension != SALSAUtility.GlobalFileProperties.LocalVectorDimension) { Exception e = SALSAUtility.SALSAError(" Inconsistent Small Dimension in Rotations " + PointVectorDimension.ToString()); throw (e); } int InitializationFileType = -1; int InitializationNumberofPoints = -1; // Set Reference Points if (SALSA_ProcessVariedandFixed.AreValuesSet(SALSAUtility.GlobalPointProperties)) { SALSAUtility.SALSAPrint(2, " Reference Points Taken from basic Labels File " + ManxcatCentral.Configuration.DataLabelsFileName); } else { // Set up Values of Used Points from Initialization File var InitializationFileProperties = new SALSAFileProperties(); var InitializationPointProperties = new SALSADataPointProperties[SALSAUtility.NumberOriginalPoints]; string OriginalMDSFileName = ManxcatCentral.Configuration.InitializationFileName; // if (!OriginalMDSFileName.Contains(":") || !OriginalMDSFileName.Contains("$")) // OriginalMDSFileName = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + OriginalMDSFileName; SALSAUtility.SALSAPrint(2, " Reference Points Taken from Initialization File " + OriginalMDSFileName); // Begin Changes saliya 03/25/11 // Note. I think we can agree on the zero based indices. The following should be removed then. if (OriginalMDSFileName.Contains("SIMPLE")) { InitializationFileProperties.LocalPointStartIndex = 1; } // End Changes saliya 03/25/11 SALSA_Properties.ReadDataPointFile(OriginalMDSFileName, ref InitializationFileType, InitializationFileProperties, ref InitializationPointProperties, ref InitializationNumberofPoints); if ((SALSAUtility.NumberOriginalPoints < InitializationNumberofPoints) || (InitializationFileProperties.NumberOriginalPoints != SALSAUtility.NumberOriginalPoints)) { Exception e = SALSAUtility.SALSAError(" Inconsistent Initialization File Point Counts " + InitializationNumberofPoints.ToString() + " or " + InitializationFileProperties.NumberOriginalPoints.ToString() + " Expected is " + SALSAUtility.NumberOriginalPoints.ToString()); throw (e); } for (int InitialIndex = 0; InitialIndex < InitializationNumberofPoints; InitialIndex++) { int OriginalIndex = InitializationPointProperties[InitialIndex].OriginalPointNumber; if (!InitializationPointProperties[InitialIndex].valuesset) { continue; } SALSAUtility.GlobalPointProperties[OriginalIndex].x = InitializationPointProperties[InitialIndex].x; SALSAUtility.GlobalPointProperties[OriginalIndex].y = InitializationPointProperties[InitialIndex].y; SALSAUtility.GlobalPointProperties[OriginalIndex].z = InitializationPointProperties[InitialIndex].z; SALSAUtility.GlobalPointProperties[OriginalIndex].valuesset = InitializationPointProperties[InitialIndex].valuesset; } if (!SALSA_ProcessVariedandFixed.AreValuesSet(SALSAUtility.GlobalPointProperties)) { Exception e = SALSAUtility.SALSAError(" Reference Points Not set"); throw (e); } } // Now Read Points to be Rotated // Set up Values of Used Points from Rotation File int RotationFileType = -1; RotationFileProperties = new SALSAFileProperties(); RotationPointProperties = new SALSADataPointProperties[SALSAUtility.NumberOriginalPoints]; if (RotationOption_GenerateTestInput == 0) { int RotationNumberofPoints = -1; string RotationFileName = ManxcatCentral.Configuration.RotationLabelsFileName; // if (!RotationFileName.Contains(":")) // RotationFileName = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + RotationFileName; if (RotationFileName.Contains("SIMPLE")) { RotationFileProperties.LocalPointStartIndex = 1; } SALSAUtility.SALSAPrint(2, " Points to be rotated Taken from File " + RotationFileName); SALSA_Properties.ReadDataPointFile(RotationFileName, ref RotationFileType, RotationFileProperties, ref RotationPointProperties, ref RotationNumberofPoints); if ((SALSAUtility.NumberOriginalPoints < RotationNumberofPoints) || (RotationFileProperties.NumberOriginalPoints != SALSAUtility.NumberOriginalPoints)) { Exception e = SALSAUtility.SALSAError(" Inconsistent Rotation File Point Counts " + RotationNumberofPoints.ToString() + " or " + RotationFileProperties.NumberOriginalPoints.ToString() + " Expected is " + SALSAUtility.NumberOriginalPoints.ToString()); throw (e); } if (!SALSA_ProcessVariedandFixed.AreValuesSet(RotationPointProperties)) { Exception e = SALSAUtility.SALSAError(" Points to rotate Not set"); throw (e); } } // End normal case when we read file of points to be rotated else { // Generate file to be Rotated RotationFileType = InitializationFileType; for (int PointIndex = 0; PointIndex < SALSAUtility.NumberOriginalPoints; PointIndex++) { RotationPointProperties[PointIndex] = new SALSADataPointProperties(); } double fudgex = 1.0; double fudgey = 1.0; double fudgez = 1.0; if (RotationOption_GenerateTestInput == 1) { SALSAUtility.SALSAPrint(2, " Points to be rotated generated as -x -y -z "); fudgex = -1.0; fudgey = -1.0; fudgez = -1.0; } if (RotationOption_GenerateTestInput == 2) { SALSAUtility.SALSAPrint(2, " Points to be rotated generated as -x -y +z "); fudgex = -1.0; fudgey = -1.0; } if (RotationOption_GenerateTestInput == 3) { SALSAUtility.SALSAPrint(2, " Points to be rotated generated as -x +y +z "); fudgex = -1.0; } if (RotationOption_GenerateTestInput >= 4) { SALSAUtility.SALSAPrint(2, " Points to be rotated generated as +x +y +z "); } for (int GlobalPointIndex = 0; GlobalPointIndex < SALSAUtility.PointCount_Global; GlobalPointIndex++) { int OriginalIndex = SALSAUtility.UsedPointtoOriginalPointMap[GlobalPointIndex]; RotationPointProperties[OriginalIndex].x = fudgex * SALSAUtility.GlobalPointProperties[OriginalIndex].x; ; RotationPointProperties[OriginalIndex].y = fudgey * SALSAUtility.GlobalPointProperties[OriginalIndex].y; RotationPointProperties[OriginalIndex].z = fudgez * SALSAUtility.GlobalPointProperties[OriginalIndex].z; RotationPointProperties[OriginalIndex].valuesset = SALSAUtility.GlobalPointProperties[OriginalIndex].valuesset; } } // Set up operational data FirstData = new double[SALSAUtility.PointCount_Global][]; // Initial point data SecondData = new double[SALSAUtility.PointCount_Global][]; ; // Second point data FirstMean = new double[PointVectorDimension]; // Mean of initial point data SecondMean = new double[PointVectorDimension]; for (int GlobalPointIndex = 0; GlobalPointIndex < SALSAUtility.PointCount_Global; GlobalPointIndex++) { FirstData[GlobalPointIndex] = new double[PointVectorDimension]; SecondData[GlobalPointIndex] = new double[PointVectorDimension]; } // Process Data ProcessPointData(SALSAUtility.GlobalPointProperties, FirstData, FirstMean, out FirstScale); ProcessPointData(RotationPointProperties, SecondData, SecondMean, out SecondScale); ScalePosition = 6; if (PointVectorDimension == 2) { ScalePosition = 3; } NumberSubRotations = 3; if (PointVectorDimension == 2) { NumberSubRotations = 1; } // This holds the inversion transformation CurrentCosmicScaling = new double[PointVectorDimension, PointVectorDimension]; SavedCosmicScaling = new double[Hotsun.InitializationLoops][, ]; for (int scalingindex = 0; scalingindex < Hotsun.InitializationLoops; scalingindex++) { SavedCosmicScaling[scalingindex] = new double[PointVectorDimension, PointVectorDimension]; } // If ManxcatCentral.MetadataforRun.MinimumDistance is positive, it is absolute Minimum Distance // If ManxcatCentral.MetadataforRun.MinimumDistance is negative, it is - multiplier of System Average if (ManxcatCentral.Configuration.MinimumDistance < 0) { MinimumDistance = -ManxcatCentral.Configuration.MinimumDistance * FirstScale; } else { MinimumDistance = ManxcatCentral.Configuration.MinimumDistance; } string firstM = ""; string secondM = ""; for (int LocalVectorIndex = 0; LocalVectorIndex < PointVectorDimension; LocalVectorIndex++) { firstM += FirstMean[LocalVectorIndex].ToString("E4") + " "; secondM += SecondMean[LocalVectorIndex].ToString("E4") + " "; } SALSAUtility.SALSAPrint(0, " First Scale " + FirstScale.ToString("E4") + " Second Scale " + SecondScale.ToString("E4") + " First Mean " + firstM + " Second Mean " + secondM); }
// End UpdateManxcatMDS_Option12_Cluster() public static void UpdateManxcatMDS_Option12_TraditionalDirectory() { string labelFileDirectory = ManxcatCentral.Configuration.ClusterDirectory; // Input Directory string MDSandClusterDirectory = labelFileDirectory + "\\" + ManxcatCentral.Configuration.RunSetLabel + "-R" + ManxcatCentral.Configuration.RunNumber.ToString() + "-ManxcatMDS"; // Output Directory if (Directory.Exists(MDSandClusterDirectory)) { SALSAUtility.SALSAPrint(0, "The directory " + MDSandClusterDirectory + " exists"); } else { DirectoryInfo di = Directory.CreateDirectory(MDSandClusterDirectory); SALSAUtility.SALSAPrint(0, "The directory " + MDSandClusterDirectory + " was created successfully at " + Directory.GetCreationTime(MDSandClusterDirectory)); } string[] SMACOFfileEntries = Directory.GetFiles(MDSandClusterDirectory); string TypicalMDSFileName = ManxcatCentral.Configuration.ReducedVectorOutputFileName; if (!TypicalMDSFileName.Contains(":")) { TypicalMDSFileName = ManxcatCentral.ResultDirectoryName + "\\SIMPLE" + TypicalMDSFileName; } if (!File.Exists(TypicalMDSFileName)) { Exception e = SALSAUtility.SALSAError(" File " + TypicalMDSFileName + " Does Not Exist"); throw (e); } int NumberMDSPoints = 0; var InitialMDSString = new string[SALSAUtility.PointCount_Global]; var ColorValue = new int[SALSAUtility.PointCount_Global]; if (ReadMDSCluster_File(TypicalMDSFileName, InitialMDSString, ColorValue, ref NumberMDSPoints)) { Console.WriteLine(TypicalMDSFileName + " Read Successfully"); } string[] fileEntries = Directory.GetFiles(labelFileDirectory); foreach (string LabelFileName in fileEntries) { if (LabelFileName.Contains(".dot")) { continue; } if (LabelFileName.Contains("Summary")) { continue; } if (LabelFileName.Contains("Timing")) { continue; } string LabelFileName1 = LabelFileName.Replace(labelFileDirectory + "\\", ""); string coreFileName = MDSandClusterDirectory + "\\" + "MDSManxcat-" + LabelFileName1; if (File.Exists(coreFileName)) { continue; } int NumberOfLabels = 0; var CategoryLabel = new string[SALSAUtility.PointCount_Global]; ReadClusterLabel_File(LabelFileName, CategoryLabel, ref NumberOfLabels); if (NumberOfLabels != SALSAUtility.PointCount_Global) { Exception e = SALSAUtility.SALSAError(" Illegal Count " + NumberOfLabels.ToString() + " in File " + LabelFileName); throw (e); } var CategoryIndex = new int[SALSAUtility.PointCount_Global]; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { CategoryIndex[PointIndex] = Convert.ToInt32(CategoryLabel[PointIndex]); } WriteColor_Cluster(coreFileName, InitialMDSString, CategoryIndex, SALSAUtility.PointCount_Global, false); SALSAUtility.SALSAPrint(0, "Traditional Directory MDS Info Added to " + coreFileName); } }
// End UpdateManxcatMDS_Option12_FamilybyCuts public static void UpdateManxcatMDS_Option12_Cluster() { // Read Cluster Information with File specified in ConversionInformation string ClusterFileName = ManxcatCentral.Configuration.ConversionInformation; int NumberOfClusterLines = 0; var CategoryLabel = new string[SALSAUtility.PointCount_Global]; ReadClusterLabel_File(ClusterFileName, CategoryLabel, ref NumberOfClusterLines); if (NumberOfClusterLines != SALSAUtility.PointCount_Global) { Exception e = SALSAUtility.SALSAError(" Illegal Count " + NumberOfClusterLines.ToString() + " in File " + ClusterFileName); throw (e); } var CategoryIndex = new int[SALSAUtility.PointCount_Global]; int minlabel = SALSAUtility.PointCount_Global; int maxlabel = 0; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { CategoryIndex[PointIndex] = Convert.ToInt32(CategoryLabel[PointIndex]); if (minlabel > CategoryIndex[PointIndex]) { minlabel = CategoryIndex[PointIndex]; } if (maxlabel < CategoryIndex[PointIndex]) { maxlabel = CategoryIndex[PointIndex]; } } int TotalNumberClusters = maxlabel - minlabel + 1; var ClusterCounts = new int[TotalNumberClusters]; for (int Icount = 0; Icount < TotalNumberClusters; Icount++) { ClusterCounts[Icount] = 0; } for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { ++ClusterCounts[CategoryIndex[PointIndex] - minlabel]; } for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { int Icount = CategoryIndex[PointIndex]; SALSAUtility.GlobalPointProperties[PointIndex].cluster = Icount; SALSAUtility.GlobalPointProperties[PointIndex].clusterlabel = "Cluster " + Icount.ToString() + " out of " + TotalNumberClusters.ToString() + " Count " + ClusterCounts[Icount - minlabel].ToString(); } SALSAUtility.GlobalFileProperties.ClusterName = "Clusters from " + ClusterFileName; SALSAUtility.GlobalFileProperties.ClusterStartIndex = minlabel; string cleandate = SALSAUtility.startTime.ToLocalTime().ToString(); cleandate = cleandate.Replace(":", "."); cleandate = cleandate.Replace(" ", "-"); string OldComment = SALSAUtility.GlobalFileProperties.Comment; if (OldComment != "") { OldComment += "\n"; } OldComment += "Cluster Information added from " + ClusterFileName + " Time " + cleandate; SALSAUtility.GlobalFileProperties.Comment = OldComment; // Write new label file string labelfilename = ManxcatCentral.Configuration.DataLabelsFileName; if (!labelfilename.Contains(":") && !labelfilename.Contains("$")) { labelfilename = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + labelfilename; } SALSA_Properties.WriteDataPointFile(labelfilename, ManxcatCentral.Configuration.Write2Das3D, "colon,SameFileName ", SALSAUtility.GlobalFileProperties, SALSAUtility.GlobalPointProperties, SALSAUtility.NumberOriginalPoints); SALSAUtility.SALSAPrint(0, "Cluster Info Added to " + labelfilename + " from " + ClusterFileName); ManxcatCentral.Configuration.Comment += "\nCluster Info Added to " + labelfilename + " from " + ClusterFileName; }
// End UpdateManxcatMDS_Option12() public static void UpdateManxcatMDS_Option12_Functions(bool statistics) { // Setup weight: Only used in statistics var weights = new double[SALSAUtility.PointCount_Global]; ManxcatMDS.WeightingOption = ManxcatCentral.Configuration.WeightingOption; ManxcatMDS.SetupWeightings(weights); string TypicalMDSFileName = ""; // Setup MDS: Only used in statistics var MDSPoints = new double[SALSAUtility.PointCount_Global, Hotsun.ParameterVectorDimension]; if (statistics) { // Set up statistics by reading MDS file TypicalMDSFileName = ManxcatCentral.ResultDirectoryName + "\\SIMPLE" + ManxcatCentral.Configuration.ReducedVectorOutputFileName; if (!File.Exists(TypicalMDSFileName)) { Exception e = SALSAUtility.SALSAError(" File " + TypicalMDSFileName + " Does Not Exist"); throw (e); } int NumberMDSPoints = 0; var InitialMDSString = new string[SALSAUtility.PointCount_Global]; var ColorValue = new int[SALSAUtility.PointCount_Global]; if (ReadMDSCluster_File(TypicalMDSFileName, InitialMDSString, ColorValue, ref NumberMDSPoints)) { SALSAUtility.SALSAPrint(0, "MDS File " + TypicalMDSFileName + " Read Successfully"); } for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { // Extract MDS POints string inputLineStr = InitialMDSString[PointIndex].Trim(); string[] split = inputLineStr.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length < Hotsun.ParameterVectorDimension) { Exception e = SALSAUtility.SALSAError(" Point " + PointIndex.ToString() + " has wrong number of Entries " + inputLineStr + " Entries" + split.Length); throw (e); } for (int VectorIndex = 0; VectorIndex < Hotsun.ParameterVectorDimension; VectorIndex++) { MDSPoints[PointIndex, VectorIndex] = Convert.ToDouble(split[VectorIndex].Trim()); } } } // End set up of Statistics // Set Mapping var ClusterLabel = new int[SALSAUtility.PointCount_Global]; int NumDivisions = 10; int BasicSize = SALSAUtility.PointCount_Global / NumDivisions; int LeftOver = SALSAUtility.PointCount_Global - BasicSize * NumDivisions; int DivisionCount = 0; int Limit = 0; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { if (PointIndex >= Limit) { ++DivisionCount; Limit += BasicSize; if (LeftOver >= DivisionCount) { ++Limit; } } ClusterLabel[PointIndex] = DivisionCount; } String FunctionFileName = ManxcatCentral.Configuration.ConversionInformation; if (!FunctionFileName.Contains(":")) { FunctionFileName = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + FunctionFileName; } var functionkeys = new double[SALSAUtility.PointCount_Global]; var functionlabels = new int[SALSAUtility.PointCount_Global]; int pickout = -1; while (true) { ++pickout; bool endofdata = true; int NumberofLines = 0; double sumabs = 0.0; double totalweight = 0.0; double meangamma = 0.0; double vargamma = 0.0; var meanxyz = new double[Hotsun.ParameterVectorDimension]; var varxyz = new double[Hotsun.ParameterVectorDimension, Hotsun.ParameterVectorDimension]; var correlxyzgamma = new double[Hotsun.ParameterVectorDimension]; if (statistics) { for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { meanxyz[VectorIndex1] = 0.0; for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varxyz[VectorIndex1, VectorIndex2] = 0.0; } correlxyzgamma[VectorIndex1] = 0.0; } } try { // Check if file exists if (!File.Exists(FunctionFileName)) { Exception e = SALSAUtility.SALSAError("File " + FunctionFileName + " does not exists."); throw (e); } // Create a new stream to read from a file using (StreamReader sr = File.OpenText(FunctionFileName)) { // Read contents of a file, line by line, into a string String inputLineStr; while ((inputLineStr = sr.ReadLine()) != null) { inputLineStr = inputLineStr.Trim(); string[] split = inputLineStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length <= pickout) { break; } endofdata = false; string usethis = split[pickout].Trim(); if (usethis.Length < 1) { continue; //replace empty line } double gamma = Convert.ToDouble(usethis); functionkeys[NumberofLines] = gamma; functionlabels[NumberofLines] = NumberofLines; sumabs += Math.Abs(gamma); if (statistics) { double wgt = weights[NumberofLines]; meangamma += gamma * wgt; vargamma += wgt * gamma * gamma; totalweight += wgt; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { meanxyz[VectorIndex1] += MDSPoints[NumberofLines, VectorIndex1] * wgt; correlxyzgamma[VectorIndex1] += MDSPoints[NumberofLines, VectorIndex1] * gamma * wgt; for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varxyz[VectorIndex1, VectorIndex2] += MDSPoints[NumberofLines, VectorIndex1] * MDSPoints[NumberofLines, VectorIndex2] * wgt; } } } ++NumberofLines; } sr.Close(); } } catch (Exception e) { SALSAUtility.SALSAError("Failed to read data from " + FunctionFileName + " " + e); throw (e); } if (endofdata) { break; } if (NumberofLines != SALSAUtility.PointCount_Global) { SALSAUtility.SALSAError("Incorrect Function count read " + NumberofLines + " Expected " + SALSAUtility.PointCount_Global); } // Set Statistics if (statistics) { var alpha = new double[Hotsun.ParameterVectorDimension]; double alphanorm = 0.0; meangamma = meangamma / totalweight; vargamma = (vargamma / totalweight) - meangamma * meangamma; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { meanxyz[VectorIndex1] = meanxyz[VectorIndex1] / totalweight; } for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varxyz[VectorIndex1, VectorIndex2] = (varxyz[VectorIndex1, VectorIndex2] / totalweight) - meanxyz[VectorIndex1] * meanxyz[VectorIndex2]; } correlxyzgamma[VectorIndex1] = correlxyzgamma[VectorIndex1] / totalweight - meangamma * meanxyz[VectorIndex1]; alpha[VectorIndex1] = correlxyzgamma[VectorIndex1] / varxyz[VectorIndex1, VectorIndex1]; alphanorm += alpha[VectorIndex1] * alpha[VectorIndex1]; } // invert Matrix to find best alpha var ConventionalFirst = new double[Hotsun.ParameterVectorDimension, 1]; var ConventionalAnswer = new double[Hotsun.ParameterVectorDimension][]; var ConventionalMatrix = new double[Hotsun.ParameterVectorDimension, Hotsun.ParameterVectorDimension]; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { ConventionalAnswer[VectorIndex1] = new double[1]; ConventionalFirst[VectorIndex1, 0] = correlxyzgamma[VectorIndex1] / Math.Sqrt(varxyz[VectorIndex1, VectorIndex1]); for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { ConventionalMatrix[VectorIndex1, VectorIndex2] = varxyz[VectorIndex1, VectorIndex2] / Math.Sqrt( varxyz[VectorIndex1, VectorIndex1] * varxyz[VectorIndex2, VectorIndex2]); } } LMatrix cMatrix = LMatrix.Create(ConventionalMatrix); LMatrix RightHandSide = LMatrix.Create(ConventionalFirst); LMatrix MatrixAnswer = cMatrix.Solve(RightHandSide); ConventionalAnswer = MatrixAnswer; alphanorm = 0.0; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { alpha[VectorIndex1] = ConventionalAnswer[VectorIndex1][0] / Math.Sqrt(varxyz[VectorIndex1, VectorIndex1]); alphanorm += alpha[VectorIndex1] * alpha[VectorIndex1]; } double Fullcorrelation = 0.0; double varalphaxyz = 0.0; for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { alpha[VectorIndex1] = alpha[VectorIndex1] / Math.Sqrt(alphanorm); Fullcorrelation += alpha[VectorIndex1] * correlxyzgamma[VectorIndex1]; } for (int VectorIndex1 = 0; VectorIndex1 < Hotsun.ParameterVectorDimension; VectorIndex1++) { for (int VectorIndex2 = 0; VectorIndex2 < Hotsun.ParameterVectorDimension; VectorIndex2++) { varalphaxyz += alpha[VectorIndex1] * alpha[VectorIndex2] * varxyz[VectorIndex1, VectorIndex2]; } } Console.WriteLine(varalphaxyz + " " + Fullcorrelation + " " + vargamma); Fullcorrelation = Fullcorrelation / (Math.Sqrt(vargamma * varalphaxyz)); SALSAUtility.SALSAPrint(0, "Column " + pickout.ToString() + " File " + FunctionFileName + " MDS File " + TypicalMDSFileName + " Total Weight " + totalweight.ToString() + " Correlation " + Fullcorrelation.ToString("F4") + " Direction " + alpha[0].ToString("F4") + " " + alpha[1].ToString("F4") + " " + alpha[2].ToString("F4")); } // Set Divisions Array.Sort(functionkeys, functionlabels); var PointColors = new int[SALSAUtility.PointCount_Global]; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { int UnsortedPosition = functionlabels[PointIndex]; PointColors[UnsortedPosition] = ClusterLabel[PointIndex]; } string OutputFileExtension = ManxcatCentral.Configuration.ConversionInformation; OutputFileExtension = OutputFileExtension.Replace(".dat", ""); OutputFileExtension = OutputFileExtension.Replace(".txt", ""); OutputFileExtension = OutputFileExtension + "-" + pickout.ToString() + ".txt"; string labelFileDirectory = ManxcatCentral.Configuration.ClusterDirectory; // Directory for Colors attached to Points string OutputFileName = labelFileDirectory + "\\" + OutputFileExtension; WritePointCluster(OutputFileName, PointColors, SALSAUtility.PointCount_Global); double test = 0.1 * sumabs / NumberofLines; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { int UnsortedPosition = functionlabels[PointIndex]; if (Math.Abs(functionkeys[UnsortedPosition]) < test) { PointColors[UnsortedPosition] = 0; } } OutputFileName = OutputFileName.Replace(".txt", "NZ.txt"); WritePointCluster(OutputFileName, PointColors, SALSAUtility.PointCount_Global); } // End while over pickout (columns in data) return; }
// End UpdateManxcatMDS_Option12_Functions public static void UpdateManxcatMDS_Option12_FamilybyCuts() { String CutFileName = ManxcatCentral.Configuration.ConversionInformation; if (!CutFileName.Contains(":")) { CutFileName = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + CutFileName; } var inputdata = new string[SALSAUtility.PointCount_Global]; int NumberofCuts = 0; string FamilyLabel = "Family"; int FamilyNumber = 1; if (ManxcatCentral.Configuration.ConversionOption.ToLower().Contains("family2")) { FamilyNumber = 2; } ReadString_File(CutFileName, inputdata, ref NumberofCuts); if (NumberofCuts <= 0) { Exception e = SALSAUtility.SALSAError("Too few points " + NumberofCuts.ToString() + " in Cut File " + CutFileName); throw (e); } var CutStart = new int[NumberofCuts]; var CutLabel = new string[NumberofCuts]; int CountFamilies = 0; int itmpold = -2; for (int CutPos = 0; CutPos < NumberofCuts; CutPos++) { string[] split = inputdata[CutPos].Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); int itmp = Convert.ToInt32(split[0]); if (itmp < 0) { FamilyLabel = split[1]; continue; } if (itmp <= itmpold) { continue; } CutStart[CountFamilies] = itmp; CutLabel[CountFamilies] = FamilyLabel + "-" + split[1]; ++CountFamilies; itmpold = itmp; } if (FamilyNumber == 1) { SALSAUtility.GlobalFileProperties.FamilyName1 = FamilyLabel + " from " + CutFileName; } if (FamilyNumber == 2) { SALSAUtility.GlobalFileProperties.FamilyName2 = FamilyLabel + " from " + CutFileName; } int minlabel = 1; int CurrentFamily = 0; var CategoryIndex = new int[SALSAUtility.PointCount_Global]; for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { if (CurrentFamily < CountFamilies - 1) { if ((PointIndex + 1) >= CutStart[CurrentFamily + 1]) { ++CurrentFamily; } } CategoryIndex[PointIndex] = minlabel + CurrentFamily; continue; } var FamilyCounts = new int[CountFamilies]; for (int Icount = 0; Icount < CountFamilies; Icount++) { FamilyCounts[Icount] = 0; } for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { ++FamilyCounts[CategoryIndex[PointIndex] - minlabel]; } for (int PointIndex = 0; PointIndex < SALSAUtility.PointCount_Global; PointIndex++) { int Icount = CategoryIndex[PointIndex]; if (FamilyNumber == 1) { SALSAUtility.GlobalPointProperties[PointIndex].family1 = Icount; SALSAUtility.GlobalPointProperties[PointIndex].familylabel1 = CutLabel[Icount - minlabel] + " " + Icount.ToString() + " out of " + CountFamilies.ToString() + " Count " + FamilyCounts[Icount - minlabel]. ToString(); } if (FamilyNumber == 2) { SALSAUtility.GlobalPointProperties[PointIndex].family2 = Icount; SALSAUtility.GlobalPointProperties[PointIndex].familylabel2 = CutLabel[Icount - minlabel] + " " + Icount.ToString() + " out of " + CountFamilies.ToString() + " Count " + FamilyCounts[Icount - minlabel]. ToString(); } } string cleandate = SALSAUtility.startTime.ToLocalTime().ToString(); cleandate = cleandate.Replace(":", "."); cleandate = cleandate.Replace(" ", "-"); string OldComment = SALSAUtility.GlobalFileProperties.Comment; if (OldComment != "") { OldComment += "\n"; } OldComment += "Family" + FamilyNumber.ToString() + " " + FamilyLabel + " Information added from " + CutFileName + " Time " + cleandate; SALSAUtility.GlobalFileProperties.Comment = OldComment; // Write new label file string labelfilename = ManxcatCentral.Configuration.DataLabelsFileName; if (!labelfilename.Contains(":")) { labelfilename = ManxcatCentral.Configuration.ControlDirectoryName + "\\" + labelfilename; } SALSA_Properties.WriteDataPointFile(labelfilename, ManxcatCentral.Configuration.Write2Das3D, "colon,SameFileName ", SALSAUtility.GlobalFileProperties, SALSAUtility.GlobalPointProperties, SALSAUtility.NumberOriginalPoints); SALSAUtility.SALSAPrint(0, "Family" + FamilyNumber.ToString() + " " + FamilyLabel + " Info Added to " + labelfilename + " from " + CutFileName); ManxcatCentral.Configuration.Comment += "\nFamily" + FamilyNumber.ToString() + " " + FamilyLabel + " Info Added to " + labelfilename + " from " + CutFileName; }