Skip to content

Commit

Permalink
1.0.4 - Code cleaning, removes unnecessary deps, updates to the laste…
Browse files Browse the repository at this point in the history
…st LightGBM
  • Loading branch information
Mottl committed Aug 15, 2024
1 parent 45623f8 commit 85c5d0c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
if: matrix.os == 'macos-latest'
run: |
brew install cmake libomp
cargo build --features=polars #--features=openmp
cargo build --features=openmp --features=polars
- name: Build for Ubuntu
if: matrix.os == 'ubuntu-latest'
run: |
Expand All @@ -41,7 +41,7 @@ jobs:
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --features=polars -- --no-deps #--features=openmp
args: --features=polars --features=openmp -- --no-deps
format_check:
name: Run Rustfmt
runs-on: ubuntu-latest
Expand Down
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
[package]
name = "lightgbm3"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
authors = ["Dmitry Mottl <dmitry.mottl@gmail.com>", "vaaaaanquish <6syun9@gmail.com>"]
authors = [
"Dmitry Mottl <dmitry.mottl@gmail.com>",
"vaaaaanquish <6syun9@gmail.com>",
"paq <89paku@gmail.com>",
"Benjamin Ellis <benjaminjellis@protonmail.com>",
]
license = "MIT"
repository = "https://github.com/Mottl/lightgbm3-rs"
description = "Rust bindings for LightGBM library"
Expand All @@ -13,11 +18,9 @@ readme = "README.md"
exclude = [".gitignore", ".github", ".gitmodules", "examples", "benches", "lightgbm3-sys"]

[dependencies]
lightgbm3-sys = { path = "lightgbm3-sys", version = "1.0.3" }
libc = "0.2.155"
derive_builder = "0.20.0"
serde_json = "1.0.122"
polars = { version = "0.41.3", optional = true }
lightgbm3-sys = { path = "lightgbm3-sys", version = "1.0.4" }
serde_json = "1.0.125"
polars = { version = "0.42.0", optional = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion lightgbm3-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lightgbm3-sys"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
authors = ["Dmitry Mottl <dmitry.mottl@gmail.com>", "vaaaaanquish <6syun9@gmail.com>"]
build = "build.rs"
Expand Down
2 changes: 1 addition & 1 deletion lightgbm3-sys/lightgbm
35 changes: 17 additions & 18 deletions src/booster.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! LightGBM booster

use libc::{c_char, c_double, c_longlong, c_void};
use serde_json::Value;
use std::os::raw::{c_char, c_longlong, c_void};
use std::{convert::TryInto, ffi::CString};

use crate::{dataset::DType, Dataset, Error, Result};
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Booster {
let mut out_num_iterations = 0;
let mut handle = std::ptr::null_mut();
lgbm_call!(lightgbm3_sys::LGBM_BoosterCreateFromModelfile(
filename_str.as_ptr() as *const c_char,
filename_str.as_ptr(),
&mut out_num_iterations,
&mut handle
))?;
Expand All @@ -70,7 +70,7 @@ impl Booster {
let mut out_num_iterations = 0;
let mut handle = std::ptr::null_mut();
lgbm_call!(lightgbm3_sys::LGBM_BoosterLoadModelFromString(
cstring.as_ptr() as *const c_char,
cstring.as_ptr(),
&mut out_num_iterations,
&mut handle
))?;
Expand All @@ -86,7 +86,7 @@ impl Booster {
0_i32,
-1_i32,
0_i32,
filename_str.as_ptr() as *const c_char
filename_str.as_ptr(),
))?;
Ok(())
}
Expand All @@ -102,8 +102,8 @@ impl Booster {
-1_i32,
0_i32,
0,
&mut out_size as *mut _,
std::ptr::null_mut() as *mut i8
&mut out_size,
std::ptr::null_mut(),
))?;

// write data to buffer and convert
Expand All @@ -119,7 +119,7 @@ impl Booster {
-1_i32,
0_i32,
buffer.len() as c_longlong,
&mut out_size as *mut _,
&mut out_size,
buffer.as_mut_ptr() as *mut c_char
))?;

Expand Down Expand Up @@ -210,7 +210,7 @@ impl Booster {
let mut handle = std::ptr::null_mut();
lgbm_call!(lightgbm3_sys::LGBM_BoosterCreate(
dataset.handle,
params_cstring.as_ptr() as *const c_char,
params_cstring.as_ptr(),
&mut handle
))?;

Expand Down Expand Up @@ -254,12 +254,11 @@ impl Booster {
}
let n_rows = flat_x.len() / n_features as usize;
let params_cstring = parameters
.map(|s| CString::new(s))
.map(CString::new)
.unwrap_or(CString::new(""))
.unwrap();
let mut out_length: c_longlong = 0;

let out_result: Vec<f64> = vec![Default::default(); n_rows * self.n_classes as usize];
let mut out_result: Vec<f64> = vec![Default::default(); n_rows * self.n_classes as usize];
lgbm_call!(lightgbm3_sys::LGBM_BoosterPredictForMat(
self.handle,
flat_x.as_ptr() as *const c_void,
Expand All @@ -270,9 +269,9 @@ impl Booster {
predict_type.into(), // predict_type
0_i32, // start_iteration
self.max_iterations, // num_iteration, <= 0 means no limit
params_cstring.as_ptr() as *const c_char,
params_cstring.as_ptr(),
&mut out_length,
out_result.as_ptr() as *mut c_double
out_result.as_mut_ptr()
))?;

Ok(out_result)
Expand Down Expand Up @@ -421,22 +420,22 @@ impl Booster {
let mut cur_iteration: i32 = 0;
lgbm_call!(lightgbm3_sys::LGBM_BoosterGetCurrentIteration(
self.handle,
&mut cur_iteration as *mut _
&mut cur_iteration
))?;
Ok(cur_iteration + 1)
}

/// Gets features names.
pub fn feature_name(&self) -> Result<Vec<String>> {
let num_feature = self.inner_num_features()?;
let feature_name_length = 32;
let feature_name_length = 64;
let mut num_feature_names = 0;
let mut out_buffer_len = 0;
let out_strs = (0..num_feature)
.map(|_| {
CString::new(" ".repeat(feature_name_length))
.unwrap()
.into_raw() as *mut c_char
.into_raw()
})
.collect::<Vec<_>>();
lgbm_call!(lightgbm3_sys::LGBM_BoosterGetFeatureNames(
Expand All @@ -457,12 +456,12 @@ impl Booster {
/// Get feature importance. Refer to [`ImportanceType`]
pub fn feature_importance(&self, importance_type: ImportanceType) -> Result<Vec<f64>> {
let num_feature = self.inner_num_features()?;
let out_result: Vec<f64> = vec![Default::default(); num_feature as usize];
let mut out_result: Vec<f64> = vec![Default::default(); num_feature as usize];
lgbm_call!(lightgbm3_sys::LGBM_BoosterFeatureImportance(
self.handle,
0_i32,
importance_type.into(),
out_result.as_ptr() as *mut c_double
out_result.as_mut_ptr()
))?;
Ok(out_result)
}
Expand Down
12 changes: 6 additions & 6 deletions src/dataset.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! LightGBM Dataset used for training

use libc::{c_char, c_void};
use lightgbm3_sys::{DatasetHandle, C_API_DTYPE_FLOAT32, C_API_DTYPE_FLOAT64};
use std::os::raw::c_void;
use std::{self, ffi::CString};

#[cfg(feature = "polars")]
Expand Down Expand Up @@ -103,14 +103,14 @@ impl Dataset {
n_rows as i32,
n_features,
if is_row_major { 1_i32 } else { 0_i32 }, // is_row_major – 1 for row-major, 0 for column-major
params.as_ptr() as *const c_char,
params.as_ptr(),
reference,
&mut dataset_handle
))?;

lgbm_call!(lightgbm3_sys::LGBM_DatasetSetField(
dataset_handle,
label_str.as_ptr() as *const c_char,
label_str.as_ptr(),
label.as_ptr() as *const c_void,
n_rows as i32,
C_API_DTYPE_FLOAT32 as i32 // labels should be always float32
Expand Down Expand Up @@ -174,8 +174,8 @@ impl Dataset {
let mut handle = std::ptr::null_mut();

lgbm_call!(lightgbm3_sys::LGBM_DatasetCreateFromFile(
file_path_str.as_ptr() as *const c_char,
params.as_ptr() as *const c_char,
file_path_str.as_ptr(),
params.as_ptr(),
std::ptr::null_mut(),
&mut handle
))?;
Expand Down Expand Up @@ -275,7 +275,7 @@ impl Dataset {
let field_name = CString::new("weight").unwrap();
lgbm_call!(lightgbm3_sys::LGBM_DatasetSetField(
self.handle,
field_name.as_ptr() as *const c_char,
field_name.as_ptr(),
weights.as_ptr() as *const c_void,
weights.len() as i32,
C_API_DTYPE_FLOAT32 as i32, // weights other than float32 are not supported by LightGBM
Expand Down

0 comments on commit 85c5d0c

Please sign in to comment.