Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed GSL dependency #35

Merged
merged 2 commits into from
Jun 19, 2023
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
627 changes: 276 additions & 351 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
GSL = "4.0" # GNU Stats Library, for psi function
statrs = "0.16.0" # For betaln function
mongodb = "2.1"
bson = { version = "2", features = ["chrono-0_4"] } # Needed for using chrono datetime in doc
Expand Down
19 changes: 13 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# STEP 0: Statically build node client
FROM node as client-builder
FROM node:lts-hydrogen as client-builder
WORKDIR /client
COPY client/public public
COPY client/src src
Expand All @@ -14,11 +14,13 @@ RUN yarn install
RUN yarn build

# STEP 1: Compile backend
FROM rust as builder
FROM rust:1-alpine3.18 as builder
WORKDIR /usr/src/jury

RUN apt update
RUN apt install libgsl-dev -y
# This is important, see https://github.com/rust-lang/docker-rust/issues/85
ENV RUSTFLAGS="-C target-feature=-crt-static"

RUN apk add --no-cache musl-dev

# Jank way to cache built rust dependencies
RUN mkdir src
Expand All @@ -36,16 +38,21 @@ ARG JURY_ADMIN_PASSWORD=$JURY_ADMIN_PASSWORD
ARG FILESERVER="/public"

RUN cargo install --locked --path .
RUN ls -la /usr/local/cargo/bin

# STEP 2: Main running container
FROM debian:bullseye-slim
FROM alpine:3.18

# Extra dependencies needed
RUN apk add --no-cache ca-certificates openssl-dev bash openssl libgcc libstdc++

ENV FILESERVER="/public"
ENV ROCKET_ADDRESS=0.0.0.0

EXPOSE $PORT

RUN apt-get update && apt-get install -y libgsl-dev && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/jury /usr/local/bin/jury
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=client-builder /client/build /public

CMD ["jury"]
2 changes: 1 addition & 1 deletion client/dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node as client-builder
FROM node:lts-hydrogen as client-builder
WORKDIR /client

ENV REACT_APP_JURY_NAME=$REACT_APP_JURY_NAME
Expand Down
4 changes: 1 addition & 3 deletions dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
FROM rust
FROM rust:1.70
WORKDIR /jury

RUN apt update
RUN apt install libgsl-dev -y
RUN cargo install cargo-watch

COPY public /public
Expand Down
2 changes: 1 addition & 1 deletion src/util/crowd_bt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rgsl::psi::diagamma::psi;
use statrs::function::beta;
use crate::util::digamma::psi;

pub const GAMMA: f64 = 0.1;
// const LAMBDA: f64 = 1.0; // regularization parameter - not used??
Expand Down
46 changes: 46 additions & 0 deletions src/util/digamma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Bernardo, J. M. (1976). Algorithm AS 103: Psi (Digamma) Function.
* Journal of the Royal Statistical Society.
* Series C (Applied Statistics), 25(3), 315-317.
* https://doi.org/10.2307/2347257
*/

pub fn psi(x: f64) -> f64 {
let euler_mascheroni = 0.57721566490153286060;
let mut r: f64;
let mut value: f64;
let mut x2: f64;

// Make sure input is greater than 0
if x <= 0.0 {
eprintln!("Error: Digamma psi function called with argument <= 0");
return 0.0;
}

// Approximate for small values
if x <= 0.000001 {
return - euler_mascheroni - 1.0 / x + 1.6449340668482264365 * x;
}

// Reduce to DIGAMMA(X + N)
value = 0.0;
x2 = x;
while x2 < 8.5 {
value -= 1.0 / x2;
x2 += 1.0;
}

// Use Stirling's (actually de Moivre's) expansion.
r = 1.0 / x2;
value += f64::ln(x2) - 0.5 * r;

r *= r;
value = value
- r * (1.0 / 12.0
- r * (1.0 / 120.0
- r * (1.0 / 252.0
- r * (1.0 / 240.0
- r * (1.0 / 132.0)))));

return value;
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod crowd_bt;
pub mod parse_csv;
pub mod types;
pub mod tasks;
pub mod digamma;
23 changes: 23 additions & 0 deletions tests/util_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use jury::util;
use jury::util::crowd_bt;

#[test]
Expand All @@ -10,3 +11,25 @@ fn math_test() {
assert_eq!(e, 3.057070240002119);
assert_eq!(f, 0.6301076201202875);
}

#[test]
fn diagamma_test() {
let tests : [[f64; 2]; 11] = [
[1.0E+00, -0.5772156649E+00],
[1.1E+00, -0.4237549404E+00],
[1.2E+00, -0.2890398965E+00],
[1.3E+00, -0.1691908888E+00],
[1.4E+00, -0.6138454458E-01],
[1.5E+00, 0.3648997397E-01],
[1.6E+00, 0.1260474527E+00],
[1.7E+00, 0.2085478748E+00],
[1.8E+00, 0.2849914332E+00],
[1.9E+00, 0.3561841611E+00],
[2.0E+00, 0.4227843350E+00],
];

for i in 0..11 {
let res = util::digamma::psi(tests[i][0]);
assert!(res - tests[i][1] < 1.0E-10);
}
}