Skip to content

Commit

Permalink
To expose more arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
yjcyxky committed May 29, 2024
1 parent 2c881ed commit ab6036a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"antd": "5.8.0",
"antd-schema-form": "^4.5.1",
"axios": "^1.1.2",
"biominer-components": "0.3.20",
"biominer-components": "0.3.21",
"biomsa": "^0.3.3",
"bootstrap": "^5.3.3",
"classnames": "^2.3.0",
Expand Down
14 changes: 10 additions & 4 deletions studio/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5518,16 +5518,22 @@ bio.io@^1.0.6:
xhr "^2.2.0"
xmldoc "^0.5.1"

[email protected]:
version "0.3.20"
resolved "https://registry.yarnpkg.com/biominer-components/-/biominer-components-0.3.20.tgz#f907ebe9463687b007033aa48bf49dd6abe16106"
integrity sha512-uWqleFUY5igsCWRgc5E9RbTVFtZY+eLlwUdIzoOIE3egJ3L47XysnpsCVI+pokcyf4BOLGVFc2M7RcW/2xIt4w==
biomedgps-graph@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/biomedgps-graph/-/biomedgps-graph-0.1.0.tgz#bb864f0f4154fb2ea2b3627e839afd9647bdcc67"
integrity sha512-wS1F738hXdSCFP8eqBrw0NPHtIKl24GPv9uPPmJ2OEWBYymj5XR5zgGwwJt40aEa59S7GpRURCP7XsbAmct8Sg==

[email protected]:
version "0.3.21"
resolved "https://registry.yarnpkg.com/biominer-components/-/biominer-components-0.3.21.tgz#392d66f0ae7629945a5625c92588e299cf93b00f"
integrity sha512-JwSGFNVuolHW5PfWsu6cyagwgGkZ2gxWOdvZoqwmJlDV4BKjqB6yzT9dnRRV7Gv0MztDHQ2GqZo+Qr2+eTEy7w==
dependencies:
"@fingerprintjs/fingerprintjs" "^4.2.2"
ag-grid-community "^31.0.1"
ag-grid-enterprise "^31.0.1"
ag-grid-react "^31.0.1"
axios "^1.6.7"
biomedgps-graph "^0.1.0"
html-react-parser "^5.1.8"
lodash "^4.17.21"
moment "^2.30.1"
Expand Down
4 changes: 2 additions & 2 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "biomedgps-graph"
description = ""
version = "0.1.0"
description = "A bunch of graph algorithms for biomedgps."
version = "0.2.0"
authors = ["Jingcheng Yang <[email protected]>"]
edition = "2021"

Expand Down
6 changes: 6 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ Ensure you have the following tools installed:
```

Open `http://localhost:8000` in your browser and see the output.

4. Publish the package:

```sh
wasm-pack publish
```
2 changes: 2 additions & 0 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ <h1>WebAssembly Centrality Calculation</h1>
const result = wasm.calculate_centrality(filteredRelations);
const genes = result.filter(entity => entity.entity_type === "Gene");

console.log(genes);

// Populate DataTable with data
$('#example').DataTable({
data: genes,
Expand Down
102 changes: 90 additions & 12 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,66 @@ struct CentralityResult {
pagerank_score: f64,
}

fn calculate_centralities(relations: Vec<Relation>) -> Vec<CentralityResult> {
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Options {
undirected: bool,
betweenness_normalized: bool,
betweenness_endpoints: bool,
betweenness_parallel_threshold: usize,
closeness_wf_improved: bool,
eigenvector_weight_enabled: bool,
eigenvector_max_iter: usize,
eigenvector_tol: f64,
}

impl Options {
fn new() -> Options {
Options {
undirected: true,
betweenness_normalized: true,
betweenness_endpoints: false,
betweenness_parallel_threshold: 100,
closeness_wf_improved: false,
eigenvector_weight_enabled: true,
eigenvector_max_iter: 100,
eigenvector_tol: 0.000001,
}
}

fn update_undirected(&mut self, value: bool) {
self.undirected = value;
}

fn update_betweenness_normalized(&mut self, value: bool) {
self.betweenness_normalized = value;
}

fn update_betweenness_endpoints(&mut self, value: bool) {
self.betweenness_endpoints = value;
}

fn update_betweenness_parallel_threshold(&mut self, value: usize) {
self.betweenness_parallel_threshold = value;
}

fn update_closeness_wf_improved(&mut self, value: bool) {
self.closeness_wf_improved = value;
}

fn update_eigenvector_weight_enabled(&mut self, value: bool) {
self.eigenvector_weight_enabled = value;
}

fn update_eigenvector_max_iter(&mut self, value: usize) {
self.eigenvector_max_iter = value;
}

fn update_eigenvector_tol(&mut self, value: f64) {
self.eigenvector_tol = value;
}
}

fn calculate_centralities(relations: Vec<Relation>, options: Options) -> Vec<CentralityResult> {
// let mut graph = DiGraph::<String, f64>::new();
let mut graph = UnGraph::<String, f64>::new_undirected();
let mut node_indices: HashMap<String, NodeIndex> = HashMap::new();
Expand All @@ -54,14 +113,25 @@ fn calculate_centralities(relations: Vec<Relation>) -> Vec<CentralityResult> {
graph.add_edge(source_index, target_index, rel.score);
}

let betweenness_scores = betweenness_centrality(&graph, false, true, 100);
let closeness_scores = closeness_centrality(&graph, false);
let betweenness_scores = betweenness_centrality(
&graph,
options.betweenness_endpoints,
options.betweenness_normalized,
options.betweenness_parallel_threshold,
);
let closeness_scores = closeness_centrality(&graph, options.closeness_wf_improved);
// More details on the parameters for eigenvector_centrality can be found at https://docs.rs/rustworkx-core/latest/rustworkx_core/centrality/fn.eigenvector_centrality.html
let eigenvector_scores = match eigenvector_centrality(
&graph,
|edge: EdgeReference<f64>| -> Result<f64, ()> { Ok(*edge.weight()) },
None,
None,
|edge: EdgeReference<f64>| -> Result<f64, ()> {
if options.eigenvector_weight_enabled {
Ok(*edge.weight())
} else {
Ok(1.0)
}
},
Some(options.eigenvector_max_iter),
Some(options.eigenvector_tol),
) {
Ok(scores) => scores.unwrap_or(vec![]),
Err(_) => vec![],
Expand Down Expand Up @@ -119,9 +189,14 @@ fn calculate_centralities(relations: Vec<Relation>) -> Vec<CentralityResult> {
}

#[wasm_bindgen]
pub fn calculate_centrality(relations: JsValue) -> JsValue {
pub fn calculate_centrality(relations: JsValue, options: JsValue) -> JsValue {
let relations: Vec<Relation> = from_value(relations).unwrap();
let results = calculate_centralities(relations);
let options: Options = if options.is_undefined() {
Options::new()
} else {
from_value(options).unwrap()
};
let results = calculate_centralities(relations, options);
to_value(&results).unwrap()
}

Expand Down Expand Up @@ -164,7 +239,10 @@ mod tests {
},
];

let results = calculate_centralities(relations);
let options = Options::new();
println!("options: {:?}", &options);

let results = calculate_centralities(relations, options);
for result in &results {
println!(
"entity_id: {}, entity_type: {}, betweenness_score: {}, closeness_score: {}, eigenvector_score: {}, degree_score: {}, pagerank_score: {}",
Expand All @@ -182,9 +260,9 @@ mod tests {

let a_node = results.iter().find(|r| r.entity_id == "A").unwrap();
assert_eq!(a_node.entity_type, "Person");
assert_eq!(a_node.betweenness_score, 2.0);
assert_eq!(a_node.closeness_score, 0.0);
assert_eq!(a_node.eigenvector_score, 0.0);
assert_eq!(a_node.betweenness_score, 0.0);
assert_eq!(a_node.closeness_score, 1.0);
assert_eq!(a_node.eigenvector_score, 0.5773502691896258);
assert_eq!(a_node.degree_score, 0.0);
assert_eq!(a_node.pagerank_score, 0.0);
}
Expand Down

0 comments on commit ab6036a

Please sign in to comment.