Skip to content

Commit 2ee920f

Browse files
committed
LOCO_OUT/ERR/EXIT; first functions for RCPP object writing
1 parent 444e633 commit 2ee920f

17 files changed

Lines changed: 224 additions & 149 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Description: Quickly detect local correlation patterns between features in singl
99
space from neighbourhood to neighbourhood.
1010
License: GPL-3.0 license
1111
LinkingTo: Rcpp
12-
Imports: Rcpp
12+
Imports: Rcpp,ggplot2
1313
SystemRequirements: C++17
1414
RoxygenNote: 7.1.2

R/loco.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,19 @@ run_loco <- function(
6565
)
6666

6767
return(res)
68+
}
69+
70+
# TODO: add umap coords to res, when plot_n is called the first time these umap coords are filled
71+
72+
#' Plot correlation for features by neighbourhoods
73+
#' Therefore create UMAP space first to then plot neighbourhoods into the same space
74+
#' @import ggplot2
75+
#' @export
76+
plot_neighbourhood <- function(res, featureA = "x", featureB = "y")
77+
{
78+
79+
80+
p +
81+
ggplot2::geom_point(alpha = 0.7) +
82+
ggplot2::theme_minimal()
6883
}

src/GraphData.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ namespace InitializationHelper
3737
}
3838
else
3939
{
40-
std::cout << "Error: No distance metric for cell-similarity graph\n";
41-
exit(1);
40+
LOCO_OUT << "Error: No distance metric for cell-similarity graph\n";
41+
LOCO_EXIT(1);
4242
}
4343

4444
//save the distance
@@ -79,24 +79,24 @@ namespace InitializationHelper
7979
if(graphData->distances_precalcualted())
8080
{
8181
if (statusUpdate)
82-
std::cout << "\tSTEP[0a]:\tCalculate kNN cell-cell distances\n";
82+
LOCO_OUT << "\tSTEP[0a]:\tCalculate kNN cell-cell distances\n";
8383

8484
graphData->build_knn_adjacency(graphData->get_knn());
8585

8686
if (statusUpdate)
87-
std::cout << "\tSTEP[0b]:\tSorting cell-cell distances\n";
87+
LOCO_OUT << "\tSTEP[0b]:\tSorting cell-cell distances\n";
8888

8989
graphData->sort_adjacencyList();
9090
}
9191
else
9292
{
9393
if (statusUpdate)
94-
std::cout << "\tSTEP[0a]:\tCreate KD-tree only\n";
94+
LOCO_OUT << "\tSTEP[0a]:\tCreate KD-tree only\n";
9595

9696
graphData->create_kd_tree();
9797

9898
if (statusUpdate)
99-
std::cout << "Skipping distance calculations and do not fill adj. list\n";
99+
LOCO_OUT << "Skipping distance calculations and do not fill adj. list\n";
100100

101101
}
102102

@@ -112,17 +112,17 @@ namespace InitializationHelper
112112

113113
if(statusUpdate)
114114
{
115-
std::cout << "\tSTEP[0a]:\tCalculate cell-cell distances\n";
115+
LOCO_OUT << "\tSTEP[0a]:\tCalculate cell-cell distances\n";
116116
if(!graphData->distances_precalcualted())
117117
{
118-
std::cout << "WARNING: Distances get precalcualted despite seeting the flag to not precalcualte!!!\n";
119-
std::cout << "Distances can only be NOT calcualted when calcualting distances with the KD-tree\n";
118+
LOCO_OUT << "WARNING: Distances get precalcualted despite seeting the flag to not precalcualte!!!\n";
119+
LOCO_OUT << "Distances can only be NOT calcualted when calcualting distances with the KD-tree\n";
120120
}
121121
}
122122

123-
std::cout << "CALCULATING DISTANCES FROM \n";
124-
for(auto x : cellStateGenes){std::cout << x << " ";}
125-
std::cout << "\n";
123+
LOCO_OUT << "CALCULATING DISTANCES FROM \n";
124+
for(auto x : cellStateGenes){LOCO_OUT << x << " ";}
125+
LOCO_OUT << "\n";
126126

127127
//boost::asio::thread_pool pool_dist(threads);
128128
ThreadPool pool_dist(threads);
@@ -210,11 +210,11 @@ namespace InitializationHelper
210210
pool_dist.wait_for_tasks();
211211

212212
printProgress(1);
213-
std::cout << "\n";
213+
LOCO_OUT << "\n";
214214

215215
if(statusUpdate)
216216
{
217-
std::cout << "\tSTEP[0b]:\tSorting cell-cell distances\n";
217+
LOCO_OUT << "\tSTEP[0b]:\tSorting cell-cell distances\n";
218218
}
219219
//sort the values: before we simply inserted them all to the back
220220
graphData->sort_adjacencyList();
@@ -419,7 +419,7 @@ if(!corrStateGenes.empty()){geneSize = corrStateGenes.size();}
419419
{
420420
//eigenvalues ordered from highest to lowest
421421
double eigenVal = pca.eigenvalues.at<double>(i);
422-
std::cout << "NEXT EIGENVAL: " << eigenVal << "\n";
422+
LOCO_OUT << "NEXT EIGENVAL: " << eigenVal << "\n";
423423
}
424424
}*/
425425

@@ -437,7 +437,7 @@ void GraphData::build_knn_adjacency(int k)
437437
adjacencyList.clear();
438438
adjacencyList.reserve(N); // unordered_map reserve
439439

440-
std::cout << "creating cell-cell dist with k " << k << "\n";
440+
LOCO_OUT << "creating cell-cell dist with k " << k << "\n";
441441
// 3. For each node, query its k nearest neighbors (the point itself is part of the results and will be added to cells for this N)
442442
for (size_t i = 0; i < N; ++i)
443443
{
@@ -488,7 +488,7 @@ void GraphData::build_knn_adjacency(int k)
488488

489489
void GraphData::create_kd_tree()
490490
{
491-
std::cout << "\t# BUILDING KD-TREE\n";
491+
LOCO_OUT << "\t# BUILDING KD-TREE\n";
492492
const int dim = get_node_at(0)->dimensions();
493493

494494
typedef KDTreeVectorOfNodePtrAdaptor<> my_kd_tree_t;
@@ -521,9 +521,9 @@ void GraphData::search_kd_tree()
521521

522522
if(i == nodes.size()-1)
523523
{
524-
std::cout << "knnSearch(nn="<<num_results<<"): \n";
524+
LOCO_OUT << "knnSearch(nn="<<num_results<<"): \n";
525525
for (size_t i = 0; i < num_results; i++)
526-
std::cout << "ret_index["<<i<<"]=" << ret_indexes[i] << " out_dist_sqr=" << out_dists_sqr[i] << std::endl;
526+
LOCO_OUT << "ret_index["<<i<<"]=" << ret_indexes[i] << " out_dist_sqr=" << out_dists_sqr[i] << std::endl;
527527
}
528528
}
529529
}
@@ -561,7 +561,7 @@ void GraphData::brute_force_get_points_within_radius(node node, double radius)
561561
}
562562
}
563563

564-
//std::cout << "MATCHES FOUND: " << solutionNodes.size() << "\n";
564+
//LOCO_OUT << "MATCHES FOUND: " << solutionNodes.size() << "\n";
565565

566566
}
567567

src/GraphData.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "correlationUtils.hpp"
1616
#include "generalUtils.hpp"
1717
#include "threadPool.hpp"
18+
#include "loco_io.h"
1819

1920
// vector of positions of the dimensions that contribute to a subspace
2021
typedef std::vector<int> subSpace;
@@ -268,16 +269,16 @@ class GraphData
268269
{
269270
if(!distancesPrecalculated)
270271
{
271-
std::cerr << "requesting to return distances from adjacency list, but they have not been precalcualted!\n";
272-
exit(EXIT_FAILURE);
272+
LOCO_ERR << "requesting to return distances from adjacency list, but they have not been precalcualted!\n";
273+
LOCO_EXIT(EXIT_FAILURE);
273274
}
274275

275276
std::vector<int> adjNodes;
276277
const OrderedNeighborDistanceHash neighbors = get_adjacent_nodes(node);
277278
if(knn > neighbors.size())
278279
{
279-
std::cerr << "Requesting more neighbors from node than it has!!!\n";
280-
std::cerr << knn << " requested, but only" << neighbors.size() << " neighbors exist\n";
280+
LOCO_ERR << "Requesting more neighbors from node than it has!!!\n";
281+
LOCO_ERR << knn << " requested, but only" << neighbors.size() << " neighbors exist\n";
281282
}
282283
size_t count = 0;
283284
for(const std::pair<nodePtr, const double> neighbor : neighbors)
@@ -293,15 +294,15 @@ class GraphData
293294
{
294295
if(!distancesPrecalculated)
295296
{
296-
std::cerr << "requesting to return distances from adjacency list, but they have not been precalcualted!\n";
297-
exit(EXIT_FAILURE);
297+
LOCO_ERR << "requesting to return distances from adjacency list, but they have not been precalcualted!\n";
298+
LOCO_EXIT(EXIT_FAILURE);
298299
}
299300

300301
std::vector<nodePtr> adjNodes;
301302
const OrderedNeighborDistanceHash neighbors = get_adjacent_nodes(node);
302303
if(knn > neighbors.size())
303304
{
304-
std::cerr << "Requesting more neighbors from node than it has!!!\n";
305+
LOCO_ERR << "Requesting more neighbors from node than it has!!!\n";
305306
}
306307
size_t count = 0;
307308
for(const std::pair<const nodePtr, const double> neighbor : neighbors)
@@ -320,7 +321,7 @@ class GraphData
320321
{
321322
if(distancesPrecalculated)
322323
{
323-
std::cout << "WARNING: requesting to return distances from KD-tree but distances were precalcualted!\n";
324+
LOCO_OUT << "WARNING: requesting to return distances from KD-tree but distances were precalcualted!\n";
324325
}
325326

326327
std::vector<int> adjNodes;
@@ -358,7 +359,7 @@ class GraphData
358359
{
359360
if(distancesPrecalculated)
360361
{
361-
std::cout << "WARNING: requesting to return distances from KD-tree but distances were precalcualted!\n";
362+
LOCO_OUT << "WARNING: requesting to return distances from KD-tree but distances were precalcualted!\n";
362363
}
363364
std::vector<nodePtr> adjNodes;
364365

@@ -412,15 +413,15 @@ class GraphData
412413
//writes edges between nodes to terminal
413414
void print_adjacency_by_name() const
414415
{
415-
std::cout << "ADJ LIST:\n" << "_______________\n";
416+
LOCO_OUT << "ADJ LIST:\n" << "_______________\n";
416417
for(nodePtr nodeTmp : nodes)
417418
{
418-
std::cout << "FROM: " << nodeTmp->get_name() << "\n";
419+
LOCO_OUT << "FROM: " << nodeTmp->get_name() << "\n";
419420
for(const std::pair<const nodePtr, const double> neighbor : get_adjacent_nodes(nodeTmp))
420421
{
421-
std::cout << neighbor.first->get_name() << "(" << std::to_string(neighbor.second) << ")" << " - ";
422+
LOCO_OUT << neighbor.first->get_name() << "(" << std::to_string(neighbor.second) << ")" << " - ";
422423
}
423-
std::cout << "\n";
424+
LOCO_OUT << "\n";
424425
}
425426
}
426427
void print_adjacency_by_order() const
@@ -431,21 +432,21 @@ class GraphData
431432
for(size_t j = 0; j < nodes.size(); ++j)
432433
{
433434
double dist = nodeNeighborList.at(nodes.at(j));
434-
std::cout << std::to_string(i) << " " << std::to_string(j) << " " << std::to_string(dist) << "\n";
435+
LOCO_OUT << std::to_string(i) << " " << std::to_string(j) << " " << std::to_string(dist) << "\n";
435436
}
436437
}
437438
}
438439

439440
//writes nodes and their values to terminal
440441
void print_data()
441442
{
442-
std::cout << "NODE VECTORS:\n" << "_______________\n";
443+
LOCO_OUT << "NODE VECTORS:\n" << "_______________\n";
443444
for(nodePtr nodeTmp : nodes)
444445
{
445-
std::cout << "#: " << nodeTmp->get_name() << "\n";
446+
LOCO_OUT << "#: " << nodeTmp->get_name() << "\n";
446447
for( const double& dim : nodeTmp->all_values())
447448
{
448-
std::cout << std::to_string(dim) << "\n";
449+
LOCO_OUT << std::to_string(dim) << "\n";
449450
}
450451
}
451452
}

src/GraphHandler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ void GraphHandler::fill_distance_matrix(double** weightMatrix)
112112
bool descending = data->is_node_order_descending();
113113
if( (descending == true) && bandwidth != -1)
114114
{
115-
std::cerr << "When nodes are in descending order we can not apply gaussian kernel, we assumes this is use for protein \
115+
LOCO_ERR << "When nodes are in descending order we can not apply gaussian kernel, we assumes this is use for protein \
116116
correclation graphs, where only un-scaled values make sense \n";
117-
std::exit(EXIT_FAILURE);
117+
LOCO_EXIT(EXIT_FAILURE);
118118
}
119119

120120
int numberNodes = data->number_of_nodes();
@@ -179,8 +179,8 @@ void GraphHandler::fill_knn_matrix(double** weightMatrix)
179179
{
180180
if(data->is_node_order_descending() == true)
181181
{
182-
std::cerr << "For KNN graphs we must order nodes in ascending order \n";
183-
std::exit(EXIT_FAILURE);
182+
LOCO_ERR << "For KNN graphs we must order nodes in ascending order \n";
183+
LOCO_EXIT(EXIT_FAILURE);
184184
}
185185
int numberNodes = data->number_of_nodes();
186186
//initialize matrix with zeroes

src/GraphHandler.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <unordered_map>
1010
#include <exception>
1111
#include <numeric>
12+
#include "loco_io.h"
1213

1314
#include "GraphData.hpp"
1415

@@ -103,8 +104,8 @@ class GraphHandler
103104
//this only works for KNN graphs, not for K-DIST graphs
104105
if(knn == 0)
105106
{
106-
std::cerr << "Error: Can not calculate neighbors when knn is set to zero";
107-
exit(EXIT_FAILURE);
107+
LOCO_ERR << "Error: Can not calculate neighbors when knn is set to zero";
108+
LOCO_EXIT(EXIT_FAILURE);
108109
}
109110

110111
//get all neighbors from data (fastest way, it has all neighbors ordered)

0 commit comments

Comments
 (0)