Exemplo n.º 1
0
void example_distance_array(int nrows, int ncols, double** data, int** mask)
/* Calculate the distance matrix between microarrays using the Spearman rank
 * correlation. */
{ int i, j;
  double** distMatrix;
  double* weight = malloc(nrows*sizeof(double));
  printf("========== Spearman distance matrix between microarrays =========\n");
  for (i = 0; i < nrows; i++) weight[i] = 1.0;
  distMatrix = distancematrix(nrows, ncols, data, mask, weight, 's', 1);
  if (!distMatrix)
  { printf ("Insufficient memory to store the distance matrix\n");
    free(weight);
    return;
  }
  printf("   Microarray:");
  for(i=0; i<ncols-1; i++) printf("%9d", i);
  printf("\n");
  for(i=0; i<ncols; ++i)
  { printf("Microarray %2d: ",i);
    for(j=0; j<i; ++j) printf(" %f",distMatrix[i][j]);
    printf("\n");
  }
  printf("\n");
  free(weight);
  for(i = 0; i < ncols; i++) free(distMatrix[i]);
  free(distMatrix);
  return;
}
Exemplo n.º 2
0
void HClusterDlg::SpatialConstraintClustering()
{
    int transpose = 0; // row wise
    fastcluster::cluster_result Z2(rows-1);

    if (chk_contiguity->GetValue()) {
        vector<boost::uuids::uuid> weights_ids;
        WeightsManInterface* w_man_int = project->GetWManInt();
        w_man_int->GetIds(weights_ids);

        int sel = combo_weights->GetSelection();
        if (sel < 0) sel = 0;
        if (sel >= weights_ids.size()) sel = weights_ids.size()-1;

        boost::uuids::uuid w_id = weights_ids[sel];
        GalWeight* gw = w_man_int->GetGal(w_id);

        if (gw == NULL) {
            wxMessageDialog dlg (this, _("Invalid Weights Information:\n\n The selected weights file is not valid.\n Please choose another weights file, or use Tools > Weights > Weights Manager\n to define a valid weights file."), _("Warning"), wxOK | wxICON_WARNING);
            dlg.ShowModal();
            return;
        }
        //GeoDaWeight* weights = w_man_int->GetGal(w_id);
        // Check connectivity
        if (!CheckConnectivity(gw)) {
            wxString msg = _("The connectivity of selected spatial weights is incomplete, please adjust the spatial weights.");
            wxMessageDialog dlg(this, msg, _("Warning"), wxOK | wxICON_WARNING );
            dlg.ShowModal();
            return;
        }

        double** ragged_distances = distancematrix(rows, columns, input_data,  mask, weight, dist, transpose);
        double** distances = DataUtils::fullRaggedMatrix(ragged_distances, rows, rows);
        for (int i = 1; i < rows; i++) free(ragged_distances[i]);
        free(ragged_distances);
        std::vector<bool> undefs(rows, false);
        SpanningTreeClustering::AbstractClusterFactory* redcap = new SpanningTreeClustering::FirstOrderSLKRedCap(rows, columns, distances, input_data, undefs, gw->gal, NULL, 0);
        for (int i=0; i<redcap->ordered_edges.size(); i++) {
            Z2[i]->node1 = redcap->ordered_edges[i]->orig->id;
            Z2[i]->node2 = redcap->ordered_edges[i]->dest->id;
            Z2[i]->dist = redcap->ordered_edges[i]->length;
        }

        delete redcap;
    }
}
bool BagOfFeatures::buildHierarchicalTree(int transpose,
                                          char dist,
                                          char method,
                                          double** distmatrix)
{
//double** buildMatrixHierarchical(const ImageFeatures *featurePts, const int feature_count,
//			const int image_count, Node*& tree )
    // Check to makes sure that features were found
    if(trainObject ==  NULL || numFeatures == 0 || descrSize == 0)
        return false;

	int i, j;
	int k = 0, l = 0, m;
	int size;
	int totalImages = 0;

    cout << "Initializing the data..." << endl;

	// Initialize the data
	hClusterData = new double* [numFeatures];
	for(i = 0; i < numFeatures; i++)
		hClusterData[i] = new double [descrSize];

    // Allocate mask and set it all to 1 (assume no missing data)
	int ** mask = new int* [numFeatures];
	for(i = 0; i < numFeatures; i++)
	{
	    mask[i] = new int [descrSize];
		for(j = 0; j < descrSize; j++)
			mask[i][j] = 1;
	}

	// Set the weights equal, all 1
	double* weight = new double [descrSize];
	for(i = 0; i < descrSize; i++)
		weight[i] = 1.0;


	// For each class
    for(m = 0; m < numClasses; m++)
    {
        totalImages = data[m].getTrainSize();
        // For each image in that class...
        for(l = 0; l < totalImages; l++)
        {
            size = trainObject[m].featureSet[l].size;
            // for each feature in that image...
            for(i = 0; i < size; i++)
            {
                // Copy the descriptor into the data array
                for(j = 0; j < descrSize; j++)
                {
                    hClusterData[k][j] = (double)trainObject[m].featureSet[l].descriptors[i][j];
                    //cout << hClusterData[k][j] << " ";
                }
                //cout << endl;
                k++;
            }
        }
    }

    cout << "Calculating the distance matrix..." << endl;

    distmatrix = distancematrix (descrSize, numFeatures,  hClusterData, mask, weight, dist, transpose);

    cout << "Building Hierarchical Tree" << endl;
	// Centroid Hierarchical Clustering
	// feature_count X DESCRIPTOR_SIZE
	// The feature vectors
	// mask (all 1s)
	// weights (all 1s)
	hTree = treecluster(descrSize, numFeatures, hClusterData, mask, weight, transpose, dist, method, distmatrix);

	// Release the mask
	for(i = 0; i < numFeatures; i++)
		delete [] mask[i];
	delete [] mask;
	// Release the weight
	delete [] weight;

    // Make sure that the tree was allocated
	if(!hTree)
	{
		cout << "Could not allocate the tree: Insufficient memory..." << endl;
		for(i = 0; i < numFeatures; i++)
            delete [] hClusterData[i];
        delete [] hClusterData;
		return false;
	}

    return true;
}