Initial commit

This commit is contained in:
2023-06-22 19:48:06 +02:00
parent 7a4cf6211c
commit 48b68173a3
13 changed files with 4472 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 Ricardo Montañana Gómez
Copyright (c) 2020-2022, Ricardo Montañana Gómez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

1
MANIFEST.in Normal file
View File

@@ -0,0 +1 @@
include src/csrc/Test.h

View File

@@ -1,2 +1,10 @@
# PythonCythonTemplate
Python project template with a C++ extension
# TEST
## Project template with a python package and a C++ extension
### Installation
```bash
pip install -e .
python test.py
```

54
pyproject.toml Normal file
View File

@@ -0,0 +1,54 @@
[build-system]
requires = ["setuptools", "cython", "wheel"]
build-backend = "setuptools.build_meta"
[tool.setuptools]
package-dir = { "" = "src" }
license-files = ["LICENSE"]
[tool.setuptools.dynamic]
version = { attr = "testcython.__version__" }
[project]
name = "testcython"
description = "A test with cython."
readme = "README.md"
authors = [
{ name = "Ricardo Montañana", email = "ricardo.montanana@alu.uclm.es" },
]
dynamic = ['version']
dependencies = ["scipy", "numpy", "pandas", "scikit-learn"]
requires-python = ">=3.8"
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Science/Research",
"Intended Audience :: Developers",
"Topic :: Software Development",
"Topic :: Scientific/Engineering",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
]
[tool.black]
line-length = 79
target_version = ['py38', 'py39', 'py310']
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
'''

24
setup.py Normal file
View File

@@ -0,0 +1,24 @@
"""
Calling
$python setup.py build_ext --inplace
will build the extension library in the current file.
"""
from setuptools import Extension, setup
setup(
ext_modules=[
Extension(
name="testcython.ctest",
sources=[
"src/testcython/ctest.pyx",
"src/csrc/Test.cpp",
],
language="c++",
include_dirs=["testcython"],
extra_compile_args=[
"-std=c++17",
],
),
]
)

34
src/csrc/Test.cpp Normal file
View File

@@ -0,0 +1,34 @@
# include "Test.h"
# include <iostream>
namespace testSpace {
Test::Test() = default;
Test::~Test() = default;
void Test::fit(samples_t& samples, labels_t& labels, weights_t& weights)
{
this->labels = labels;
this->weights = weights;
this->samples = samples;
}
void Test::print()
{
cout << "Labels: ";
for (auto item : labels) {
cout << item << ", ";
}
cout << "end." << endl;
cout << "Weights: ";
for (auto item : weights) {
cout << item << ", ";
}
cout << "end." << endl;
cout << "Samples: ";
for (auto item : samples) {
cout << "[";
for (auto item2 : item) {
cout << item2 << ", ";
}
cout << "], ";
}
}
}

27
src/csrc/Test.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef TEST_H
#define TEST_H
#include <vector>
#include <string>
namespace testSpace {
using namespace std;
typedef int value_t;
typedef float precision_t;
typedef vector<value_t> labels_t;
typedef vector<precision_t> weights_t;
typedef vector<vector<value_t>> samples_t;
class Test {
protected:
samples_t samples = samples_t();
labels_t labels = labels_t();
weights_t weights = weights_t();
public:
Test();
void fit(samples_t&, labels_t&, weights_t&);
~Test();
void print();
static inline string version() { return "0.1.0"; }
};
}
#endif

View File

@@ -0,0 +1,3 @@
from ._version import __version__
all = ["TestCython", "__version__"]

View File

@@ -0,0 +1 @@
__version__ = "0.1.1"

4263
src/testcython/ctest.cpp Normal file

File diff suppressed because it is too large Load Diff

29
src/testcython/ctest.pyx Normal file
View File

@@ -0,0 +1,29 @@
# distutils: language = c++
# cython: language_level = 3
from libcpp.vector cimport vector
from libcpp.string cimport string
cdef extern from "../csrc/Test.h" namespace "testSpace":
ctypedef float precision_t
cdef cppclass Test:
Test() except +
void fit(vector[vector[int]]&, vector[int]&, vector[precision_t]&) except +
void print()
string version()
cdef class CTest:
cdef Test *thisptr
def __cinit__(self):
self.thisptr = new Test()
def __dealloc__(self):
del self.thisptr
def fit(self, X, y, weights):
self.thisptr.fit(X, y, weights)
return self
def print(self,):
self.thisptr.print()
return self
def get_version(self):
return self.thisptr.version()
def __reduce__(self):
return (CTest, ())

View File

@@ -0,0 +1,21 @@
from .ctest import CTest
from ._version import __version__
class TestCython:
def __init__(self):
self.test = CTest()
def test_cython(self):
X = [[x for x in range(i, i + 3)] for i in range(1, 30, 3)]
weights = [25 / (i + 1) for i in range(10)]
labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
self.test.fit(X, labels, weights)
print("Hello from test_cython()!")
print("My version: ", self.version())
print("CTest version: ", self.test.get_version().decode("utf-8"))
self.test.print()
@staticmethod
def version():
return __version__

4
test.py Normal file
View File

@@ -0,0 +1,4 @@
from testcython.testcython import TestCython
test = TestCython()
test.test_cython()