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

add MatMiner datasets #181

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions docs/src/datasets/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Pages = ["misc.md"]
```@docs
BostonHousing
Iris
MatMiner
Mutagenesis
Titanic
```
3 changes: 3 additions & 0 deletions src/MLDatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ include("datasets/misc/boston_housing.jl")
export BostonHousing
include("datasets/misc/iris.jl")
export Iris
include("datasets/misc/matminer.jl")
export MatMiner
include("datasets/misc/mutagenesis.jl")
export Mutagenesis
include("datasets/misc/titanic.jl")
Expand Down Expand Up @@ -150,6 +152,7 @@ function __init__()

# misc
__init__iris()
__init__matminer()
__init__mutagenesis()

#text
Expand Down
68 changes: 68 additions & 0 deletions src/datasets/misc/matminer.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function __init__matminer()
DEPNAME = "MatMiner"
DOCS = "https://hackingmaterials.lbl.gov/matminer/"

register(ManualDataDep(
DEPNAME,
"""
Datahub: $DEPNAME.
Website: $DOCS
""",
))
end

"""
MatMiner(name; dir=nothing)


# Examples

```julia
data = MatMiner("steels")
```
"""
struct MatMiner <: AbstractDataset
name::String
metadata
data
end

function MatMiner(name::String; dir=nothing)
jsondata, metadata, dir = matminer_getdata(name; dir)
data = JSON3.copy(jsondata)
metadata = JSON3.copy(metadata)
return MatMiner(name, metadata, data)
end

function matminer_metadata(mmdir)
LINK = "https://raw.githubusercontent.com/hackingmaterials/matminer/main/matminer/datasets/dataset_metadata.json"
path = joinpath(mmdir, "dataset_metadata.json")
if !isfile(path)
DataDeps.fetch_default(LINK, mmdir)
end
@assert isfile(path)
return read_json(path)
end

function matminer_getdata(name; dir = nothing)
create_default_dir("MatMiner")
mmdir = isnothing(dir) ? datadep"MatMiner" : dir
metadata = matminer_metadata(mmdir)
@assert name in keys(metadata) "Dataset $name not found among $(string.(keys(metadata)))"
if !isdir(joinpath(mmdir, name))
mkdir(joinpath(mmdir, name))
end
dir = joinpath(mmdir, name)
url = metadata[name]["url"]
@assert metadata[name]["file_type"] == "json.gz"
filepath = joinpath(dir, name * ".json")
if !isfile(filepath)
DataDeps.fetch_default(url, dir)
currdir = pwd()
cd(dir) # Needed since `unpack` extracts in working dir
DataDeps.unpack(filepath * ".gz")
cd(currdir)
end
jsondata = read_json(filepath)
return jsondata, metadata, dir
end