text:
from __future__ import annotations

import datetime
import decimal
import enum
import itertools
import uuid
from operator import attrgetter

import numpy as np
import pandas as pd
from public import public

from ibis.common import exceptions as com
from ibis.common.validators import immutable_property
from ibis.expr import datatypes as dt
from ibis.expr import rules as rlz
from ibis.expr import types as ir
from ibis.expr.operations.core import Node, UnaryOp, ValueOp, distinct_roots
from ibis.util import frozendict

try:
    import shapely
except ImportError:
    BaseGeometry = type(None)
else:
    BaseGeometry = shapely.geometry.base.BaseGeometry

@public
class TableColumn(ValueOp):
    """Selects a column from a `TableExpr`."""

    table = rlz.table
    name = rlz.instance_of((str, int))

    output_shape = rlz.Shape.COLUMNAR

    def __init__(self, table, name):
        schema = table.schema()

        if isinstance(name, int):
            name = schema.name_at_position(name)

        if name not in schema:
            raise com.IbisTypeError(
                f"value {name!r} is not a field in {table.columns}"
            )

        super().__init__(table=table, name=name)

    def parent(self):
        return self.table

    def resolve_name(self):
        return self.name

    def has_resolved_name(self):
        return True

    def root_tables(self):
        return self.table.op().root_tables()

    @property
    def output_dtype(self):
        return self.table._get_type(self.name)

@public
class RowID(ValueOp):
    """The row number (an autonumeric) of the returned result."""

    output_shape = rlz.Shape.COLUMNAR
    output_dtype = dt.int64

    def resolve_name(self):
        return 'rowid'

    def has_resolved_name(self):
        return True

@public
def find_all_base_tables(expr, memo=None):
    if memo is None:
        memo = {}

    node = expr.op()

    if isinstance(expr, ir.TableExpr) and node.blocks():
        if expr not in memo:
            memo[node] = expr
        return memo

    for arg in expr.op().flat_args():
        if isinstance(arg, ir.Expr):
            find_all_base_tables(arg, memo)

    return memo

@public
class TableArrayView(ValueOp):

    """
    (Temporary?) Helper operation class for SQL translation (fully formed table
    subqueries to be viewed as arrays)
    """

    table = rlz.table

    output_shape = rlz.Shape.COLUMNAR

    @property
    def output_dtype(self):
        return self.table._get_type(self.name)

    @property
    def name(self):
        return self.table.schema().names[0]

@public
class Cast(ValueOp):
    """Explicitly cast value to a specific data type."""

    arg = rlz.any
    to = rlz.datatype

    output_shape = rlz.shape_like("arg")
    output_dtype = property(attrgetter("to"))

    # see #396 for the issue preventing an implementation of resolve_name

@public
class TypeOf(UnaryOp):
    output_dtype = dt.string

@public
class IsNull(UnaryOp):
    """Return true if values are null.

    Returns
    -------
    ir.BooleanValue
        Value expression indicating whether values are null
    """

    output_dtype = dt.boolean

@public
class NotNull(UnaryOp):
    """Returns true if values are not null

    Returns
    -------
    ir.BooleanValue
        Value expression indicating whether values are not null
    """

    output_dtype = dt.boolean

@public
class ZeroIfNull(UnaryOp):
    output_dtype = rlz.dtype_like("arg")

@public
class IfNull(ValueOp):
    """
    Equivalent to (but perhaps implemented differently):

    case().when(expr.notnull(), expr)
          .else_(null_substitute_expr)
    """

    arg = rlz.any
    ifnull_expr = rlz.any

    output_dtype = rlz.dtype_like("args")
    output_shape = rlz.shape_like("args")

@public
class NullIf(ValueOp):
    """Set values to NULL if they equal the null_if_expr"""

    arg = rlz.any
    null_if_expr = rlz.any
    output_dtype = rlz.dtype_like("args")
    output_shape = rlz.shape_like("args")

@public
class CoalesceLike(ValueOp):

    # According to Impala documentation:
    # Return type: same as the initial argument value, except that integer
    # values are promoted to BIGINT and floating-point values are promoted to
    # DOUBLE; use CAST() when inserting into a smaller numeric column
    arg = rlz.value_list_of(rlz.any)

    output_shape = rlz.shape_like('arg')

    @immutable_property
    def output_dtype(self):
        # filter out null types
        non_null_exprs = [arg for arg in self.arg if arg.type() != dt.null]
        if non_null_exprs:
            return rlz.highest_precedence_dtype(non_null_exprs)
        else:
            return dt.null

@public
class Coalesce(CoalesceLike):
    pass

@public
class Greatest(CoalesceLike):
    pass

@public
class Least(CoalesceLike):
    pass

@public
class Literal(ValueOp):
    value = rlz.one_of(
        (
            rlz.instance_of(
                (
                    BaseGeometry,
                    bytes,
                    datetime.date,
                    datetime.datetime,
                    datetime.time,
                    datetime.timedelta,
                    enum.Enum,
                    float,
                    frozenset,
                    int,
                    frozendict,
                    np.generic,
                    np.ndarray,
                    pd.Timedelta,
                    pd.Timestamp,
                    str,
                    tuple,
                    type(None),
                    uuid.UUID,
                    decimal.Decimal,
                )
            ),
            # this seems buggy
            rlz.is_computable_input,
        )
    )
    dtype = rlz.datatype

    output_shape = rlz.Shape.SCALAR
    output_dtype = property(attrgetter("dtype"))

    def root_tables(self):
        return []

@public
class NullLiteral(Literal):
    """Typeless NULL literal"""

    value = rlz.optional(type(None))
    dtype = rlz.optional(rlz.instance_of(dt.Null), default=dt.null)

@public
class ScalarParameter(ValueOp):
    _counter = itertools.count()

    dtype = rlz.datatype
    counter = rlz.optional(
        rlz.instance_of(int), default=lambda: next(ScalarParameter._counter)
    )

    output_shape = rlz.Shape.SCALAR
    output_dtype = property(attrgetter("dtype"))

    def resolve_name(self):
        return f'param_{self.counter:d}'

    def __hash__(self):
        return hash((self.dtype, self.counter))

    @property
    def inputs(self):
        return ()

    def root_tables(self):
        return []

@public
class ValueList(ValueOp):
    """Data structure for a list of value expressions"""

    # NOTE: this proxies the ValueOp behaviour to the underlying values

    values = rlz.tuple_of(rlz.any)

    output_type = ir.ListExpr
    output_dtype = rlz.dtype_like("values")
    output_shape = rlz.shape_like("values")

    def root_tables(self):
        return distinct_roots(*self.values)

@public
class Constant(ValueOp):
    output_shape = rlz.Shape.SCALAR

@public
class TimestampNow(Constant):
    output_dtype = dt.timestamp

@public
class RandomScalar(Constant):
    output_dtype = dt.float64

@public
class E(Constant):
    output_dtype = dt.float64

@public
class Pi(Constant):
    output_dtype = dt.float64

@public
class StructField(ValueOp):
    arg = rlz.struct
    field = rlz.instance_of(str)

    output_shape = rlz.shape_like("arg")

    @immutable_property
    def output_dtype(self):
        struct_dtype = self.arg.type()
        value_dtype = struct_dtype[self.field]
        return value_dtype

    def resolve_name(self):
        return self.field

    def has_resolved_name(self):
        return True

@public
class DecimalPrecision(UnaryOp):
    arg = rlz.decimal
    output_dtype = dt.int32

@public
class DecimalScale(UnaryOp):
    arg = rlz.decimal
    output_dtype = dt.int32

@public
class Hash(ValueOp):
    arg = rlz.any
    how = rlz.isin({'fnv', 'farm_fingerprint'})

    output_dtype = dt.int64
    output_shape = rlz.shape_like("arg")

@public
class HashBytes(ValueOp):
    arg = rlz.one_of({rlz.value(dt.string), rlz.value(dt.binary)})
    how = rlz.isin({'md5', 'sha1', 'sha256', 'sha512'})

    output_dtype = dt.binary
    output_shape = rlz.shape_like("arg")

@public
class SummaryFilter(ValueOp):
    expr = rlz.instance_of(ir.TopKExpr)

    output_dtype = dt.boolean
    output_shape = rlz.Shape.COLUMNAR

# TODO(kszucs): shouldn't we move this operation to either
# analytic.py or reductions.py?
@public
class TopK(Node):
    arg = rlz.column(rlz.any)
    k = rlz.non_negative_integer
    by = rlz.one_of(
        (
            rlz.function_of("arg", preprocess=ir.relations.find_base_table),
            rlz.any,
        )
    )

    output_type = ir.TopKExpr

    def blocks(self):
        return True

    def root_tables(self):
        args = (arg for arg in self.flat_args() if isinstance(arg, ir.Expr))
        return distinct_roots(*args)

# TODO(kszucs): we should merge the case operations by making the
# cases, results and default optional arguments like they are in
# api.py
@public
class SimpleCase(ValueOp):
    base = rlz.any
    cases = rlz.value_list_of(rlz.any)
    results = rlz.value_list_of(rlz.any)
    default = rlz.any

    output_shape = rlz.shape_like("base")

    def __init__(self, cases, results, **kwargs):
        assert len(cases) == len(results)
        super().__init__(cases=cases, results=results, **kwargs)

    def root_tables(self):
        return distinct_roots(*self.flat_args())

    @immutable_property
    def output_dtype(self):
        # TODO(kszucs): we could extend the functionality of
        # rlz.shape_like to support varargs with .flat_args()
        # to define a subset of input arguments
        values = self.results + [self.default]
        return rlz.highest_precedence_dtype(values)

@public
class SearchedCase(ValueOp):
    cases = rlz.value_list_of(rlz.boolean)
    results = rlz.value_list_of(rlz.any)
    default = rlz.any

    output_shape = rlz.shape_like("cases")

    def __init__(self, cases, results, default):
        assert len(cases) == len(results)
        super().__init__(cases=cases, results=results, default=default)

    def root_tables(self):
        return distinct_roots(*self.flat_args())

    @immutable_property
    def output_dtype(self):
        exprs = self.results + [self.default]
        return rlz.highest_precedence_dtype(exprs)

max_stars_repo_name: jayceslesar/ibis

max_issues_repo_name: marlenezw/ibis

max_forks_repo_name: marlenezw/ibis


text:
from typing import Tuple
import numpy as np
from scipy import linalg

from kb_learning.kernel import KilobotEnvKernel

import logging
logger = logging.getLogger('kb_learning.gp')

class SparseWeightedGP:
    def __init__(self, kernel: KilobotEnvKernel, output_dim: int, noise_variance=1e-6,
                 mean_function=None, output_bounds: Tuple[np.ndarray, np.ndarray]=None, eval_mode=False):
        # TODO add documentation
        """

        :param kernel:
        :param output_bounds:
        """
        self.mean_function = mean_function
        self.noise_variance = noise_variance
        self.min_variance = 1e-8
        self.cholesky_regularizer = 1e-9

        self.Q_Km = None
        self.alpha = None
        self.trained = False

        self.sparse_inputs = None
        self.k_cholesky = None
        self.kernel = kernel
        self.output_dim = output_dim
        self.output_bounds = output_bounds

        self.eval_mode = eval_mode

    def to_dict(self):
        input_dict = dict()
        input_dict['class'] = 'SparseWeightedGP'
        input_dict['kernel'] = self.kernel.to_dict()
        input_dict['noise_variance'] = self.noise_variance
        input_dict['mean_function'] = self.mean_function
        input_dict['output_bounds'] = self.output_bounds
        input_dict['output_dim'] = self.output_dim
        input_dict['eval_mode'] = self.eval_mode

        if self.trained:
            gp_dict = dict()
            gp_dict['Q_Km'] = self.Q_Km
            gp_dict['alpha'] = self.alpha
            gp_dict['sparse_inputs'] = self.sparse_inputs
            gp_dict['k_cholesky'] = self.k_cholesky
            input_dict['gp'] = gp_dict

        return input_dict

    @staticmethod
    def from_dict(input_dict: dict):
        policy_class = input_dict.pop('class')
        assert policy_class == 'SparseWeightedGP'
        gp_dict = input_dict.pop('gp', None)
        kernel_dict = input_dict.pop('kernel')
        input_dict['kernel'] = KilobotEnvKernel.from_dict(kernel_dict)
        spwgp = SparseWeightedGP(**input_dict)

        if gp_dict:
            spwgp.Q_Km = gp_dict['Q_Km']
            spwgp.alpha = gp_dict['alpha']
            spwgp.sparse_inputs = gp_dict['sparse_inputs']
            spwgp.k_cholesky = gp_dict['k_cholesky']
            spwgp.trained = True

        return spwgp

    def train(self, inputs, outputs, weights, sparse_inputs, *args, **kwargs):
        """
        :param inputs: N x d_input
        :param outputs: N x d_output
        :param weights: N
        :param sparse_inputs: M x d_output
        :return:
        """
        if outputs.ndim == 1:
            outputs = outputs.reshape((-1, 1))

        self.sparse_inputs = sparse_inputs

        weights /= weights.max()

        # kernel matrix on subset of samples
        K_m = self.kernel(sparse_inputs)
        K_mn = self.kernel(sparse_inputs, inputs)

        # fix cholesky with regularizer
        reg_I = self.cholesky_regularizer * np.eye(K_m.shape[0])
        while True:
            try:
                K_m_c = np.linalg.cholesky(K_m), True
                logger.debug('regularization for cholesky: {}'.format(reg_I[0, 0]))
                break
            except np.linalg.LinAlgError:
                K_m += reg_I
                reg_I *= 2
        else:
            raise Exception("SparseGPPolicy: Cholesky decomposition failed")

        L = self.kernel.diag(inputs) - np.sum(K_mn * linalg.cho_solve(K_m_c, K_mn), axis=0).squeeze() \
            + self.noise_variance * (1 / weights)
        L = 1 / L

        Q = K_m + (K_mn * L).dot(K_mn.T)
        if self.mean_function:
            outputs -= self.mean_function(inputs)

        self.alpha = (np.linalg.solve(Q, K_mn) * L).dot(outputs)

        self.Q_Km = np.linalg.pinv(K_m) - np.linalg.pinv(Q)

        self.trained = True

    def get_mean(self, inputs, return_k=False):
        if self.trained:
            k = self.kernel(inputs, self.sparse_inputs)
            mean = k.dot(self.alpha)
        else:
            k = None
            mean = np.zeros((inputs.shape[0], self.output_dim))

        if self.mean_function:
            mean += self.mean_function(inputs)

        if return_k:
            return mean, k

        return mean

    def get_mean_sigma(self, inputs):
        mean, k = self.get_mean(inputs, True)

        if self.trained:
            variance = self.kernel.diag(inputs) - np.sum(k.T * self.Q_Km.dot(k.T), axis=0) \
                        + self.noise_variance
        else:
            variance = self.kernel.variance + self.noise_variance

        if np.isscalar(variance):  # single number
            variance = np.array([variance])

        variance[variance < self.min_variance] = self.min_variance

        sigma = np.sqrt(variance)

        return mean, sigma

    def sample(self, inputs):
        if inputs.ndim == 1:
            inputs = np.array([inputs])

        gp_mean, gp_sigma = self.get_mean_sigma(inputs)

        if gp_mean.ndim > 1:
            samples = np.random.normal(gp_mean, gp_sigma[:, None])
        else:
            samples = np.random.normal(gp_mean, gp_sigma)

        if self.output_bounds:
            # check samples against bounds from action space
            samples = np.minimum(samples, self.output_bounds[1])
            samples = np.maximum(samples, self.output_bounds[0])

        return np.reshape(samples, (-1, self.output_dim))

    def __call__(self, inputs):
        if self.eval_mode:
            return self.get_mean(inputs)
        return self.sample(inputs)

ext: py

reason: import numpy,from scipy


text:
import numpy as np

from rllab.envs.mujoco.hill.hill_env import HillEnv
from rllab.envs.mujoco.hopper_env import HopperEnv
from rllab.misc.overrides import overrides
import rllab.envs.mujoco.hill.terrain as terrain
from rllab.spaces import Box

class HopperHillEnv(HillEnv):

    MODEL_CLASS = HopperEnv

    @overrides
    def _mod_hfield(self, hfield):
        # clear a flat patch for the robot to start off from
        return terrain.clear_patch(hfield, Box(np.array([-1.0, -1.0]), np.array([-0.5, -0.5])))

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 10:29:52 2019

@author: Titus
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

main_df=pd.read_csv(r"../../58-32_main_geophysical_well_log.csv")

for col in main_df.columns:
  main_df=main_df[main_df[col]!=1950]

sonic_df=pd.read_csv(r"../../58-32_sonic_log_data.csv")
for col in sonic_df.columns:
  sonic_df=sonic_df[sonic_df[col]>-500]

litho_df=pd.read_csv("../../58-32_xray_diffraction_data.csv")
litho_df=litho_df.replace('tr',0.1)
litho_df=litho_df.replace('t',0.1)
litho_df['% chlorite in C/S']=litho_df['% chlorite in C/S'].str.replace(r'%', r'.0').astype('float') / 100.0
litho_df=litho_df.fillna(0)

for col in litho_df.columns:
  litho_df[col]=litho_df[col].astype('float')
litho_cols=litho_df.columns

fmi=np.genfromtxt(r"..\..\58-32_logs\Logs\University_of_Utah_MU_ESW1_FMI_HD_7440_7550ft_Dip_Final_2ndRun_DFC.las",skip_header=150)
fmi_cols='''TDEP               .ft                                     :  {F}
Breakout_Flag      .unitless                               :  {F}
Conductive_Continuous_Fracture_COUNT .unitless                               :  {F}
Conductive_Fracture_Count_Summation .unitless                               :  {F}
Conductive_Part_Resistive_Fracture_COUNT .unitless                               :  {F}
Count_SUM          .unitless                               :  {F}
Longitudinal_Induced_Fracture_Flag .unitless                               :  {F}
P10_Conductive_Continuous_Fracture .1/ft                                   :  {F}
P10_Conductive_Continuous_Fracture_C .1/ft                                   :  {F}
P10_Conductive_Part_Resistive_Fracture .1/ft                                   :  {F}
P10_Conductive_Part_Resistive_Fracture_C .1/ft                                   :  {F}
P10_SUM            .1/ft                                   :  {F}
P10_SUM_C          .1/ft                                   :  {F}
P21_Conductive_Continuous_Fracture .1/ft                                   :  {F}
P21_Conductive_Continuous_Fracture_Trace .1/ft                                   :  {F}
P21_Conductive_Fracture .1/ft                                   :  {F}
P21_Conductive_Fracture_Integration .1/ft                                   :  {F}
P21_Conductive_Part_Resistive_Fracture .1/ft                                   :  {F}
P21_Conductive_Part_Resistive_Fracture_Trace .1/ft                                   :  {F}
P21_Resistive_Fracture .1/ft                                   :  {F}
P21_Resistive_Fracture_Integration .1/ft                                   :  {F}
P21_SUM            .1/ft                                   :  {F}
P21_Trace_SUM      .1/ft                                   :  {F}
P32_Conductive_Continuous_Fracture .1/ft                                   :  {F}
P32_Conductive_Continuous_Fracture_Trace .1/ft                                   :  {F}
P32_Conductive_Part_Resistive_Fracture .1/ft                                   :  {F}
P32_Conductive_Part_Resistive_Fracture_Trace .1/ft                                   :  {F}
P32_SUM            .1/ft                                   :  {F}
P32_Trace_SUM      .1/ft                                   :  {F}
P33_Conductive_Continuous_Fracture_Trace .v/v                                    :  {F}
P33_Conductive_Fracture_Trace .v/v                                    :  {F}
P33_Conductive_Fracture_Trace_Integration .v/v                                    :  {F}
P33_Conductive_Part_Resistive_Fracture_Trace .v/v                                    :  {F}
P33_Trace_SUM      .v/v                                    :  {F}
Resistive_Fracture_Count_Summation .unitless                               :  {F}
Shear_Induced_Fracture_Flag .unitless                               :  {F}
Tensile_Induced_Fracture_Flag .unitless                               :  {F}'''
fmi_cols=fmi_cols.replace(' ','').replace(':','').replace('{F}','').split('\n')
fmi_df=pd.DataFrame()
for ind,col in enumerate(fmi_cols):
  fmi_df[col]=fmi[:,ind]
hi=fmi_df.describe()
col_float=hi.loc['max'].values!=hi.loc['max'].astype(int).values
col_float=np.where(col_float==True)[0].tolist()
rara=[fmi_cols[i] for i in col_float]

fmi_df.plot(rara[0],[
 'P10_Conductive_Continuous_Fracture_C.1/ft',
 'P10_Conductive_Part_Resistive_Fracture_C.1/ft',
 'P10_SUM_C.1/ft',
 'P21_Conductive_Continuous_Fracture.1/ft',
 'P21_Conductive_Continuous_Fracture_Trace.1/ft',
 'P21_Conductive_Fracture.1/ft',
 'P21_Conductive_Part_Resistive_Fracture.1/ft',
 'P21_Conductive_Part_Resistive_Fracture_Trace.1/ft',
 'P21_SUM.1/ft',
 'P21_Trace_SUM.1/ft',
 'P32_Conductive_Continuous_Fracture.1/ft',
 'P32_Conductive_Continuous_Fracture_Trace.1/ft',
 'P32_Conductive_Part_Resistive_Fracture.1/ft',
 'P32_Conductive_Part_Resistive_Fracture_Trace.1/ft',
 'P32_SUM.1/ft',
 'P32_Trace_SUM.1/ft',
 'P33_Conductive_Continuous_Fracture_Trace.v/v',
 'P33_Conductive_Fracture_Trace.v/v',
 'P33_Conductive_Fracture_Trace_Integration.v/v',
 'P33_Conductive_Part_Resistive_Fracture_Trace.v/v',
 'P33_Trace_SUM.v/v'])
plt.show()
#%%
start=0
j=0
cols=main_df.columns
plt.close('all')
fig,ax=plt.subplots(nrows=4,ncols=5)

for colno in range(start,start+20):
  if (colno-start)%5==0 and colno-start!=0:
    j+=1

  ax[j,(colno-start)%5].plot(main_df[cols[0]],main_df[cols[colno]].rolling(100).mean(),label='{0}'.format(cols[colno]))
  ax[j,(colno-start)%5].legend(loc=2)

plt.show()
#%%
start=6
j=0

#plt.close('all')
fig,ax=plt.subplots(nrows=4,ncols=5)
cols=sonic_df.columns
for colno in range(start,start+20):
  if (colno-start)%5==0 and colno-start!=0:
    j+=1

  ax[j,(colno-start)%5].plot(sonic_df[cols[0]],sonic_df[cols[colno]].rolling(100).mean(),label='{0}'.format(cols[colno]))
  ax[j,(colno-start)%5].legend(loc=2)

plt.show()
#%%
plt.close('all')
litho_df.plot(x='Lower Depth Range (m)',y=['Plagioclase', 'K-feldspar', 'Quartz'])
plt.plot(sonic_df['Depth (m)'],sonic_df['SPHI'].rolling(50).mean()*60,label='Sonic Poro')
plt.plot(sonic_df['Depth (m)'],sonic_df['PR'].rolling(50).mean()*60,label='poisson')
plt.plot(main_df['Depth (m)'],main_df['HGR']/10+70,label='Gamma')
plt.legend(loc=2)
plt.show()
#%%

max_stars_repo_name: titusquah/Final-Project

max_issues_repo_name: titusquah/Final-Project

max_forks_repo_name: titusquah/Final-Project


text:
# -*- coding: utf-8 -*-
import numpy as np
import sys
#import dynamic_graph.sot.torque_control.utils.plot_utils as plot_utils
import matplotlib as mpl
mpl.rcParams['lines.linewidth']     = 4;
import matplotlib.pyplot as plt
from motor_model import Motor_model
from dynamic_graph.sot.torque_control.hrp2.control_manager_conf import IN_OUT_GAIN
from identify_motor_static import identify_motor_static
from identify_motor_low_level import identify_motor_low_level
from identify_motor_vel import identify_motor_vel
from identify_motor_acc import identify_motor_acc
from identification_utils import jID
'''
motor model :
i(t) = Kt*tau(t) + Kv*dq(t) + Ka*ddq(t) + Kf*Sign(dq)
'''
#~ DZ=70
DZ                                  = 0.0
dt                                  = 0.001
ZERO_VEL_THRESHOLD                  = 0.1
POSITIVE_VEL_THRESHOLD              = 0.001
ZERO_ACC_THRESHOLD                  = 0.2
ZERO_JERK_THRESHOLD                 = 3.0
CURRENT_SATURATION                  = 9.5
Nvel                                = 10
SHOW_THRESHOLD_EFFECT               = True
USING_CONTROL_AS_CURRENT_MEASURE    = False
INVERT_CURRENT                      = False
result_dir                          = '../../../../../results/hrp2_motor_identification/'
IDENTIFICATION_MODE ='static'
IDENTIFICATION_MODE ='vel'
#IDENTIFICATION_MODE ='acc'
#IDENTIFICATION_MODE ='test_model' #Compare Model Vs Measurment

#JOINT_NAME = 'rhy';
#JOINT_NAME = 'rhr';
#JOINT_NAME = 'rhp';
#JOINT_NAME = 'rk';
#JOINT_NAME = 'rap';
#JOINT_NAME = 'rar';

#JOINT_NAME = 'lhy'; #USING_CONTROL_AS_CURRENT_MEASURE = True  # 6
#JOINT_NAME = 'lhr';  #USING_CONTROL_AS_CURRENT_MEASURE = True  # 7
JOINT_NAME = 'lhp';  #USING_CONTROL_AS_CURRENT_MEASURE = True  # 8
#JOINT_NAME = 'lk';  # 9 ok
#JOINT_NAME = 'lap'; # 10 ok
#JOINT_NAME = 'lar'; # 11 ok

#data_folder='../../results/20161114_153220_rk_vel/'
#data_folder='../../results/20161114_151812_rhp_vel/'
#data_folder='../../results/20170203_164133_com_sin_z_001/'

#JOINT_NAME = 'rhp'
#data_folder='../../results/20170203_164133_com_sin_z_001/'
#data_folder= '../../results/20161114_152706_rk_acc/' ; INVERT_CURRENT = True
#data_folder = result_dir + '../20170911172000_com_sin_3cm/';

if (IDENTIFICATION_MODE != 'test_model') :
    if(JOINT_NAME == 'rhy' ):
        INVERT_CURRENT = True
        Nvel = 9
        data_folder_static = result_dir+'20161114_135332_rhy_static/';
        data_folder_vel    = result_dir+'20161114_143152_rhy_vel/';
        data_folder_acc    = result_dir+'20161114_142351_rhy_acc/';
    if(JOINT_NAME == 'rhr' ):
        INVERT_CURRENT = True
        Nvel = 10
        data_folder_static = result_dir+'20161114_144232_rhr_static/';
        data_folder_vel    = result_dir+'20161114_150356_rhr_vel/';
        data_folder_acc    = result_dir+'20161114_145456_rhr_acc/';
    if(JOINT_NAME == 'rhp' ):
        data_folder_static = result_dir+'20161114_150722_rhp_static/';
        data_folder_vel    = result_dir+'20161114_151812_rhp_vel/';
        data_folder_acc    = result_dir+'20161114_151259_rhp_acc/';
    if(JOINT_NAME == 'rk' ):
        INVERT_CURRENT = True
        data_folder_static = result_dir+'20161114_152140_rk_static/';
#        data_folder_static = result_dir+'../20170918_180023_rk_current_chirp/'
        data_folder_vel    = result_dir+'20161114_153220_rk_vel/';
        data_folder_acc    = result_dir+'20161114_152706_rk_acc/';
    if(JOINT_NAME == 'rap' ):
        INVERT_CURRENT = True
        data_folder_static = result_dir+'20161114_153739_rap_static/';
        data_folder_vel    = result_dir+'20161114_154559_rap_vel/';
        data_folder_acc    = result_dir+'20161114_154316_rap_acc/';
    if(JOINT_NAME == 'rar' ):
        data_folder_static = result_dir+'20161114_154945_rar_static/';
        data_folder_vel    = result_dir+'20161114_160038_rar_vel/';
        data_folder_acc    = result_dir+'20161114_155545_rar_acc/';

    if(JOINT_NAME == 'lhy' ):
        data_folder_static = result_dir+'20171002_163413_lhy_static/';
        data_folder_vel    = result_dir+'/20171002_151718_lhy_vel/';
        data_folder_acc    = result_dir+'20170113_144710_lhy_const_acc/';
    if(JOINT_NAME == 'lhr' ):
        data_folder_static = result_dir+'20171002_164436_lhr_static/';
        data_folder_vel    = result_dir+'20171002_153334_lhr_vel/';
        data_folder_acc    = result_dir+'20170113_145826_lhr_const_acc/';
    if(JOINT_NAME == 'lhp' ):
        data_folder_static = result_dir+'20171002_165335_lhp_static/';
        data_folder_vel    = result_dir+'20171002_154449_lhp_vel/';
        data_folder_acc    = result_dir+'20170113_151103_lhp_const_acc/';
    if(JOINT_NAME == 'lk' ):
        data_folder_static = result_dir+'20170113_151748_lk_static/';
        data_folder_vel    = result_dir+'20170113_152924_lk_const_vel/';
        data_folder_acc    = result_dir+'20170113_152606_lk_const_acc/';
    if(JOINT_NAME == 'lap' ):
        data_folder_static = result_dir+'20170113_154007_lap_static/';
        data_folder_vel    = result_dir+'20170113_154834_lap_const_vel/';
        data_folder_acc    = result_dir+'20170113_154303_lap_const_acc/';
    if(JOINT_NAME == 'lar' ):
        data_folder_static = result_dir+'20170113_155150_lar_static/';
        data_folder_vel    = result_dir+'20170113_160057_lar_const_vel/';
        data_folder_acc    = result_dir+'20170113_155706_lar_const_acc/';

if (IDENTIFICATION_MODE=='static') : data_folder = data_folder_static
if (IDENTIFICATION_MODE=='vel')    : data_folder = data_folder_vel
if (IDENTIFICATION_MODE=='acc')    : data_folder = data_folder_acc

JOINT_ID = jID[JOINT_NAME]
DATA_FILE_NAME = 'data_j'+str(JOINT_ID)+'.npz';

''' Load data from file '''
try:
    data = np.load(data_folder+DATA_FILE_NAME);
    enc = np.squeeze(data['enc']);
    dq = np.squeeze(data['dq']);
    ddq = np.squeeze(data['ddq']);
    tau = np.squeeze(data['tau']);
    ctrl = np.squeeze(data['ctrl']);
    current = np.squeeze(data['current']);
except IOError:
    print "Impossible to read data file %s" % (data_folder+DATA_FILE_NAME);
    sys.exit("Run script compress_identification_data.py to generate data file from tracer log files.");

maskSaturation=np.logical_and( (current>-CURRENT_SATURATION) , (current<CURRENT_SATURATION))
if USING_CONTROL_AS_CURRENT_MEASURE:
    maskCtrlNotInDZ = np.logical_and( ctrl >= (DZ *0.8) ,ctrl <= -(DZ*0.8)  )
    maskSaturation=np.logical_or(maskSaturation, maskCtrlNotInDZ)

if INVERT_CURRENT:
    current = - current

enc     = enc    [maskSaturation];
dq      = dq     [maskSaturation];
ddq     = ddq    [maskSaturation];
tau     = tau    [maskSaturation];
ctrl    = ctrl   [maskSaturation];
current = current[maskSaturation];

if USING_CONTROL_AS_CURRENT_MEASURE:
    current_est = current.copy()
    #~ maskUpDZ = ctrl > DZ
    #~ maskDnDZ = ctrl < DZ
    #~ maskInDZ = np.logical_and( ctrl <= DZ ,ctrl >= -DZ )
    current = ctrl / IN_OUT_GAIN
    #~ current[maskUpDZ] =  (ctrl[maskUpDZ]-DZ) /IN_OUT_GAIN
    #~ current[maskDnDZ] =  (ctrl[maskDnDZ]+DZ) /IN_OUT_GAIN
    #~ current[maskInDZ] =  0.0

#IDENTIFICATION_MODE='low_level'
if(IDENTIFICATION_MODE=='low_level'):
    identify_motor_low_level(dq, ctrl, current);

#Ktau,Tau0 Identification
if(IDENTIFICATION_MODE=='static'):
    (Ktp, Ktn, Ks, DZ) = identify_motor_static(enc, dq, ctrl, current, tau, JOINT_ID, JOINT_NAME, ZERO_VEL_THRESHOLD,
                                               POSITIVE_VEL_THRESHOLD, SHOW_THRESHOLD_EFFECT);
    #save parameters for next identification level**********************
    np.savez(data_folder+'motor_param_'+JOINT_NAME+'.npz',Ktp=Ktp, Ktn=Ktn, Ks=Ks, DZ=DZ)
    plt.savefig(data_folder+"static_"+JOINT_NAME+".jpg")
    plt.show()

if(IDENTIFICATION_MODE=='vel' or IDENTIFICATION_MODE=='acc'):
    #load parameters from last identification level*********************
    try:
        data_motor_param = np.load(data_folder_static+'motor_param_'+JOINT_NAME+'.npz')
        Ktp=(data_motor_param['Ktp'].item())
        Ktn=(data_motor_param['Ktn'].item())
    except IOError:
        print "Impossible to read data file %s" % (data_folder_static+'motor_param_'+JOINT_NAME+'.npz');
        sys.exit("Run identification on static experiments.");

#Kd Identification
if(IDENTIFICATION_MODE=='vel'):
    Ks =(data_motor_param['Ks'].item())
    (Kvp, Kvn, Kfp, Kfn, DeadZone, K_bemf) = identify_motor_vel(dt, dq, ddq, ctrl, current, tau, Ktp, Ktn, Ks,
                                                              ZERO_VEL_THRESHOLD, ZERO_ACC_THRESHOLD,
                                                              Nvel, SHOW_THRESHOLD_EFFECT);
    np.savez(data_folder+'motor_param_'+JOINT_NAME+'.npz', Ktp=Ktp, Ktn=Ktn, Kvp=Kvp, Kvn=Kvn,
             Kfp=Kfp, Kfn=Kfn, DeadZone=DeadZone, K_bemf=K_bemf)
    warning = ""
    if USING_CONTROL_AS_CURRENT_MEASURE :
        warning = " (Current sensor not used)"

    print "cur_sens_gains[%d]= %f #Using %s"% (JOINT_ID, Ks, data_folder_static.split('/')[-2] + warning);
    print "deadzone[%d]      = %f #Using %s"% (JOINT_ID, data_motor_param['DZ'].item(), data_folder_static.split('/')[-2] + warning);
    print "deadzone[%d]      = %f #Using %s"% (JOINT_ID, DeadZone, data_folder_vel.split('/')[-2] + warning);
    print "K_bemf[%d]        = %f # [Amp/Rad.s-1] Using %s"% (JOINT_ID, K_bemf, data_folder_vel.split('/')[-2] + warning);
    print 'Kt_p[%d]          = %f #Using %s' % (JOINT_ID, Ktp, data_folder_static.split('/')[-2] + warning);
    print 'Kt_n[%d]          = %f #Using %s' % (JOINT_ID, Ktn, data_folder_static.split('/')[-2] + warning);
    print 'Kv_p[%d]          = %f #Using %s' % (JOINT_ID, Kvp, data_folder_vel.split('/')[-2] + warning);
    print 'Kv_n[%d]          = %f #Using %s' % (JOINT_ID, Kvn, data_folder_vel.split('/')[-2] + warning);
    print 'Kf_p[%d]          = %f #Using %s' % (JOINT_ID, Kfp, data_folder_vel.split('/')[-2] + warning);
    print 'Kf_n[%d]          = %f #Using %s' % (JOINT_ID, Kfn, data_folder_vel.split('/')[-2] + warning);

    plt.savefig(data_folder+"vel_"+JOINT_NAME+".jpg")

#J Identification
if(IDENTIFICATION_MODE=='acc'):
    Kvp=(data_motor_param['Kvp'].item())
    (Kap, Kan, Kfp, Kfn) = identify_motor_acc(dt, dq, ddq, current, tau, Ktp, Kvp,
                                              POSITIVE_VEL_THRESHOLD, ZERO_JERK_THRESHOLD,
                                              SHOW_THRESHOLD_EFFECT);
    print 'Ka_p[%d] = %f' % (JOINT_ID,Kap);
    print 'Ka_n[%d] = %f' % (JOINT_ID,Kan);
    print 'Kf_p[%d] = %f' % (JOINT_ID,Kfp);
    print 'Kf_n[%d] = %f' % (JOINT_ID,Kfn);

#model vs measurement
if (IDENTIFICATION_MODE=='test_model'):
    #load motor parameters
    import dynamic_graph.sot.torque_control.hrp2.motors_parameters as hrp2_motors_parameters
    Kt_p = hrp2_motors_parameters.Kt_p
    Kt_n = hrp2_motors_parameters.Kt_n

    Kf_p = hrp2_motors_parameters.Kf_p
    Kf_n = hrp2_motors_parameters.Kf_n

    Kv_p = hrp2_motors_parameters.Kv_p
    Kv_n = hrp2_motors_parameters.Kv_n

    Ka_p = hrp2_motors_parameters.Ka_p
    Ka_n = hrp2_motors_parameters.Ka_n

    motor = Motor_model(Kt_p[JOINT_ID], Kt_n[JOINT_ID],
                        Kf_p[JOINT_ID], Kf_n[JOINT_ID],
                        Kv_p[JOINT_ID], Kv_n[JOINT_ID],
                        Ka_p[JOINT_ID], Ka_n[JOINT_ID],dqThreshold=0.01)

    tau_motor=np.zeros(len(tau))
    tau_motor_current=np.zeros(len(tau))
    tau_motor_vel=np.zeros(len(tau))
    tau_motor_acc=np.zeros(len(tau))
    i_motor=np.zeros(len(current))
    for idx in range(len(tau)):
        tau_motor[idx]          =motor.getTorque    (current[idx], dq[idx], ddq[idx])
        tau_motor_current[idx]  =motor.getTorque    (current[idx], 0.0,     0.0)
        tau_motor_vel[idx]      =motor.getTorque    (0.0,          dq[idx], 0.0)
        tau_motor_acc[idx]      =motor.getTorque    (0.0,          0.0,     ddq[idx])
        i_motor[idx]            =motor.getCurrent   (tau[idx],     dq[idx], ddq[idx])

    plt.figure()
    alpha = 0.7
    plt.plot(tau,               alpha=alpha, label='torque from dynamic model')
    plt.plot(tau_motor,         alpha=alpha, label='torque from motor model')
    plt.plot(tau_motor_current, alpha=alpha, label='torque from motor model (cur only)')
    plt.plot(tau_motor_vel,     alpha=alpha, label='torque from motor model (vel only)')
    plt.plot(tau_motor_acc,     alpha=alpha, label='torque from motor model (acc only)')
    plt.legend()

    plt.figure()
    plt.subplot(211)
    plt.plot(dq, label='dq')
    plt.legend()
    plt.subplot(212)
    plt.plot(current)
    plt.plot(i_motor)
    plt.legend(['measured current','Estimated current with model'])

    plt.figure()
    plt.plot(current, label='current')
    plt.plot(ctrl/IN_OUT_GAIN, label='ctrl')
    plt.legend()
    plt.show()

max_stars_repo_name: jviereck/sot-torque-control

max_issues_repo_name: jviereck/sot-torque-control

max_forks_repo_name: jviereck/sot-torque-control


text:
import numpy as np
import GPy
import tqdm
import pickle
import os

from .predict import predict

from chemprop.utils import get_avg_UQ
from argparse import Namespace
from sklearn.ensemble import RandomForestRegressor
from chemprop.data import MoleculeDataLoader, StandardScaler
import torch.nn as nn

# Future TODO:
# 1) Support multi-tasking
# 2) Continue testing models work properly w/ num_folds and ensemble_size
# 3) Add unc_estimator to training (script chooses best model with validation)
# 4) Make sure scalars are working properly (esp. with ensembles & dropout_vi)

class UncertaintyEstimator:
    """
    An UncertaintyEstimator calculates uncertainty when passed a model.
    Certain UncertaintyEstimators also augment the model and alter prediction
    values. Note that many UncertaintyEstimators compute unscaled uncertainty
    values. These are only meaningful relative to one another.
    """
    def __init__(self,
                 args: Namespace,
                 data,
                 scaler: StandardScaler,
                 ):
        """
        Constructs an UncertaintyEstimator.
        :param train_data: The data a model was trained on.
        :param val_data: The validation/supplementary data for a model.
        :param test_data: The data to test the model with.
        :param scaler: A scaler the model uses to transform input data.
        :param args: The command line arguments.
        """

        self.args = args
        self.scaler = scaler
        self.data = data
        self.data_loader = MoleculeDataLoader(
                            dataset=data,
                            batch_size=args.batch_size,
                            num_workers=args.num_workers
        )
        self.data_len = len(data)
        if args.checkpoint_paths is not None:
            self.data_width = len(args.checkpoint_paths)

        self.split_UQ = args.split_UQ
        self.counter = 0

    def process_model(self, model: nn.Module, N=0):
        """Perform initialization using model and prior data.
        :param model: The model to learn the uncertainty of.
        :int N: Enumerate of model we are currently processing.
        """
        pass

    def calculate_UQ(self,
                     val_predictions: np.ndarray,
                     test_predictions: np.ndarray):
        """
        Compute uncertainty on self.val_data and self.test_data predictions.
        :param val_predictions: The predictions made on self.val_data.
        :param test_predictions: The predictions made on self.test_data.
        :return: Validation set predictions, validation set uncertainty,
                 test set predictions, and test set uncertainty.
        """
        pass

    def _scale_uncertainty(self, uncertainty: float) -> float:
        """
        Rescale uncertainty estimates to account for scaled input.
        :param uncertainty: An unscaled uncertainty estimate.
        :return: A scaled uncertainty estimate.
        """
        return self.scaler.stds * uncertainty

class Dropout_VI(UncertaintyEstimator):
    """
    Uncertainty method for calculating aleatoric + epistemic UQ
    by using dropout and averaging over a number of predictions.
    """

    def __init__(self, args, data, scaler):
        super().__init__(args, data, scaler)
        self.num_preds = args.num_preds
        self._create_matrix()

    def _create_matrix(self):
        self.data_width *= self.num_preds
        self.sum_batch = np.zeros((self.data_len, self.data_width))
        self.sum_var = np.zeros((self.data_len, self.data_width))

    def process_model(self, model, N=0):
        offset = N * self.num_preds

        for i in tqdm.tqdm(range(self.num_preds)):
            batch_preds, var_preds = predict(
                                        model=model,
                                        data_loader=self.data_loader,
                                        disable_progress_bar=True,
                                        scaler=self.scaler
                                        )
            # Ensure they are in proper list form
            # TODO: Allow for multitasking instead
            batch_preds = [item for sublist in batch_preds for item in sublist]
            var_preds = [item for sublist in var_preds for item in sublist]
            self.sum_batch[:, i + offset] = batch_preds
            self.sum_var[:, i + offset] = var_preds

    def calculate_UQ(self):
        avg_preds = np.nanmean(self.sum_batch, 1).tolist()
        avg_UQ = get_avg_UQ(self.sum_var, avg_preds, self.sum_batch, return_both=self.split_UQ)

        return avg_preds, self._scale_uncertainty(avg_UQ)

class Ensemble_estimator(UncertaintyEstimator):
    """
    Uncertainty method for calculating UQ by averaging
    over a number of ensembles of models.
    """

    def __init__(self, args, data, scaler):
        super().__init__(args, data, scaler)
        self._create_matrix()

    def _create_matrix(self):
        self.sum_batch = np.zeros((self.data_len, self.data_width))
        self.sum_var = np.zeros((self.data_len, self.data_width))

    def process_model(self, model, N=0):
        batch_preds, var_preds = predict(
                            model=model,
                            data_loader=self.data_loader,
                            disable_progress_bar=True,
                            scaler=self.scaler
                            )

        # Ensure they are in proper list form
        batch_preds = [item for sublist in batch_preds for item in sublist]
        var_preds = [item for sublist in var_preds for item in sublist]
        self.sum_batch[:, N] = batch_preds
        self.sum_var[:, N] = var_preds

    def calculate_UQ(self):
        avg_preds = np.nanmean(self.sum_batch, 1).tolist()

        aleatoric = np.nanmean(self.sum_var, 1).tolist()
        epistemic = np.var(self.sum_batch, 1).tolist()

        total_unc = aleatoric + epistemic

        if self.split_UQ:
            return avg_preds, (aleatoric, epistemic)
        else:
            return avg_preds, self._scale_uncertainty(total_unc)

class ExposureEstimator(UncertaintyEstimator):
    """
    An ExposureEstimator drops the output layer
    of the provided model after training.
    The "exposed" final hidden-layer is used to calculate uncertainty.
    """
    def __init__(self,
                 args: Namespace,
                 data,
                 scaler: StandardScaler):
        super().__init__(args, data, scaler)

        self.sum_last_hidden = np.zeros((len(self.data.smiles()), self.args.hidden_size))
        self.final_preds = np.zeros((len(self.data.smiles()), 1))
        self.num_models = self.args.num_folds

    def process_model(self, model: nn.Module, N=0):
        model.eval()
        model.use_last_hidden = False
        self.num_tasks = model.output_size

        last_hidden = predict(
            model=model,
            data_loader=self.data_loader,
            scaler=None
        )
        model.use_last_hidden = True

        self.sum_last_hidden += np.array(last_hidden)
        self.counter += 1

        # Get normal preds too
        preds = predict(
            model=model,
            data_loader=self.data_loader,
            scaler=self.scaler
        )
        self.final_preds += np.array(preds)

    def _compute_hidden_vals(self):
        avg_last_hidden = self.sum_last_hidden / self.counter

        return avg_last_hidden

class RandomForestEstimator(ExposureEstimator):

    def calculate_UQ(self):
        """
        A RandomForestEstimator trains a random forest to
        operate on data transformed by the provided model.
        Predictions are calculated using the output of the random forest.
        Reported uncertainty is the variance of trees in the forest.
        """

        avg_last_hidden_test = self._compute_hidden_vals()

        test_predictions = np.ndarray(
            shape=(len(self.data.smiles()), self.num_models))
        test_uncertainty = np.ndarray(
            shape=(len(self.data.smiles()), self.num_models))

        for index in range(self.num_models):
            model_path = os.path.join(self.args.unc_save_dir,
                                      f'unc_estimator_{index}.pickle')

            # load model first
            with open(model_path, 'rb') as f:
                forest = pickle.load(f)

            avg_test_preds = forest.predict(avg_last_hidden_test)
            test_predictions[:, index] = avg_test_preds

            individual_test_predictions = np.array([estimator.predict(
                avg_last_hidden_test) for estimator in forest.estimators_])

            test_uncertainty[:, index] = np.std(individual_test_predictions,
                                                axis=0)

        test_predictions = np.nanmean(test_predictions, 1)
        test_uncertainty = np.nanmean(test_uncertainty, 1)

        test_preds = self.scaler.inverse_transform(test_predictions).tolist()
        var_preds = self._scale_uncertainty(test_uncertainty).tolist()

        # TODO: Test returning average of D-MPNN preds instead of RF preds
        return test_preds, var_preds

    def train_estimator(self, path):
        """
        Trains an RF estimator during D-MPNN model training and saves it for
        later use during prediction time.
        :str path: path to save RF model
        """

        avg_last_hidden = self._compute_hidden_vals()

        transformed_targets = self.scaler.transform(
            np.array(self.data.targets()))

        n_trees = 128
        for task in range(self.num_tasks):
            forest = RandomForestRegressor(n_estimators=n_trees)
            forest.fit(avg_last_hidden, transformed_targets[:, task])

        # Save model for use in preds
        with open(path, 'wb') as f:
            pickle.dump(forest, f)

class GaussianProcessEstimator(ExposureEstimator):
    """
    A GaussianProcessEstimator trains a Gaussian process to
    operate on data transformed by the provided model.
    Uncertainty and predictions are calculated using
    the output of the Gaussian process.
    """
    def calculate_UQ(self):

        avg_last_hidden_test = self._compute_hidden_vals()

        test_predictions = np.ndarray(
            shape=(len(self.data.smiles()), self.num_models))
        test_uncertainty = np.ndarray(
            shape=(len(self.data.smiles()), self.num_models))

        for index in range(self.num_models):
            model_path = os.path.join(self.args.unc_save_dir,
                                      f'unc_estimator_{index}.pickle')

            # load model first
            with open(model_path, 'rb') as f:
                gaussian = pickle.load(f)

            avg_test_preds, avg_test_var = gaussian.predict(
                avg_last_hidden_test)

            test_predictions[:, index:index + 1] = avg_test_preds
            test_uncertainty[:, index:index + 1] = np.sqrt(avg_test_var)

        test_predictions = np.nanmean(test_predictions, 1)
        test_uncertainty = np.nanmean(test_uncertainty, 1)

        # test_preds = self.scaler.inverse_transform(test_predictions).tolist()
        test_preds = [item for sublist in self.final_preds for item in sublist]
        var_preds = self._scale_uncertainty(test_uncertainty).tolist()

        # TODO: Test returning average of D-MPNN preds instead of Gaussian pred
        return test_preds, var_preds

    def train_estimator(self, path):
        """
        Trains a gaussian estimator during D-MPNN model training and saves it
        for later use during prediction time.
        :str path: path to save gaussian model
        """
        avg_last_hidden = self._compute_hidden_vals()

        transformed_targets = self.scaler.transform(
            np.array(self.data.targets()))

        for task in range(self.num_tasks):
            kernel = GPy.kern.Linear(input_dim=self.args.hidden_size)

            # Was sparse -- maybe thats why values so close?
            gaussian = GPy.models.GPRegression(
                avg_last_hidden,
                transformed_targets[:, task:task + 1], kernel)

            gaussian.optimize()

        # Save model for use in preds
        with open(path, 'wb') as f:
            pickle.dump(gaussian, f)

class MVEEstimator(UncertaintyEstimator):
    """
    An MVEEstimator alters NN structure to produce twice as many outputs.
    Half correspond to predicted labels and half correspond to uncertainties.
    """
    def __init__(self,
                 args: Namespace,
                 data,
                 scaler: StandardScaler):

        super().__init__(args, data, scaler)

        # Hard coded to do ONE task ... change if used for multitask
        self.sum_test_uncertainty = np.zeros((len(self.data.smiles()), 1))

        self.sum_test_preds = np.zeros((len(self.data.smiles()), 1))

    def process_model(self, model: nn.Module, N=0):

        test_preds, test_uncertainty = predict(
            model=model,
            data_loader=self.data_loader,
            scaler=self.scaler,
        )

        if len(test_preds) != 0:
            self.sum_test_preds += np.array(test_preds)
            self.sum_test_uncertainty += np.array(test_uncertainty).clip(min=0)
            self.counter += 1

    def calculate_UQ(self):
        test_preds = self.sum_test_preds / self.counter
        var_preds = np.sqrt(self.sum_test_uncertainty / self.counter)
        test_preds = [item for sublist in test_preds for item in sublist]
        var_preds = [item for sublist in var_preds for item in sublist]

        return test_preds, var_preds

def uncertainty_estimator_builder(uncertainty_method: str):
    """
    Directory for getting the correct class of uncertainty method
    depending upon which one the user has specified.
    """

    return {
        'Dropout_VI': Dropout_VI,
        'Ensemble': Ensemble_estimator,
        'random_forest': RandomForestEstimator,
        'gaussian': GaussianProcessEstimator,
        'mve': MVEEstimator
    }[uncertainty_method]

max_stars_repo_name: opnbnch/chemprop

max_issues_repo_name: opnbnch/chemprop

max_forks_repo_name: opnbnch/chemprop


text:
import numpy as np
from numpy.testing import assert_array_equal
import pytest
import dask.array as da
import zarr

from skallel_tensor.utils import DictGroup
from skallel_tensor.api import GroupSelection, GroupConcatenation
from skallel_tensor import (
    select_slice,
    select_indices,
    select_mask,
    select_range,
    select_values,
    concatenate,
)

def test_select_slice():

    # Setup.
    pos = np.arange(100)
    gt = np.random.randint(low=-1, high=4, size=(100, 10))

    # Numpy array.
    for a in pos, gt:
        expect = a[10:20:2]
        actual = select_slice(a, start=10, stop=20, step=2, axis=0)
        assert isinstance(actual, np.ndarray)
        assert_array_equal(expect, actual)

    # Dask array.
    for a in pos, gt:
        expect = a[10:20:2]
        d = da.from_array(a)
        actual = select_slice(d, start=10, stop=20, step=2, axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())

    # Numpy group.
    g = DictGroup({"variants": {"POS": pos}, "calldata": {"GT": gt}})
    actual = select_slice(g, start=10, stop=20, step=2, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], np.ndarray)
    assert isinstance(actual["calldata"]["GT"], np.ndarray)
    assert_array_equal(pos[10:20:2], actual["variants"]["POS"])
    assert_array_equal(gt[10:20:2], actual["calldata"]["GT"])

    # Check mapping methods work.
    assert len(g) == len(actual)
    assert sorted(g) == sorted(actual)
    assert sorted(g.keys()) == sorted(actual.keys())
    for v in actual.values():
        assert isinstance(v, (np.ndarray, GroupSelection))
    for k, v in actual.items():
        assert k in g
        assert isinstance(v, (np.ndarray, GroupSelection))
    for k in g:
        assert k in actual

    # Zarr group.
    g = zarr.group()
    g.create_dataset("variants/POS", data=pos)
    g.create_dataset("calldata/GT", data=gt)
    actual = select_slice(g, start=10, stop=20, step=2, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], da.Array)
    assert isinstance(actual["calldata"]["GT"], da.Array)
    assert_array_equal(pos[10:20:2], actual["variants"]["POS"].compute())
    assert_array_equal(gt[10:20:2], actual["calldata"]["GT"].compute())

def test_select_indices():

    # Setup.
    pos = np.arange(100)
    gt = np.random.randint(low=-1, high=4, size=(100, 10))
    indices = np.arange(1, 99, 3)

    # Numpy array.
    for a in pos, gt:
        expect = a.take(indices, axis=0)
        actual = select_indices(a, indices, axis=0)
        assert isinstance(actual, np.ndarray)
        assert_array_equal(expect, actual)

    # Dask array.
    for a in pos, gt:
        expect = a.take(indices, axis=0)
        d = da.from_array(a)
        actual = select_indices(d, indices, axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())

    # Numpy group.
    g = DictGroup({"variants": {"POS": pos}, "calldata": {"GT": gt}})
    actual = select_indices(g, indices, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], np.ndarray)
    assert isinstance(actual["calldata"]["GT"], np.ndarray)
    assert_array_equal(pos.take(indices, axis=0), actual["variants"]["POS"])
    assert_array_equal(gt.take(indices, axis=0), actual["calldata"]["GT"])

    # Zarr group.
    g = zarr.group()
    g.create_dataset("variants/POS", data=pos)
    g.create_dataset("calldata/GT", data=gt)
    actual = select_indices(g, indices, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], da.Array)
    assert isinstance(actual["calldata"]["GT"], da.Array)
    assert_array_equal(
        pos.take(indices, axis=0), actual["variants"]["POS"].compute()
    )
    assert_array_equal(
        gt.take(indices, axis=0), actual["calldata"]["GT"].compute()
    )

def test_select_mask():

    # Setup.
    pos = np.arange(100)
    gt = np.random.randint(low=-1, high=4, size=(100, 10))
    mask = np.zeros(100, dtype=bool)
    mask[1:99:3] = True

    # Numpy array.
    for a in pos, gt:
        expect = a.compress(mask, axis=0)
        actual = select_mask(a, mask, axis=0)
        assert isinstance(actual, np.ndarray)
        assert_array_equal(expect, actual)

    # Dask array.
    for a in pos, gt:
        expect = a.compress(mask, axis=0)
        d = da.from_array(a)
        actual = select_mask(d, mask, axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())
        # With mask as dask array.
        actual = select_mask(d, da.from_array(mask), axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())

    # Numpy group.
    g = DictGroup({"variants": {"POS": pos}, "calldata": {"GT": gt}})
    actual = select_mask(g, mask, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], np.ndarray)
    assert isinstance(actual["calldata"]["GT"], np.ndarray)
    assert_array_equal(pos.compress(mask, axis=0), actual["variants"]["POS"])
    assert_array_equal(gt.compress(mask, axis=0), actual["calldata"]["GT"])

    # Zarr group.
    g = zarr.group()
    g.create_dataset("variants/POS", data=pos)
    g.create_dataset("calldata/GT", data=gt)
    actual = select_mask(g, mask, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], da.Array)
    assert isinstance(actual["calldata"]["GT"], da.Array)
    assert_array_equal(
        pos.compress(mask, axis=0), actual["variants"]["POS"].compute()
    )
    assert_array_equal(
        gt.compress(mask, axis=0), actual["calldata"]["GT"].compute()
    )

def test_select_range():

    # Setup.
    pos = np.arange(1, 300, 3)
    gt = np.random.randint(low=-1, high=4, size=(100, 10))

    # Numpy array.
    for a in pos, gt:
        expect = a[10:20]
        actual = select_range(a, pos, begin=30, end=60, axis=0)
        assert isinstance(actual, np.ndarray)
        assert_array_equal(expect, actual)

    # Dask array.
    for a in pos, gt:
        expect = a[10:20]
        d = da.from_array(a)
        actual = select_range(d, pos, begin=30, end=60, axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())

    # Numpy group.
    g = DictGroup({"variants": {"POS": pos}, "calldata": {"GT": gt}})
    actual = select_range(g, "variants/POS", begin=30, end=60, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], np.ndarray)
    assert isinstance(actual["calldata"]["GT"], np.ndarray)
    assert_array_equal(pos[10:20], actual["variants"]["POS"])
    assert_array_equal(gt[10:20], actual["calldata"]["GT"])

    # Zarr group.
    g = zarr.group()
    g.create_dataset("variants/POS", data=pos)
    g.create_dataset("calldata/GT", data=gt)
    actual = select_range(g, "variants/POS", begin=30, end=60, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], da.Array)
    assert isinstance(actual["calldata"]["GT"], da.Array)
    assert_array_equal(pos[10:20], actual["variants"]["POS"].compute())
    assert_array_equal(gt[10:20], actual["calldata"]["GT"].compute())

def test_select_values():

    # Setup.
    pos = np.arange(1, 300, 3)
    gt = np.random.randint(low=-1, high=4, size=(100, 10))
    query = [31, 61]

    # Numpy array.
    for a in pos, gt:
        expect = a[[10, 20]]
        actual = select_values(a, pos, query, axis=0)
        assert isinstance(actual, np.ndarray)
        assert_array_equal(expect, actual)

    # Dask array.
    for a in pos, gt:
        expect = a[[10, 20]]
        d = da.from_array(a)
        actual = select_values(d, pos, query, axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())

    # Numpy group.
    g = DictGroup({"variants": {"POS": pos}, "calldata": {"GT": gt}})
    actual = select_values(g, "variants/POS", query, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], np.ndarray)
    assert isinstance(actual["calldata"]["GT"], np.ndarray)
    assert_array_equal(pos[[10, 20]], actual["variants"]["POS"])
    assert_array_equal(gt[[10, 20]], actual["calldata"]["GT"])

    # Zarr group.
    g = zarr.group()
    g.create_dataset("variants/POS", data=pos)
    g.create_dataset("calldata/GT", data=gt)
    actual = select_values(g, "variants/POS", query, axis=0)
    assert isinstance(actual, GroupSelection)
    assert isinstance(actual["variants"]["POS"], da.Array)
    assert isinstance(actual["calldata"]["GT"], da.Array)
    assert_array_equal(pos[[10, 20]], actual["variants"]["POS"].compute())
    assert_array_equal(gt[[10, 20]], actual["calldata"]["GT"].compute())

    # Errors.
    with pytest.raises(KeyError):
        select_values(gt, pos, query=[1, 999], axis=0)

def test_concatenate():

    # Setup.
    pos = np.arange(1, 300, 3)
    gt = np.random.randint(low=-1, high=4, size=(100, 10))

    # Numpy array.
    for a in pos, gt:

        # Concatenate dim0.
        expect = np.concatenate([a, a], axis=0)
        actual = concatenate([a, a], axis=0)
        assert isinstance(actual, np.ndarray)
        assert_array_equal(expect, actual)

    # Concatenate dim1.
    expect = np.concatenate([gt, gt], axis=1)
    actual = concatenate([gt, gt], axis=1)
    assert isinstance(actual, np.ndarray)
    assert_array_equal(expect, actual)

    # Dask array.
    for a in pos, gt:

        # Concatenate dim0.
        expect = np.concatenate([a, a], axis=0)
        d = da.from_array(a)
        actual = concatenate([d, d], axis=0)
        assert isinstance(actual, da.Array)
        assert_array_equal(expect, actual.compute())

    # Concatenate dim1.
    expect = np.concatenate([gt, gt], axis=1)
    d = da.from_array(gt)
    actual = concatenate([d, d], axis=1)
    assert isinstance(actual, da.Array)
    assert_array_equal(expect, actual)

    # Numpy group.
    g = DictGroup({"variants": {"POS": pos}, "calldata": {"GT": gt}})
    actual = concatenate([g, g], axis=0)
    assert isinstance(actual, GroupConcatenation)
    assert isinstance(actual["variants"]["POS"], np.ndarray)
    assert isinstance(actual["calldata"]["GT"], np.ndarray)
    assert_array_equal(
        np.concatenate([pos, pos], axis=0), actual["variants"]["POS"]
    )
    assert_array_equal(
        np.concatenate([gt, gt], axis=0), actual["calldata"]["GT"]
    )

    # Check mapping methods work.
    assert len(g) == len(actual)
    assert sorted(g) == sorted(actual)
    assert sorted(g.keys()) == sorted(actual.keys())
    for v in actual.values():
        assert isinstance(v, (np.ndarray, GroupConcatenation))
    for k, v in actual.items():
        assert k in g
        assert isinstance(v, (np.ndarray, GroupConcatenation))
    for k in g:
        assert k in actual

    # Zarr group.
    g = zarr.group()
    g.create_dataset("variants/POS", data=pos)
    g.create_dataset("calldata/GT", data=gt)
    actual = concatenate([g, g], axis=0)
    assert isinstance(actual["variants"]["POS"], da.Array)
    assert isinstance(actual["calldata"]["GT"], da.Array)
    assert_array_equal(
        np.concatenate([pos, pos], axis=0), actual["variants"]["POS"].compute()
    )
    assert_array_equal(
        np.concatenate([gt, gt], axis=0), actual["calldata"]["GT"].compute()
    )

    # Test errors.
    with pytest.raises(TypeError):
        concatenate({"gt": gt}, axis=0)
    with pytest.raises(ValueError):
        concatenate([gt], axis=0)
    with pytest.raises(NotImplementedError):
        x = [1, 2, 3, 4]
        concatenate([x, x], axis=0)

max_stars_repo_name: alimanfoo/skallel-tensor

max_issues_repo_name: alimanfoo/skallel-tensor

max_forks_repo_name: alimanfoo/skallel-tensor

reason: import numpy,from numpy


text:
'''

PROGRAM TO RUN GUI ON SELECTED TARGETS FROM ROBOAO DATABASE

-----------------------------------------------------------

Prompts user for path to directory containing directory for each target as well as the names of each of these directories

Targets must have been run through 'preGUI.py' before using this program

Returns 'guiOut.txt' containing target name, tags gui, and object coordinates when applicable

Each time this GUI is run in the same directory, it will overwrite info in previously created text files

USING THE GUI

-------------

Navigation:

    Keys: left/right arrow keys or space bar to progress one at a time, Ctrl+left/right arrow keys progresses

    through 50 targets at a time

    Buttons: < or > to progress one at a time, << or >> to progress 10 at a time

Quality Check:

    Each target is tagged 'good' as default. Selecting 'good', 'uncertain', or 'bad' will overwrite other tags.

Location:

    Bottom left image is from DSS database, roboao field size is marked in faint brown box. Smaller green-outlined image

    is data, full field (35.6 arcsec). Presumed target location is marked in green. If this matches DSS image do nothing.

    If not, click on target and green circle will appear. Each time this position is re-selected it will overwrite

    previous position. Can also tag image for not enough stars or bad pointing. Reseting location undoes all location

    selections.

Companion:

    If no companion do mothing. Choose location of companion by clicking on the companion in any of the four

    bottom imags. For distant compaion, click in upper right image (8 arcsec across). Selecitons in close and far

    field are both saved and updated with new clicks. Resetting removes tags and clicked locations.

Notes Entry:

    Add any text note with no spaces or commas, press enter to save. Each time enter is pressed target's text file will

    be appended with note.

KEY FOR guiOut.txt

------------------

G: good

U: uncertain

B: bad

NES: not enough stars

INP: incorecct pointing

DDB: needs different database

CB: possible Close Binary

NM: needs to be looked at manually

Location: pixel coordinates of corrected target location

Companion: pixel coordinates of companion within 1.75"

Far Companion: pixel corrdinates companion within 4"

For Questions: Claire Lamman, claire.lamman@colorado.edu

'''

import Tkinter as tk

from Tkinter import *

from PIL import Image, ImageTk

import glob, sys, os

import numpy as np

#FILE PATHS, TARGET NAMES & INFO

path = raw_input('Enter path to data directory (Ex- C:/Users/Claire/RoboAO/Data/):') #prompt user for directory path

programnum = raw_input('Enter Program Number (Ex- 3):')                               #prompt user for program number

dpath = sorted(glob.glob(path+programnum+'_*'))                           #target directories

nlist = [s for s in os.listdir(path) if s.startswith(programnum+'_')]                     #make list of directory path

names = [i.split(".")[0] for i in (nlist[0::1])]                #Create list of object names

def convertCoords(xo, yo, pngDim, pngArcsec, xoff, yoff):

    '''

    Convert coordinates on a Tkinter canvas to cooresponding fits pixel coordinates for a specific target

    ----------------

    INPUT PARAMETERS

    xo, yo = original coordinates on canvas, relative to upper left corner, int

    pngDim = dimmensions of square Tkinter canvas, int

    pngArcsec = arcsecond across image displayed on square canvas, int

    xoff, yoff = target's offset from center of original fits image, in pixels, int

    '''

    cx = (xo-(pngDim/2.))*(pngArcsec/pngDim)        #Coordinates relative to center of png in arc seconds

    cy = ((pngDim/2.)-yo)*(pngArcsec/pngDim)

    dx = cx*(1000./17.4)                            #Pixel coordinates relative to target in 2048x2048 image

    dy = cy*(1000./17.4)                            #...17.4 miliarcsec/pixel

    fx = dx+xoff+1024                               #pixel coordinates in original fits file relative to lower left corner

    fy = dy+yoff+1024                               #...and offset by target's offset from center

    return fx, fy

class MyApp(object):
    """GUI to view, tag, and select various locations for data of multiple roboao targets"""
    def __init__(self, root):
        root = Toplevel()                           #Toplevel, not Tk to make python happy
        root.wm_title("Target Examination")         #Window Title
        root.configure(background='black')          #black background
        root.geometry('{}x{}'.format(1300, 750))    #sixe of window
        self.buttonframe = Frame(root, bd=1)        #use grids to controll label positions
        self.buttonframe.grid(row=8, column=20)

        #FRAME FOR CANVASES
        #position selector
        frame1 = Frame(root, highlightbackground="DarkOliveGreen4", highlightthickness=2, width=200, height=200) #frame color, size
        frame1.grid(row=6, column=4, rowspan=3, padx=20, pady=30, sticky=NW)                            #location on grid, space in y direction
        frame1.pack_propagate(False)

        #close companion selector
        frame2 = Frame(root, highlightbackground="cadet blue", highlightthickness=2, width=900, height=225) #frame color, size
        frame2.grid(row=9, column=4, rowspan=1, columnspan=50, sticky=NE)   #location on grid, spans columns, stuck to East
        frame2.pack_propagate(False)

        #far companion selector
        frame3 = Frame(root, highlightbackground="cadet blue", highlightthickness=2, width=400, height=400) #frame color, size
        frame3.grid(row=1, column=7, rowspan=7, columnspan=1, padx=20, sticky=S)    #location on grid, y space, stuck to West
        frame3.pack_propagate(False)

        root.bind('<Return>', self.enterKey)
        root.bind('<Left>', self.leftKey)               #Bind arrow and space bar keys for moving through targets
        root.bind('<Right>', self.rightKey)
        root.bind('<space>', self.spaceKey)
        root.bind('<Control-Left>', self.ctrleft)
        root.bind('<Control-Right>', self.ctright)
        root.protocol("WM_DELETE_WINDOW", on_closing)   #If window closes shut everything down

        com0=[500]*(len(names)*2)                     #for saving circle coordinates
        com1=[1000]*(len(names)*2)
        com2=[500]*(len(names)*2)

        global ct           #declare global variable
        ct=-1               #start with -1 so becomes 0 when function is called upon gui opening
        global nxtTarget
        def nxtTarget(f):   #Function to bring up individual information for each target
            global ct       #redefine ct globally each time function is called
            ct+=f

            if (ct+1)>len(names):               #Close window if there are no more targets
                on_closing()

            Label(root, text='no spaces, [Enter] to save', fg='dim gray', bg='black', font=("Helvetica", 8)).grid(row=7, column=6, sticky=SW, pady=20)
            global e1
            e1=Entry(root, width=25)
            e1.grid(row=7, column=6, sticky=SW)

            canvas1.coords(circle0, com0[ct*2]-8, com0[(ct*2)+1]-8, com0[ct*2]+8, com0[(ct*2)+1]+8,)   #update circle positions
            canvas2.coords(circle1, com1[ct*2]-8, com1[(ct*2)+1]-8, com1[ct*2]+8, com1[(ct*2)+1]+8)
            canvas2.coords(circle2, com1[ct*2]+225-8, com1[(ct*2)+1]-8, com1[ct*2]+225+8, com1[(ct*2)+1]+8)
            canvas2.coords(circle3, com1[ct*2]+450-8, com1[(ct*2)+1]-8, com1[ct*2]+450+8, com1[(ct*2)+1]+8)
            canvas2.coords(circle4, com1[ct*2]+675-8, com1[(ct*2)+1]-8, com1[ct*2]+675+8, com1[(ct*2)+1]+8)
            canvas3.coords(circle5, com2[ct*2]-8, com2[(ct*2)+1]-8, com2[ct*2]+8, com2[(ct*2)+1]+8)

            #OPEN FILES
            inf=[]
            tinf = open(dpath[ct]+'/info4GUI.txt', 'r').readlines()   #target info
            for t in tinf:
                inf.extend([float(t.strip('\n'))])

            ftsim = sorted(glob.glob(dpath[ct]+'/*.png'))                         #all png image
            xpix = inf[1]                                  #get pixel coordinates of target
            ypix = inf[0]
            global xoffset
            xoffset = xpix-1024                                                  #target position relative to center of fits
            global yoffset
            yoffset = ypix-1024

            #CREATE TEXT FILE TO STORE RESULTS
            if os.path.isfile(dpath[ct]+'/guiOut.txt')==False:                  #only create file if one doesn't already exist
                guiOut = open(dpath[ct]+'/guiOut.txt', 'w+')                                                    #open/create blank file
                guiOut.write("%s \n%s \n%s \n%s \n%s \n%s \n%s \n%s \n%s \n%s \n%s \n%s \n" % ('Target:'+names[ct],       #create blank template
                    'G:x', 'U:-', 'B:-', 'NES:-', 'IP:-', 'DDB:-', 'CB:-', 'NM:-', 'Location:-', 'Companion:-', 'Far Companion:-'))
                guiOut.close()                                                                                #close file

            tinfr = []                                      #Blank array to store target info
            for i in tinf:
                if i != tinf[-1]:                           #Ignore last item in list: 'OK'
                    tinfr.append(str(round(float(i), 2)))   #round to two sf, convert back to string

            #SET UP WINDOW
            #Target Info
            Label(root, text=names[ct], fg='white', bg='black', font=("Helvetica", 16))\
                  .grid(row=0, column=0, columnspan=10, sticky=W)    #target name (color, font, location)
            Label(root, text='Core: '+str(inf[2])+'     Halo: '+str(inf[3])+'     Strehl: '+str(inf[4])+'%     Mag: '+str(inf[5]),\
                  fg='white', bg='black', font=('Helvetica', 12))\
            .grid(row=1, column=0, columnspan=4, sticky=W)          #target information (color, font, location)
            Label(root, text='Displaying Target  '+str(ct+1)+'/'+str(len(names)), fg='white', bg='black', font=('Helvetica', 12))\
            .grid(row=0, column=7, padx=20, sticky=E)                        #status on total targets, current/total (color, font, location)

            #Set up Images
            photo2 = ImageTk.PhotoImage((Image.open(ftsim[0])).resize((250, 150), Image.ANTIALIAS)) #contrast curve
            photo4 = ImageTk.PhotoImage((Image.open(ftsim[3])).resize((200, 200), Image.ANTIALIAS)) #full field image
            photo5 = ImageTk.PhotoImage((Image.open(ftsim[4])).resize((375, 375), Image.ANTIALIAS)) #database field
            photo6 = ImageTk.PhotoImage((Image.open(ftsim[1])).resize((900, 225), Image.ANTIALIAS)) #fits collage
            photo7 = ImageTk.PhotoImage((Image.open(ftsim[2])).resize((400, 400), Image.ANTIALIAS)) #larger field for companion

            label2 = Label(root, image=photo2)                          #create labels from the 2 images not displayed on a canvas
            label5 = Label(root, image=photo5, bg='DarkOliveGreen4')    #This one with a green border
            label2.image = photo2                                       #keep a reference!
            label5.image = photo5
            label2.grid(row=2, column=0, rowspan=4, padx=10, sticky=N)                     #contrast curve location
            label5.grid(row=6, column=0, rowspan=4, columnspan=4, pady=30, sticky=N) #database image location

            #Update previously created three canvases with images for new target
            photo4.image=photo4                     #full field image
            canvas1.itemconfig(img1, image=photo4)
            photo6.image=photo6                     #fits collage
            canvas2.itemconfig(img2, image=photo6)
            photo7.image=photo7                     #larger field for companion
            canvas3.itemconfig(img3, image=photo7)

        def selectloc1(event):
            '''For Location Check: save cursor position and place circle on mouse release'''
            x, y = event.x, event.y                                             #save mouse coordinates on canvas
            canvas1.coords(circle0, x-8, y-8, x+8, y+8)                         #cirlce around selected location
            fx, fy = convertCoords(x, y, 200., 35.6352, 0, 0)                   #convert to pixel coordinates
            editOut(9, 'Location: ('+str(fx)+' '+str(fy)+')')
            com0[ct*2], com0[(ct*2)+1] = x, y                                   #save circle location

        def selectloc2(event):
            '''For Close Companion Location: save cursor position and place circle on each of 4 images'''
            x, y = event.x, event.y                                             #save mouse coordinates on canvas
            #To account for user not clicking in left most image
            imcl = 225.      #lendth of one image in canvas
            if (x-imcl)<0:        #if click is in first image
                x-=0
            elif (x-(imcl*2.))<0: #if click is in second image...
                x-=imcl           #subtract 1 image length
            elif (x-(imcl*3.))<0: #if click is in third image...
                x-=(imcl*2.)      #subtract 2 image lengths
            elif (x-(imcl*4.))<0: #if click is in fourth image...
                x-=(imcl*3.)      #subtract 3 image lengths
            canvas2.coords(circle1, x-8, y-8, x+8, y+8)                         #circle around selected location for each image
            canvas2.coords(circle2, x+225-8, y-8, x+225+8, y+8)
            canvas2.coords(circle3, x+450-8, y-8, x+450+8, y+8)
            canvas2.coords(circle4, x+675-8, y-8, x+675+8, y+8)
            fx, fy = convertCoords(x, y, 225., 1.5, xoffset, yoffset)            #convert to pixel coordinates
            editOut(10, 'Companion: ('+str(fx)+' '+str(fy)+')')                  #update out text file
            com1[ct*2], com1[(ct*2)+1] = x, y                                    #save circle location

        def selectloc3(event):
            '''For Far Companion Location: save cursor position and place circle on mouse release'''
            x, y = event.x, event.y                                             #save mouse corrdinates on canvas
            canvas3.coords(circle5, x-8, y-8, x+8, y+8)                         #circle around selected location
            fx, fy = convertCoords(x, y, 400., 8., xoffset, yoffset)            #convert to pixel coordinates
            editOut(11, 'Far Companion: ('+str(fx)+' '+str(fy)+')')             #update out text file
            com2[ct*2], com2[(ct*2)+1] = x, y                                   #save circle location

        def editOut(line, mrk):
            '''Function to replace specific line in results file with given string'''
            with open(dpath[ct]+'/guiOut.txt', 'r') as file:
                data = file.readlines()                                         #read list of lines into data
            data[line] = mrk+'\n'                                               #replace line with new entry and end that line
            with open(dpath[ct]+'/guiOut.txt', 'w') as file:
                file.writelines(data)                                           #rewrite out file

        def rmCircle(circlenum):
            '''Remove previously placed circle from view in canvas'''
            if circlenum==0:                                                    #circle for location selection
                com0[ct*2], com0[(ct*2)+1] = 500, 501
            if circlenum==1:                                                    #circles for companion selection
                com1[ct*2], com1[(ct*2)+1] = 5000, 5001
                com2[ct*2], com2[(ct*2)+1] = 5000, 5001
            canvas1.coords(circle0, com0[ct*2]-8, com0[(ct*2)+1]-8, com0[ct*2]+8, com0[(ct*2)+1]+8,)   #update circle positions
            canvas2.coords(circle1, com1[ct*2]-8, com1[(ct*2)+1]-8, com1[ct*2]+8, com1[(ct*2)+1]+8)
            canvas2.coords(circle2, com1[ct*2]+225-8, com1[(ct*2)+1]-8, com1[ct*2]+225+8, com1[(ct*2)+1]+8)
            canvas2.coords(circle3, com1[ct*2]+450-8, com1[(ct*2)+1]-8, com1[ct*2]+450+8, com1[(ct*2)+1]+8)
            canvas2.coords(circle4, com1[ct*2]+675-8, com1[(ct*2)+1]-8, com1[ct*2]+675+8, com1[(ct*2)+1]+8)
            canvas3.coords(circle5, com2[ct*2]-8, com2[(ct*2)+1]-8, com2[ct*2]+8, com2[(ct*2)+1]+8)

        def sequence(*functions):
            '''Funciton to combine multiple functions (so buttons can have multiple commands)'''
            def func(*args, **kwargs):
                return_value = None
                for function in functions:
                    return_value = function(*args, **kwargs)
                return return_value
            return func

        global save_note
        def save_note():
            '''function to append note to targets text file'''
            with open(dpath[ct]+'/guiOut.txt', 'a') as file:
                file.write(', Note: %s' % (e1.get()))

        #CANVASES FOR POSITION SELECTION
        #Position Check
        canvas1 = Canvas(frame1, width=200, height=200, highlightthickness=0, cursor='@arrow.cur')  #create canvas, size, cursor
        canvas1.pack(expand = YES, fill = BOTH)
        img1 = canvas1.create_image(0, 0, anchor=NW)                                    #give canvas no image initially
        circle0 = canvas1.create_oval(500, 500, 501, 501, outline='chartreuse2')        #create out-of-sight circle
        canvas1.bind("<ButtonPress-1>", selectloc1)                                     #if click here, call selectloc1 function

        canvas2 = Canvas(frame2, width=900, height=225, highlightthickness=0, cursor='@arrow.cur')  #canvas frame, size, cursor
        canvas2.pack(expand = YES, fill = BOTH)
        img2 = canvas2.create_image(0, 0, anchor=NW)                                    #no initial image
        circle1 = canvas2.create_oval(500, 500, 501, 501, outline='chartreuse2')        #create out-of-sight circle for each image
        circle2 = canvas2.create_oval(500, 500, 501, 501, outline='chartreuse2')
        circle3 = canvas2.create_oval(500, 500, 501, 501, outline='chartreuse2')
        circle4 = canvas2.create_oval(500, 500, 501, 501, outline='black')
        canvas2.bind("<ButtonPress-1>", selectloc2)                                     #if click here, call selectloc2 function

        canvas3 = Canvas(frame3, width=400, height=400, highlightthickness=0, cursor='@arrow.cur')  #canvas frame, location, cursor
        canvas3.pack(expand = YES, fill = BOTH)
        img3 = canvas3.create_image(0, 0, anchor=NW)                                    #set up for but don't include image yet
        circle5 = canvas3.create_oval(500, 500, 501, 501, outline='black')        #create out-of-sight circle
        canvas3.bind("<ButtonPress-1>", selectloc3)                                     #if click here, call selectloc3 function

        canvas3.create_oval(0, 0, 400, 400, outline='white')

        #LABELS AND BUTTONS
        #Input Field for Notes
        Label(root, text="Note:", fg='white', bg='black', font=("Helvetica", 12)).grid(row=7, column=6, sticky=SW, pady=35)
        #Quality Check Label
        Label(root, text='QUALITY CHECK', fg='white', bg='black', font=("Helvetica", 12, 'bold'))\
            .grid(row=2, column=1, columnspan=3, padx=15, sticky=N) #location on grid
        #Location Label
        Label(root, text='LOCATION', fg='DarkOliveGreen4', bg='black', font=("Helvetica", 12, 'bold'))\
            .grid(row=2, column=4, sticky=N)
        #Companion Label
        Label(root, text='COMPANION', fg='cadet blue', bg='black', font=("Helvetica", 12, 'bold'))\
            .grid(row=2, column=6, padx=10, sticky=N)
        #Image Labels
        Label(root, text='DSS Database Image', fg='DarkOliveGreen4', bg='black', font=('Helvetica', 8))\
            .grid(row=9, column=0, sticky=SW, padx=35, pady=55)                        #Database iamge label
        Label(root, text='Full Field Fits                            N ^    E ->', fg='DarkOliveGreen4', bg='black', font=('Helvetica', 8))\
            .grid(row=8, column=4, sticky=NW, padx=20, pady=10)                        #Full field
        Label(root, text='Field = 1.5" across', fg='cadet blue', bg='black', font=('Helvetica', 8))\
            .grid(row=9, column=4, sticky=SW, padx=10)                        #Full field
        Label(root, text='Field = 8" across', fg='cadet blue', bg='black', font=('Helvetica', 8))\
            .grid(row=8, column=7, sticky=NW, padx=30)                        #Full field

        #Button, name, command, size, color, .position
        G = Button(root, text = u'\u2713', command=lambda: sequence(editOut(1,'G:x'), editOut(2,'U:-'), editOut(3, 'B:-')),\
              height=1, width=3, bg='chartreuse4', font=('Helvetica', 12)).grid(row=3, column=1, sticky=N)            #good
        U = Button(root, text="?", command=lambda: sequence(editOut(1,'G:-'), editOut(2,'U:x'), editOut(3, 'B:-')),\
              height=1, width=3, bg='gold3', font=('Helvetica', 12, 'bold')).grid(row=3, column=2, sticky=N)                     #unsure
        B = Button(root, text="X", command=lambda: sequence(editOut(1,'G:-'), editOut(2,'U:-'), editOut(3, 'B:x')),\
              height=1, width=3, bg='IndianRed4', font=('Helvetica', 12, 'bold')).grid(row=3, column=3, sticky=N) #bad
        Bck = Button(root, text="<", command=lambda: nxtTarget(-1), height=1, width=2, bg='grey',\
              font=('Helvetica', 12, 'bold')).grid(row=4, column=1, sticky=NE) #back
        Nxt = Button(root, text=">", command=lambda: nxtTarget(1), height=1, width=2, bg='grey',\
              font=('Helvetica', 12, 'bold')).grid(row=4, column=3, sticky=NW) #next
        FBW = Button(root, text="<<", command=lambda: nxtTarget(-10), height=1, width=2, bg='grey',\
              font=('Helvetica', 12, 'bold')).grid(row=5, column=1, sticky=NE) #fast-backwards
        FFW = Button(root, text=">>", command=lambda: nxtTarget(10), height=1, width=2, bg='grey',\
              font=('Helvetica', 12, 'bold')).grid(row=5, column=3, sticky=NW) #fast-forward
        NES = Button(root, text="Not Enough Stars", command=lambda: editOut(4, 'NES:x'), height=1, width=15, bg='DarkOliveGreen4',\
              font=('Helvetica', 12)).grid(row=3, column=4, padx=20, sticky=N)            #not enought stars
        INP = Button(root, text="Incorrect Pointing", command=lambda: editOut(5, 'INP:x'), height=1, width=15, bg='DarkOliveGreen4',\
              font=('Helvetica', 12)).grid(row=4, column=4, padx=20, sticky=N)  #incorrect pointing
        DDB = Button(root, text="Needs diff Database", command=lambda: editOut(6, 'DDB:x'), height=1, width=15, bg='DarkOliveGreen4',\
              font=('Helvetica', 12)).grid(row=5, column=4, sticky=N, padx=20)  #Needs different database
        LOk = Button(root, text = 'Reset Location', command=lambda: sequence(editOut(9, 'Location:-'), rmCircle(0), editOut(5, 'INP:-'), editOut(4, 'NES:-'), editOut(6, 'DDB:-')),\
              height=1, width=15, fg='DarkOliveGreen4', bg='gray25', font=('Helvetica', 12)).grid(row=7, column=6, sticky=NW)  #location is OK
        NC = Button(root, text="Reset Companion", command=lambda: sequence(editOut(10, 'Companion:-'), editOut(11, 'Far Companion:-'), rmCircle(1), editOut(7, 'CB:-'), editOut(8, 'NM:-')),\
              height=1, width=15, fg='cadet blue', bg='gray25', font=('Helvetica', 12)).grid(row=7, column=6, pady=40, sticky=NW)  #no companion
        CB = Button(root, text="Poss. Close Binary", command=lambda: editOut(7, 'CB:x'),\
              height=1, width=15, bg='cadet blue', font=('Helvetica', 12)).grid(row=3, column=6, sticky=NW)  #possible close binary
        NM = Button(root, text="Look at Manually", command=lambda: editOut(8, 'NM:x'),\
              height=1, width=15, bg='cadet blue', font=('Helvetica', 12)).grid(row=4, column=6, sticky=NW)  #no companion

        nxtTarget(1) #Star GUI with first target

    @staticmethod           #use static function so can be called by all classes
    def enterKey(event):     #if press left arrow key, go back one target
        save_note()

    @staticmethod           #use static function so can be called by all classes
    def leftKey(event):     #if press left arrow key, go back one target
        nxtTarget(-1)

    @staticmethod
    def rightKey(event):    #if press right arrow key, go to next target
        nxtTarget(1)

    @staticmethod
    def spaceKey(event):    #if press space bar, go to next target
        nxtTarget(1)

    @staticmethod
    def ctrleft(event):    #if press space bar, go to next target
        nxtTarget(-50)

    @staticmethod
    def ctright(event):    #if press space bar, go to next target
        nxtTarget(50)

def on_closing():       #If window closes shut everything down
    root.destroy()      #Kill GUI
    sys.exit()          #shut down (might make python angry, can be removed)

root = Tk()
root.withdraw()   #stop extra window from appearing
MyApp(root)
root.title('Tkinter Widgets')
root.mainloop()

max_stars_repo_name: cmlamman/GRAM-GUI

max_issues_repo_name: cmlamman/GRAM-GUI

max_forks_repo_name: cmlamman/GRAM-GUI