text:
import keras
import keras.backend as K
import numpy as np
import tensorflow as tf
from sklearn.model_selection import ParameterGrid

import pbt
from pbt.utils import train_population

VALIDATION_SPLIT = 0.3
# population_size = 20
BATCH_SIZE = 32  # one step in a member is equivalent to train on a batch
TOTAL_STEPS = 1000  # total number of steps before ending training
STEPS_READY = 50  # number of steps before updating a member
STEPS_SAVE = 100  # number of steps before saving the status of the population

# ------------------------------------------
# SEARCH SPACE
# ------------------------------------------
l1_values = np.linspace(1e-5, 0.01, num=4).tolist()
l2_values = np.linspace(1e-5, 0.01, num=4).tolist()
param_grid = ParameterGrid(dict(l1=l1_values, l2=l2_values))

def build_fn(data_dim, l1=1e-5, l2=1e-5):
    """Returns a function which can be called with no parameters to create a new
     model.

    The returned function is required by Member, which invokes it to construct a
    new instance of the model.

    Returns:
        function: a function that can be called to get a compiled Keras model.

    """

    def _build_fn():
        # Set seed to get same weight initialization
        np.random.seed(42)
        model = keras.models.Sequential([
            keras.layers.Dense(64,
                               activation='relu',
                               input_shape=(data_dim,),
                               kernel_regularizer=
                               pbt.hyperparameters.l1_l2(l1, l2)),
            keras.layers.Dense(1,
                               kernel_regularizer=
                               pbt.hyperparameters.l1_l2(l1, l2)),
        ])
        model.compile(optimizer='adam', loss='mean_squared_error',
                      metrics=['mse'])
        return model

    return _build_fn

def show_results(df):
    print('***** RESULTS *****')
    population_size = df['model_id'].nunique()
    df_final = df.tail(population_size)
    print('** Ranking **')
    print(df_final[['model_id', 'loss']].sort_values('loss'))
    print('** Statistics **')
    print(df_final['loss'].describe())
    print('** Best hyperparameters **')
    best = df.iloc[[df_final['loss'].idxmin()]]
    print(best.filter(regex=".+:.+"))

def main():
    # The following configuration is to make sure the final comparison is not
    # biased by randomness (e.g., initialization)
    session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                                  inter_op_parallelism_threads=1)
    sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
    K.set_session(sess)

    # Load dataset. Split training dataset into train and validation sets
    dataset = tf.keras.datasets.boston_housing
    (x_train, y_train), (x_test, y_test) = dataset.load_data()
    print('Train examples: {}, test examples: {}'.format(
        x_train.shape[0], x_test.shape[0]
    ))
    data_dim = x_train.shape[1]

    # ------------------------------------------
    # GRID SEARCH
    # ------------------------------------------

    # We create a population where we never explore new hyperparameters
    population = []
    for h in param_grid:
        member = pbt.members.Member(build_fn(data_dim, **h),
                                    steps_ready=None)
        population.append(member)

    res_gd = train_population(population, x_train, y_train, BATCH_SIZE,
                              TOTAL_STEPS, STEPS_SAVE, VALIDATION_SPLIT)
    show_results(res_gd)

    # ------------------------------------------
    # PBT
    # ------------------------------------------
    K.clear_session()

    # This time, the members will explore new hyperparameters
    population = []
    for h in param_grid:
        member = pbt.members.Member(build_fn(data_dim, **h),
                                    steps_ready=STEPS_READY)
        population.append(member)

    res_pbt = train_population(population, x_train, y_train, BATCH_SIZE,
                               TOTAL_STEPS, STEPS_SAVE, VALIDATION_SPLIT)
    show_results(res_pbt)

if __name__ == "__main__":
    main()

max_stars_repo_name: SamuelMarks/pbt-keras

max_issues_repo_name: SamuelMarks/pbt-keras

max_forks_repo_name: SamuelMarks/pbt-keras


text:
#!/usr/bin/env python

import rospy
from geometry_msgs.msg import PoseStamped, TwistStamped
from styx_msgs.msg import Lane
from std_msgs.msg import Int32
from scipy.spatial import KDTree

import copy

LOOKAHEAD_WPS = 100 # Number of waypoints we will publish.

class WPControl(object):

    def __init__(self):

        self.waypoints = None
        self.waypoints_pos = None
        self.wp_tree = None

    def waypoints_cb(self, waypoints):
        if not self.waypoints_pos:
            self.waypoints = waypoints
            self.waypoints_pos = [ [w.pose.pose.position.x, w.pose.pose.position.y] for w in waypoints.waypoints]
            # KDTree for waypoints search
            self.wp_tree = KDTree(self.waypoints_pos)

    def is_ready(self):
        return self.wp_tree is not None

    def waypoint(self, i):
        return self.waypoints.waypoints[i]

    # Next waypoint ahead of the vehicle
    def next_waypoint_i(self, pose):
        position = [pose.position.x, pose.position.y]
        current_wp_i = self.wp_tree.query(position, 1)[1]
        return current_wp_i

    def copy_wps(self, start_i, count):
        last_i = min(len(self.waypoints.waypoints), start_i + count)
        lane = Lane()
        lane.header = self.waypoints.header
        lane.waypoints = copy.deepcopy(self.waypoints.waypoints[start_i : last_i])
        return lane

class WaypointUpdater(object):

    def __init__(self):

        rospy.init_node('waypoint_updater')

        self.wp_control = WPControl()

        rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb)
        rospy.Subscriber('/base_waypoints', Lane, self.wp_control.waypoints_cb)
        rospy.Subscriber('/current_velocity', TwistStamped, self.velocity_cb)
        rospy.Subscriber('/traffic_waypoint', Int32, self.traffic_cb)

        self.final_waypoints_pub = rospy.Publisher('final_waypoints', Lane, queue_size=1)

        self.vehicle = None

        self.next_light_i = -1
        self.vehicle_velocity = 0

        self.loop()

    def pose_cb(self, msg):
        self.vehicle = msg

    def traffic_cb(self, msg):
        self.next_light_i = msg.data

    def velocity_cb(self, velocity):
        self.vehicle_velocity = velocity.twist.linear.x

    def loop(self):
        rate = rospy.Rate(10)
        while not rospy.is_shutdown():
            if self.vehicle and self.wp_control.is_ready() and self.vehicle_velocity:
                start_i = self.wp_control.next_waypoint_i(self.vehicle.pose) # next waypoint to vehicle
                if self.next_light_i != -1:
                    step_count = self.next_light_i - start_i
                    lane = self.wp_control.copy_wps(start_i, step_count)
                    self.decelerate(lane.waypoints, start_i)
                else:
                    lane = self.wp_control.copy_wps(start_i, LOOKAHEAD_WPS)
                self.final_waypoints_pub.publish(lane)

            rate.sleep()

    def decelerate(self, waypoints, start_i):
        step_count = len(waypoints) - 1
        stop_i = step_count - 2
        target_speed = self.vehicle_velocity
        velocity_decrement = target_speed / step_count if step_count > 0 else target_speed
        for i, wp in enumerate(waypoints):
            target_speed -= velocity_decrement
            if target_speed <= 1 or i >= stop_i: target_speed = 0
            wp.twist.twist.linear.x = target_speed #set waypoint target speed

if __name__ == '__main__':
    try:
        WaypointUpdater()
    except rospy.ROSInterruptException:
        rospy.logerr('Could not start waypoint updater node.')

ext: py

max_stars_repo_name: timout/CarND-Capstone

max_issues_repo_name: timout/CarND-Capstone

max_forks_repo_name: timout/CarND-Capstone

reason: from scipy


text:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))

from codes.problems.OneMax import OneMax
from codes.problems.function_Ackley import function_Ackley
from codes.problems.function_Griewank import function_Griewank
from codes.problems.function_Michalewicz import function_Michalewicz
from codes.problems.function_Rastrigin import function_Rastrigin
from codes.problems.function_Schwefel import function_Schwefel
from codes.problems.function_StyblinskiTang import function_StyblinskiTang
from codes.problems.function_XinSheYang import function_XinSheYang

from codes.algorithms.GA import GA
from codes.algorithms.PfGA import PfGA
from codes.algorithms.ABC import ABC
from codes.algorithms.Bat import Bat
from codes.algorithms.Cuckoo import Cuckoo
from codes.algorithms.Cuckoo_greedy import Cuckoo_greedy
from codes.algorithms.DE import DE
from codes.algorithms.Firefly import Firefly
from codes.algorithms.Harmony import Harmony
from codes.algorithms.PSO import PSO
from codes.algorithms.WOA import WOA
from codes.algorithms.GA_BLXa import GA_BLXa
from codes.algorithms.GA_SPX import GA_SPX
from codes.algorithms.Tabu import Tabu

def create(D, N):
    funcs = [
        OneMax(D),
        function_Ackley(D),
        function_Griewank(D),
        function_Michalewicz(D),
        function_Rastrigin(D),
        function_Schwefel(D),
        function_StyblinskiTang(D),
        function_XinSheYang(D),
    ]
    algs= [
        GA(N, save_elite=False, select_method="ranking", mutation=0.05),
        PfGA(mutation=0.5),
        ABC(N, follow_bee=15, visit_max=30),
        Bat(N, frequency_min=0, frequency_max=0.05, good_bat_rate=0.1, volume_init=0.5, pulse_convergence_value=0.9, pulse_convergence_speed=0.1),
        Cuckoo(N, scaling_rate=1.0, levy_rate=1.0, bad_nest_rate=0.1),
        Cuckoo_greedy(N, epsilon=0.5, bad_nest_rate=0.1),
        DE(N, crossover_rate=0, scaling=0.4),
        Firefly(N, attracting_degree=0.08, absorb=50.0, alpha=0.02, is_normalization=True),
        Harmony(N, bandwidth=1.0, enable_bandwidth_rate=True, select_rate=0.4, change_rate=0.9),
        PSO(N, inertia=0.2, global_acceleration=0.2, personal_acceleration=0.2),
        WOA(N, a_decrease=2/50, logarithmic_spiral=1),
        GA_BLXa(N, save_elite=False, select_method="ranking", mutation=0.05, blx_a=0.1),
        GA_SPX(N, save_elite=False, select_method="ranking", mutation=0.05),
        Tabu(N, epsilon=0, tabu_list_size=10, tabu_range_rate=0.1),
    ]
    return funcs, algs

def anime2(func, alg):
    tmp_dir = os.path.join(os.path.dirname(__file__), '..', 'tmp')

    alg.init(func)
    N = 100

    fig = plt.figure()

    func_x = []
    func_y = []
    diff_num = 1000
    for i in range(diff_num):
        diff = (func.MAX_VAL-func.MIN_VAL)
        x = func.MIN_VAL + (i/diff_num)*diff
        y = func.eval(np.asarray([x]))
        func_x.append(x)
        func_y.append(y)

    x_pos = []
    y_score = []
    max_pos = []
    max_score = []
    for _ in range(N):
        alg.step()

        pos = []
        score = []
        for e in alg.getElements():
            pos.append(e.getArray()[0])
            score.append(e.getScore())
        x_pos.append(pos)
        y_score.append(score)

        max_pos.append(alg.getMaxElement().getArray()[0])
        max_score.append(alg.getMaxElement().getScore())

    def plot(i):
        plt.cla()

        plt.plot(func_x, func_y, label=func.__class__.__name__, linewidth="0.5")
        plt.plot(x_pos[i], y_score[i], 'o', color="orange", markeredgecolor="black")
        plt.plot(max_pos[i], max_score[i], 'o', color="red", markeredgecolor="black")
        plt.title('step={}'.format(i))

    ani = animation.FuncAnimation(fig, plot, N, interval=200)
    path = os.path.join(tmp_dir, "{}_{}_2.gif".format(func.__class__.__name__, alg.__class__.__name__))
    print(path)
    ani.save(path, writer="imagemagick")
    #plt.show()

def main2():
    funcs, algs = create(1, 6)
    for func in funcs:
        for alg in algs:
            anime2(func, alg)

def anime3(func, alg):
    tmp_dir = os.path.join(os.path.dirname(__file__), '..', 'tmp')

    alg.init(func)
    N = 100

    fig = plt.figure()

    # function
    func_y = []
    diff_num = 50
    diff = (func.MAX_VAL-func.MIN_VAL)
    for x1 in range(diff_num):
        x1 = func.MIN_VAL + (x1/diff_num)*diff
        d = []
        for x2 in range(diff_num):
            x2 = func.MIN_VAL + (x2/diff_num)*diff
            y = func.eval(np.asarray([x2, x1]))
            d.append(y)
        func_y.insert(0, d)

    # search
    pos1 = []
    pos2 = []
    max_pos1 = []
    max_pos2 = []
    for _ in range(N):
        alg.step()

        pos1_ = []
        pos2_ = []
        for e in alg.getElements():
            pos1_.append(e.getArray()[0])
            pos2_.append(e.getArray()[1])
        pos1.append(pos1_)
        pos2.append(pos2_)

        max_pos1.append(alg.getMaxElement().getArray()[0])
        max_pos2.append(alg.getMaxElement().getArray()[1])

    extent = (func.MIN_VAL, func.MAX_VAL, func.MIN_VAL, func.MAX_VAL)
    plt.imshow(func_y, interpolation="nearest",  cmap="jet", extent=extent)
    plt.colorbar()
    def plot(i):
        plt.cla()

        plt.imshow(func_y, interpolation="nearest",  cmap="jet", extent=extent)
        plt.plot(pos1[i], pos2[i], 'o', color="orange", markeredgecolor="black")
        plt.plot(max_pos1[i], max_pos2[i], 'o', color="red", markeredgecolor="black")
        plt.title('step={}'.format(i))

    ani = animation.FuncAnimation(fig, plot, N, interval=200)

    path = os.path.join(tmp_dir, "{}_{}_3.gif".format(func.__class__.__name__, alg.__class__.__name__))
    print(path)
    ani.save(path, writer="imagemagick")
    #plt.show()

def main3():
    funcs, algs = create(2, 20)
    for func in funcs:
        for alg in algs:
            anime3(func, alg)

if __name__ == "__main__":
    main2()
    main3()

max_stars_repo_name: pocokhc/metaheuristics

max_issues_repo_name: pocokhc/metaheuristics

max_forks_repo_name: pocokhc/metaheuristics

reason: import numpy


text:
# this file is for models to perform comment_generation
import markovify
import numpy as np
import re
from sklearn.feature_extraction.text import TfidfVectorizer
import time

class Markov():
    def __init__(self, post_train, post_test, comment_list_train, comment_list_test):
        vocabulary = post_train + post_test
        for comment in comment_list_train:
            if isinstance(comment, str):
                vocabulary.append(comment)
        for comment in comment_list_test:
            if isinstance(comment, str):
                vocabulary.append(comment)
        self.tfidf = TfidfVectorizer().fit(['' if x is np.nan else x for x in vocabulary])

    def add_comment_to_text(self, comment_list, text_dict, cluster_id):
        markov_text = text_dict.get(cluster_id, '')
        for comment in comment_list:
            if isinstance(comment, str):
                markov_text += ' ' + comment
        return markov_text

    def generate_markov_dict(self, posts, cluster_ids, comment_list):
        print('generating markov dict ...')
        markov_text_dict = {}
        for i in range(len(posts)):
            cluster_id = cluster_ids[i]
            markov_text_dict[cluster_id] = self.add_comment_to_text(comment_list[i], markov_text_dict, cluster_id)
            if i % 100 == 0:
                print('post ' + str(i) + '\'s word dict merged')
        return markov_text_dict

    def get_text_similarity(self, gen_comment, comment_list):
        comment_text = ''
        for comment in comment_list:
            if isinstance(comment, str):
                comment_text += ' ' + comment
        tfidf = self.tfidf.transform([gen_comment]+[comment_text])
        return (tfidf * tfidf.T).toarray()[0][1]

    def generate_comment(self, markov_dict, posts, cluster_ids, comment_list, pos_tag_flag, filename):
        # to diversify the generated comments between same cluster
        # we will add some amount of post vocabulary to the markov dict
        print('generating comments ...')
        gen_comments = []
        text_similarities = []
        start_time = time.time()
        test_range = 100
        for i in range(test_range): #range(len(posts)): # it takes 3s for one post and ~25 hr for all 3000 test post
            combined_text = markov_dict.get(cluster_ids[i], '') + posts[i]
            gen_comment = ''
            try:
                if pos_tag_flag is False:
                    text_model = markovify.Text(combined_text)
                else:
                    text_model = markovify.combine([markovify.Text(markov_dict.get(cluster_ids[i], '')), markovify.Text(posts[i])], [2, 1])
                gen_comment = text_model.make_sentence()
                gen_comment = gen_comment if gen_comment is not None else ''
                text_similarity = self.get_text_similarity(gen_comment, comment_list[i])
                text_similarities.append(text_similarity)
            except:
                gen_comment = ''
            gen_comments.append(gen_comment)
            print('-------------------------------------------')
            print('post is:')
            print(posts[i])
            print('.....below is generated comment.....')
            print(gen_comment)
            print('-------------------------------------------')
            if i % 10 == 0:
                if i == 0:
                    print("--- %s seconds for 1 post ---" % (time.time() - start_time))
                else:
                    print("--- %s seconds in total ---" % (time.time() - start_time))
                print('post ' + str(i) + '\'s comment generated')

        with open(filename, 'w') as f:
            for item in gen_comments:
                f.write("%s\n" % item)

        text_similarity = sum(text_similarities)/len(text_similarities) if len(text_similarities) != 0 else 0
        print('The text similarity is '+str(text_similarity))
        print('The text generation rate is '+str(len(text_similarities)/test_range)) #/len(posts))


text:
import os
from tqdm import tqdm

import torch
from torch.utils.data import DataLoader

from paired_dataset import PairedDataset
from logger import Logger, Visualizer
import imageio
import numpy as np

from sync_batchnorm import DataParallelWithCallback
from normalize_kp import normalize_kp

def animate(config, generator, kp_detector, checkpoint, log_dir, dataset):
    log_dir = os.path.join(log_dir, 'animation')
    png_dir = os.path.join(log_dir, 'png')
    animate_params = config['animate_params']

    dataset = PairedDataset(initial_dataset=dataset, number_of_pairs=animate_params['num_pairs'])
    dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1)

    if checkpoint is not None:
        Logger.load_cpk(checkpoint, generator=generator, kp_detector=kp_detector)
    else:
        raise AttributeError("Checkpoint should be specified for mode='animate'.")

    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    if not os.path.exists(png_dir):
        os.makedirs(png_dir)

    if torch.cuda.is_available():
        generator = DataParallelWithCallback(generator)
        kp_detector = DataParallelWithCallback(kp_detector)

    generator.eval()
    kp_detector.eval()

    for it, x in tqdm(enumerate(dataloader)):
        with torch.no_grad():
            predictions = []
            visualizations = []

            driving_video = x['driving_video']
            source_frame = x['source_video'][:, :, 0, :, :]

            kp_source = kp_detector(source_frame)
            kp_driving_initial = kp_detector(driving_video[:, :, 0])

            for frame_idx in range(driving_video.shape[2]):
                driving_frame = driving_video[:, :, frame_idx]
                kp_driving = kp_detector(driving_frame)
                kp_norm = normalize_kp(kp_source=kp_source, kp_driving=kp_driving,
                                       kp_driving_initial=kp_driving_initial, **animate_params['normalization_params'])
                out = generator(source_frame, kp_source=kp_source, kp_driving=kp_norm)

                out['kp_driving'] = kp_driving
                out['kp_source'] = kp_source
                out['kp_norm'] = kp_norm

                del out['sparse_deformed']

                predictions.append(np.transpose(out['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0])

                visualization = Visualizer(**config['visualizer_params']).visualize(source=source_frame,
                                                                                driving=driving_frame, out=out)
                visualization = visualization
                visualizations.append(visualization)

            predictions = np.concatenate(predictions, axis=1)
            result_name = "-".join([x['driving_name'][0], x['source_name'][0]])
            imageio.imsave(os.path.join(png_dir, result_name + '.png'), (255 * predictions).astype(np.uint8))

            image_name = result_name + animate_params['format']
            imageio.mimsave(os.path.join(log_dir, image_name), visualizations)

max_stars_repo_name: Adjective-Object/first-order-motion-tk

max_issues_repo_name: Adjective-Object/first-order-motion-tk

max_forks_repo_name: Adjective-Object/first-order-motion-tk


text:
import logging
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

class ChartIndicatorException(Exception):
	pass

class PlottingExeception(ChartIndicatorException):
	pass

class TraceCandlesException(ChartIndicatorException):
	pass

class ErrorImplementingIndicator(ChartIndicatorException):
	pass

log = logging.getLogger("candlestick-chart-indicator")

class CandlestickChartIndicator(ABC):
	"""
	Base class responsible for the implementation of candlestick graphics, and their data.

	detail:
		This class implements a "Chain of Responsibility" design pattern.
		https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern.
	"""
	@abc.abstractmethod
	def inicate(self):
		pass

class MA(CandlestickChartIndicator):
	"""
	Class responsible for implementing a simple Moving Average that stops
	filter out price fluctuations helping to identify trends.
	"""
	def indicate(self, data_frame, data=[], **kwargs):
		try:
			ma = data_frame['close'].rolling(window=kwargs.get("days", 21)).mean()
			trace_avg = go.Scatter(x=ma.index, y=MA, name='MA', line=dict(color='#BEBECF'), opacity=0.8)
			data.append(trace_avg)
		except (ErrorImplementingIndicator, TypeError) as e:
			log.warning(f"Error implementing 'ma' indicator: {e}")
		finally:
			return data

class EMA(CandlestickChartIndicator):
	"""
	Class responsible for implementing an exponential moving average
	EMA = Price today * K + EMA yesterday x (1-k) where K = 2 /(N+1)
	"""
	def indicate(self, data_frame, data=[], **kwargs):
		try:
			k = (2 / (kwargs.get("days", 21) + 1))
			ma = data_frame['close'].rolling(window=kwargs.get("days", 21)).mean()
			ema_data = pd.DataFrame(index=ma.index)
			ema_data['PRICE'] = data_frame['close']
			ema_data['MA'] = ma
			ema_data['EMA'] = np.NaN
			ema_data['EMA'][0] = ema_data['MA'][1]
			for i in range(1, len(ema_data)):
				ema_data['EMA'][i] = (ema_data['PRICE'][i] * k) + ((1-k) * ema_data['EMA'][i-1])
			trace_ema = go.Scatter(
				x=ema_data.index, y=ema_data['MA'], name='EMA', line=dict(color='#17BECF'), opacity=0.8)
			data.append(trace_ema)
		except (ErrorImplementingIndicator, TypeError) as e:
			log.warning(f"Error implementing 'ema' indicator: {e}")
		finally:
			return data

class CrossingMovingAvarege(CandlestickChartIndicator):
	"""
	Class responsible for implementing the crossing of moving averages that consists of indicating
	buying and selling an asset whenever the averages cross.

	detail:
		This indicator consists of 2 sets of simple moving averages. an acquaintance
		as short average or short and another known as long average or long whenever short crosses
		the long down we make a sale, whenever the long crosses the short up we buy.
	"""
	def indicate(self, data_frame, data=[], **kwargs):
		try:
			short_rolling = data_frame['close'].rolling(window=kwargs.get("short_rolling", 9)).mean()
			long_rolling  = data_frame['close'].rolling(window=kwargs.get("long_rolling", 21)).mean()
			trace_short_rolling = go.Scatter(
				x=short_rolling.index, y=short_rolling, name='SHORT', line=dict(color='#17BECF'), opacity=0.5)
			trace_long_rolling  = go.Scatter(
				x=long_rolling.index, y=long_rolling, name='LONG', line=dict(color='#17becf'), opacity=0.5)
			data.append(trace_short_rolling)
			data.append(trace_long_rolling)
		except (ErrorImplementingIndicator, TypeError) as e:
			log.warning(f"Error implementing 'crossing moving avarege' indicator: {e}")
		finally:
			return data

class MACD(CandlestickChartIndicator):

	"""
	Class responsible for implementing a MACD -> Convergence - Divergence
	of the moving average, which uses 3 exponential moving averages.
	"""
	def indicator(self, data_frame, data=[], **kwargs):
		try:
			high_average = data_frame['max'].rolling(window=kwargs.get("high", 8)).mean()
			low_average  = data_frame['min'].rolling(window=kwargs.get("low", 8)).mean()
			hilo_high = pd.DataFrame(index=data_frame.index)
			hilo_low  = pd.DataFrame(index=data_frame.index)
			hilo_high['max'] = np.where(data_frame['close'] > high_average, low_average, np.NaN)
			hilo_low['min']  = np.where(data_frame['close'] < low_average, high_average, np.NaN)
			trace_high = go.Scatter(x=hilo_high.index, y=hilo_high, line=dict(color='#17BECF'), opacity=1)
			trace_low = go.Scatter(x=hilo_low.index, y=hilo_low, line=dict(color='#B22222'), opacity=1)
			data.append(trace_high)
			data.append(trace_low)
		except (ErrorImplementingIndicator, TypeError) as e:
			log.warning(f"Error implementing 'macd' indicator: {e}")
		finally:
			return data

class BollingerBands(CandlestickChartIndicator):
	"""
	Class responsible for implementing boolinger bands based on variations
	prices at standard deviation levels.

	detail:
	This indicator is able to measure price volatility.
	"""
	def indicate(self, data_frame, data=[], **kwargs):
		try:
			df_avg = data_frame['close'].rolling(window=kwargs.get("days", 21)).mean().dropna()
			df_std = data_frame['close'].rolling(window=kwargs.get("days", 21)).std().dropna()
			df_bollinger = pd.DataFrame(index=df_avg.index)

			df_bollinger['mband'] = df_avg
			df_bollinger['uband'] = df_avg + df_std.apply(lambda x: (x * 2))
			df_bollinger['iband'] = df_avg - df_std.apply(lambda x: (x * 2))
			df_price = data_frame[df_bollinger.index[0]:]

			trace_prices = go.Candlestick(
			   x     = df_price.index,
			   open  = df_price['open'],
			   high  = df_price['max'],
			   low   = df_price['min'],
			   close = df_price['close'],
			   name='prices')
			uband = go.Scatter(
				x=df_bollinger.index, y=df_bollinger['uband'], name='Upper Band',
				line=dict(color='#17BECF'), opacity=0.8)
			mband = go.Scatter(
				x=df_bollinger.index, y=df_bollinger['mband'], name='Moving Band',
				line=dict(color='#B22222'), opacity=0.5)
			iband = go.Scatter(
				x=df_bollinger.index, y=df_bollinger['iband'], name='Lower Band',
				line=dict(color='#17BECF'), opacity=0.8)
			data.append(uband)
			data.append(mband)
			data.append(iband)
			data.append(trace_prices)
		except (ErrorImplementingIndicator, TypeError) as e:
			log.warning(f"Error implementing 'bollinger bands' indicator: {e}")
		finally:
			return data

# end-of-file


text:
###############################################################################
# fitMAPs: fit the density distribution of MAPs
###############################################################################
import sys
import os, os.path
import pickle
import copy
import numpy
from galpy.util import save_pickles
import define_rcsample
import fitDens
_NSAMPLES= 50000
# Globals
locations= None
distmods= None
effsel= None
effsel_mar= None
effsel_drim= None
effsel_sale= None
effsel_zero= None
def fitMAPs(type,savefilename):
    setup_selection_function()
    if os.path.exists(savefilename):
        with open(savefilename,'rb') as savefile:
            bf= pickle.load(savefile)
            samples= pickle.load(savefile)
            bf_g15= pickle.load(savefile)
            samples_g15= pickle.load(savefile)
            bf_zero= pickle.load(savefile)
            samples_zero= pickle.load(savefile)
            bii= pickle.load(savefile)
    else:
        bf= []
        samples= []
        bf_g15= []
        samples_g15= []
        bf_zero= []
        samples_zero= []
        bii= 0
    maps= define_rcsample.MAPs()
    for ii, map in enumerate(maps.map()):
        if ii < bii: continue
        tbf, ts= fitmap(map,type=type,dmap='marshall06')
        bf.append(tbf)
        samples.append(ts)
        tbf, ts= fitmap(map,type=type,dmap='green15')
        bf_g15.append(tbf)
        samples_g15.append(ts)
        tbf, ts= fitmap(map,type=type,dmap='zero')
        bf_zero.append(tbf)
        samples_zero.append(ts)
        print ii, numpy.median(samples[-1],axis=1)
        save_pickles(savefilename,bf,samples,
                     bf_g15,samples_g15,
                     bf_zero,samples_zero,
                     ii+1)
    return None

def fitmap(tdata,type='brokenexp',dmap='marshall06'):
    tlocations= copy.deepcopy(locations)
    tdistmods= copy.deepcopy(distmods)
    if dmap == 'green15':
        teffsel= copy.deepcopy(effsel)
    elif dmap.lower() == 'marshall06':
        teffsel= copy.deepcopy(effsel_mar)
    elif dmap.lower() == 'zero':
        teffsel= copy.deepcopy(effsel_zero)
    bf, samples= fitDens.fitDens(tdata,
                                 numpy.array(tlocations),
                                 copy.deepcopy(teffsel),
                                 tdistmods,type=type,verbose=False,
                                 mcmc=True,nsamples=_NSAMPLES)
    bf= fitDens.fitDens(tdata,
                        numpy.array(tlocations),
                        copy.deepcopy(teffsel),
                        tdistmods,type=type,verbose=False,
                        init=numpy.median(samples,axis=1))
    return (bf, samples)

def setup_selection_function():
    # Green et al.
    global locations, distmods, effsel, effsel_mar, effsel_drim, effsel_sale
    global effsel_zero
    with open('../essf/essf_green15.sav','rb') as savefile:
        locations= pickle.load(savefile)
        effsel= pickle.load(savefile)
        distmods= pickle.load(savefile)
    # Marshall et al. (2006)
    with open('../essf/essf_marshall06.sav','rb') as savefile:
        locations= pickle.load(savefile)
        effsel_mar= pickle.load(savefile)
    # Fill in regions not covered by Sale map
    effsel_mar[effsel_mar < -0.5]= effsel[effsel_mar < -0.5]
    # Sale et al. (2014)
    with open('../essf/essf_sale14.sav','rb') as savefile:
        locations= pickle.load(savefile)
        effsel_sale= pickle.load(savefile)
    # Fill in regions not covered by Marshall map
    effsel_sale[effsel_sale < -0.5]= effsel[effsel_sale < -0.5]
    # Drimmel et al (2003)
    with open('../essf/essf_drimmel03.sav','rb') as savefile:
        locations= pickle.load(savefile)
        effsel_drim= pickle.load(savefile)
    # Zero
    with open('../essf/essf_zero.sav','rb') as savefile:
        locations= pickle.load(savefile)
        effsel_zero= pickle.load(savefile)
    return None

if __name__ == '__main__':
    fitMAPs(sys.argv[1],sys.argv[2])

max_stars_repo_name: jobovy/apogee-maps

max_issues_repo_name: jobovy/apogee-maps

max_forks_repo_name: jobovy/apogee-maps


text:
import bpy
import numpy as np

import blendtorch.btb as btb

import math
import mathutils
import os
import sys
import random
from mathutils import Vector, Matrix, Euler

def rotate_eye(obj, x_angle, z_angle):
    obj.rotation_mode = 'XYZ'
    deg_to_rad = np.pi * 2/360
    x_rot = -x_angle * deg_to_rad
    z_rot = z_angle * deg_to_rad
    euler_rotation =  Euler((x_rot, 0, z_rot) , 'XYZ')
    obj.rotation_euler = euler_rotation
    outer_eye.rotation_euler = Euler((-x_rot - np.pi/2, 0, z_rot) , 'XYZ')
    obj_rot_mat = euler_rotation.to_matrix()

    #obj_rot_mat = initPoseMat

    if obj.parent:
        P = obj.parent.matrix.decompose()[1].to_matrix()
        obj_rot_mat = P * obj_rot_mat * P.inverted()

scene = bpy.context.scene
armature = bpy.data.objects['Armature Head']
camera_obj = bpy.data.objects['Camera']
camera = bpy.data.cameras['Camera']

eyeLbone = armature.pose.bones['def_eye.L']
eyeLblinkbone = armature.pose.bones['eyeblink.L']
eyeRbone = armature.pose.bones['def_eye.R']
eyeRblinkbone = armature.pose.bones['eyeblink.R']

outer_eye = bpy.data.collections["Collection 1"].objects['eye-outer']

armature_matrix = armature.matrix_world

def main():
    # Parse script arguments passed via blendtorch launcher
    btargs, remainder = btb.parse_blendtorch_args()

    cam = bpy.context.scene.camera

    def pre_frame():
        # Called before processing a frame
        #set the parameters here

        elev = 0
        azim = 15

        input_eye_closedness = 0.0

        z_angle = -azim
        x_angle = -elev

        for c in list(eyeLbone.constraints.values()):
            eyeLbone.constraints.remove(c)
        for c in list(eyeRbone.constraints.values()):
            eyeRbone.constraints.remove(c)

        # # Set eye target

        rotate_eye(eyeLbone, x_angle, z_angle)

        # # Set eye blink location
        eyeLblinkbone.location[2] = input_eye_closedness * eyeLblinkbone.constraints['Limit Location'].max_z
        eyeRblinkbone.location[2] = input_eye_closedness * eyeRblinkbone.constraints['Limit Location'].max_z

    def post_frame(off, pub, anim, cam):
        # Called every after Blender finished processing a frame.
        # Will be sent to one of the remote dataset listener connected.
        pub.publish(
            image=off.render(),
#             xy=cam.object_to_pixel(cube),
            frameid=anim.frameid
        )

    # Data source
    pub = btb.DataPublisher(btargs.btsockets['DATA'], btargs.btid)

    # Setup default image rendering
    cam = btb.Camera()
    off = btb.OffScreenRenderer(camera=cam, mode='rgb')
    off.set_render_style(shading='RENDERED', overlays=False)

    # Setup the animation and run endlessly
    anim = btb.AnimationController()
    anim.pre_frame.add(pre_frame)
    anim.post_frame.add(post_frame, off, pub, anim, cam)
    anim.play(frame_range=(0,100), num_episodes=-1, use_animation=False)

main()


text:
# Short script to confirm transferred file integrity using md5 checksums.
# Prerequisite is that checsum were generated using create_checksums.bat
# A comparsions of the sets of laz tiles and dtm tiles is also carried out.
# Jakob Assmann j.assmann@bios.au.dk 16 January 2020

# Imports
import os
import glob
import numpy as np
import pandas
import re
from dklidar import settings

### Read in filenames using unix filename extensions from glob
### 1) Pointcloud files (laz)
##orig_md5_files = glob.glob(settings.laz_folder + '*.md5')
##local_md5_files = glob.glob(settings.laz_folder + '*.local_md5')
### 2) DTM files (tif)
##orig_md5_files.extend(glob.glob(settings.dtm_folder + "*.md5"))
##local_md5_files.extend(glob.glob(settings.dtm_folder + "*.local_md5"))
##
### Initiate empty lists
##md5_orig = list()
##md5_local = list()
##
### Fill lists with md5 sums from files
##for file_name in orig_md5_files:
##    file = open(file_name)
##    md5_orig.append(file.read(32))
##for file_name in local_md5_files:
##    file = open(file_name)
##    md5_local.append(file.read(32))
##
### Zip all lists into one data frame
##df = pandas.DataFrame(zip(orig_md5_files, md5_orig, local_md5_files, md5_local),
##                      columns=['orig_file', 'orig_md5', 'local_file', 'local_md5'])
##
### Add md5_vheck comparison column to df
##md5_check = df['orig_md5'] == df['local_md5']
##df['md5_check'] = md5_check
##
### Print dataframe overiview to console
##print()
##print('df overview:')
##print(df.head())
##print()
##
### Filter rows where the check returned false
##print('Non matching md5 checksums:' + str(np.sum([not i for i in df['md5_check']])))
##damaged_files = df[df['md5_check'] == False]
##print(damaged_files)
##
### Export csv
##damaged_files.to_csv(settings.laz_folder + '../damaged_files.csv', index=False)
##
# ---------------------------------------------------
# Check for completeness of datasets

# Load file names
dtm_files = glob.glob(settings.dtm_folder + '/*.tif')
laz_files = glob.glob(settings.laz_folder + '/*.laz')

# initiate empty lists for tile_ids
dtm_tile_ids = []
laz_tile_ids = []

# fill dictionaries with tile_id, as well as row number and column number for each file name:
for file_name in dtm_files:
    tile_id = re.sub('.*DTM_1km_(\d*_\d*).tif', '\g<1>', file_name)
    dtm_tile_ids.append(tile_id)

for file_name in laz_files:
    tile_id = re.sub('.*PUNKTSKY_1km_(\d*_\d*).laz', '\g<1>', file_name)
    laz_tile_ids.append(tile_id)

# Determine differences between sets of tiles
missing_laz_tiles = set(dtm_tile_ids) - set(laz_tile_ids)
missing_dtm_tiles = set(laz_tile_ids) - set(dtm_tile_ids)

df_missing_dtm = pandas.DataFrame(zip(missing_dtm_tiles), columns=['tile_id'])
df_missing_dtm.to_csv(settings.dtm_folder + '../missing_dtm_tile_ids.csv', index=False)

# Print out a quick overview of data frame for control
print(df_missing_dtm.head())

df_missing_laz = pandas.DataFrame(zip(missing_laz_tiles), columns=['tile_id'])
df_missing_laz.to_csv(settings.laz_folder + '../missing_laz_tile_ids.csv', index=False)

# Print out a quick overview of data frame for control
print(df_missing_laz.head())

# Export lists to files
# DTMs with missing LAZs
out_file = open(settings.dtm_folder + '../dtm_files_with_missing_laz.txt', 'a+')
for tile_id in missing_laz_tiles:
    out_file.write(settings.dtm_folder + 'DTM_1km_' + tile_id + '.tif\n')
out_file.close()

# LAZs with missing DTMs
out_file = open(settings.laz_folder + '../laz_files_with_missing_dtm.txt', 'a+')
for tile_id in missing_dtm_tiles:
    out_file.write(settings.laz_folder  +'PUNKTSKY_1km_' + tile_id + '.laz\n')
out_file.close()

max_stars_repo_name: jakobjassmann/ecodes-dk-lidar

max_issues_repo_name: jakobjassmann/ecodes-dk-lidar

max_forks_repo_name: jakobjassmann/ecodes-dk-lidar


text:
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
import numpy as np

from models import ValueNetwork, PolicyNetwork

class DRTRPOAgent():
    """
    DR TRPO
    """
    def __init__(self, env, gamma, lr):
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        self.env = env
        self.obs_dim = env.observation_space.shape[0]
        self.action_dim = env.action_space.n

        self.gamma = gamma
        self.lr = lr

        self.value_network = ValueNetwork(self.obs_dim, 1)
        self.policy_network = PolicyNetwork(self.obs_dim, self.action_dim)

        self.value_optimizer = optim.Adam(self.value_network.parameters(), lr=self.lr)
        self.policy_optimizer = optim.Adam(self.policy_network.parameters(), lr=self.lr)

    def get_action(self, state):
        state = torch.FloatTensor(state).to(self.device)
        logits = self.policy_network.forward(state)
        dist = logits
        probs = Categorical(dist)
        return probs.sample().cpu().detach().item()

    def compute_adv_mc(self, trajectory):
        """
        Compute the advantage of all (st,at) in trajectory.
        The advantage is estimated using MC: i.e. discounted reward sum (from trajectory) - value (from NN)
        """
        states = torch.FloatTensor([sars[0] for sars in trajectory]).to(self.device)
        actions = torch.LongTensor([sars[1] for sars in trajectory]).view(-1, 1).to(self.device)
        rewards = torch.FloatTensor([sars[2] for sars in trajectory]).to(self.device)
        next_states = torch.FloatTensor([sars[3] for sars in trajectory]).to(self.device)
        dones = torch.FloatTensor([sars[4] for sars in trajectory]).view(-1, 1).to(self.device)

        # compute value target
        discounted_rewards = [torch.sum(torch.FloatTensor([self.gamma**i for i in range(rewards[j:].size(0))])\
             * rewards[j:]) for j in range(rewards.size(0))]
        value_targets = torch.FloatTensor(discounted_rewards).view(-1, 1).to(self.device)

        # compute value loss
        values = self.value_network.forward(states)
        value_loss = F.mse_loss(values, value_targets.detach())

        advantages = value_targets - values
        return advantages, value_loss

    def compute_adv_td(self, state, next_state, reward):
        """
        Compute the advantage of a single (s,a) using TD: i.e. r + v(s') - v(s) - depends highly on the accuracy of NN
        """
        state = torch.FloatTensor(state).to(self.device)
        next_state = torch.FloatTensor(next_state).to(self.device)
        reward = torch.as_tensor(reward)
        state_value = self.value_network.forward(state)
        next_state_value = self.value_network.forward(next_state)
        value_target = reward + next_state_value
        advantage = value_target - state_value
        value_loss = F.mse_loss(state_value, value_target)
        return advantage, value_loss

    def compute_policy_loss_kl(self, state, state_adv, beta):
        """
        Policy loss of DR TRPO (KL Constraint).
        """
        state = torch.FloatTensor(state).to(self.device)
        logits = self.policy_network.forward(state)
        pi_dist = logits
        state_adv = torch.FloatTensor(state_adv).to(self.device)
        denom = torch.sum(torch.exp(state_adv/beta)*pi_dist)
        new_pi_dist = torch.exp(state_adv/beta)*pi_dist/denom
        return F.mse_loss(pi_dist, new_pi_dist)

    def compute_policy_loss_wass(self, state, state_adv, beta):
        """
        Policy loss of DR TRPO (Wasserstein Constraint).
        """
        state = torch.FloatTensor(state).to(self.device)
        logits = self.policy_network.forward(state)
        pi_dist = logits
        state_adv = torch.FloatTensor(state_adv).to(self.device)

        """Find argmax_j {A(s,aj) - β*d(aj,ai)}."""
        best_j = []
        for i in range(self.action_dim):
            opt_j = 0
            opt_val = state_adv[opt_j] - beta*self.compute_distance(opt_j,i)
            for j in range(self.action_dim):
                cur_val = state_adv[j] - beta*self.compute_distance(j,i)
                if cur_val > opt_val:
                    opt_j = j
                    opt_val = cur_val
            best_j.append(opt_j)

        new_pi_dist = torch.zeros(self.action_dim)
        for j in range(self.action_dim):
            for i in range(self.action_dim):
                if j == best_j[i]:
                    new_pi_dist[j] += pi_dist[i]

        return F.mse_loss(pi_dist, new_pi_dist)

    def compute_distance(self, a1, a2):
        if a1 == a2:
            return 0
        else:
            return 1

    def update(self, value_loss, policy_loss):
        self.value_optimizer.zero_grad()
        value_loss.backward()
        self.value_optimizer.step()

        self.policy_optimizer.zero_grad()
        policy_loss.backward()
        self.policy_optimizer.step()

max_stars_repo_name: kadysongbb/dr-trpo

max_issues_repo_name: kadysongbb/dr-trpo

max_forks_repo_name: kadysongbb/dr-trpo


text:
# -*- coding: utf-8 -*-
# @Author  : admin
# @Time    : 2018/11/15
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
from copy import deepcopy
from os import path as osp

import numpy as np
import pickle
from datasets.dataset_factory import get_dataset
import random

import pickle
import cv2
import xarray as xr
import scipy.io as sio

def int_list(temp_list):
    temp = []
    for i in range(len(temp_list)):
        temp.append(int(temp_list[i]))
    return temp

def load_data_from_path_pose(path):
    mat = sio.loadmat(path)['matrix']
    return mat

def load_data_from_path(path, resolution = 64):
    cut_padding = int(float(resolution) / 64 * 10)
    return img2xarray(path[0], resolution)[:, :, cut_padding:-cut_padding].astype('float32') / 255.0

def img2xarray(filepath, resolution):
    # print(flie_path)
    # load data from a given every file path
    imgs = sorted(list(os.listdir(filepath)))
    frame_list = [np.reshape(
        cv2.resize(cv2.imread(osp.join(filepath, _img_path)), (64, 64), interpolation=cv2.INTER_CUBIC),
        [resolution, resolution, -1])[:, :, 0]
                  for _img_path in imgs
                  if osp.isfile(osp.join(filepath, _img_path))]
    num_list = list(range(len(frame_list)))
    data_dict = xr.DataArray(
        frame_list,
        coords={'frame': num_list},
        dims=['frame', 'img_y', 'img_x'],
    )
    return data_dict

def split_val_dataset(seq, date, label):

    temp_label = None
    index_list = 2*[]
    index_len = []
    k = 0
    j = 0
    for i in range(len(label)):
        if temp_label == None:
            temp_label = label[0]
            index_list += [0]
            j += 1

        elif temp_label == label[i]:
            index_list[k] += i
            j += 1
        else:
            temp_label = label[i]
            k += 1
            index_list += [i]
            index_len.append(j)
            j = 0

    seq_train = []
    date_train = []
    label_train = []

    seq_val = []
    date_val = []
    label_val = []

    for j in range(len(index_list)):
        if index_len[i] > 3:

            num = index_len[i] - int(index_len[i]/10) + 1

            seq_train += [seq[i] for i in index_list[j][:num]]
            date_train += [date[i] for i in index_list[j][:num]]
            label_train += [label[i] for i in index_len[j][:num]]

            seq_val += [seq[i] for i in index_list[j][num:]]
            date_val += [date[i] for i in index_list[j][num:]]
            label_val += [label[i] for i in index_len[j][num:]]
        else:
            seq_train += [seq[i] for i in index_list[j]]
            date_train += [date[i] for i in index_list[j]]
            label_train += [label[i] for i in index_len[j]]

    return seq_train, date_train, label_train, seq_val, date_val, label_val

def load_data(config, dataset_path, resolution, dataset, pid_num, cache, pid_shuffle=False):
    seq_dir = list()
    vID = list()
    label = list()
    print("Dataset path is ", dataset_path)
    # load all data from file path

    for _label in sorted(list(os.listdir(dataset_path))):
        label_path = osp.join(dataset_path, _label)        # 005
        for seqs in sorted(list(os.listdir(label_path))):
            seqs_path = osp.join(label_path, seqs)

            # skip the empty file
            if len(os.listdir(seqs_path)) == 0:
                print("empty file:", seqs_path)
                continue
            seq_dir.append([seqs_path])
            int_label = int(_label)
            label.append(int_label)
            vID.append(seqs)

    label_set = list(set(label))
    train_list, val_list = label_set[:pid_num], label_set[pid_num:]

    if pid_shuffle:
        np.random.shuffle(train_list)

    train_source = get_dataset(
        [seq_dir[i] for i, l in enumerate(label) if l in train_list],
        [vID[i] for i, l in enumerate(label) if l in train_list],
        [label[i] for i, l in enumerate(label) if l in train_list],
        config)

    # val_source = get_dataset(
    #     [seq_dir[i] for i, l in enumerate(label) if l in val_list],
    #     [vID[i] for i, l in enumerate(label) if l in val_list],
    #     [label[i] for i, l in enumerate(label) if l in val_list],
    #     config)

    return train_source, train_list

def load_probe_data(config, dataset_path, pid_shuffle=False):
    seq_dir = list()
    vID = list()
    label = list()
    print("path", dataset_path)
    # load all data from file path

    for seqs in sorted(list(os.listdir(dataset_path))):
        seqs_path = osp.join(dataset_path, seqs)

        # skip the empty file
        if len(os.listdir(seqs_path)) == 0:
            print("empty file:", seqs_path)
            continue
        seq_dir.append([seqs_path])
        int_label = int(0)
        label.append(int_label)
        vID.append(seqs)

    source = get_dataset(seq_dir, vID, label, config)

    return source, label

def load_gallery_data(config, dataset_path, pid_shuffle=False):
    seq_dir = list()
    vID = list()
    label = list()
    print("path", dataset_path)
    # load all data from file path

    for _label in sorted(list(os.listdir(dataset_path))):
        label_path = osp.join(dataset_path, _label)        # 005
        for seqs in sorted(list(os.listdir(label_path))):
            seqs_path = osp.join(label_path, seqs)

            # check if there is image?

            if len(os.listdir(seqs_path)) == 0:
                # print("no image in ", seqs_path)
                continue
            seq_dir.append([seqs_path])
            int_label = int(_label)
            label.append(int_label)
            vID.append(seqs)

    gallery_data = [seq_dir, vID, label]

    return gallery_data

def save_data_to_pickle(data, data_path):
    print("begin save data.")
    with open(data_path, 'wb') as file:
        pickle.dump(data, file)

    print("saving complete!!")

def load_all_data_frome_pickle(data_path):
    with open(data_path, 'rb') as file:
        data = pickle.load(file)
    print("load data complete!!")
    return data

def get_initial(config, train=False):
    print("Initializing data source...")
    train_source, train_list = load_data(config, config.data.dir, config.data.resolution, config.data.name, config.data.pid_num, cache=train)

    val_source = None

    if config.train.validation:
        val_path = "PATH_TO_VALIDATION_SET"
        val_source, val_list = load_data(config, val_path, config.data.resolution, config.data.name, config.data.pid_num, config.data.appendix, cache=train)

    if train:
        data_path = os.path.join(config.data.cache_path, config.data.name+"_"+"train.npy")

        if os.path.exists(data_path):  # if the pickle file exists, dirctly load data
            print("Loading training data from pickle")
            train_source = load_all_data_frome_pickle(data_path)

        else:
            print("Loading training data...")
            train_source.load_all_data()
            save_data_to_pickle(train_source, data_path)

    print("Data initialization complete.")
    return train_source, val_source, train_list

def get_gallery_data(test_dataset, test_gallery):
    seq_dir_gallery, vID_gallery, label_gallery = test_gallery[0], test_gallery[1], test_gallery[2]
    data_gallery = list()

    for i in range(len(label_gallery)):
        data_gallery.append(test_dataset.loader(seq_dir_gallery[i]))
    test_gallery = [data_gallery, vID_gallery, label_gallery]

    return test_gallery

def get_initial_test(config, train= False, test= True ):
    print("Initialzing test dataset...")
    # gallery
    gallery_data = load_gallery_data(config, config.test.gallery_dir)

    # probe
    test_probe_source, test_probe_list = load_probe_data(config, config.test.probe_dir)

    data_path = os.path.join(config.data.cache_path, config.data.name + "_" + "test.npy")

    if os.path.exists(data_path):  # if the pickle file exists, dirctly load data
        print("Loading testing data from pickle")
        test_probe_source = load_all_data_frome_pickle(data_path)

    else:
        print("Loading testing data...")
        test_probe_source.load_all_data()
        save_data_to_pickle(test_probe_source, data_path)

    test_gallery = get_gallery_data(test_probe_source, gallery_data)
    #
    print("len probe set = ", len(test_probe_source), ", len gallery set = ", len(test_gallery[2]))
    return test_probe_source,  test_gallery

reason: import numpy,import scipy


text:
import os
import sys

# Flask
from flask import Flask, redirect, url_for, request, render_template, Response, jsonify, redirect
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from PIL import Image
from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image

# Some utilites
import numpy as np
from util import base64_to_pil

# Declare a flask app
app = Flask(__name__)

# You can use pretrained model from Keras
# Check https://keras.io/applications/
#from keras.applications.mobilenet_v2 import MobileNetV2
#model = MobileNetV2(weights='imagenet')

#print('Model loaded. Check http://127.0.0.1:5000/')

# Model saved with Keras model.save()
MODEL_PATH = 'model.h5'

# Load your own trained model
model = load_model(MODEL_PATH)
#model._make_predict_function()          # Necessary
print('Model loaded. Start serving...')
print('Model loaded. Check http://127.0.0.1:5000/')

def model_predict(img, model):
    img = img.resize((224, )*2, resample=Image.LANCZOS)
    x = np.empty((1, 224, 224, 3), dtype=np.uint8);
    x[0, :, :, :] = np.array(img)
    y = model.predict(x)
    y = y > 0.5
    y = y.astype(int).sum(axis=1) - 1
    preds = y[0]
    return preds

@app.route('/', methods=['GET'])
def index():
    # Main page
    return render_template('index.html')

@app.route('/predict', methods=['GET', 'POST'])
def predict():
    if request.method == 'POST':
        # Get the image from post request
        img = base64_to_pil(request.json)

        # Save the image to ./uploads
        # img.save("./uploads/image.png")

        # Make prediction
        preds = model_predict(img, model)

        # Process your result for human
        if preds == 0:
            result = 'No DR'
        elif preds == 1:
            result = 'Mild'
        elif preds == 2:
            result = 'Moderate'
        elif preds == 3:
            result = 'Severe'
        else:
            result = 'Proliferative DR'
        # Serialize the result, you can add additional fields
        return jsonify(result=result)

    return None

if __name__ == '__main__':
    # app.run(port=5002, threaded=False)

    # Serve the app with gevent
    http_server = WSGIServer(('0.0.0.0', 5000), app)
    http_server.serve_forever()


text:
import cv2
from cv2 import imread
from keras.models import load_model
from PIL import Image
import numpy as np

"""
Live Predict
This script loads a pre-trained CNN model and classifies American Sign Language
finger spelling from from live camera input with no filtering on the camera frame
Signum: Software Design SP18 Final Project
Isaac Vandor, Utsav Gupta, Diego Berny
"""

#model = load(trained_model='model.h5')
model = load_model('models/model.h5')
#result = predict(trained_model=model, test_image=frame)
#'Data/OutputData/scaledoutput.jpg'
alphabets = {"A": 0, "B":1, "C": 2, "D":3, "E": 4, "F": 5,
            "G": 6, "H": 7, "I": 8, "J": 9, "K": 10, "L": 11,
            "M": 12, "N": 13, "O": 14, "P": 15, "Q": 16, "S": 17,
            "T": 18, "U": 19, "V": 20, "W": 21, "X": 22, "Y": 23}

#alphabet = find_alphabet(letter_list=result, letter_dict=alphabets)
#print("The alphabet is: " + alphabet)

capture_region_x=0.55  # roi x start point
capture_region_y=0.9  # roi y start point
size = 200

# ====================== Live loop ======================
# =======================================================
camera_input = int(input('Enter camera number: '))
video_capture = cv2.VideoCapture(camera_input)
video_capture.set(10,200)
#print("Press b to capture background & begin detection or r to reset")

while video_capture.isOpened():
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    frame = cv2.flip(frame, 1)  # flip the frame horizontally
    cv2.rectangle(frame, (int(capture_region_x * frame.shape[1]), 0),
                 (frame.shape[1], int(capture_region_y * frame.shape[0])), (255, 0, 0), 2)

    frame = cv2.resize(frame, (size, size))
    img = frame
    img = img.astype(np.float32)/255.0      # convert to float32
    img = img[:,:,::-1]         # convert from RGB to BGR

    result = model.predict(np.expand_dims(frame, axis=0))[0]

    letter_list=result
    letter_dict = alphabets
    idx = letter_list.argmax(axis=0)    # find the index of the biggest argument

    # look for the key corresponding to the biggest argument
    decoded = [key for key, value in letter_dict.items() if value == idx]
    alphabet = decoded[0]

    width = int(video_capture.get(3) + 0.25)
    height = int(video_capture.get(4) + 0.25)

    # Annotate image with most probable prediction
    cv2.putText(frame, text=alphabet,
                org=(width // 2 + 50, height // 2 + 50),
                fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                fontScale=10, color=(255, 255, 0),
                thickness=15, lineType=cv2.LINE_AA)

    result = predict(trained_model=model, test_image=frame)
    alphabet = find_alw4phabet(letter_list=result, letter_dict=alphabets)
    print("The alphabet is: " + alphabet)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    k = cv2.waitKey(10)
    if k == 27:  # press ESC to exit
        break
# Release the capture
video_capture.release()
cv2.destroyAllWindows()
0


text:
import numpy as np
import arcpy
arcpy.CheckOutExtension("Spatial")
import sys
import os
from arcpy.sa import *
import time

def SnapExtent(lExtent, lRaster):
	'''Returns a given extent snapped to the passed raster.

	Parameters
	----------
	lExtent : str
		ESRI ArcPy extent string.

	lRaster : str
		Path to raster dataset.

	Returns
	-------
	extent : str
		ESRI ArcPy extent string.
	'''
	pExtent = lExtent.split()
	extent = lExtent

	lt = ["rasterdataset","rasterband"]
	dsc = arcpy.Describe(lRaster)
	if dsc.DatasetType.lower() in lt:
		iCell = dsc.MeanCellWidth
		xmin = round(float(pExtent[0]) / iCell) * iCell
		ymin = round(float(pExtent[1]) / iCell) * iCell
		xmax = round(float(pExtent[2]) / iCell) * iCell
		ymax = round(float(pExtent[3]) / iCell) * iCell
		extent = str(xmin) + " " + str(ymin) + " " + str(xmax) + " " + str(ymax)

	return extent

def bathymetricGradient(workspace, snapGrid, hucPoly, hydrographyArea, hydrographyFlowline, hydrographyWaterbody,cellsize, version = None):
	'''Generates the input datasets from hydrography features for enforcing a bathymetic gradient in hydroDEM (bowling).

	Parameters
	----------
	workspace : str
		Path to the geodatabase workspace.
	snapGrid : str
		Path to the raster snap grid used for the project.
	hucPoly : str
		Path to the bounding polygon for the local folder for which inputs are generated.
	hydrographyArea : str
		Path to the double line stream features.
	hydrographyFlowline : str
		Path to the flowline features.
	hydrographyWaterbody : str
		Path to the waterbody features.
	cellsize : str
		Output cell size to use for rasterization.
	version : str (optional)
		Package version number.

	Returns
	-------
	hydro_flowlines : raster
		Grid representation of flowlines.
	hydro_areas : raster
		Grid representation of double line streams and flowlines.

	Notes
	-----
	Outputs are written to the workspace.
	'''
	if version:
		arcpy.AddMessage('StreamStats Data Preparation Tools version: %s'%(version))

	arcpy.env.overwriteOutput = True # Set script to overwrite if files exist
	arcpy.AddMessage("Starting Bathymetric Gradient Preparations....")

	# Set the Geoprocessing environment...
	arcpy.env.scratchWorkspace = workspace
	arcpy.env.workspace = workspace

	# test if input files are present
	inputFiles = [snapGrid, hucPoly, hydrographyArea, hydrographyFlowline, hydrographyWaterbody]
	for fl in inputFiles:
		if arcpy.Exists(fl) == False:
			arcpy.AddMessage('%s missing.'%fl)
			arcpy.AddMessage('Please supply required input. Stopping program.')
			sys.exit(0)

	# Setup local variables and temporary layer files
	arcpy.AddMessage("Setting up variables...")

	#temporary features
	nhd_flow_feat = "nhd_flow"
	nhd_flow_Layer = "nhd_flow_Layer"

	nhd_area_feat = "nhd_area"
	nhd_area_Layer = "nhd_area_Layer"

	nhd_wb_feat = "nhd_wb"
	nhd_wb_Layer = "nhd_wb_Layer"

	#Output rastsers
	wbtempraster = os.path.join(arcpy.env.workspace,"nhdwb_tmp")
	areatempraster = os.path.join(arcpy.env.workspace,"nhdarea_tmp")
	mosaiclist = wbtempraster + ";" + areatempraster
	outraster1 = "hydro_flowlines"
	outraster2 = "hydro_areas"

	#convert to temporary shapefiles
	arcpy.FeatureClassToFeatureClass_conversion(hydrographyArea, arcpy.env.workspace, nhd_area_feat)
	arcpy.AddField_management(nhd_area_feat,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
	arcpy.CalculateField_management(nhd_area_feat,"dummy","1", "PYTHON")

	arcpy.FeatureClassToFeatureClass_conversion(hydrographyWaterbody, arcpy.env.workspace, nhd_wb_feat)
	arcpy.AddField_management(nhd_wb_feat,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
	arcpy.CalculateField_management(nhd_wb_feat,"dummy","1", "PYTHON")

	arcpy.FeatureClassToFeatureClass_conversion(hydrographyFlowline, arcpy.env.workspace, nhd_flow_feat)
	arcpy.AddField_management(nhd_flow_feat,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
	arcpy.CalculateField_management(nhd_flow_feat,"dummy","1", "PYTHON")

	try:
		#hydrographyArea Processing
		arcpy.AddMessage("Creating temporary selection layers...")
		arcpy.MakeFeatureLayer_management(nhd_area_feat, nhd_area_Layer, "FType = 460", "", "")

		#hydrographyWaterbody Processing
		arcpy.MakeFeatureLayer_management(nhd_wb_feat, nhd_wb_Layer, "FType = 390 OR FType = 361", "", "")

		#hydrographyFlowline Processing
		arcpy.MakeFeatureLayer_management(nhd_flow_feat, nhd_flow_Layer, "", "", "")
		arcpy.SelectLayerByLocation_management(nhd_flow_Layer, "WITHIN", nhd_wb_Layer, "", "NEW_SELECTION")
		arcpy.SelectLayerByLocation_management(nhd_flow_Layer, "WITHIN", nhd_area_Layer, "", "ADD_TO_SELECTION")
	except:
		arcpy.AddMessage(arcpy.GetMessages())

	#get snap grid cell size
	dsc_snap = arcpy.Describe(snapGrid)
	snap_cellsize = dsc_snap.MeanCellWidth

	# Set raster processing parameters
	arcpy.AddMessage("Processing rasters...")
	dsc = arcpy.Describe(hucPoly)
	extent = str(dsc.extent)
	arcpy.env.cellSize = snap_cellsize
	arcpy.env.mask = snapGrid
	arcpy.env.extent = SnapExtent(extent, snapGrid)

	# Process: Feature to Raster1 - NHD Area...
	try:
		arcpy.SelectLayerByLocation_management(nhd_area_Layer, "INTERSECT", nhd_flow_Layer, "0", "NEW_SELECTION")
		arcpy.FeatureToRaster_conversion(nhd_area_Layer, "dummy", areatempraster, cellsize)
	except:
		arcpy.CreateRasterDataset_management(arcpy.env.workspace,"nhdarea_tmp","10","8_BIT_UNSIGNED",snapGrid)
		arcpy.AddMessage(arcpy.GetMessages())

	# Process: Feature to Raster2 - NHD Waterbody...
	try:
		arcpy.SelectLayerByLocation_management(nhd_wb_Layer, "INTERSECT", nhd_flow_Layer, "0", "NEW_SELECTION")
		arcpy.FeatureToRaster_conversion(nhd_wb_Layer, "dummy", wbtempraster, cellsize)
	except:
		arcpy.CreateRasterDataset_management(arcpy.env.workspace,"nhdwb_tmp","10","8_BIT_UNSIGNED",snapGrid)
		arcpy.AddMessage(arcpy.GetMessages())

	# Process: Feature to Raster3 - NHD Flowline.  This is the first output
	try:
		arcpy.FeatureToRaster_conversion(nhd_flow_Layer, "dummy", outraster1, cellsize)
	except:
		arcpy.AddMessage(arcpy.GetMessages())

	# Process: Mosaic NHD Area and NHD Waterbody rasters To New Raster.  This is the second output
	try:
		arcpy.MosaicToNewRaster_management(mosaiclist, workspace, outraster2, "", "8_BIT_UNSIGNED", "", "1", "BLEND", "FIRST")
	except:
		arcpy.AddMessage(arcpy.GetMessages())

	#Delete temp files and rasters
	arcpy.AddMessage("Cleaning up...")
	for fl in [areatempraster,wbtempraster,nhd_wb_feat,nhd_flow_feat,nhd_area_feat]:
		if arcpy.Exists(fl): arcpy.Delete_management(fl)

	arcpy.AddMessage("Done!")

def coastaldem(Input_Workspace, grdNamePth, InFeatureClass, OutRaster, seaLevel, version = None):
	'''Sets elevations for water and other areas in digital elevation model.

	Parameters
	----------
	Input_Workspace : str
		Input workspace, output raster will be written to this location.
	grdNamePth : str
		Path to the input DEM grid.
	InFeatureClass : str
		Path to the LandSea feature class.
	OutRaster : str
		Output DEM grid name.
	seaLevel : float
		Elevation at which to make the sea.
	version : str (optional)
		StreamStats Data Preparation Tools version number

	Returns
	-------
	OutRaster : raster
		Output raster with coastal areas corrected.

	Notes
	-----
	Outputs are written to the workspace.
	'''
	if version:
		arcpy.AddMessage('StreamStats Data Preparation Tools version: %s'%(version))

	try:
		# set working folder
		arcpy.env.workspace = Input_Workspace
		arcpy.env.scratchWorkspace = arcpy.env.workspace

		arcpy.env.extent = grdNamePth
		arcpy.env.snapRaster = grdNamePth
		arcpy.env.outputCoordinateSystem = grdNamePth
		arcpy.env.cellSize = grdNamePth

		# this is assuming land is 1 and sea is -1
		arcpy.PolygonToRaster_conversion(InFeatureClass, "Land", "mskg")

		mskg = Raster("mskg") # load the mask grid
		grdName = Raster(grdNamePth) # load the

		seag = Con(mskg == -1, float(seaLevel))

		landg = Con((mskg == 1) & (grdName <= 0), 1, grdName)

		nochgg = Con(mskg == 0, grdName)

		arcpy.MosaicToNewRaster_management([seag,landg,nochgg],arcpy.env.workspace,OutRaster,None, "32_BIT_SIGNED", None, 1, "FIRST") # mosaic and produce new raster

	except:
		e = sys.exc_info()[1]
		print(e.args[0])
		arcpy.AddError(e.args[0])

	return None

def hydrodem(outdir, huc8cov, origdemPth, dendrite, snap_grid, bowl_polys, bowl_lines, inwall, drainplug, buffdist, inwallbuffdist, inwallht, outwallht, agreebuf, agreesmooth, agreesharp, bowldepth, cellsz, scratchWorkspace, version = None):
	'''Hydro-enforce a DEM using hydrography data sets.

	This function is used by the National StreamStats Team as the optimal approach for preparing a state's physiographic datasets for watershed delineations. It takes as input, a digital elevation model (DEM), and enforces this data to recognize the supplied hydrography as correct. Supplied watershed boundaries can also be recognized as correct if available for a given state/region. This function assumes that the DEM has first been projected to a state's projection of choice. This function prepares data to be used in the ESRI ArcHydro data model (the GIS database environment for National StreamStats).

	Parameters
	----------
	outdir : DEworkspace
		Working directory.
	huc8cov : DEFeatureClass
		Local division feature class, often HUC8, this will be the outer wall of the hydroDEM.
	origdemPth : str
		Path to the original, projected DEM.
	dendrite : str
		Path to the dendrite feature class to be used.
	snap_grid : str
		Path to a raster dataset to use as a snap_grid to align all the watersheds, often the same as the DEM.
	bowl_polys : str
		Path to the bowling area raster generated from the bathymetric gradient tool.
	bowl_lines : str
		Path to the bowling line raster generated from the bathymetric gradient tool.
	inwall : str
		Path to the feature class to be used for inwalling.
	drainplug :
		Path to the feature class used for inserting sinks into the dataset.
	buffdist : float
		Distance to buffer the outer wall, same units as the projection.
	inwallbuffdist :
		Distance to buffer the inner walls, same units as the projection.
	inwallht :
		Inwall height, same units as the projection.
	outwallht :
		Inwall height, same units as the projection.
	agreebuf :
		AGREE function buffer distance, same units as the projection.
	agreesmooth :
		AGREE function smoothing distance, same units as the projection.
	agreesharp :
		AGREE function sharp distance, same units as the projection.
	bowldepth :
		Bowling depth, same units as the projection.
	cellsz :
		Cell size, same units as the projection.
	scratchWorkspace : str
		Path to scratch workspace.
	version : str (optional)
		Package version number.

	Returns (saved to outDIR)
	-------
	filldem : raster
		hydro-enforced DEM raster grid saved to outDir.
	fdirg : raster
		HydroDEM FDR raster grid saved to outDir.
	faccg : raster
		HydroDEM FAC raster grid saved to outDir.
	sink_path : feature class
		Sink feature class saved to outDir.
	'''
	strtTime = time.time()
	if version:
		arcpy.AddMessage('StreamStats Data Preparation Tools version: %s'%(version))

	arcpy.AddMessage("HydroDEM is running")

	## put some checks here about the _bypass variables
	dp_bypass = False
	iw_bypass = False
	bowl_bypass = False

	if inwall == None:
		iw_bypass = True

	if drainplug == None:
		dp_bypass = True

	if (bowl_polys == None) or (bowl_lines == None):
		bowl_bypass = True

	arcpy.AddMessage('bowl_bypass is %s'%str(bowl_bypass))
	arcpy.AddMessage('dp_bypass is %s'%str(dp_bypass))
	arcpy.AddMessage('iw_bypass is %s'%str(iw_bypass))

	# test if full path datasets exist
	for fl in [outdir,origdemPth,snap_grid,scratchWorkspace]:
		assert arcpy.Exists(fl) == True, "%s does not exist"%(fl)

	dsc = arcpy.Describe(snap_grid)
	assert dsc.extent.XMin % 1 == 0, "Snap Grid origin not divisible by 1."

	# set working directory and environment
	arcpy.env.workspace = outdir
	arcpy.env.cellSize = cellsz
	arcpy.env.overwriteOutput = True
	arcpy.env.scratchWorkspace = scratchWorkspace
	arcpy.env.outputCoordinateSystem = origdemPth
	arcpy.env.snapRaster = snap_grid

	# test if other datasets exist
	testDsets = [huc8cov,dendrite]

	# add datasets based on the bypass flags
	if not dp_bypass:
		testDsets.append(drainplug)

	if not iw_bypass:
		testDsets.append(inwall)

	if not bowl_bypass:
		testDsets.append(bowl_polys)
		testDsets.append(bowl_lines)

	for fl in testDsets:
		print(fl)

	for fl in testDsets:
		arcpy.AddMessage("Checking if %s exists."%(fl))
		assert arcpy.Exists(fl) == True, "%s does not exist"%(fl)

	tmpLocations = [] # make a container for temp locations that will be deleted at the end

	# buffer the huc8cov
	hucbuff = os.path.join(arcpy.env.workspace,'hucbuff') # some temp location
	tmpLocations.append(hucbuff)
	arcpy.AddMessage('	Buffering Local Divisons')
	arcpy.Buffer_analysis(huc8cov, hucbuff, buffdist) # do we need to buffer if this is done in the setup tool, maybe just pass hucbuff to the next step from the parameters...
	arcpy.AddField_management(hucbuff,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
	arcpy.CalculateField_management(hucbuff,"dummy","1", "PYTHON")

	arcpy.env.extent = hucbuff # set the extent to the buffered HUC

	# rasterize the buffered local division
	arcpy.AddMessage('	Rasterizing %s'%hucbuff)
	outGrid = os.path.join(arcpy.env.workspace,'hucbuffRast')
	tmpLocations.append(outGrid)
	arcpy.PolygonToRaster_conversion(hucbuff,"dummy",outGrid,cellsize = cellsz)

	# rasterize the dendrite
	arcpy.AddMessage('	Rasterizing %s'%dendrite)
	dendriteGridpth = os.path.join(arcpy.env.workspace,'tmpDendriteRast')
	tmpLocations.append(dendriteGridpth)

	# may need to add a field to dendrite to rasterize it...
	arcpy.AddField_management(dendrite,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
	arcpy.CalculateField_management(dendrite,"dummy","1", "PYTHON")
	arcpy.FeatureToRaster_conversion(dendrite,"dummy",dendriteGridpth, cell_size = cellsz)
	dendriteGrid = Raster(dendriteGridpth)

	origdem = Raster(origdemPth)

	arcpy.env.mask = outGrid # set mask (L169 in hydroDEM_work_mod.aml)

	elevgrid = agree(origdem, dendriteGrid, int(agreebuf), int(agreesmooth), int(agreesharp)) # run agree function

	# burning streams and adding walls
	arcpy.AddMessage('	Starting Walling') # (L182 in hydroDEM_work_mod.aml)

	ridgeNLpth = os.path.join(arcpy.env.workspace,'ridgeRast')
	tmpLocations.append(ridgeNLpth)
	# may need to add a field to huc8cov to rasterize it...
	arcpy.AddField_management(huc8cov,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
	arcpy.CalculateField_management(huc8cov,"dummy","1", "PYTHON")
	arcpy.FeatureToRaster_conversion(huc8cov,"dummy",ridgeNLpth,cell_size = cellsz) # rasterize the local divisions feature
	#ridgeEXP = 'some temp location'
	ridgeNL = Raster(ridgeNLpth) # load ridgeNL
	ridgeEXP = Expand(ridgeNL,2,[1]) # the last parameter is the zone to be expanded, this might need to be added to the dummy field above...

	ridgeW = SetNull((IsNull(ridgeNL) == 0) & (IsNull(ridgeEXP) == 0), ridgeEXP)
	demRidge8 = elevgrid + Con((IsNull(ridgeW) == 0) & (IsNull(dendriteGrid)), outwallht, 0)

	arcpy.AddMessage('	Walling Complete')

	if not dp_bypass: # (if bypass is false, as in do not bypass) dp_bypass is defined after the main code in the original AML
		if int(arcpy.GetCount_management(drainplug).getOutput(0)) > 0:
			dpg_path = os.path.join(arcpy.env.workspace,'sinklnk')
			#tmpLocations.append(dpg_path)
			#arcpy.AddField_management(drainplug,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
			#arcpy.CalculateField_management(drainplug,"dummy","1", "PYTHON")
			arcpy.FeatureToRaster_conversion(drainplug,"OBJECTID",dpg_path,cell_size = cellsz) # (L195 in hydroDEM_work_mod.aml)
			dpg = Raster(dpg_path) # load the raster object
		else:
			tmp = CreateConstantRaster(0) # if the feature class is empty, make a dummy raster
			dpg = SetNull(tmp,tmp,"VALUE = 0") # set all zeros to null.
	else: # if the drain pugs are bypassed
		arcpy.AddMessage("	Bypassing Drain Plugs")
		tmp = CreateConstantRaster(0) # if the feature class is empty, make a dummy raster
		dpg = SetNull(tmp,tmp,"VALUE = 0") # set all zeros to null.

	if not bowl_bypass: # (if bypass is false, as in do not bypass) bowl_bypass is defined after the main code in the original AML
		arcpy.AddMessage('	Starting Bowling')
		blp_name = os.path.join(arcpy.env.workspace,'blp')
		tmpLocations.append(blp_name)

		bowlLines = Raster(bowl_lines)

		#arcpy.MosaicToNewRaster_management([bowlLines,dpg],arcpy.env.workspace,blp_name, None, "32_BIT_SIGNED", None, 1, "FIRST") # probably need some more options
		#blp = Raster(blp_name)
		#eucd = SetNull(IsNull(bowl_polys), EucDistance(blp)) # (L210 in hydroDEM_work_mod.aml)

		eucd = SetNull(IsNull(bowl_polys), EucDistance(bowlLines)) # (L210 in hydroDEM_work_mod.aml)
		demRidge8wb = demRidge8 - Con(IsNull(eucd) == 0, (bowldepth / (eucd+1)), 0)
		#demRidge8wb.save(os.path.join(arcpy.env.workspace,'demRidge8wb'))
		arcpy.AddMessage('	Bowling complete')

	else: # if bypass is true, skip
		demRidge8wb = demRidge8
		arcpy.AddMessage('	Bowling Skipped')

	if not iw_bypass:
		arcpy.AddMessage('	Starting Inwalling')
		iwb_name = os.path.join(arcpy.env.workspace,'tmp_inwall_buff')
		tmpLocations.append(iwb_name)
		if arcpy.Exists(iwb_name):
			arcpy.AddMessage("%s exists, please delete or rename before proceeding."%(iwb_name))
		arcpy.Buffer_analysis(inwall,iwb_name,inwallbuffdist) #(L223 in hydroDEM_work_mod.aml)

		tmpGrd_name = os.path.join(arcpy.env.workspace,'tmpGrd')
		tmpLocations.append(tmpGrd_name)

		arcpy.AddField_management(iwb_name,"dummy","SHORT",None,None,None,None,"NULLABLE","NON_REQUIRED",None)
		arcpy.CalculateField_management(iwb_name,"dummy","1", "PYTHON")

		arcpy.FeatureToRaster_conversion(iwb_name,"dummy",tmpGrd_name, cell_size = cellsz)
		tmpGrd = Raster(tmpGrd_name)

		# Only inwalls where there are not streams and there are inwalls.
		dem_enforced = demRidge8wb + Con((IsNull(tmpGrd) == 0) & (IsNull(dendriteGrid)), inwallht, 0) #(L226 in hydroDEM_work_mod.aml)
		arcpy.AddMessage('	Inwalling Complete')
	else:
		#if arcpy.Exists(dem_enforced):
		#	del dem_enforced
		dem_enforced = demRidge8wb
		arcpy.AddMessage('	Inwalling Skipped')

	if not dp_bypass:
		detmp = Con(IsNull(dpg),dem_enforced)
		del dem_enforced
		dem_enforced = detmp #(L242 in hydroDEM_work_mod.aml)

	arcpy.env.extent = ridgeEXP
	arcpy.env.mask = ridgeEXP # mask to HUC
	arcpy.cellSize = origdem

	dem_enforced.save(os.path.join(arcpy.env.workspace,'dem_enforced'))

	arcpy.AddMessage("	Starting Fill")
	filldem = Fill(dem_enforced,None)
	fdirg2 = FlowDirection(filldem, 'FORCE') # this works...
	arcpy.AddMessage("	Fill Complete")

	# set the mask and extent for the FAC and FDR grids, which should be clipped to the huc bounding polygon.
	arcpy.env.extent = huc8cov
	arcpy.env.mask = huc8cov

	if not dp_bypass:
		fdirg = Int(Con(IsNull(dpg) == 0, 0, fdirg2)) # (L256 in hydroDEM_work_mod.aml), insert a zero where drain plugs were.
	else:
		fdirg = Int(fdirg2)

	# might need to save the fdirg, delete it from the python workspace, and reload it...
	arcpy.AddMessage('	Starting Flow Accumulation')
	faccg = FlowAccumulation(fdirg, None, "INTEGER")
	arcpy.AddMessage('	Flow Accumulation Complete')

	arcpy.AddMessage('	Creating Sink Features')
	fsinkg = Con((filldem - origdem) > 1, 1)

	fsinkc_name = 'fsinkc'
	arcpy.RasterToPolygon_conversion(fsinkg, fsinkc_name, 'NO_SIMPLIFY') # (L273 in hydroDEM_work_mod.aml), outputs fsinkc
	del fsinkg

	arcpy.AddMessage('	Sink Creation Complete')

	# save the grids to the workspace
	filldem.save(os.path.join(arcpy.env.workspace,"hydrodem"))
	del filldem

	fdirg.save(os.path.join(arcpy.env.workspace,"fdr"))
	del fdirg
	faccg.save(os.path.join(arcpy.env.workspace,"fac"))
	del faccg

	# clean the environment of temp files
	for fl in tmpLocations: # delete tmp files
		if arcpy.Exists(fl):
			try:
				arcpy.Delete_management(fl)
			except:
				arcpy.AddMessage("Failed to Delete: %s"%fl)

	totalTime = time.time() - strtTime
	arcpy.AddMessage('HydroDEM Complete, %s minutes.'%(totalTime/60.))

	return None

def agree(origdem, dendrite, agreebuf, agreesmooth, agreesharp):
	'''Function to adjust a DEM to match a vector.

	Parameters
	----------
	origdem : Raster Object
		Original DEM with the desired cell size.
	dendrite : Raster Object
		Dendrite feature layer to adjust the DEM.
	agreebuf : float
		Buffer smoothing distance (same units as horizontal map units).
	agreesmooth : float
		Smoothing distance (same units as the vertical map units).
	agreesharp : float
		Distance for sharp feature (same units as the vertical map units).

	Returns
	-------
	elevgrid : Raster Object
		Conditioned elevation grid.

	Notes
	-----
	Original function by Ferdi Hellweger, http://www.ce.utexas.edu/prof/maidment/gishydro/ferdi/research/agree/agree.html
	'''
	arcpy.AddMessage('	Starting AGREE')

	# code to check that all inputs exist

	cellsize = (float(arcpy.GetRasterProperties_management(origdem, "CELLSIZEX").getOutput(0)) + float(arcpy.GetRasterProperties_management(origdem, "CELLSIZEY").getOutput(0)))/2. # compute the raster cell size

	arcpy.AddMessage('		Setting Environment Variables')
	arcpy.env.extent = origdem # (L130 AGREE.aml)
	arcpy.env.cellSize = cellsize # (L131 AGREE.aml)
	arcpy.env.snapRaster = origdem

	#arcpy.AddMessage('	Rasterizing the Dendrite.')
	#tmpLocations = []
	#dendriteGridPth = os.path.join(arcpy.env.workspace,'tmpDendrite') # might need to add a field for rasterization
	#tmpLocations.append(dendriteGridPth)
	#arcpy.AddField_management

	#arcpy.FeaturetoRaster_conversion(dendrite,dendriteGridPth)

	arcpy.AddMessage('		Computing smooth drop/raise grid...')
	# expression = 'int ( setnull ( isnull ( vectgrid ), ( \"origdem\" + \"greesmooth\" ) ) )'

	#dendriteGrid = Raster(dendriteGridPth)
	#origdem = Raster(origdem)

	smogrid = Int(SetNull(IsNull(dendrite), (origdem + agreesmooth))) # compute the smooth drop/raise grid (L154 in AGREE.aml)

	arcpy.AddMessage('		Computing vector distance grids...')
	vectdist = EucDistance(smogrid)
	# Need to produce vectallo (stores the elevation of the closest vector cell), is this the same as the smogrid?
	vectallo = EucAllocation(smogrid) # Roland Viger thinks the original vectallo is an allocation grid, that can be made with EucAllocation.

	arcpy.AddMessage('		Computing buffer grids...')
	bufgrid1 = Con((vectdist > (agreebuf - (cellsize / 2.))), 1, 0)
	bufgrid2 = Int(SetNull(bufgrid1 == 0, origdem)) # (L183 in AGREE.aml)

	arcpy.AddMessage('		Computing buffer distance grids...')
	# compute euclidean distance and allocation grids
	bufdist = EucDistance(bufgrid2)
	bufallo = EucAllocation(bufgrid2)

	arcpy.AddMessage('		Computing smooth modified elevation grid...')
	smoelev =  vectallo + ((bufallo - vectallo) / (bufdist + vectdist)) * vectdist

	arcpy.AddMessage('		Computing sharp drop/raise grid...')
	#shagrid = int ( setnull ( isnull ( vectgrid ), ( smoelev + %sharpdist% ) ) )
	shagrid = Int(SetNull(IsNull(dendrite), (smoelev + agreesharp)))

	arcpy.AddMessage('		Computing modified elevation grid...')
	elevgrid = Con(IsNull(dendrite), smoelev, shagrid)

	arcpy.AddMessage('	AGREE Complete')

	return elevgrid

def adjust_accum(facPth, fdrPth, upstreamFACpths,upstreamFDRpths, workspace, version = None):
	'''Adjust a downstream flow accumulation (FAC) raster based on upstream flow accumulation rasters.

	This function adjusts the FAC of a downstream HUC to include flow accumulations from upstream HUCs. Run this from the downstream HUC workspace. The function will leave the original FAC grids intact and will create a grid named "fac_global" in the same directory as the original FAC raster. To get true accumulation values in HUCs downstream of other non-headwater HUCs, proceed from upstream HUCs to downstream HUCs in order, and specify the fac_global grid for any upstream HUC that has one. (It is not essential that the fac_global contain true global fac values, and in some cases it is not possible since the values get too large to be stored in a raster file. In practice, as long as the receiving cells have accumulation values larger than the stream definition threshold (150,000 cells for 10-m grids), then the ESRI ArcHydro data model will still function.

	Parameters
	----------
	facPth : str
		Path to downstream flow accumulation grid.
	fdrPth : str
		Path to downstream flow direction grid.
	upstreamFACpths : list
		List of paths to upstream flow accumulation grids.
	upstreamFDRpths : list
		List of paths to upstream flow direction grids.
	workspace : str
		local geodatabase to work in.
	version : str (optional)
		Stream Stats datapreptool version number.

	Returns
	-------
	facGlobal : raster
		Adjusted flow accumulation raster created in the same directory as fac.

	Examples
	--------
	adjust_accum("./01010001/fac", 2, ["./01010002/fac", "./01010003/fac"])
	'''
	if version:
		arcpy.AddMessage('StreamStats Data Preparation Tools version: %s'%(version))

	arcpy.AddMessage("Preparing environment.")
	arcpy.env.workspace = workspace
	arcpy.env.scratchWorkspace = workspace

	#test that everything exists
	assert arcpy.Exists(facPth), "Raster %s does not exist"%(facPth)

	assert arcpy.Exists(fdrPth), "Raster %s does not exist"%(fdrPth)

	for fl in upstreamFACpths:
		assert arcpy.Exists(fl), "Raster %s does not exist"%(fl)

	for fl in upstreamFDRpths:
		assert arcpy.Exists(fl), "Raster %s does not exist"%(fl)

	# load the upstream rasters into a structure
	upstreamFACs = []
	upstreamFDRs = []
	for fac,fdr in zip(upstreamFACpths,upstreamFDRpths):
		upstreamFACs.append(Raster(fac))
		upstreamFDRs.append(Raster(fdr))

	downstream = Raster(facPth) # load the downstream raster
	downstreamFDR = Raster(fdrPth)

	# get raster cell dimensions
	dsc = arcpy.Describe(downstream)
	dx = dsc.children[0].MeanCellWidth
	dy = dsc.children[0].MeanCellHeight

	arcpy.AddMessage("Processing upstream rasters...")
	costPaths = []
	for fac,fdr in zip(upstreamFACs,upstreamFDRs): # iterate through the rasters
		arcpy.env.extent = fac
		arcpy.env.cellSize = fac
		arcpy.env.overwriteOutput = True

		loc = Con(fac == fac.maximum,fdr) # make a locator raster of the outlet of the outlet
		#flowDir = int(loc.maximum) # get the flow direction of the selected cell

		# now get the location of the cell...
		arcpy.RasterToPoint_conversion(loc,"pt") # conver to feature class
		with arcpy.da.SearchCursor("pt",["grid_code","SHAPE@X","SHAPE@Y"]) as cursor: # read the data
			with arcpy.da.UpdateCursor("pt",["SHAPE@X","SHAPE@Y"]) as updateCursor: # update the data
				for row, uprow in zip(cursor,updateCursor):
					# extract coordinates
					flowDir = row[0]
					x = row[1]
					y = row[2]

					# figure out the correction
					if flowDir == 1: # east
						xCoor = dx
						yCoor = 0
					elif flowDir == 2: # southeast
						xCoor = dx
						yCoor = dy*-1
					elif flowDir == 4: # south
						xCoor = 0
						yCoor = dy * -1
					elif flowDir == 8: # southwest
						xCoor = dx
						yCoor = dy * -1
					elif flowDir == 16: # west
						xCoor = dx * -1
						yCoor = 0
					elif flowDir == 32: # northwest
						xCoor = dx * -1
						yCoor = dy
					elif flowDir == 64: # north
						xCoor = 0
						yCoor = dy
					elif flowDir == 128: # northeast
						xCoor = dx
						yCoor = dy

					# update their position
					x += xCoor
					y += yCoor

					# insert back into feature class
					uprow[0] = x
					uprow[1] = y
					updateCursor.updateRow(uprow)

		# now trace the least cost downstream from the point
		arcpy.env.extent = downstream
		arcpy.env.cellSize = downstream

		ones = Con(IsNull(downstream) == 0,1) # make a constant raster

		#ones.save("constant")
		costPth = CostPath("pt",ones,downstreamFDR,path_type = "EACH_CELL") # trace path and append to list

		tmp = Con(IsNull(costPth)==0,fac.maximum,0)
		#tmp.save("costPath")
		costPaths.append(tmp) # attribute the cost path with the fac max value, all the cost paths will be added together later.

		if arcpy.Exists("pt"): arcpy.Delete_management("pt") # clean up

	# now that all cost paths have been generatate, sum them with the downstream FAC gid to get the final FAC grid.
	arcpy.AddMessage("Correcting downstream FAC.")
	arcpy.env.extent = downstream
	arcpy.env.cellSize = downstream

	for pth in costPaths:
		downstream += pth

	downstream.save("fac_global")

def adjust_accum_simple(ptin, fdrin, facin, filin, facout, incrval, version=None):
	'''Simple flow accumulation grid adjustment.

	Adds a value to the flow accumulation grid given an input point using a least-cost-path to cascade down through the flow direction grid.

	Parameters
	----------
	ptin : str (feature class)
		Point feature class representing one inlet to the downstream DEM.
	fdrin : str (raster)
		Flow direction raster.
	facin : str (raster)
		Name of the flow accumulation raster.
	filin : str (raster)
		Burned DEM to use as cost surface.
	facout : str (raster)
		Output name of adjusted FAC grid.
	incrval : int
		Value to adjust the downstream FAC grid by.
	version : str
		Stream Stats version number.

	Returns
	-------
	hydrodemfac_global : raster
		Adjusted FAC grid written to facout.

	'''

	if version:
		arcpy.AddMessage('StreamStats Data Preparation Tools version: %s'%(version))

	# check that everything exists
	for fl in [ptin, facin, filin]:
		assert arcpy.Exists(fl) == True, "%s does not exist."%(fl)

	arcpy.env.workspace = os.path.dirname(facin) # set workspace
	arcpy.env.scratchWorkspace = arcpy.env.workspace
	arcpy.env.snapRaster = fdrin
	arcpy.env.outputCoordinateSystem = fdrin
	arcpy.env.extent = fdrin
	arcpy.env.overwriteOutput = True

	if sys.version_info[0] >= 3:
		arcpy.AddMessage("\tComputing least-cost-path.")
		costPth = CostPath(ptin,filin,fdrin,path_type = "EACH_CELL", force_flow_direction_convention = "FLOW_DIRECTION", destination_field = "OBJECTID") # compute least cost path downstream from inlet point.
	else:
		arcpy.AddMessage("\tComputing least-cost-path.")
		costPth = CostPath(ptin,filin,fdrin,path_type = "EACH_CELL", destination_field = "OBJECTID")

	c = Con(costPth, incrval) # convert the cost path to the increase value
	c.save('costPath')
	c1 = Con(IsNull(c),0,incrval) # fill with zeros
	arcpy.AddMessage("\tComputing correction raster.")

	FAC = Raster(facin) # load the FAC raster to be corrected
	correction = Con(IsNull(FAC),FAC,c1) # fill the edges with NoData
	correction.save('corr')
	arcpy.AddMessage("\tApplying corretion raster.")
	corrFAC = FAC + correction # add the correction, hopefully this doesn't overwrite no data values on the FAC grid.

	corrFAC.save(facout) # save the output raster
	arcpy.AddMessage("\tDone!")
	return None

def postHydroDEM(workspace, facPth, fdrPth, thresh1, thresh2, sinksPth = None, version = None):
	'''Generate stream reaches, adjoint catchments, and drainage points

	Parameters
	----------
	workspace : str
		database-type workspace to output rasters and feature classes.
	facPth : str
		Path to the flow accumulation grid produced by hydroDEM.
	fdrPth : str
		Path to the flow direction grid produced by hydroDEM.
	thresh1 : int
		Threshold used to produce the str grid, in raster cells, usually equal to 15,000,000 :math:`m^2`.
	thresh2 : int
		Threshold used to produce the str900 grid or similar, in raster cells, usually equal to 810,000 :math:`m^2`.
	sinksPth : str (optional)
		Path to the snklnk grid, optional.
	version : str (optional)
		StreamStats DataPrepTools version to be printed.

	Returns
	-------
	str : raster
		Stream raster where fac > 15,000,000 :math:`m^2`.
	str<thresh2> : raster
		Stream raster where fac > 810,000 :math:`m^2`.
	strlnk : raster
		Raster with streams labeled with index values.
	lnk : raster
		Merged stream and sink raster.
	cat : raster
		Catchment raster.
	drainageLine : feature class
		Vectorized streams.
	catchment : feature class
		Vectorized catchments.
	adjointCatchment : feature class
		Vectorized catchments for use in delineation.
	drainagePoint : feature class
		Point located at the greatest flow accumulation value in each catchment.

	Notes
	-----
	This tool requires ESRI ArcHydro to be installed and currently only works with Python 2.
	'''

	if version:
		arcpy.AddMessage('StreamStats Data Preparation Tools version: %s'%(version))

	pyVer = int(sys.version[:1])

	if pyVer == 3: # for python 3
		arcpy.AddMessage("This tool only runs in Python 2....")
		sys.exit(0)
		#from archydro.streamdefinition import StreamDefinition
		#from archydro.streamsegmentation import StreamSegmentation
		#from archydro.drainagelineprocessing import DrainageLineProcessing
		#from archydro.combinestreamlinkandsinklink import CombineStreamLinkandSinkLink
		#from archydro.catchmentgriddelineation import CatchmentGridDelineation
		#from archydro.catchmentpolygonprocessing import CatchmentPolygonProcessing
		#from archydro.adjointcatchmentprocessing import AdjointCatchmentProcessing
		#from archydro.drainagepointprocessing import DrainagePointProcessing
	else: # for python 2
		#from ArcHydroTools import StreamDefinition
		#from ArcHydroTools import StreamSegmentation
		from ArcHydroTools import DrainageLineProcessing
		from ArcHydroTools import CombineStreamLinkAndSinkLink
		from ArcHydroTools import CatchmentGridDelineation
		from ArcHydroTools import CatchmentPolyProcessing
		from ArcHydroTools import AdjointCatchment
		from ArcHydroTools import DrainagePointProcessing

	arcpy.AddMessage("Starting Post HydroDEM Processing.")

	arcpy.env.extent = facPth
	arcpy.env.snapRaster = facPth
	arcpy.env.cellsize = facPth
	arcpy.env.overwriteOutput = True
	arcpy.AddMessage("	Environment set.")

	finalSpace = os.path.split(workspace)[0]

	fac = Raster(facPth)
	# generate the str900 grid
	str900Pth = os.path.join(finalSpace,'str'+str(thresh2))
	str900 = Con(fac > thresh2,1,None)
	str900.save(str900Pth)
	arcpy.AddMessage("	str%s created."%(str(thresh2)))

	# generate the str grid
	streamPth = os.path.join(finalSpace,'str')
	stream = Con(fac > thresh1,1,None)
	stream.save(streamPth)
	arcpy.AddMessage("	str raster created.")

	# generate the stream link grid
	if sinksPth != None:
		lnkPth = os.path.join(finalSpace,'strlnk')
	else:
		lnkPth = os.path.join(finalSpace, 'lnk')

	lnk = StreamLink(stream,fdrPth)
	lnk.save(lnkPth)
	arcpy.AddMessage("	lnk raster created.")

	del lnk
	del stream

	sr = arcpy.Describe(facPth).spatialReference
	arcpy.CreateFeatureDataset_management(workspace,'Layers',sr) # create featureDataset

	# Drainage Line
	drainLinePth = os.path.join(workspace,'drainageLine_tmp')
	DrainageLineProcessing(lnkPth,fdrPth,drainLinePth)
	arcpy.Copy_management(drainLinePth, os.path.join(workspace,'Layers','drainageLine')) # import drainage line into feature dataset
	arcpy.AddMessage("	DrainageLine features created.")

	if sinksPth != None: # combine sink link and stream link if sink link exists
			newlnkPth = os.path.join(finalSpace,'lnk')
			CombineStreamLinkAndSinkLink(lnkPth,sinksPth,newlnkPth)
			lnkPth = newlnkPth
			arcpy.AddMessage("	snklnk merged with lnk raster.")

	catPth = os.path.join(finalSpace,'cat')
	CatchmentGridDelineation(fdrPth,lnkPth,catPth)
	arcpy.AddMessage("	Cat raster created.")

	catchmentPth = os.path.join(workspace,'catchment_tmp')
	CatchmentPolyProcessing(catPth,catchmentPth)
	arcpy.Copy_management(catchmentPth, os.path.join(workspace,'Layers','catchment'))
	arcpy.AddMessage("	Catchment features created.")

	adjointPth = os.path.join(workspace,'adjointCatchment_tmp')
	AdjointCatchment(drainLinePth, catchmentPth,adjointPth)
	arcpy.Copy_management(adjointPth,os.path.join(workspace,'Layers','adjointCatchment'))
	arcpy.AddMessage("	AdjointCatchment features created.")

	dpPth = os.path.join(workspace,'drainagePoint_tmp')
	DrainagePointProcessing(facPth,catPth, catchmentPth,dpPth)
	arcpy.Copy_management(dpPth, os.path.join(workspace,'Layers','drainagePoint'))
	arcpy.AddMessage("	DrainagePoint features created.")

	arcpy.AddMessage("  Cleaning Up.")
	arcpy.Delete_management(drainLinePth) # clean up
	arcpy.Delete_management(adjointPth)
	arcpy.Delete_management(catchmentPth)
	arcpy.Delete_management(dpPth)

	#arcpy.AddMessage("	Moving rasters out of\n\n%s\n\nto\n\n%s"%(workspace,finalSpace))
	rasters = ['hydrodem','fac','fdr', 'hydrodemfac_global']

	moveRasters(workspace,finalSpace,rasters)

def moveRasters(source, dest, rasters, fmt = None):
	''' Move raster out of a working geodatabase to a destination folder.

	Parameters
	----------
	source : str
		Path to geodatabase containing the rasters.
	dest : str
		Path to destination location.
	rasters : list
		List of rasters to move from source to dest.
	fmt : str (optional)
		Extension indicating the raster format the output without the leading period, e.g. "tif".

	Returns
	-------
	None
	'''

	arcpy.AddMessage("		Moving %s rasters from:"%(len(rasters)))
	arcpy.AddMessage("\n")
	arcpy.AddMessage("		%s"%source)
	arcpy.AddMessage("\n")
	arcpy.AddMessage("		To:")
	arcpy.AddMessage("\n")
	arcpy.AddMessage("		%s"%dest)
	for rast in rasters:
		arcpy.AddMessage("			%s"%rast)

	arcpy.AddMessage("		######################")
	arcpy.AddMessage("		######################")

	for rast in rasters:
		if arcpy.Exists(os.path.join(source,rast)):

			if fmt == None and len(rast) > 13: # if unformatted, truncate name to 13 characters.
				outRast = rast[0:13] # keep original format
			else:
				outRast = rast

			if fmt != None:
				outRast = "%s.%s"%(rast,fmt)

			arcpy.AddMessage("		Moving %s"%rast)
			tmp = Raster(os.path.join(source,rast))
			tmp.save(os.path.join(dest,outRast))
			del tmp
		else:
			arcpy.AddMessage("		%s not found!"%rast)

max_stars_repo_name: usgs/water-datapreptools

max_issues_repo_name: usgs/water-datapreptools

max_forks_repo_name: usgs/water-datapreptools


text:
import cv2 as cv
import numpy as np
import sys
import ctypes
def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)

img1 = cv.imread("../Data/lena.jpg")
cv.imshow("img1", img1)
img1 = img1.astype(np.float32)
shift = np.array([5., 5.])
mapTest = cv.reg_MapShift(shift)

img2 = mapTest.warp(img1)

mapper = cv.reg_MapperGradShift()
mappPyr = cv.reg_MapperPyramid(mapper)

resMap = mappPyr.calculate(img1, img2)
mapShift = cv.reg.MapTypeCaster_toShift(resMap)

test = mapShift.getShift()

str = "The original image has been shifted 5 to the right and 5 down. \n"
str += "Then image registration was used to compute the shift. \n"
str += "\n\nResults:\t%.6f to the right and \t%.6f down.\n" % (test[0], test[1])
Mbox('reg_shift', str, 1)