How to use test_hi method in hypothesis

Best Python code snippet using hypothesis

bmd_final.py

Source:bmd_final.py Github

copy

Full Screen

1import click2import mlflow3import logging4import os5import os.path6import shutil7import numpy as np8import xarray as xr9import tensorflow as tf10import matplotlib.pyplot as plt11import climdex.temperature as tdex12import climdex.precipitation as pdex13import experiments.maxt_experiment_base as maxt14import experiments.prcp_experiment_base as prcp15from utils.plot import image_map_factory16from utils.preprocessing import remove_monthly_means17from utils.distributions import normal, bernoulli_gamma18from utils.data import create_time_series_train_test_generator_v219from baselines.dscnn import create_bmd_cnn1020from normalizing_flows.models import VariationalModel21from normalizing_flows.utils import get_metrics22from experiments.common import load_data23def bmd_plot(x, y, mean, samples, latlon_lo, latlon_hi):24 x_max, x_min = np.quantile(x, 0.95), np.quantile(x, 0.05)25 y_max, y_min = np.quantile(y, 0.95), np.quantile(y, 0.05)26 vmin, vmax = np.minimum(x_min, y_min), np.maximum(x_max, y_max)27 fig, axs, plot_fn = image_map_factory(2, 3, figsize=(6,4), cmap='viridis', min_max=(vmin, vmax))28 t = 0 # use first sample; the ordering should be random each epoch29 plot_fn(axs[0,0], x[t].numpy(), latlon_lo[0], latlon_lo[1], title='lo-res true')30 plot_fn(axs[0,1], y[t].numpy(), latlon_hi[0], latlon_hi[1], title='hi-res true')31 plot_fn(axs[0,2], mean[t].numpy(), latlon_hi[0], latlon_hi[1], title='hi-res predicted mean')32 for i, sample in enumerate(samples[t:t+3]):33 plot_fn(axs[1,i], samples[i].numpy(), latlon_hi[0], latlon_hi[1], title=f'sample {i}')34 return fig35def fit_bmd_maxt(fold, i, epochs, lr, batch_size, buffer_size, validate_freq):36 mlflow.log_param('fold', i+1)37 indices = tdex.indices('Time', convert_units_fn=lambda x: x + 273.15)38 data_fold = maxt.preprocess_fold_maxt(fold)39 train_lo, train_hi = data_fold.train40 test_lo, test_hi = data_fold.test41 N_train, N_test = train_lo.Time.size, test_lo.Time.size42 (ht_lr, wt_lr), (ht_hi, wt_hi) = train_lo.shape[1:3], train_hi.shape[1:3]43 monthly_means_lo, monthly_means_hi = data_fold.monthly_means44 train_ds = data_fold.train_dataset(batch_size=batch_size, buffer_size=buffer_size,45 mode='supervised')46 test_ds = data_fold.test_dataset(batch_size=batch_size, buffer_size=N_test,47 mode='test')48 scale = wt_hi // wt_lr49 encoder = create_bmd_cnn10(ht_lr, wt_lr, scale=scale, c_out=2)50 model = VariationalModel(encoder, normal(), optimizer=tf.keras.optimizers.Adam(lr=lr), output_shape=(None,ht_hi,wt_hi,1))51 ckpt_dir = f'/tmp/bmd-final'52 os.makedirs(ckpt_dir)53 for j in range(0, epochs, validate_freq):54 hist = model.fit(train_ds, epochs=validate_freq, steps_per_epoch=N_train//batch_size,55 validation_data=test_ds, validation_steps=N_test//batch_size)56 hist = get_metrics(hist)57 mlflow.log_metrics(hist)58 j += validate_freq59 mlflow.log_metric('epoch', j)60 encoder.save(f'{ckpt_dir}/bmd-epoch{j}.h5')61 mlflow.log_artifact(f'{ckpt_dir}/bmd-epoch{j}.h5', artifact_path=f'model/')62 x_true = []63 y_true = []64 y_mean = []65 y_samples = []66 for x, y in test_ds:67 x_true.append(x)68 y_true.append(y)69 y_mean.append(model.mean(x))70 y_samples.append(model.sample(x))71 x_true = tf.concat(x_true, axis=0)72 y_true = tf.concat(y_true, axis=0)73 y_mean = tf.concat(y_mean, axis=0)74 y_samples = tf.concat(y_samples, axis=0)75 fig = bmd_plot(x_true, y_true, y_mean, y_samples, (test_lo.lat, test_lo.lon), (test_hi.lat, test_hi.lon))76 plt.savefig(f'/tmp/samples-epoch{j}.png')77 mlflow.log_artifact(f'/tmp/samples-epoch{j}.png', 'figures')78 metrics = maxt.eval_metrics(indices, y_true, y_mean, test_hi.coords, monthly_means_hi)79 np.savez(f'/tmp/metrics-epoch{j}.npz', **metrics)80 mlflow.log_artifact(f'/tmp/metrics-epoch{j}.npz', 'data')81 avg_metrics = {k: float(np.mean(v)) for k,v in metrics.items()}82 mlflow.log_metrics(avg_metrics)83 # create plots84 fig = maxt.plot_indices(metrics)85 plt.savefig(f'/tmp/indices-epoch{j}.png')86 mlflow.log_artifact(f'/tmp/indices-epoch{j}.png', 'figures')87 fig = maxt.plot_error_maps(metrics, test_hi.lat, test_hi.lon)88 plt.savefig(f'/tmp/error-maps-epoch{j}.png')89 mlflow.log_artifact(f'/tmp/error-maps-epoch{j}.png', 'figures')90 shutil.rmtree(ckpt_dir)91 92def fit_bmd_prcp(fold, i, epochs, lr, batch_size, buffer_size, validate_freq):93 mlflow.log_param('fold', i+1)94 indices = pdex.indices('Time')95 data_fold = prcp.preprocess_fold_prcp(fold)96 train_lo, train_hi = data_fold.train97 test_lo, test_hi = data_fold.test98 N_train, N_test = train_lo.Time.size, test_lo.Time.size99 (ht_lr, wt_lr), (ht_hi, wt_hi) = train_lo.shape[1:3], train_hi.shape[1:3]100 train_ds = data_fold.train_dataset(batch_size=batch_size, buffer_size=buffer_size,101 mode='supervised')102 test_ds = data_fold.test_dataset(batch_size=batch_size, buffer_size=N_test,103 mode='test')104 scale = wt_hi // wt_lr105 encoder = create_bmd_cnn10(ht_lr, wt_lr, scale=scale, c_out=3)106 model = VariationalModel(encoder, bernoulli_gamma(), optimizer=tf.keras.optimizers.Adam(lr=lr), output_shape=(None,ht_hi,wt_hi,1))107 ckpt_dir = f'/tmp/bmd-prcp-final'108 os.makedirs(ckpt_dir)109 for j in range(0, epochs, validate_freq):110 hist = model.fit(train_ds, epochs=validate_freq, steps_per_epoch=N_train//batch_size,111 validation_data=test_ds, validation_steps=N_test//batch_size)112 hist = get_metrics(hist)113 mlflow.log_metrics(hist)114 j += validate_freq115 mlflow.log_metric('epoch', j)116 encoder.save(f'{ckpt_dir}/bmd-epoch{j}.h5')117 mlflow.log_artifact(f'{ckpt_dir}/bmd-epoch{j}.h5', artifact_path=f'model/')118 x_true = []119 y_true = []120 y_mean = []121 y_samples = []122 for x, y in test_ds:123 x_true.append(x)124 y_true.append(y)125 y_mean.append(model.mean(x))126 y_samples.append(model.sample(x))127 x_true = tf.concat(x_true, axis=0)128 y_true = tf.concat(y_true, axis=0)129 y_mean = tf.concat(y_mean, axis=0)130 y_samples = tf.concat(y_samples, axis=0)131 fig = bmd_plot(x_true, y_true, y_mean, y_samples, (test_lo.lat, test_lo.lon), (test_hi.lat, test_hi.lon))132 plt.savefig(f'/tmp/samples-epoch{j}.png')133 plt.close(fig)134 mlflow.log_artifact(f'/tmp/samples-epoch{j}.png', 'figures')135 y_true = tf.math.pow(y_true, 3.0)136 y_mean = tf.math.pow(y_mean, 3.0)137 metrics = prcp.eval_metrics(indices, y_true, y_mean, test_hi.coords)138 np.savez(f'/tmp/metrics-epoch{j}.npz', **metrics)139 mlflow.log_artifact(f'/tmp/metrics-epoch{j}.npz', 'data')140 avg_metrics = {k: float(np.mean(v)) for k,v in metrics.items()}141 mlflow.log_metrics(avg_metrics)142 # create plots143 fig = prcp.plot_indices(metrics)144 plt.savefig(f'/tmp/indices-epoch{j}.png')145 plt.close(fig)146 mlflow.log_artifact(f'/tmp/indices-epoch{j}.png', 'figures')147 fig = prcp.plot_error_maps(metrics, test_hi.lat, test_hi.lon)148 plt.savefig(f'/tmp/error-maps-epoch{j}.png')149 plt.close(fig)150 mlflow.log_artifact(f'/tmp/error-maps-epoch{j}.png', 'figures')151 shutil.rmtree(ckpt_dir)152@click.command(help="Fits and evaluates the Bano-Medina CNN10 on the ERA-I/Rasmussen dataset")153@click.option("--scale", type=click.INT, required=True, help="Downscaling factor")154@click.option("--epochs", type=click.INT, default=50)155@click.option("--learning-rate", type=click.FLOAT, default=1.0E-4)156@click.option("--batch-size", type=click.INT, default=100)157@click.option("--buffer-size", type=click.INT, default=2400)158@click.option("--validate-freq", type=click.INT, default=10)159@click.option("--region", type=click.STRING, default='southeast_us')160@click.option("--var", type=click.STRING, default='MAXT', help="Dataset var name")161@click.option("--test-size", type=click.INT, default=146, help='size of the test set for each fold')162@click.option("--splits", type=click.INT, default=5, help="Number of CV splits to use")163@click.option("--auth", type=click.STRING, default='gcs.secret.json', help="GCS keyfile")164@click.argument("data_lr", type=click.STRING, default="ras/daily-1deg")165def bmd(data_lr, scale, epochs, learning_rate, batch_size, buffer_size, validate_freq, region,166 var, test_size, splits, auth, **kwargs):167 mlflow.log_param('region', region)168 mlflow.log_param('var', var)169 if scale == 2:170 data_hr = 'daily-1-2deg'171 elif scale == 4:172 data_hr = 'daily-1-4deg'173 elif scale == 8:174 data_hr = 'daily-1-8deg'175 else:176 raise NotImplementedError(f'unsupported downscaling factor {scale}')177 logging.info(f'==== Starting run ====')178 data_lo, data_hi = load_data(data_lr, data_hr, region, auth, scale=scale)179 data_lo = data_lo[[var]].fillna(0.).clip(min=0.0, max=np.inf)180 data_hi = data_hi[[var]].fillna(0.).clip(min=0.0, max=np.inf)181 if var == 'PRCP':182 data_lo = prcp.preprocess_dataset(data_lo, (data_lo.Time.size, data_lo.lat.size, data_lo.lon.size), epsilon=1.0)183 data_lo = xr.where(data_lo > 1.0, data_lo, 0.0)184 data_hi = prcp.preprocess_dataset(data_hi, (data_hi.Time.size, data_hi.lat.size, data_hi.lon.size), epsilon=1.0)185 data_hi = xr.where(data_hi > 1.0, data_hi, 0.0)186 split_fn = create_time_series_train_test_generator_v2(n_splits=splits, test_size=test_size)187 folds = list(split_fn(data_lo, data_hi))188 for i, fold in enumerate(folds):189 if i < 4:190 continue191 logging.info(f'Fold {i+1}/{len(folds)}')192 with mlflow.start_run(nested=True):193 if var == 'MAXT':194 fit_bmd_maxt(fold, i, epochs, learning_rate,195 batch_size, buffer_size, validate_freq)196 elif var == 'PRCP':197 fit_bmd_prcp(fold, i, epochs, learning_rate,198 batch_size, buffer_size, validate_freq)199 else:...

Full Screen

Full Screen

cv_split.py

Source:cv_split.py Github

copy

Full Screen

1#!/usr/bin/env python2# coding=utf-83"""4Cross-validation split of BAGEL data5------------------------------------6Will produce train-test splits for all cross-validation folds, each7in its own subdirectory.8Usage: ./cv_split.py [-f 10] [-c 2] [-d cv] all-xxx.txt all-yyy.txt9-f = number of folds (default to 10)10-c = number of chunks (alternative realizations, defaults to 2)11-d = directory prefix (defaults to "cv")12"""13from __future__ import unicode_literals14from pytreex.core.util import file_stream15from getopt import getopt16import os17from tgen.logf import log_warn18import random19import sys20import re21def write_data(dir, fname_base, fname_repl, data):22 chunk_size = len(data[0])23 for chunk_idx in xrange(chunk_size):24 fname_suff = ".%d" % chunk_idx if chunk_size > 1 else ''25 file_name = os.path.join(dir, re.sub(r'^[^-._]*', fname_repl + fname_suff, fname_base))26 print 'WRITING ' + file_name27 with file_stream(file_name, 'w') as fh:28 for chunk in data:29 print >> fh, chunk[chunk_idx],30def main(argv):31 opts, files = getopt(argv, 'f:c:d:')32 folds = 1033 chunk_size = 234 dir_prefix = 'cv'35 for opt, arg in opts:36 if opt == '-f':37 folds = int(arg)38 elif opt == '-c':39 chunk_size = int(arg)40 elif opt == '-d':41 dir_prefix = arg42 if not files:43 sys.exit(__doc__)44 45 random.seed(1206)46 ordering = None47 for file in files:48 # read all data49 data = []50 with file_stream(file) as fh:51 chunk = []52 for line in fh:53 chunk.append(line)54 if len(chunk) == chunk_size:55 data.append(chunk)56 chunk = []57 if chunk:58 log_warn('Incomplete chunk at end of file %s, size %d' % (file, len(chunk)))59 if ordering is None:60 # create ordering61 ordering = range(len(data))62 random.shuffle(ordering)63 # create directories64 for fold_no in xrange(folds):65 os.mkdir(dir_prefix + "%02d" % fold_no)66 67 # output as train and test into all CV portions68 fold_size, bigger_folds = divmod(len(data), folds)69 for fold_no in xrange(folds):70 # compute test data bounds71 if fold_no < bigger_folds:72 test_lo = (fold_size + 1) * fold_no73 test_hi = (fold_size + 1) * (fold_no + 1)74 else:75 test_lo = fold_size * fold_no + bigger_folds76 test_hi = fold_size * (fold_no + 1) + bigger_folds77 # select train and test data instances78 train_data = [data[idx] for ord, idx in enumerate(ordering)79 if ord < test_lo or ord >= test_hi]80 test_data = [data[idx] for ord, idx in enumerate(ordering)81 if ord >= test_lo and ord < test_hi]82 # write them out to a file (replace `all' in name with train/test)83 fname_base = os.path.basename(file)84 write_data(dir_prefix + "%02d" % fold_no, fname_base, 'train', train_data)85 write_data(dir_prefix + "%02d" % fold_no, fname_base, 'test', test_data)86 87if __name__ == '__main__':...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1from demo import test_foo,test_hi2from plugin import pytest_depend3@pytest_depend([test_foo,test_hi])4def test_bar():5 pass6@pytest_depend([test_foo,test_hi])7def test_bar2():...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful