Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Google C/C++ Code Style settings
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never # To avoid conflict, set this "Never" and each "if statement" should include brace when coding
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterStruct: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 100
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false # Make sure the * or & align on the left
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReflowComments: false
# SeparateDefinitionBlocks: Always # Only support since clang-format 14
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++11
TabWidth: 4
UseTab: Never
19 changes: 18 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches:
- "main"
- "main"
pull_request:

env:
Expand Down Expand Up @@ -37,6 +37,23 @@ jobs:
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}

lint_cpp:
name: "Lint C++"
strategy:
matrix:
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: 'Run clang-format.'
uses: jidicula/[email protected]
with:
clang-format-version: '20'
check-path: 'src'


python_tests:
name: "Python Tests"
strategy:
Expand Down
84 changes: 42 additions & 42 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,49 @@ ci:
autofix_commit_msg: "style: pre-commit fixes"

repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
exclude: ^conda\.recipe/meta\.yaml$
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
exclude: ^conda\.recipe/meta\.yaml$
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace

# Check linting and style issues
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.11.12"
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format
exclude: ^(docs)
# Check linting and style issues
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.11.12"
hooks:
- id: ruff
args: [ "--fix", "--show-fixes" ]
- id: ruff-format
exclude: ^(docs)

# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
- id: remove-tabs
exclude: ^(docs)
# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
- id: remove-tabs
exclude: ^(docs)

# CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13
hooks:
- id: cmake-format
additional_dependencies: [pyyaml]
types: [file]
files: (\.cmake|CMakeLists.txt)(.in)?$
# CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13
hooks:
- id: cmake-format
additional_dependencies: [ pyyaml ]
types: [ file ]
files: (\.cmake|CMakeLists.txt)(.in)?$

# .clang-format file
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v13.0.0
hooks:
- id: clang-format
# .clang-format file
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v20.1.6
hooks:
- id: clang-format
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(
${SKBUILD_PROJECT_NAME}
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES CXX)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Include core modules
add_subdirectory(src/core)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ classifiers = [
"Programming Language :: Python :: 3.13",
"Private :: Do Not Upload",
]
dependencies = [
"numpy>=2.2.6",
]


[dependency-groups]
Expand Down
6 changes: 4 additions & 2 deletions src/core/add/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
#include "add.hpp"

namespace libseis {
int add(const int i, const int j) { return i + j; }
} // namespace libseis
int add(const int i, const int j) {
return i + j;
}
} // namespace libseis
2 changes: 1 addition & 1 deletion src/core/add/add.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ namespace libseis {
int add(int i, int j);
}

#endif // CPPLIB_LIBRARY_H
#endif // CPPLIB_LIBRARY_H
28 changes: 22 additions & 6 deletions src/core/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Created by David Graham on 6/8/25.
//

#include "add/add.hpp"
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "add/add.hpp"

#include "gather.hpp"

Expand Down Expand Up @@ -40,13 +40,29 @@ PYBIND11_MODULE(_num, m) {
PYBIND11_MODULE(_gather, m) {
py::class_<Gather>(m, "Gather")
// .def(py::init<int, int, int, double>())
.def(py::init<int, int, int, double>(), py::arg("id"), py::arg("nt"),
py::arg("nx"), py::arg("dt"))
.def(py::init<int, int, int, double>(), py::arg("id"), py::arg("nt"), py::arg("nx"),
py::arg("dt"))
.def_readwrite("id", &Gather::id)
.def_readwrite("dt", &Gather::dt)
.def_readwrite("nt", &Gather::nt)
.def_readwrite("nx", &Gather::nx)
.def_readwrite("dt", &Gather::dt)
.def_readonly("data", &Gather::data)
.def_property(
"data",
// Getter: return numpy array (zero-copy)
[](Gather& self) -> py::array {
return py::array({self.nt, self.nx}, {sizeof(double) * self.nx, sizeof(double)},
self.data.data(), py::cast(&self));
},
// Setter: accept 2D NumPy array and copy into C++ vector
[](Gather& self,
const py::array_t<double, py::array::c_style | py::array::forcecast>& arr) {
if (arr.ndim() != 2)
throw std::runtime_error("data must be 2D");
if (arr.shape(0) != self.nt || arr.shape(1) != self.nx)
throw std::runtime_error("Shape mismatch with (nt, nx)");

std::memcpy(self.data.data(), arr.data(), sizeof(double) * self.nt * self.nx);
})
.def("__str__", &Gather::str)
.def("from_bin_file", &Gather::from_bin_file);
}
85 changes: 23 additions & 62 deletions src/core/gather/gather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,27 @@
#include <string>
#include <vector>

// struct Gather {
// int id;
// int nt;
// int nx;
// double dt;
// std::vector<double> data;
// };
std::string Gather::str() const {
return "Gather id: " + std::to_string(id);
}

// std::vector<double> read_double(const std::string &path, int nt, int nx) {
// std::ifstream file(path, std::ios::binary);
// if (!file) {
// throw std::runtime_error("Unable to open file at " + path);
// }
//
// std::vector<double> data(nt * nx);
// file.read(reinterpret_cast<char *>(data.data()), nt * nx * sizeof(double));
// if (!file) {
// throw std::runtime_error("Failed to read data from file at " + path);
// }
//
// return data;
// }
//
// void write_double(const std::string &path, const std::vector<double> &data,
// int nt, int nx) {
// std::ofstream file(path, std::ios::binary);
// if (!file) {
// throw std::runtime_error("Unable to open file at " + path);
// }
//
// file.write(reinterpret_cast<const char *>(data.data()),
// nt * nx * sizeof(double));
// if (!file) {
// throw std::runtime_error("Failed to write data to file at " + path);
// }
// }
//
// std::unique_ptr<Gather> gain_gather(const Gather &gather, double power) {
// auto new_gather = std::make_unique<Gather>(gather);
// new_gather->data.resize(gather.nt * gather.nx);
//
// for (int trace_index = 0; trace_index < gather.nx; ++trace_index) {
// for (int sample_index = 0; sample_index < gather.nt; ++sample_index) {
// double time = static_cast<double>(sample_index) * gather.dt;
// double t_pow = std::pow(time, power);
// int data_index = trace_index * gather.nt + sample_index;
// new_gather->data[data_index] = gather.data[data_index] * t_pow;
// }
// }
//
// return new_gather;
// }
//
// void display_gather(const Gather &gather) {
// std::cout << "--------------------\n";
// std::cout << "Gather: id " << gather.id << '\n';
// std::cout << "Sample Rate: " << gather.dt << " seconds\n";
// std::cout << "Sampling Frequency: " << 1.0 / gather.dt << " Hz\n";
// std::cout << "Nyquist Frequency: " << 1.0 / (2 * gather.dt) << " Hz\n";
// std::cout << "Samples per trace: " << gather.nt << '\n';
// std::cout << "Number of traces: " << gather.nx << '\n';
// std::cout << "--------------------\n";
// }
std::ostream& operator<<(std::ostream& os, const Gather& gather) {
os << "Gather id: " << gather.id << std::endl;
return os;
}

Gather Gather::from_bin_file(const std::string& path, int nt, int nx, double dt) {
std::ifstream file(path, std::ios::binary);
if (!file) {
throw std::runtime_error("Unable to open file at " + path);
}
std::size_t total_values = static_cast<std::size_t>(nt) * nx;
std::vector<double> buffer(total_values);
file.read(reinterpret_cast<char*>(buffer.data()), total_values * sizeof(double));
if (!file) {
throw std::runtime_error("Failed to read data from file at " + path);
}
Gather result(0, nt, nx, dt);
result.data = std::move(buffer);
return result;
}
Loading