Skip to content

Commit

Permalink
start testing for new version
Browse files Browse the repository at this point in the history
  • Loading branch information
OuyangWenyu committed Mar 22, 2024
1 parent 235bb26 commit f7dc2bc
Show file tree
Hide file tree
Showing 15 changed files with 425 additions and 643 deletions.
18 changes: 0 additions & 18 deletions definitions.py

This file was deleted.

81 changes: 78 additions & 3 deletions hydromodel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,80 @@
"""Top-level package for hydromodel."""
"""
Author: Wenyu Ouyang
Date: 2024-02-09 15:56:48
LastEditTime: 2024-03-22 09:12:40
LastEditors: Wenyu Ouyang
Description: Top-level package for hydromodel
FilePath: \hydro-model-xaj\hydromodel\__init__.py
Copyright (c) 2023-2024 Wenyu Ouyang. All rights reserved.
"""

import os
from pathlib import Path
from hydroutils import hydro_file
import yaml

__author__ = """Wenyu Ouyang"""
__email__ = '[email protected]'
__version__ = '0.0.1'
__email__ = "[email protected]"
__version__ = "0.0.1"


CACHE_DIR = hydro_file.get_cache_dir()
SETTING_FILE = os.path.join(Path.home(), "hydro_setting.yml")


def read_setting(setting_path):
if not os.path.exists(setting_path):
raise FileNotFoundError(f"Configuration file not found: {setting_path}")

with open(setting_path, "r") as file:
setting = yaml.safe_load(file)

example_setting = (
"local_data_path:\n"
" root: 'D:\\data\\waterism' # Update with your root data directory\n"
" datasets-origin: 'D:\\data\\waterism\\datasets-origin' # datasets-origin is the directory you put downloaded datasets\n"
" datasets-interim: 'D:\\data\\waterism\\datasets-interim' # the other choice for the directory you put downloaded datasets\n"
" basins-origin: 'D:\\data\\waterism\\basins-origin' # the directory put your own data\n"
" basins-interim: 'D:\\data\\waterism\\basins-interim' # the other choice for your own data"
)

if setting is None:
raise ValueError(
f"Configuration file is empty or has invalid format.\n\nExample configuration:\n{example_setting}"
)

# Define the expected structure
expected_structure = {
"local_data_path": [
"root",
"datasets-origin",
"datasets-interim",
"basins-origin",
"basins-interim",
],
}

# Validate the structure
try:
for key, subkeys in expected_structure.items():
if key not in setting:
raise KeyError(f"Missing required key in config: {key}")

if isinstance(subkeys, list):
for subkey in subkeys:
if subkey not in setting[key]:
raise KeyError(f"Missing required subkey '{subkey}' in '{key}'")
except KeyError as e:
raise ValueError(
f"Incorrect configuration format: {e}\n\nExample configuration:\n{example_setting}"
) from e

return setting


try:
SETTING = read_setting(SETTING_FILE)
except ValueError as e:
print(e)
except Exception as e:
print(f"Unexpected error: {e}")
20 changes: 2 additions & 18 deletions hydromodel/datasets/data_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import pandas as pd
import pathlib
import spotpy
from pathlib import Path
import sys

from hydroutils import hydro_file

sys.path.append(os.path.dirname(Path(os.path.abspath(__file__)).parent.parent))
import definitions
from hydromodel.models.model_config import MODEL_PARAM_DICT
from hydromodel.models.xaj import xaj

Expand All @@ -34,23 +30,15 @@ def read_save_sceua_calibrated_params(basin_id, save_dir, sceua_calibrated_file_
"""
results = spotpy.analyser.load_csv_results(sceua_calibrated_file_name)
<<<<<<< HEAD
bestindex, bestobjf = spotpy.analyser.get_minlikeindex(
results
) # 结果数组中具有最小目标函数的位置的索引
=======
bestindex, bestobjf = spotpy.analyser.get_minlikeindex(results) #结果数组中具有最小目标函数的位置的索引
>>>>>>> wangjingyi1999-event
best_model_run = results[bestindex]
fields = [word for word in best_model_run.dtype.names if word.startswith("par")]
best_calibrate_params = pd.DataFrame(list(best_model_run[fields]))
save_file = os.path.join(save_dir, basin_id + "_calibrate_params.txt")
best_calibrate_params.to_csv(save_file, sep=",", index=False, header=True)
<<<<<<< HEAD
return np.array(best_calibrate_params).reshape(1, -1) # 返回一列最佳的结果
=======
return np.array(best_calibrate_params).reshape(1, -1) #返回一列最佳的结果
>>>>>>> wangjingyi1999-event


def summarize_parameters(result_dir, model_info: dict):
Expand Down Expand Up @@ -223,12 +211,8 @@ def read_and_save_et_ouputs(result_dir, fold: int):
train_period = data_info_train["time"]
test_period = data_info_test["time"]
# TODO: basins_lump_p_pe_q_fold NAME need to be unified
train_np_file = os.path.join(
exp_dir, "data_info_fold" + str(fold) + "_train.npy"
)
test_np_file = os.path.join(
exp_dir, "data_info_fold" + str(fold) + "_test.npy"
)
train_np_file = os.path.join(exp_dir, f"data_info_fold{fold}_train.npy")
test_np_file = os.path.join(exp_dir, f"data_info_fold{fold}_test.npy")
# train_np_file = os.path.join(exp_dir, f"basins_lump_p_pe_q_fold{fold}_train.npy")
# test_np_file = os.path.join(exp_dir, f"basins_lump_p_pe_q_fold{fold}_test.npy")
train_data = np.load(train_np_file)
Expand Down
Loading

0 comments on commit f7dc2bc

Please sign in to comment.