K-Nearest Neighbor Graph Testing Library
C++ Python library that is able to import exisiting NN-structures; Implements Property Testing Algorithm that rejects with high probability if queries to given structure are epsilon-far from giving a K-Nearest Neighbor Graph
relation.hpp
1 /*
2 Copyright 2018 Dennis Rohde
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 
6 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 
8 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 */
10 
11 #pragma once
12 
13 #include <cxxabi.h>
14 #include <iostream>
15 
16 #include <boost/python/numpy.hpp>
17 
18 #include "d_dim_tuple.hpp"
19 
20 namespace p = boost::python;
21 namespace np = boost::python::numpy;
22 
23 template <typename T = double>
24 class Relation : public std::vector<Tuple<T>> {
25  typedef std::vector<Tuple<T>> relation_type;
26  typedef unsigned long long index_type;
27 
28 public:
29  Relation() : relation_type{} {}
30  Relation(const index_type n) : relation_type(n) {}
31  Relation(const np::ndarray &in) : relation_type{} {
32  auto dimensions = in.get_nd();
33  if (dimensions != 2 or in.get_dtype() != np::dtype::get_builtin<T>()) {
34  int status;
35  const std::type_info &ti = typeid(T);
36  std::cerr << "Need 2-dimensional numpy array of type " << abi::__cxa_demangle(ti.name(), 0, 0, &status) << " !" << std::endl;
37  return;
38  }
39  auto size = in.get_shape();
40  auto strides = in.get_strides();
41  auto data = in.get_data();
42  for (index_type i = 0; i < size[0]; ++i) {
43  Tuple<T> newtuple{static_cast<unsigned long>(size[1])};
44  for (unsigned long j = 0; j < size[1]; ++j) {
45  newtuple[j] = *reinterpret_cast<const T*>(data + i * strides[0] + j * strides[1]);
46  }
47  this->push_back(newtuple);
48  }
49  }
50 
51  inline auto dimension() const {
52  return this->size() == 0 ? 0 : this->operator[](0).dimension();
53  }
54 
55  inline auto as_ndarray() const {
56  p::list l;
57  np::dtype dt = np::dtype::get_builtin<T>();
58  np::ndarray result = np::array(l, dt);
59  index_type size = this->size();
60  if (size == 0) {
61  return result;
62  }
63  unsigned long d = this->get(0).size();
64  for (index_type i = 0; i < size; ++i) {
65  p::list lu;
66  for (unsigned long j = 0; j < d; ++j) {
67  lu.append((*this)[i][j]);
68  }
69  l.append(lu);
70  }
71  result = np::array(l, dt);
72  return result;
73  }
74 
75  inline auto get(const index_type i) const {
76  if (i >= this->size()) {
77  throw std::invalid_argument("i >= relation size");
78  }
79  return this->operator[](i);
80  }
81 
82  inline auto as_str() const {
83  std::stringstream ss;
84  ss << *this;
85  return ss.str();
86  }
87 
88  inline auto repr() const {
89  std::stringstream ss;
90  ss << "Relation of Dimension " << dimension() << ":" << std::endl;
91  ss << as_str();
92  return ss.str();
93  }
94 
95  inline auto pbegin() const {
96  return this->begin();
97  }
98 
99  inline auto pend() const {
100  return this->end();
101  }
102 };
103 
104 template <typename T>
105 std::ostream& operator<<(std::ostream &out, const Relation<T> &r) {
106  const auto size = r.size();
107  out << "{";
108  for (auto i = size-size; i < size-1; ++i) {
109  out << r.get(i);
110  out << std::endl;
111  }
112  out << r.get(size-1) << "}";
113  return out;
114 }
Definition: relation.hpp:24
Definition: d_dim_tuple.hpp:24