Skip to content

Commit

Permalink
make random_state sklearn compliant (#97)
Browse files Browse the repository at this point in the history
* make random_state sklearn compliant

* update sonarscanner ci

* update sonarscanner ci

* update sonarqube
  • Loading branch information
rpreen authored Nov 3, 2023
1 parent ab835aa commit 94f3237
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/sonarqube_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true
Expand All @@ -60,8 +60,14 @@ jobs:
chmod +x codecov
./codecov
- name: Setup sonarqube
uses: warchant/setup-sonar-scanner@v3
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'

- name: Setup SonarQube
uses: warchant/setup-sonar-scanner@v7

- name: Sonarqube coverage
run: |
Expand All @@ -71,8 +77,8 @@ jobs:
run: sonar-scanner
-Dsonar.host.url=https://sonarcloud.io/
-Dsonar.login=${{ secrets.SONAR_TOKEN }}
-Dsonar.projectKey=rpreen_xcsf
-Dsonar.organization=rpreen
-Dsonar.projectKey=xcsf-dev_xcsf
-Dsonar.organization=xcsf-dev
-Dsonar.projectVersion=1.0
-Dsonar.projectBaseDir=./
-Dsonar.cfamily.build-wrapper-output=build/bw-output
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See the project [wiki](https://github.com/xcsf-dev/xcsf/wiki) for details on fea

[![Codacy](https://img.shields.io/codacy/grade/2213b9ad4e034482bf058d4598d1618b?logo=codacy&style=flat)](https://app.codacy.com/gh/rpreen/xcsf/dashboard)
[![CodeFactor](https://img.shields.io/codefactor/grade/github/xcsf-dev/xcsf?logo=codefactor&style=flat)](https://www.codefactor.io/repository/github/xcsf-dev/xcsf)
[![SonarCloud](https://sonarcloud.io/api/project_badges/measure?project=rpreen_xcsf&metric=alert_status)](https://sonarcloud.io/dashboard?id=rpreen_xcsf)
[![SonarCloud](https://sonarcloud.io/api/project_badges/measure?project=xcsf-dev_xcsf&metric=alert_status)](https://sonarcloud.io/dashboard?id=xcsf-dev_xcsf)
[![codecov](https://codecov.io/gh/xcsf-dev/xcsf/branch/master/graph/badge.svg?token=3bfaTvmJ8d)](https://codecov.io/gh/xcsf-dev/xcsf)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=rpreen_xcsf&metric=ncloc)](https://sonarcloud.io/dashboard?id=rpreen_xcsf)

Expand Down
2 changes: 1 addition & 1 deletion cfg/default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"omp_num_threads": 8,
"random_state": 0,
"random_state": -1,
"pop_init": true,
"max_trials": 100000,
"perf_trials": 1000,
Expand Down
8 changes: 4 additions & 4 deletions xcsf/param.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ param_init(struct XCSF *xcsf, const int x_dim, const int y_dim,
param_set_x_dim(xcsf, x_dim);
param_set_y_dim(xcsf, y_dim);
param_set_omp_num_threads(xcsf, 8);
param_set_random_state(xcsf, 0);
param_set_random_state(xcsf, -1);
param_set_pop_init(xcsf, true);
param_set_max_trials(xcsf, 100000);
param_set_perf_trials(xcsf, 1000);
Expand Down Expand Up @@ -608,10 +608,10 @@ const char *
param_set_random_state(struct XCSF *xcsf, const int a)
{
xcsf->RANDOM_STATE = a;
if (a > 0) {
rand_init_seed(a);
} else {
if (a < 0) {
rand_init();
} else {
rand_init_seed(a);
}
return NULL;
}
Expand Down
52 changes: 19 additions & 33 deletions xcsf/pybind_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,36 +327,6 @@ class XCS

/* Supervised learning */

/**
* @brief Sets XCSF input and output dimensions.
* @param [in] n_x_dim Number of input dimensions.
* @param [in] x1 Size of second input dimension.
* @param [in] n_y_dim Number of output dimensions.
* @param [in] y1 Size of second output dimension.
*/
void
set_dims(int n_x_dim, int x1, int n_y_dim, int y1)
{
py::dict kwargs;
kwargs["n_actions"] = 1;
if (n_x_dim > 1) {
kwargs["x_dim"] = x1;
} else {
kwargs["x_dim"] = 1;
}
if (n_y_dim > 1) {
kwargs["y_dim"] = y1;
} else {
kwargs["y_dim"] = 1;
}
// update external params dict
for (const auto &item : kwargs) {
params[item.first] = item.second;
}
// flush param update to make sure neural nets resize
set_params(params);
}

/**
* @brief Loads an input data structure for fitting.
* @param [in,out] data Input data structure used to point to the data.
Expand Down Expand Up @@ -853,6 +823,13 @@ class XCS
py::object parsed_json = json.attr("loads")(json_str);
py::dict result(parsed_json);
params = result;
// map None types
if (params.contains("random_state")) {
py::object rs = params["random_state"];
if (py::isinstance<py::int_>(rs) && py::int_(rs) < 0) {
params["random_state"] = py::none();
}
}
free(json_str);
}

Expand All @@ -877,14 +854,23 @@ class XCS
set_params(py::kwargs kwargs)
{
py::dict kwargs_dict(kwargs);
// update external params dict
for (const auto &item : kwargs_dict) {
params[item.first] = item.second;
}
// map None types
if (kwargs_dict.contains("random_state")) {
py::object rs = kwargs["random_state"];
if (rs.is_none()) {
kwargs_dict["random_state"] = -1;
}
}
// convert dict to JSON and parse parameters
py::module json_module = py::module::import("json");
py::object json_dumps = json_module.attr("dumps")(kwargs_dict);
std::string json_str = json_dumps.cast<std::string>();
const char *json_params = json_str.c_str();
param_json_import(&xcs, json_params);
for (const auto &item : kwargs_dict) {
params[item.first] = item.second;
}
return *this;
}

Expand Down

0 comments on commit 94f3237

Please sign in to comment.