Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0ef3d98
Process all files in zip archive
eplesiat May 4, 2023
9ca0ac8
added prov dependency
cehbrecht May 9, 2023
9c670ff
added provenance module
cehbrecht May 9, 2023
f6707a0
added prov
cehbrecht May 9, 2023
4314b62
added duck stats
cehbrecht May 9, 2023
4d4bb84
update prov
cehbrecht May 11, 2023
6978699
update stats
cehbrecht May 11, 2023
61668f7
update stats
cehbrecht May 11, 2023
e55af26
update stats
cehbrecht May 11, 2023
da91eb4
update stats
cehbrecht May 11, 2023
972df1e
Merge branch 'main' into curiousduck
cehbrecht May 11, 2023
e4eaf03
enable crai
cehbrecht May 11, 2023
1c0c75d
update stats
cehbrecht May 11, 2023
68bad56
update stats
cehbrecht May 11, 2023
6628ee2
update stats
cehbrecht May 11, 2023
10e1d41
fix stats
cehbrecht May 11, 2023
5b631dc
updated provenance
cehbrecht Sep 4, 2023
b5687b1
update prov
cehbrecht Sep 4, 2023
db90e85
using dummy code for testing
cehbrecht Sep 4, 2023
70d28c8
added dashboard process
cehbrecht Sep 4, 2023
98f5f2e
added stats to query
cehbrecht Sep 4, 2023
4604c7f
added stats info to prov
cehbrecht Sep 4, 2023
cbca5c2
fixed matplot
cehbrecht Sep 5, 2023
3f91565
updated report with histogram
cehbrecht Sep 5, 2023
0d3b3a7
update tests
cehbrecht Sep 5, 2023
12a5e84
update report
cehbrecht Sep 5, 2023
17d40c1
use yaml output
cehbrecht Sep 5, 2023
ed0aaf8
fix tests
cehbrecht Sep 5, 2023
68970df
pep8
cehbrecht Sep 5, 2023
d11bcc0
clean up
cehbrecht Sep 5, 2023
d800673
pep8 with black
cehbrecht Sep 5, 2023
60247db
clean up
cehbrecht Sep 5, 2023
89f6fa4
using crai models in metadata
cehbrecht Sep 7, 2023
e11991f
fixed path to provenance db
cehbrecht Sep 7, 2023
3ed4aaa
fix metadata
cehbrecht Sep 7, 2023
f2b3741
update dashboard
cehbrecht Sep 7, 2023
17db925
fixed param
cehbrecht Sep 7, 2023
d618abf
fixed dashboard
cehbrecht Sep 7, 2023
5ef9239
working on provenance db config
cehbrecht Sep 10, 2023
82de25d
update info_models
cehbrecht Sep 10, 2023
db340f7
use dummy models
cehbrecht Sep 10, 2023
ffd0572
fix stats hadcrud4
cehbrecht Sep 10, 2023
8dc4f4e
Retrieve model info with metadata structure from craimodels
eplesiat Jan 23, 2024
76ca1db
Fix issue loading info models
eplesiat Jan 23, 2024
c110d9a
disabled dashboard process
cehbrecht Apr 10, 2024
bef0b9c
using configured db
cehbrecht Apr 12, 2024
7fe5bec
update db
cehbrecht Apr 12, 2024
c6f095d
revert db
cehbrecht Apr 12, 2024
4e99064
working on prov store
cehbrecht Apr 12, 2024
a0f4ea3
skip histogram in provenance
cehbrecht Apr 12, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ testdata.json

# macos
.DS_Store

# tests
PET0.ESMF_LogFile
11 changes: 8 additions & 3 deletions duck/clintai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import craimodels
from climatereconstructionai import evaluate
import logging

LOGGER = logging.getLogger("PYWPS")

info_models = craimodels.info_models()
dataset_names = list(info_models.keys())


def write_clintai_cfg(base_dir, model_dir, eval_name, data_name, data_type, eval_parameters):
def write_clintai_cfg(
base_dir, model_dir, eval_name, data_name, data_type, eval_parameters
):
cfg_templ = """
--data-root-dir {{ base_dir }}
--log-dir {{ base_dir }}
Expand All @@ -31,7 +34,8 @@ def write_clintai_cfg(base_dir, model_dir, eval_name, data_name, data_type, eval
eval_name=eval_name,
data_name=data_name,
data_type=data_type,
eval_parameters=eval_parameters)
eval_parameters=eval_parameters,
)
out = base_dir / "clintai.cfg"
with open(out, "w") as fp:
fp.write(cfg)
Expand All @@ -52,7 +56,8 @@ def run(dataset, dataset_name, variable_name, outdir, update_status):
eval_name=dataset.stem,
data_name=dataset.name,
data_type=variable_name,
eval_parameters=info_models[dataset_name]["eval-parameters"])
eval_parameters=info_models[dataset_name]["eval_parameters"],
)
# print(f"written cfg {cfg_file}")
try:
evaluate(arg_file=cfg_file.as_posix(), prog_func=update_status)
Expand Down
96 changes: 96 additions & 0 deletions duck/data_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
import yaml
import pathlib
import io
import base64


def get_stats(data):
return {
"min": float(np.nanmin(data)),
"max": float(np.nanmax(data)),
"mean": float(np.nanmean(data)),
"std": float(np.nanstd(data)),
}


class DataStats(object):
def __init__(self, output_dir):
if isinstance(output_dir, pathlib.Path):
self.output_dir = output_dir
else:
self.output_dir = pathlib.Path(output_dir)
self.info = None
self.histogram = None

def gen_data_stats(self, filename, var, nbins=100):
ds = xr.open_dataset(filename)

vstats = get_stats(ds[var].values)
bins = np.linspace(vstats["min"], vstats["max"], num=nbins + 1)

ntime, nlon, nlat = ds[var].shape
mratio = np.zeros(ntime)
hist = np.zeros((ntime, nbins))
for i in range(ntime):
a = ds[var].values[i]
idx = ~np.isnan(a)
mratio[i] = idx.sum()
a = np.histogram(a[idx], bins=bins)[0]
hist[i] = a / max(a)

mratio = 1 - mratio / (nlon * nlat)

# TODO: It would be great to store the distribution graph in a database
if True:
plt.close()
plt.imshow(
hist,
aspect="auto",
origin="lower",
extent=[vstats["min"], vstats["max"], 0, ntime],
cmap="gist_ncar",
)
ax = plt.gca()
ax.grid(color="gray", linestyle="-.", linewidth=1)
plt.xlabel(var)
plt.ylabel("Timesteps")
# outfile = self.output_dir / "histogram.png"
# print(f"histogram: {outfile}")
# plt.savefig(outfile.as_posix(), dpi=50)
# store as base64
# Save the plot to a BytesIO object
buffer = io.BytesIO()
plt.savefig(buffer, format="png")
buffer.seek(0)

# Encode the BytesIO object as base64
base64_encoded_plot = base64.b64encode(buffer.read()).decode("utf-8")
# print(f"{base64_encoded_plot}")
self.histogram = base64_encoded_plot
# close plot
plt.close()

# The following information should be stored in a database
attrs = {}
orig_attrs = dict(ds.attrs)
for key in orig_attrs:
value = orig_attrs[key]
if isinstance(value, str):
attrs[key] = value

self.info = {}
self.info["Attrs"] = attrs
self.info["Dims"] = dict(ds.dims)
self.info["Vars"] = list(dict(ds.variables).keys())
self.info["Vstats"] = vstats
self.info["Mstats"] = get_stats(mratio)
# print(self.info)

def write_json(self):
outfile = self.output_dir / "info.txt"
with open(outfile.as_posix(), "w") as f:
yaml.dump(self.info, f)
return outfile
42 changes: 42 additions & 0 deletions duck/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from rdflib import Graph, URIRef
from rdflib.plugins.sparql import prepareQuery

from pywps import configuration

# Provide the path to the SQLite database in the local folder
# DB_URL = "sqlite:////var/lib/pywps/db/provenance.sqlite"
# DB_URL = "sqlite:////tmp/provenance.sqlite"
DB_URL = configuration.get_config_value("logging", "database")


class GraphDB(object):
def __init__(self):
# Create a graph with a specific backend store
self.graph = Graph(
store="SQLAlchemy", identifier=URIRef("http://example.org/graph")
)
self.graph.open(DB_URL, create=True)

def add(self, data):
new_graph = Graph()
new_graph.parse(data=data, format="turtle")

# add rdf to existing graph
for triple in new_graph:
self.graph.add(triple)
# Commit changes to the store
self.graph.commit()

def query(self, query_str):
namespaces = {
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"foaf": "http://xmlns.com/foaf/0.1/",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"prov": "http://www.w3.org/ns/prov#",
"provone": "http://purl.dataone.org/provone/2015/01/15/ontology#",
"dcterms": "http://purl.org/dc/terms/",
"clint": "urn:clint:",
}
query = prepareQuery(query_str, initNs=namespaces)
results = self.graph.query(query)
return results
3 changes: 3 additions & 0 deletions duck/default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ cleantempdir = false
level = INFO
file = duck.log
format = %(asctime)s] [%(levelname)s] line=%(lineno)s module=%(module)s %(message)s

[provenance]
db_url = sqlite:////tmp/provenance.sqlite
3 changes: 3 additions & 0 deletions duck/processes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from .wps_clintai import ClintAI

# from .wps_dashboard import Dashboard

processes = [
ClintAI(),
# Dashboard(),
]
Loading
Loading