Skip to content

Commit

Permalink
Structure around produce_or_load
Browse files Browse the repository at this point in the history
  • Loading branch information
flipgthb committed Dec 28, 2023
1 parent fd66949 commit 2d1bce6
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 355 deletions.
6 changes: 3 additions & 3 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.9.3"
manifest_format = "2.0"
project_hash = "a520117e440ed1cc55e899c509f7620083793144"
project_hash = "5902aa4cbdc90f37fe4ffcd1edcfda0f659c4971"

[[deps.AbstractPlutoDingetjes]]
deps = ["Pkg"]
Expand Down Expand Up @@ -397,9 +397,9 @@ version = "1.0.0"

[[deps.JLD2]]
deps = ["FileIO", "MacroTools", "Mmap", "OrderedCollections", "Pkg", "PrecompileTools", "Printf", "Reexport", "Requires", "TranscodingStreams", "UUIDs"]
git-tree-sha1 = "9bbb5130d3b4fa52846546bca4791ecbdfb52730"
git-tree-sha1 = "c2d0f45afcb5f6209155670bffd100c3b4937ea3"
uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
version = "0.4.38"
version = "0.4.40"

[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ authors = ["Felippe Alves"]
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataPrep = "d40174be-394b-476a-94dd-cdf19e683a9d"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DrWatson = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
FGenerators = "4fd0377b-cfdc-4941-97f4-8d7ddbb8981e"
GBPAlgorithm = "52f5dfbb-b0c2-49ff-99e4-0d4f43f103e5"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50"
Parquet2 = "98572fba-bba0-415d-956f-fa77e587d26d"
Expand Down
15 changes: 7 additions & 8 deletions notebooks/run_gbp_decoder_single_process_pluto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ using GBPAlgorithm
using PlutoUI

# ╔═╡ 9acb786d-8e76-44ee-b817-f0430405dc2a
include(srcdir("data_utilities.jl"))

# ╔═╡ af1edd4f-976c-4a43-8d76-d11c0695fa8e
include(srcdir("decoding.jl"))
begin
include(srcdir("Decoding.jl"))
using .Decoding
end

# ╔═╡ 3efb8183-f539-4de5-b792-8b81750c573e
warm_up_results = let
run_it(;
Decoding.decode_multiple(;
power_vals=0:1,with_noise_vals=true:true,pilots_vals=1:1,k_vals=2:2,
T=10,w=0.25,niter=10,pilots_period=100,step=8,is_simulation=false,
load_take=200,load_skip=0,should_save=false,savedir="test_$(today())",
start_msg="Warmming up...", showprogressinfo=false
);

run_it(;
Decoding.decode_multiple(;
power_vals=0:1,with_noise_vals=true:true,pilots_vals=1:1,k_vals=2:2,
T=10,w=0.25,niter=10,pilots_period=100,step=8,is_simulation=false,
load_take=200,load_skip=0,should_save=true,savedir="test_$(today())",
Expand Down Expand Up @@ -190,7 +190,7 @@ md"## Running the GBP decoding algorithm"
md"### Run decoder"

# ╔═╡ 668740f7-776a-4bff-b4fe-f5c023e4b6b6
run_results = run_it(; params...);
# run_results = Decoding.decode_multiple(; params...);

# ╔═╡ Cell order:
# ╠═90f835b0-a3e8-11ee-3e33-9719b5d2efcb
Expand All @@ -200,7 +200,6 @@ run_results = run_it(; params...);
# ╠═aae0e074-a287-42a4-bc6d-a6d9637aad0b
# ╠═d42b9d61-0ee7-4907-bae7-b785a6f13a99
# ╠═9acb786d-8e76-44ee-b817-f0430405dc2a
# ╠═af1edd4f-976c-4a43-8d76-d11c0695fa8e
# ╟─3efb8183-f539-4de5-b792-8b81750c573e
# ╟─7c865aca-8306-40a2-a35e-afd627c12c1d
# ╟─f7e0d5e4-0fa4-4d03-a1d8-7ab640109f0f
Expand Down
60 changes: 11 additions & 49 deletions scripts/run_gbp_algorithm_single_process.jl
Original file line number Diff line number Diff line change
@@ -1,64 +1,26 @@
using DrWatson
@quickactivate "GBPQAMDecoder"
@quickactivate :GBPQAMDecoder

using Chain,
DataFrames,
Dates,
JLD2,
LinearAlgebra,
Parquet2,
ProgressMeter,
Random,
StatsBase,
Transducers
using DataPrep
using GBPAlgorithm

include(srcdir("data_utilities.jl"))
include(srcdir("decoding.jl"))

warm_up_results = let
run_it(;
power_vals=0:1,with_noise_vals=true:true,pilots_vals=1:1,k_vals=2:2,
T=10,w=0.25,niter=10,pilots_period=100,step=8,is_simulation=false,
load_take=200,load_skip=0,should_save=false,savedir="test_$(today())",
start_msg="Warmming up...", showprogressinfo=false
);

run_it(;
power_vals=0:1,with_noise_vals=true:true,pilots_vals=1:1,k_vals=2:2,
T=10,w=0.25,niter=10,pilots_period=100,step=8,is_simulation=false,
load_take=200,load_skip=0,should_save=true,savedir="test_$(today())",
start_msg="one more, this time saving...", showprogressinfo=false
);
end

println("")

print_help() = println("""
Run `run_it(;
power_vals=-2:8,with_noise_vals=false:true,pilots_vals=1:1,k_vals=2:2,
T,w,niter,pilots_period,step,is_simulation=false,
load_take=1_000_000,load_skip=0,should_save=true,savedir="",
showprogressinfo=true,
start_msg="Running GBP decoder for all powers"
)`
to run the GBP decoding algorithm for all available power levels and noise conditions given:
- each windowed sequence with length `T` and overlap `step` in the beggining
- using a mixture with `k` Gaussian components (k in {1,2,3,4,5})
- using a gradient descent step of `w` (w in 0..1)
- on signal generated with `pilots` waves every `pilots_perior` symbols (pilots in {0,1,2,3})
- and optionally, if `should_save` saves it to a subdirectory `datadir("results",savedir)`
- `load_take` and `load_skip` refer to how many symbols to load from the signal file or generate
for a simulation
- `is_simulation` to load the model information but not the signal file and generate a signal with
the specified parameters using the respective memory and noise model
- `start_msg` can be used to identify the current task in the system log files
- `showprogressinfo` can be used to toggle the display of parameters and progress
Thw warm up results are assigned to `warm_up_results`, with a summary for all the datasets and the
decoding results split per dataset.
test_tasks = decoding_tasks_list(;
power=[-1,1],with_noise=true,pilots=0,k=2,
load_batches=2
)

Run `print_help()` to show this message again.
""")
map(test_tasks) do task_info
produce_or_load(solve_decoding_task,task_info,datadir("results","testing_script"); force=true)
end
end;

print_help()
println("")
208 changes: 208 additions & 0 deletions src/GBPQAMDecoder.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
module GBPQAMDecoder

using DrWatson
@quickactivate "GBPQAMDecoder"

export solve_decoding_task, load_problem_data, decoding_tasks_list, DecodingTask, AlgorithmParams

using Chain,
DataFrames,
Dates,
LinearAlgebra,
Parquet2,
ProgressMeter,
Random,
StatsBase,
Transducers

using DataPrep # local module
using GBPAlgorithm # local module

include(srcdir("decoding_tasks.jl"))
include(srcdir("signal_simulator.jl"))
include(srcdir("decoding.jl"))

# model_data_file(power) = datadir(
# "qam_data",
# "model_power_$(power)_wo_noise.parquet"
# )

# signal_data_file(power::Int,with_noise::Bool,pilots::Int) = datadir(
# "qam_data",
# "points_power_$(power)_pilots_$(pilots)_$(with_noise ? "w" : "wo")_noise.parquet"
# )

# noise_data_file() = datadir("noise_info.parquet")

# encoding_data_file() = datadir("constellation_info.parquet")

# dataset(fn) = Parquet2.Dataset(fn)|>DataFrame

# Base.@kwdef struct AlgorithmParams
# T::Int
# step::Int
# w::Float64=0.25
# niter::Int=10
# showprogressinfo::Bool=true
# end

# AlgorithmParams(d) = AlgorithmParams(;d...)

# Base.iterate(a::AlgorithmParams,args...) = iterate(struct2dict(a),args...)
# DrWatson.allaccess(::AlgorithmParams) = (:T,:step,:w,:niter)
# DrWatson.default_allowed(::AlgorithmParams) = (Real,)

# Base.@kwdef struct DecodingTask
# power::Int
# with_noise::Bool
# pilots::Int
# k::Int
# pilots_period::Int=100
# skip_batches::Int=0
# load_batches::Int=1_000_000
# is_simulation::Bool=false
# alg_params::AlgorithmParams=AlgorithmParams(T=10,step=8)
# pilot_point::Tuple{Int,Int}=(1,1)
# qam_encoding::Dict{Tuple{Int,Int},Int}=DataPrep.get_qam_encoding(dataset(encoding_data_file()))
# noise_info::NamedTuple=get_noise_info(dataset(noise_data_file()),power,with_noise)
# model_file::String=model_data_file(power)
# signal_file::String=signal_data_file(power,with_noise,pilots)
# end

# DecodingTask(d) = DecodingTask(;d...)

# DrWatson.default_expand(::DecodingTask) = ["alg_params"]
# DrWatson.default_allowed(::DecodingTask) = (Real,Bool,AlgorithmParams)
# DrWatson.allaccess(::DecodingTask) = (:power,:with_noise,:pilots,:pilots_period,:k,:skip_batches,:load_batches,:alg_params)
# DrWatson.default_prefix(t::DecodingTask) = t.is_simulation ? "simulation-decoded" : "points-decoded"

# function decodingtask2dict(t::DecodingTask)
# d = struct2dict(t)
# d[:alg_params] = struct2dict(t.alg_params)
# return d
# end

# function decoding_tasks_list(; alg_params=nothing,kw...)
# d = Dict(kw)
# if !isnothing(alg_params)
# d[:alg_params] = alg_params|>pairs|>Dict|>dict_list.|>AlgorithmParams
# end
# return d|>dict_list.|>DecodingTask
# end

# function add_pilots_info!(signal_table,task_info)
# (;pilots,pilots_period) = task_info
# @chain signal_table begin
# transform!(eachindex=>:t)
# transform!(:t=>ByRow(t->mod(t,1:pilots_period)<=pilots)=>:is_pilot)
# end
# end

# function collapse_prior!(factors,sequence,T,step,part_idx)
# overlap = part_idx > 1 ? pairs(last(factors.Rs,T-step)) : ()
# pilots = (r.i=>r.Ts for r in eachrow(sequence) if r.is_pilot)
# GBPAlgorithm.collapse_prior!(factors,overlap...,pilots...)
# return factors
# end

# function load_problem_data(task_info)
# (;k,qam_encoding) = task_info
# (;noise_var) = task_info.noise_info
# model_table = @chain task_info.model_file begin
# dataset
# DataPrep.parse_model_data(k,noise_var,qam_encoding)
# end

# (;load_batches,skip_batches,pilots_period) = task_info
# signal_table = if !task_info.is_simulation
# @chain task_info.signal_file begin
# Parquet2.Dataset
# Tables.rows
# _|>Drop(skip_batches*pilots_period)|>Take(load_batches*pilots_period)|>DataFrame
# DataPrep.parse_signal_data(qam_encoding)
# add_pilots_info!(task_info)
# end
# else
# @warn "Simulating signal..."
# symbol_sequence_length=min(load_batches*pilots_period,size(dataset(task_info.signal_file),1))
# add_pilots_info!(
# simulate_signal_table(
# task_info,
# symbol_sequence_length,
# model_table,
# qam_encoding
# ),
# task_info
# )
# end

# return (;model_table,signal_table)
# end

# progress_info_msg(prog,showinfo) = showinfo && next!(prog)

# function task_info_msg(task_info)
# (;T,step) = task_info.alg_params
# (;power,with_noise,pilots,k) = task_info
# @info "Running algorithm:" "Avg. Power [dBm]"=power "With 4.5dB noise"=with_noise "Pilots"=pilots "Number of mixture components"=k "Sequence length"=T "Step"=step
# end

# function save_info_msg(dir)
# @info "Saved decoded points" "Directory"=dir
# end

# function get_noise_info(noise_data,power,with_noise)
# @chain noise_data begin
# subset(:pdbm=>ByRow(p->p==power))
# map(eachrow(_)) do r
# noise_sigma = with_noise ? r.sigma*r.scale : 0.0
# noise_var = noise_sigma^2
# (;noise_sigma,noise_var,noise_scale=r.scale)
# end
# only
# end
# end

# getresults(sequence,step,part_idx) = part_idx > 1 ? last(sequence,step) : sequence

# maxparts(nsyms,partlen,step) = floor(Int,1 + (nsyms - partlen)/step)
# maxparts(seqdf::DataFrame,partlen,step) = maxparts(size(seqdf,1),partlen,step)

# function solve_decoding_task(task_info)
# task_info_msg(task_info)
# (;model_table,signal_table) = load_problem_data(task_info)
# (;T,w,niter,step,showprogressinfo) = task_info.alg_params
# N = length(task_info.qam_encoding)
# decode_iter! = GBPAlgorithm.GBPDecoder(N,T,w)
# factors = GBPAlgorithm.Factors(N,T)
# n = maxparts(size(signal_table,1),T,step)
# prog = Progress(n; showspeed=true)
# R = withprogress(eachrow(signal_table); interval=10^-2)|>
# Partition(T,step)|>
# Enumerate()|>
# Map() do (part_idx,part)
# sequence = DataFrame(part)
# transform!(sequence,eachindex=>:i)
# GBPAlgorithm.reset_msg!(decode_iter!)
# DataPrep.memory_factor!(factors.M,model_table,sequence.Rx)
# collapse_prior!(factors,sequence,T,step,part_idx)
# decode_iter!(factors)|>Drop(niter-1)|>Take(1)|>collect|>only
# GBPAlgorithm.beliefs!(factors,decode_iter!)
# transform!(sequence,:Ts=>(x->copy(factors.Rs))=>:Rs)
# transform!(sequence,[:Ts,:Rs]=>((t,r)->t.!=r)=>:error)
# progress_info_msg(prog,showprogressinfo)
# getresults(sequence,step,part_idx)
# end|>
# Take(n)|>
# foldxl(vcat)
# results = select!(R,
# :Tx=>ByRow(((x,y),)->(;Tx_x=x,Tx_y=y))=>AsTable,
# :Rx=>ByRow(((x,y),)->(;Rx_x=x,Rx_y=y))=>AsTable,
# :Ts,:Rs,:error,:is_pilot
# )

# decoding_task = decodingtask2dict(task_info)
# return @strdict decoding_task results
# end

end # end Decoding module
Loading

0 comments on commit 2d1bce6

Please sign in to comment.