Skip to content

Commit

Permalink
Merge pull request #16 from CosmologicalEmulators/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcobonici authored Sep 20, 2023
2 parents b0064a4 + da47ec9 commit dbaae6d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ on:
- develop
push:
branches:
- main
- develop
- '**'
tags: '*'
jobs:
test:
Expand Down
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
name = "Capse"
uuid = "994f66c3-d2c7-4ba6-88fb-4a10f50800ba"
authors = ["marcobonici <[email protected]>"]
version = "0.2.0"

version = "0.2.1"

[deps]
AbstractCosmologicalEmulators = "c83c1981-e5c4-4837-9eb8-c9b1572acfc6"
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

[compat]
AbstractCosmologicalEmulators = "0.3"
AbstractCosmologicalEmulators = "0.3.2"
Adapt = "3"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
![size](https://img.shields.io/github/repo-size/CosmologicalEmulators/Capse.jl)


Repo containing the CMB Angular Power Spectrum Emulator (`Capse.jl`).
Repo containing the CMB Angular Power Spectrum Emulator, `Capse.jl`.

`Capse.jl` is entirely written in `Julia`, but can be transparently called from `Python` using the [`pycapse`](https://github.com/CosmologicalEmulators/pycapse) wrapper.

## Citing

Expand Down
1 change: 0 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Capse = "994f66c3-d2c7-4ba6-88fb-4a10f50800ba"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
20 changes: 14 additions & 6 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ default(palette = palette(:tab10))
benchmark = BenchmarkTools.load("./assets/capse_benchmark.json")
path_json = "./assets/nn_setup.json"
weights = rand(20000)
ℓgrid = ones(2000)
InMinMax_array = zeros(2,2000)
OutMinMax_array = zeros(2,2000)
```

`Capse.jl` is a Julia package designed to emulate the computation of the CMB Angular Power Spectrum, with a speedup of several orders of magnitude.
Expand Down Expand Up @@ -43,8 +46,8 @@ After this, you just have to pass the `NN_dict` and the `weights` array to the `

In order to instantiate the emulator, just run

```julia
trained_emu = Capse.init_emulator(NN_dict, weights, Capse.SimpleChainsEmulator)
```@example tutorial
trained_emu = Capse.init_emulator(NN_dict, weights, Capse.SimpleChainsEmulator);
```

`SimpleChains.jl` is faster expecially for small NN on the CPU. If you prefer to use `Lux.jl`, pass as last argument `Capse.LuxEmulator`.
Expand All @@ -54,20 +57,25 @@ Each trained emulator should be shipped with a description within the JSON file.
```@example tutorial
Capse.get_emulator_description(NN_dict["emulator_description"])
```

After instantiating the NN, we need:

- the ``\ell``-grid used to train the emulator, `ℓgrid`
- the arrays used to perform the minmax normalization of both input and output features, `InMinMax_array` and `OutMinMax_array`

Now you can instantiate the emulator, using

```julia
```@example tutorial
Cℓ_emu = Capse.CℓEmulator(TrainedEmulator = trained_emu, ℓgrid = ℓgrid,
InMinMax = InMinMax_array, OutMinMax = OutMinMax_array)
InMinMax = InMinMax_array, OutMinMax = OutMinMax_array);
```

Each trained emulator should be shipped with a description within the JSON file. In order to print the description, just runs:

```@example tutorial
Capse.get_emulator_description(Cℓ_emu)
```

After loading a trained `CℓTT_emu`, feed it some input parameters `x`.
After loading a trained emulator, feed it some input parameters `x` in order to get the emulated $C_\ell$'s

```julia
x = rand(6) # generate some random input
Expand Down
22 changes: 16 additions & 6 deletions src/Capse.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Capse

using Base: @kwdef
using Adapt
using AbstractCosmologicalEmulators
import AbstractCosmologicalEmulators.get_emulator_description

export get_Cℓ, get_emulator_description

abstract type AbstractCℓEmulators end

Expand All @@ -14,19 +18,21 @@ It contains:
- TrainedEmulator::AbstractTrainedEmulators, the trained emulator
- ℓgrid::Array, the ``\\ell``-grid the emulator has been trained on.
- ℓgrid::AbstractVector, the ``\\ell``-grid the emulator has been trained on.
- InMinMax::Matrix, the `Matrix` used for the MinMax normalization of the input features
- InMinMax::AbstractMatrix, the `Matrix` used for the MinMax normalization of the input features
- OutMinMax::Matrix, the `Matrix` used for the MinMax normalization of the output features
- OutMinMax::AbstractMatrix, the `Matrix` used for the MinMax normalization of the output features
"""
@kwdef mutable struct CℓEmulator <: AbstractCℓEmulators
TrainedEmulator::AbstractTrainedEmulators
ℓgrid::Array
InMinMax::Matrix
OutMinMax::Matrix
ℓgrid::AbstractVector
InMinMax::AbstractMatrix
OutMinMax::AbstractMatrix
end

Adapt.@adapt_structure CℓEmulator

"""
get_Cℓ(CℓEmulator::AbstractCℓEmulators)
Computes and returns the ``C_\\ell``on the ``\\ell``-grid the emulator has been trained on.
Expand All @@ -47,4 +53,8 @@ function get_ℓgrid(CℓEmulator::AbstractCℓEmulators)
return CℓEmulator.ℓgrid
end

function get_emulator_description(Clemu::AbstractCℓEmulators)
get_emulator_description(Clemu.TrainedEmulator)
end

end # module
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ capse_emu = Capse.CℓEmulator(TrainedEmulator = emu, ℓgrid=ℓ_test, InMinMax
output_vec = Capse.get_Cℓ(cosmo_vec, capse_emu)
@test isapprox(output_vec[:,1], output)
@test ℓ_test == Capse.get_ℓgrid(capse_emu)
@test_logs (:warn, "We do not know which parameters were included in the emulators training space. Use this trained emulator with caution!") Capse.get_emulator_description(capse_emu)
end

0 comments on commit dbaae6d

Please sign in to comment.