Skip to content

Commit

Permalink
[c++/ci] Update make format and CI format-checker to find more C++ …
Browse files Browse the repository at this point in the history
…files (#2168)

* Update `make format` to cover more directories

* Similarly update the CI checker

* Exclude files as requested by @eddelbuettel
  • Loading branch information
johnkerl committed Feb 23, 2024
1 parent 83e51e3 commit 05e9592
Show file tree
Hide file tree
Showing 15 changed files with 670 additions and 585 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/python-ci-single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ jobs:
- name: Run pre-commit hooks on all files
run: python -m pip -v install pre-commit && pre-commit run -a -v

# Skip files in apis/r/src which are:
# * nanoarrow.c/h
# * Auto-generated by Rcpp
# * Things which Dirk doesn't want to be format-checked
- name: Check C++ Format
shell: bash
run: ./scripts/run-clang-format.sh . clang-format 0 $(find libtiledbsoma -name "*.cc" -or -name "*.h" | grep -v external)
run: ./scripts/run-clang-format.sh . clang-format 0 $(find libtiledbsoma apis/python/src -name "*.cc" -or -name "*.cpp" -or -name "*.h" | grep -v external)

build:
runs-on: ${{ inputs.os }}
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ data:

# format
# -------------------------------------------------------------------
# Skip files in apis/r/src which are:
# * nanoarrow.c/h
# * Auto-generated by Rcpp
# * Things which Dirk doesn't want to be format-checked
.PHONY: check-format
check-format:
@./scripts/run-clang-format.sh . clang-format 0 \
`find libtiledbsoma -name "*.cc" -or -name "*.h"`
`find libtiledbsoma apis/python/src -name "*.cc" -or -name "*.cpp" -or -name "*.h"`

.PHONY: format
format:
@./scripts/run-clang-format.sh . clang-format 1 \
`find libtiledbsoma -name "*.cc" -or -name "*.h"`
`find libtiledbsoma apis/python/src -name "*.cc" -or -name "*.cpp" -or -name "*.h"`

# clean
# -------------------------------------------------------------------
Expand Down
112 changes: 56 additions & 56 deletions apis/python/src/tiledbsoma/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,64 +80,64 @@ std::unordered_map<std::string, tiledb_datatype_t> _np_name_to_tdb_dtype = {
};

py::dtype tdb_to_np_dtype(tiledb_datatype_t type, uint32_t cell_val_num) {
if (type == TILEDB_CHAR || type == TILEDB_STRING_UTF8 ||
type == TILEDB_STRING_ASCII) {
std::string base_str = (type == TILEDB_STRING_UTF8) ? "|U" : "|S";
if (cell_val_num < TILEDB_VAR_NUM)
base_str += std::to_string(cell_val_num);
return py::dtype(base_str);
}

if (cell_val_num == 1) {
if (type == TILEDB_STRING_UTF16 || type == TILEDB_STRING_UTF32)
TPY_ERROR_LOC("Unimplemented UTF16 or UTF32 string conversion!");
if (type == TILEDB_STRING_UCS2 || type == TILEDB_STRING_UCS4)
TPY_ERROR_LOC("Unimplemented UCS2 or UCS4 string conversion!");

if (_tdb_to_np_name_dtype.count(type) == 1)
return py::dtype(_tdb_to_np_name_dtype[type]);
}

if (cell_val_num == 2) {
if (type == TILEDB_FLOAT32)
return py::dtype("complex64");
if (type == TILEDB_FLOAT64)
return py::dtype("complex128");
}

if (cell_val_num == TILEDB_VAR_NUM)
return tdb_to_np_dtype(type, 1);

if (cell_val_num > 1) {
py::dtype base_dtype = tdb_to_np_dtype(type, 1);
py::tuple rec_elem = py::make_tuple("", base_dtype);
py::list rec_list;
for (size_t i = 0; i < cell_val_num; i++)
rec_list.append(rec_elem);
// note: we call the 'dtype' constructor b/c py::dtype does not accept
// list
auto np = py::module::import("numpy");
auto np_dtype = np.attr("dtype");
return np_dtype(rec_list);
}

TPY_ERROR_LOC("tiledb datatype not understood ('" +
tiledb::impl::type_to_str(type) +
if (type == TILEDB_CHAR || type == TILEDB_STRING_UTF8 ||
type == TILEDB_STRING_ASCII) {
std::string base_str = (type == TILEDB_STRING_UTF8) ? "|U" : "|S";
if (cell_val_num < TILEDB_VAR_NUM)
base_str += std::to_string(cell_val_num);
return py::dtype(base_str);
}

if (cell_val_num == 1) {
if (type == TILEDB_STRING_UTF16 || type == TILEDB_STRING_UTF32)
TPY_ERROR_LOC("Unimplemented UTF16 or UTF32 string conversion!");
if (type == TILEDB_STRING_UCS2 || type == TILEDB_STRING_UCS4)
TPY_ERROR_LOC("Unimplemented UCS2 or UCS4 string conversion!");

if (_tdb_to_np_name_dtype.count(type) == 1)
return py::dtype(_tdb_to_np_name_dtype[type]);
}

if (cell_val_num == 2) {
if (type == TILEDB_FLOAT32)
return py::dtype("complex64");
if (type == TILEDB_FLOAT64)
return py::dtype("complex128");
}

if (cell_val_num == TILEDB_VAR_NUM)
return tdb_to_np_dtype(type, 1);

if (cell_val_num > 1) {
py::dtype base_dtype = tdb_to_np_dtype(type, 1);
py::tuple rec_elem = py::make_tuple("", base_dtype);
py::list rec_list;
for (size_t i = 0; i < cell_val_num; i++)
rec_list.append(rec_elem);
// note: we call the 'dtype' constructor b/c py::dtype does not accept
// list
auto np = py::module::import("numpy");
auto np_dtype = np.attr("dtype");
return np_dtype(rec_list);
}

TPY_ERROR_LOC(
"tiledb datatype not understood ('" + tiledb::impl::type_to_str(type) +
"', cell_val_num: " + std::to_string(cell_val_num) + ")");
}

tiledb_datatype_t np_to_tdb_dtype(py::dtype type) {
auto name = py::str(py::getattr(type, "name"));
if (_np_name_to_tdb_dtype.count(name) == 1)
return _np_name_to_tdb_dtype[name];
auto name = py::str(py::getattr(type, "name"));
if (_np_name_to_tdb_dtype.count(name) == 1)
return _np_name_to_tdb_dtype[name];

auto kind = py::str(py::getattr(type, "kind"));
if (kind == py::str("S"))
return TILEDB_STRING_ASCII;
if (kind == py::str("U"))
return TILEDB_STRING_UTF8;
auto kind = py::str(py::getattr(type, "kind"));
if (kind == py::str("S"))
return TILEDB_STRING_ASCII;
if (kind == py::str("U"))
return TILEDB_STRING_UTF8;

TPY_ERROR_LOC("could not handle numpy dtype");
TPY_ERROR_LOC("could not handle numpy dtype");
}

/**
Expand All @@ -158,8 +158,8 @@ py::object _buffer_to_table(std::shared_ptr<ArrayBuffers> buffers) {
for (auto& name : buffers->names()) {
auto column = buffers->at(name);
auto [pa_array, pa_schema] = ArrowAdapter::to_arrow(column);
auto array = pa_array_import(py::capsule(pa_array.get()),
py::capsule(pa_schema.get()));
auto array = pa_array_import(
py::capsule(pa_array.get()), py::capsule(pa_schema.get()));
array_list.append(array);
names.append(name);
}
Expand All @@ -168,7 +168,7 @@ py::object _buffer_to_table(std::shared_ptr<ArrayBuffers> buffers) {
}

std::optional<py::object> to_table(
std::optional<std::shared_ptr<ArrayBuffers>> buffers){
std::optional<std::shared_ptr<ArrayBuffers>> buffers) {
// If more data was read, convert it to an arrow table and return
if (buffers.has_value()) {
return _buffer_to_table(*buffers);
Expand All @@ -178,4 +178,4 @@ std::optional<py::object> to_table(
return std::nullopt;
}

}
} // namespace tiledbsoma
Loading

0 comments on commit 05e9592

Please sign in to comment.