-
Notifications
You must be signed in to change notification settings - Fork 47
ChickenPox
graph dataset
#233
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
Merged
CarloLucibello
merged 7 commits into
JuliaML:master
from
aurorarossi:add-chickenpox-dataset
Jul 9, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9efb01
Add ChickenPox export
aurorarossi c62d0e0
Add ChickenPox dataset export
aurorarossi 30bfc50
Add ChickenPox test
aurorarossi 9f68121
Add ChickenPox dataset
aurorarossi 6cf9b26
Fix a alphabetical order
aurorarossi 7bf8729
Add more info and example
aurorarossi b92acf0
Add more tests
aurorarossi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
function __init__chickenpox() | ||
DEPNAME = "ChickenPox" | ||
LINK = "https://graphmining.ai/temporal_datasets/" | ||
register(ManualDataDep(DEPNAME, | ||
""" | ||
Dataset: $DEPNAME | ||
Website : $LINK | ||
""")) | ||
end | ||
|
||
function chickenpox_datadir(dir = nothing) | ||
dir = isnothing(dir) ? datadep"ChickenPox" : dir | ||
LINK = "http://www-sop.inria.fr/members/Aurora.Rossi/data/chickenpox.json" | ||
if length(readdir((dir))) == 0 | ||
DataDeps.fetch_default(LINK, dir) | ||
end | ||
@assert isdir(dir) | ||
return dir | ||
end | ||
|
||
function generate_task(data::AbstractArray, num_timesteps_in::Int, num_timesteps_out::Int) | ||
features = [] | ||
targets = [] | ||
for i in 1:(size(data,3)-num_timesteps_in-num_timesteps_out) | ||
push!(features, data[:,:,i:i+num_timesteps_in-1]) | ||
push!(targets, data[:,:,i+num_timesteps_in:i+num_timesteps_in+num_timesteps_out-1]) | ||
end | ||
return features, targets | ||
end | ||
|
||
function create_chickenpox_dataset( normalize::Bool, num_timesteps_in::Int, num_timesteps_out::Int, dir) | ||
name_file = joinpath(dir, "chickenpox.json") | ||
data = read_json(name_file) | ||
src = zeros(Int, length(data["edges"])) | ||
dst = zeros(Int, length(data["edges"])) | ||
for (i, edge) in enumerate(data["edges"]) | ||
src[i] = edge[1] + 1 | ||
dst[i] = edge[2] + 1 | ||
end | ||
f = Float32.(stack(data["FX"])) | ||
f = reshape(f, 1, size(f, 1), size(f, 2)) | ||
|
||
metadata = Dict(key => value + 1 for (key, value) in data["node_ids"]) | ||
|
||
if normalize | ||
f = (f .- Statistics.mean(f, dims=(2))) ./ Statistics.std(f, dims=(2)) #Z-score normalization | ||
end | ||
|
||
x, y = generate_task(f, num_timesteps_in, num_timesteps_out) | ||
|
||
g = Graph(; edge_index = (src, dst), | ||
node_data = (features = x, targets = y)) | ||
return g, metadata | ||
end | ||
|
||
""" | ||
ChickenPox(; normalize= true, num_timesteps_in = 8 , num_timesteps_out = 8, dir = nothing) | ||
|
||
The ChickenPox dataset contains county-level chickenpox cases in Hungary between 2004 and 2014. | ||
|
||
`ChickenPox` is composed of a graph with nodes representing counties and edges representing the neighborhoods, and a metadata dictionary containing the correspondence between the node indices and the county names. | ||
|
||
The node features are the number of weekly chickenpox cases in each county. They are represented as an array of arrays of size `(1, num_nodes, num_timesteps_in)`. The target values are the number of weekly chickenpox cases in each county. They are represented as an array of arrays of size `(1, num_nodes, num_timesteps_out)`. In both cases. two consecutive arrays are shifted by one-time step. | ||
|
||
The dataset was taken from the [Pytorch Geometric Temporal repository](https://pytorch-geometric-temporal.readthedocs.io/en/latest/modules/dataset.html#torch_geometric_temporal.dataset.chickenpox.ChickenpoxDatasetLoader) and more information about the dataset can be found in the paper ["Chickenpox Cases in Hungary: a Benchmark Dataset for | ||
Spatiotemporal Signal Processing with Graph Neural Networks"](https://arxiv.org/pdf/2102.08100). | ||
|
||
|
||
# Keyword Arguments | ||
- `normalize::Bool`: Whether to normalize the data using Z-score normalization. Default is `true`. | ||
- `num_timesteps_in::Int`: The number of time steps, in this case, the number of weeks, for the input features. Default is `8`. | ||
- `num_timesteps_out::Int`: The number of time steps, in this case, the number of weeks, for the target values. Default is `8`. | ||
- `dir::String`: The directory to save the dataset. Default is `nothing`. | ||
|
||
# Examples | ||
```julia-repl | ||
julia> using JSON3 # import JSON3 | ||
|
||
julia> dataset = ChickenPox() | ||
dataset ChickenPox: | ||
metadata => Dict{Symbol, Any} with 20 entries | ||
graphs => 1-element Vector{MLDatasets.Graph} | ||
|
||
julia> dataset.graphs[1].num_nodes # 20 counties | ||
20 | ||
|
||
julia> size(dataset.graphs[1].node_data.features[1]) | ||
(1, 20, 8) | ||
|
||
julia> dataset.metadata[:BUDAPEST] # The node 5 correponds to Budapest county | ||
5 | ||
``` | ||
""" | ||
struct ChickenPox <: AbstractDataset | ||
metadata::Dict{Symbol, Any} | ||
graphs::Vector{Graph} | ||
end | ||
|
||
function ChickenPox(; normalize::Bool = true, num_timesteps_in::Int = 8 , num_timesteps_out::Int = 8, dir = nothing) | ||
create_default_dir("ChickenPox") | ||
dir = chickenpox_datadir(dir) | ||
g, metadata = create_chickenpox_dataset(normalize, num_timesteps_in, num_timesteps_out, dir) | ||
return ChickenPox(metadata, [g]) | ||
end | ||
|
||
Base.length(d::ChickenPox) = length(d.graphs) | ||
Base.getindex(d::ChickenPox, ::Colon) = d.graphs[1] | ||
Base.getindex(d::ChickenPox, i) = getindex(d.graphs, i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.