text:
"""Get Highway data from lines, polygons and points table in database.

and returns csv files for each.
"""
import pandas as pd
from db_connect import dbconn_from_args
import logging
import os
from pathlib import Path
import numpy as np

logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
                    filename="../code/log/flexigis_highways.log",
                    level=logging.DEBUG)

# output data destination
destination = "../data/02_urban_output_data/"
if Path(destination).exists():
    logging.info("directory {} already exists.".
                 format("02_urban_output_datas"))
    pass
else:
    os.mkdir(destination)
    logging.info("directory {} succesfully created!.".
                 format("02_urban_output_datas"))

def compute_area(dataset, width):
    """Compute area for each line feature and return a dataframe object.

    dataset: Dataframe object
    width: Dictionary, unique highway category as key and the width in meters
    as value.

    """
    #Area = []
    dataset['area'] = np.nan
    for key, value in width.items():
        #area = dataset.loc[key]["length"]*value
        dataset.loc[key, 'area'] = dataset.loc[key]["length"]*value
        #Area.append(area)
    #Area = pd.concat(Area)
    #dataset["area"] = Area.values
    dataset_new = dataset.reset_index()
    return dataset_new

def data_to_csv(dataset, name="name"):
    """Write data to csv file.

    dataset: dataframe object
    name: str object, name of the output csv file (the table name).
    """
    dataset_new = dataset["geometry"].str.split(";", n=1, expand=True)
    dataset["polygon"] = dataset_new[1]
    dataset = dataset.drop(columns=["geometry"])
    dataset = dataset.rename(columns={"polygon": "geometry"})
    dataset.to_csv(name, encoding="utf-8")

class GetLines:
    """Object class that gets data from database and export output to csv.

    get_line_from_db: returns querried database table as pandas dataframe.
    get_line_features: gets highway categories(lines), calculate thier area and
    returns dataset as csv file.
    """

    def get_line_from_db(self, cur, conn):
        """Query database and return data as dataframe."""
        # fetch high way column from db (line table)
        self.cur = cur
        self.conn = conn
        self.table = "planet_osm_line"
        self.ways_column = "highway"

        # query database
        # Convert postgres encoded geometric projection to EPSG:3857 format
        # EPSG:3857 is a Spherical Mercator projection coordinate system
        # popularized by web services such as Google and later OpenStreetMap.
        # https://wiki.openstreetmap.org/wiki/EPSG:3857
        sql = "SELECT osm_id, highway, ST_Length(ST_Transform(way, 3857)) as l,\
         ST_ASEWKT(ST_Transform(way, 3857)) as p FROM planet_osm_line"

        self.cur.execute(sql)
        self.rows = self.cur.fetchall()

        # save selected columns as pandas dataframe
        self.df = pd.DataFrame(self.rows, columns=["osm_id", self.ways_column,
                                                   "length", "geometry"])
        self.data = self.df.dropna().sort_values(by="highway")
        logging.info("line properties for highway extracted from database.")
        return self.data

    # get features from dataframe
    def get_line_features(self, dataset):
        """Get OSM High way features."""
        self.highway_feature = {'living_street', 'motorway', 'pedestrian',
                                'primary', 'secondary', 'service', 'tertiary',
                                'trunk', 'motorway_link', 'primary_link',
                                'secondary_link', 'tertiary_link',
                                'trunk_link'}
        self.width = [7.5, 15.50, 7.5, 10.5, 9.5, 7.5, 9.5, 9.5, 6.5, 6.5,
                      6.5, 6.5, 6.5]
        self.highway_width_ = dict(zip(self.highway_feature, self.width))
        self.dataset = dataset.loc[dataset["highway"].
                                   isin(self.highway_feature)]
        self.new_data_ = self.dataset.set_index("highway")

        # compute area and save data to csv
        check_features_ = set(self.new_data_.index.unique()).intersection(self.highway_feature)
        width_ = {k: self.highway_width_[k] for k in check_features_}
        new_data = compute_area(self.new_data_, width_)
        data_to_csv(new_data, destination+self.table+".csv")
        logging.info("csv file of line properties generated.")

class GetPolygons:
    """Gets highway polygons from database and export output to csv.

    get_polygons_from_db: returns querried database table as pandas dataframe.
    get_polygons_features: returns csv file of highway categories(polygons).
    """

    def get_polygons_from_db(self, cur, conn):
        """Query database and return data as dataframe."""
        # fetch high way column from db (line table)
        self.cur = cur
        self.conn = conn
        self.table = "planet_osm_polygon"
        self.ways_column = "highway"

        sql = "SELECT osm_id, highway, ST_Area(ST_Transform(way, 3857)) a,\
         ST_ASEWKT(ST_Transform(way, 3857))as p FROM planet_osm_polygon"

        self.cur.execute(sql)
        self.rows = self.cur.fetchall()

        # save selected columns as pandas dataframe
        self.df = pd.DataFrame(self.rows, columns=[
            "osm_id", self.ways_column,
            "area", "geometry"])
        self.data = self.df.dropna().sort_values(by="highway")
        logging.info("polygon properties for highway extracted from database.")
        return self.data
    # get features from dataframe

    def get_polygons_features(self, dataset):
        """Get OSM High way features."""
        self.highway_feature = ['crossing', 'footway', 'living_street',
                                'pedestrian', 'platform', 'residential',
                                'service', 'traffic_island']
        self.dataset = dataset.loc[dataset["highway"].
                                   isin(self.highway_feature)]
        self.new_data_polygons = self.dataset.set_index(["highway"])
        data_to_csv(self.new_data_polygons, destination+self.table+".csv")
        logging.info("csv file for polygons generated.")

class GetPoints:
    """Gets highway points(Nodes) from database and export output to csv.

    get_point_from_db: returns querried database table as pandas dataframe.
    get_point_features: returns csv file of highway categories(points)
    """

    def get_point_from_db(self, cur, conn):
        """Extract database table."""
        # fetch high way column from db (line table)
        self.cur = cur
        self.conn = conn
        self.table = "planet_osm_point"
        self.ways_column = "highway"

        # query database
        sql = "SELECT osm_id, highway,ST_ASEWKT(ST_Transform(way, 3857))as p,\
         ST_X(ST_Transform (way, 3857)) as Longitude,\
          ST_Y(ST_Transform(way, 3857)) as Latitude FROM planet_osm_point"

        self.cur.execute(sql)
        self.rows = self.cur.fetchall()

        # save selected columns as pandas dataframe
        self.df = pd.DataFrame(self.rows, columns=[
            "osm_id", self.ways_column, "geometry", "Longitude", "Latitude"])
        self.data = self.df.dropna().sort_values(by="highway")
        logging.info("node properties for highway extracted from database.")
        return self.data
    # get features from dataframe

    def get_point_features(self, dataset):
        """Get OSM High way features."""
        self.highway_feature = ['bus_stop', 'crossing', 'give_way',
                                'motorway_junction', 'passing_place',
                                'platform', 'speed_camera', 'stop',
                                'street_lamp', 'traffic_signals']
        self.dataset = dataset.loc[dataset["highway"].
                                   isin(self.highway_feature)]
        self.new_data_points = self.dataset.set_index(["highway"])
        data_to_csv(self.new_data_points, destination+self.table+".csv")
        logging.info("csv file for points generated.")

if __name__ == "__main__":
    conn = dbconn_from_args()
    cur = conn.cursor()

    # get higways from lines
    print("  === HIGHWAY LINES ====")
    lines = GetLines()
    data_line = lines.get_line_from_db(cur, conn)
    lines.get_line_features(data_line)

    # get highway polygons
    print("  === HIGHWAY SHAPES ====")
    squares = GetPolygons()
    data_square = squares.get_polygons_from_db(cur, conn)
    squares.get_polygons_features(data_square)

    # get highway points
    print("  === HIGHWAY POINTS ====")
    points = GetPoints()
    data_point = points.get_point_from_db(cur, conn)
    points.get_point_features(data_point)


text:
import numpy as np
from ann.ecom_process import get_data

X, Y, _, _ = get_data()

# randomly initialize weights
M = 5  # hidden unit
N = X.shape[0]  # samples size
D = X.shape[1]  # no. features
K = len(set(Y))  # no. target

W1 = np.random.randn(D, M)
b1 = np.zeros(M)
W2 = np.random.randn(M, K)
b2 = np.zeros(K)

def softmax(a):
    expA = np.exp(a)
    return expA / expA.sum(axis=1, keepdims=True)

def forward(X, W1, b1, W2, b2):
    Z = X.dot(W1) + b1
    Z = np.tanh(Z)
    return softmax(Z.dot(W2) + b2)

P_Y_given_X = forward(X, W1, b1, W2, b2)
predictions = np.argmax(P_Y_given_X, axis=1)

def classification_rate(Y, P):
    return np.mean(Y == P)

print("Score", classification_rate(Y, predictions))

ext: py

max_stars_repo_name: opplieam/Udemy-Lazy

max_issues_repo_name: opplieam/Udemy-Lazy

max_forks_repo_name: opplieam/Udemy-Lazy

reason: import numpy


text:
#
# This code is intentionally missing!
# Read the directions on the course lab page!
#
import numpy as np
import matplotlib
import matplotlib as plt

import pandas as pd
from sklearn import preprocessing
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.manifold import Isomap

X = pd.read_csv('Datasets/parkinsons.data')
y = X.status
X = X.drop(['name','status'],axis=1)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=7)

prepro = preprocessing.StandardScaler()
prepro.fit(X_train)
X_train = prepro.transform(X_train)
X_test = prepro.transform(X_test)

#test components 4 to 6 and neighbors 2 to 5 inclusive on both
iso = Isomap(n_components=6,n_neighbors=5)
iso.fit(X_train)
X_train = iso.transform(X_train)
X_test = iso.transform(X_test)

best_score = [0,0,0]
for C in np.arange(0.05,2,0.05):
  for gamma in np.arange(0.001,0.1,0.001):
    svc = svm.SVC(C=C, gamma=gamma)
    svc.fit(X_train,y_train)
    score = svc.score(X_test,y_test)
    if score > best_score[0]:
      best_score[0] = score
      best_score[1] = C
      best_score[2] = gamma
print 'The best score was: ', best_score[0]
print 'with a C of: ' , best_score[1]
print 'and a gamma of: ' , best_score[2]


text:
"""
Contains torch Modules for value networks. These networks take an
observation dictionary as input (and possibly additional conditioning,
such as subgoal or goal dictionaries) and produce value or
action-value estimates or distributions.
"""
import numpy as np
from collections import OrderedDict

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributions as D

import robomimic.utils.tensor_utils as TensorUtils
from robomimic.models.obs_nets import MIMO_MLP
from robomimic.models.distributions import DiscreteValueDistribution

class ValueNetwork(MIMO_MLP):
    """
    A basic value network that predicts values from observations.
    Can optionally be goal conditioned on future observations.
    """
    def __init__(
        self,
        obs_shapes,
        mlp_layer_dims,
        value_bounds=None,
        visual_feature_dimension=64,
        visual_core_class='ResNet18Conv',
        visual_core_kwargs=None,
        obs_randomizer_class=None,
        obs_randomizer_kwargs=None,
        use_spatial_softmax=False,
        spatial_softmax_kwargs=None,
        goal_shapes=None,
    ):
        """
        Args:
            obs_shapes (OrderedDict): a dictionary that maps modality to
                expected shapes for observations.

            mlp_layer_dims ([int]): sequence of integers for the MLP hidden layers sizes.

            value_bounds (tuple): a 2-tuple corresponding to the lowest and highest possible return
                that the network should be possible of generating. The network will rescale outputs
                using a tanh layer to lie within these bounds. If None, no tanh re-scaling is done.

            visual_feature_dimension (int): feature dimension to encode images into.

            visual_core_class (str): specifies Visual Backbone network for encoding images.

            visual_core_kwargs (dict): arguments to pass to @visual_core_class.

            obs_randomizer_class (str): specifies a Randomizer class for the input modality

            obs_randomizer_kwargs (dict): kwargs for the observation randomizer

            use_spatial_softmax (bool): if True, introduce a spatial softmax layer at
                the end of the visual backbone network, resulting in a sharp bottleneck
                representation for visual inputs.

            spatial_softmax_kwargs (dict): arguments to pass to spatial softmax layer.

            goal_shapes (OrderedDict): a dictionary that maps modality to
                expected shapes for goal observations.
        """
        self.value_bounds = value_bounds
        if self.value_bounds is not None:
            # convert [lb, ub] to a scale and offset for the tanh output, which is in [-1, 1]
            self._value_scale = (float(self.value_bounds[1]) - float(self.value_bounds[0])) / 2.
            self._value_offset = (float(self.value_bounds[1]) + float(self.value_bounds[0])) / 2.

        assert isinstance(obs_shapes, OrderedDict)
        self.obs_shapes = obs_shapes

        # set up different observation groups for @MIMO_MLP
        observation_group_shapes = OrderedDict()
        observation_group_shapes["obs"] = OrderedDict(self.obs_shapes)

        self._is_goal_conditioned = False
        if goal_shapes is not None and len(goal_shapes) > 0:
            assert isinstance(goal_shapes, OrderedDict)
            self._is_goal_conditioned = True
            self.goal_shapes = OrderedDict(goal_shapes)
            observation_group_shapes["goal"] = OrderedDict(self.goal_shapes)
        else:
            self.goal_shapes = OrderedDict()

        output_shapes = self._get_output_shapes()
        super(ValueNetwork, self).__init__(
            input_obs_group_shapes=observation_group_shapes,
            output_shapes=output_shapes,
            layer_dims=mlp_layer_dims,
            visual_feature_dimension=visual_feature_dimension,
            visual_core_class=visual_core_class,
            visual_core_kwargs=visual_core_kwargs,
            obs_randomizer_class=obs_randomizer_class,
            obs_randomizer_kwargs=obs_randomizer_kwargs,
            use_spatial_softmax=use_spatial_softmax,
            spatial_softmax_kwargs=spatial_softmax_kwargs,
        )

    def _get_output_shapes(self):
        """
        Allow subclasses to re-define outputs from @MIMO_MLP, since we won't
        always directly predict values, but may instead predict the parameters
        of a value distribution.
        """
        return OrderedDict(value=(1,))

    def output_shape(self, input_shape=None):
        """
        Function to compute output shape from inputs to this module.

        Args:
            input_shape (iterable of int): shape of input. Does not include batch dimension.
                Some modules may not need this argument, if their output does not depend
                on the size of the input, or if they assume fixed size input.

        Returns:
            out_shape ([int]): list of integers corresponding to output shape
        """
        return [1]

    def forward(self, obs_dict, goal_dict=None):
        """
        Forward through value network, and then optionally use tanh scaling.
        """
        values = super(ValueNetwork, self).forward(obs=obs_dict, goal=goal_dict)["value"]
        if self.value_bounds is not None:
            values = self._value_offset + self._value_scale * torch.tanh(values)
        return values

    def _to_string(self):
        return "value_bounds={}".format(self.value_bounds)

class ActionValueNetwork(ValueNetwork):
    """
    A basic Q (action-value) network that predicts values from observations
    and actions. Can optionally be goal conditioned on future observations.
    """
    def __init__(
        self,
        obs_shapes,
        ac_dim,
        mlp_layer_dims,
        value_bounds=None,
        visual_feature_dimension=64,
        visual_core_class='ResNet18Conv',
        visual_core_kwargs=None,
        obs_randomizer_class=None,
        obs_randomizer_kwargs=None,
        use_spatial_softmax=False,
        spatial_softmax_kwargs=None,
        goal_shapes=None,
    ):
        """
        Args:
            obs_shapes (OrderedDict): a dictionary that maps modality to
                expected shapes for observations.

            ac_dim (int): dimension of action space.

            mlp_layer_dims ([int]): sequence of integers for the MLP hidden layers sizes.

            value_bounds (tuple): a 2-tuple corresponding to the lowest and highest possible return
                that the network should be possible of generating. The network will rescale outputs
                using a tanh layer to lie within these bounds. If None, no tanh re-scaling is done.

            visual_feature_dimension (int): feature dimension to encode images into.

            visual_core_class (str): specifies Visual Backbone network for encoding images.

            visual_core_kwargs (dict): arguments to pass to @visual_core_class.

            obs_randomizer_class (str): specifies a Randomizer class for the input modality

            obs_randomizer_kwargs (dict): kwargs for the observation randomizer

            use_spatial_softmax (bool): if True, introduce a spatial softmax layer at
                the end of the visual backbone network, resulting in a sharp bottleneck
                representation for visual inputs.

            spatial_softmax_kwargs (dict): arguments to pass to spatial softmax layer.

            goal_shapes (OrderedDict): a dictionary that maps modality to
                expected shapes for goal observations.
        """

        # add in action as a modality
        new_obs_shapes = OrderedDict(obs_shapes)
        new_obs_shapes["action"] = (ac_dim,)
        self.ac_dim = ac_dim

        # pass to super class to instantiate network
        super(ActionValueNetwork, self).__init__(
            obs_shapes=new_obs_shapes,
            mlp_layer_dims=mlp_layer_dims,
            value_bounds=value_bounds,
            visual_feature_dimension=visual_feature_dimension,
            visual_core_class=visual_core_class,
            visual_core_kwargs=visual_core_kwargs,
            obs_randomizer_class=obs_randomizer_class,
            obs_randomizer_kwargs=obs_randomizer_kwargs,
            use_spatial_softmax=use_spatial_softmax,
            spatial_softmax_kwargs=spatial_softmax_kwargs,
            goal_shapes=goal_shapes,
        )

    def forward(self, obs_dict, acts, goal_dict=None):
        """
        Modify forward from super class to include actions in inputs.
        """
        inputs = dict(obs_dict)
        inputs["action"] = acts
        return super(ActionValueNetwork, self).forward(inputs, goal_dict)

    def _to_string(self):
        return "action_dim={}\nvalue_bounds={}".format(self.ac_dim, self.value_bounds)

class DistributionalActionValueNetwork(ActionValueNetwork):
    """
    Distributional Q (action-value) network that outputs a categorical distribution over
    a discrete grid of value atoms. See https://arxiv.org/pdf/1707.06887.pdf for
    more details.
    """
    def __init__(
        self,
        obs_shapes,
        ac_dim,
        mlp_layer_dims,
        value_bounds,
        num_atoms,
        visual_feature_dimension=64,
        visual_core_class='ResNet18Conv',
        visual_core_kwargs=None,
        obs_randomizer_class=None,
        obs_randomizer_kwargs=None,
        use_spatial_softmax=False,
        spatial_softmax_kwargs=None,
        goal_shapes=None,
    ):
        """
        Args:
            obs_shapes (OrderedDict): a dictionary that maps modality to
                expected shapes for observations.

            ac_dim (int): dimension of action space.

            mlp_layer_dims ([int]): sequence of integers for the MLP hidden layers sizes.

            value_bounds (tuple): a 2-tuple corresponding to the lowest and highest possible return
                that the network should be possible of generating. This defines the support
                of the value distribution.

            num_atoms (int): number of value atoms to use for the categorical distribution - which
                is the representation of the value distribution.

            visual_feature_dimension (int): feature dimension to encode images into.

            visual_core_class (str): specifies Visual Backbone network for encoding images.

            visual_core_kwargs (dict): arguments to pass to @visual_core_class.

            obs_randomizer_class (str): specifies a Randomizer class for the input modality

            obs_randomizer_kwargs (dict): kwargs for the observation randomizer

            use_spatial_softmax (bool): if True, introduce a spatial softmax layer at
                the end of the visual backbone network, resulting in a sharp bottleneck
                representation for visual inputs.

            spatial_softmax_kwargs (dict): arguments to pass to spatial softmax layer.

            goal_shapes (OrderedDict): a dictionary that maps modality to
                expected shapes for goal observations.
        """

        # parameters specific to DistributionalActionValueNetwork
        self.num_atoms = num_atoms
        self._atoms = np.linspace(value_bounds[0], value_bounds[1], num_atoms)

        # pass to super class to instantiate network
        super(DistributionalActionValueNetwork, self).__init__(
            obs_shapes=obs_shapes,
            ac_dim=ac_dim,
            mlp_layer_dims=mlp_layer_dims,
            value_bounds=value_bounds,
            visual_feature_dimension=visual_feature_dimension,
            visual_core_class=visual_core_class,
            visual_core_kwargs=visual_core_kwargs,
            obs_randomizer_kwargs=obs_randomizer_kwargs,
            use_spatial_softmax=use_spatial_softmax,
            spatial_softmax_kwargs=spatial_softmax_kwargs,
            goal_shapes=goal_shapes,
        )

    def _get_output_shapes(self):
        """
        Network outputs log probabilities for categorical distribution over discrete value grid.
        """
        return OrderedDict(log_probs=(self.num_atoms,))

    def forward_train(self, obs_dict, acts, goal_dict=None):
        """
        Return full critic categorical distribution.

        Args:
            obs_dict (dict): batch of observations
            acts (torch.Tensor): batch of actions
            goal_dict (dict): if not None, batch of goal observations

        Returns:
            value_distribution (DiscreteValueDistribution instance)
        """

        # add in actions
        inputs = dict(obs_dict)
        inputs["action"] = acts

        # network returns unnormalized log probabilities (logits) for each of the value atoms
        logits = MIMO_MLP.forward(self, obs=inputs, goal=goal_dict)["log_probs"]

        # turn these logits into a categorical distribution over the value atoms.
        # (unsqueeze to make sure atoms are compatible with batch operations)
        value_atoms = torch.Tensor(self._atoms).unsqueeze(0).to(logits.device)
        return DiscreteValueDistribution(values=value_atoms, logits=logits)

    def forward(self, obs_dict, acts, goal_dict=None):
        """
        Return mean of critic categorical distribution. Useful for obtaining
        point estimates of critic values.

        Args:
            obs_dict (dict): batch of observations
            acts (torch.Tensor): batch of actions
            goal_dict (dict): if not None, batch of goal observations

        Returns:
            mean_value (torch.Tensor): expectation of value distribution
        """
        vd = self.forward_train(obs_dict=obs_dict, acts=acts, goal_dict=goal_dict)
        return vd.mean()

    def _to_string(self):
        return "action_dim={}\nvalue_bounds={}\nnum_atoms={}".format(self.ac_dim, self.value_bounds, self.num_atoms)

max_stars_repo_name: huihanl/robomimic

max_issues_repo_name: huihanl/robomimic

max_forks_repo_name: huihanl/robomimic


text:
import numpy as np
from brainscore.metrics import Metric, Score

NSAMPLES = 1000

class BootstrapDistributionSimilarity(Metric):
    def __init__(self, similarity_func, property_name, ns=NSAMPLES):
        self.similarity_func = similarity_func
        self.property_name = property_name
        self.ns = ns

    def __call__(self, model_property, data_property):
        bins = data_property.attrs[self.property_name+'_bins']
        data_property = data_property.loc[:, self.property_name].values
        model_property = model_property.loc[:, self.property_name].values

        data_property[data_property < bins[0]] = bins[0]
        data_property[data_property > bins[-1]] = bins[-1]
        model_property[model_property < bins[0]] = bins[0]
        model_property[model_property > bins[-1]] = bins[-1]

        n_neurons = data_property.shape[0]

        data_hist = np.histogram(data_property, bins=bins)[0]
        data_hist = data_hist / data_hist.sum()

        model_hist = np.zeros((self.ns, data_hist.shape[0]))
        dist_similarity = np.zeros(self.ns)

        for s in range(self.ns):
            sample = np.random.choice(model_property, n_neurons)
            model_hist[s, :] = np.histogram(sample, bins=bins)[0]
            model_hist[s, :] = model_hist[s, :] / model_hist[s, :].sum()
            dist_similarity[s] = self.similarity_func(model_hist[s, :], data_hist)

        if np.isnan(dist_similarity).sum() > NSAMPLES / 10:
            center = 0
            error = 0
        else:
            center = np.nanmean(dist_similarity)
            error = np.nanstd(dist_similarity)

        score = Score([center, error], coords={'aggregation': ['center', 'error']}, dims=('aggregation',))
        score.attrs[Score.RAW_VALUES_KEY] = dist_similarity

        self.data_hist = data_hist
        self.model_hist = model_hist
        self.bins = bins

        return score

    def get_dists(self):
        return self.data_hist, self.model_hist, self.bins

def ks_similarity(p, q):
    z1 = np.zeros_like(p)
    z2 = np.zeros_like(p)
    z1[0] = 1
    z2[-1] = 1

    kolm_raw = np.max(np.abs(np.cumsum(p)-np.cumsum(q)))
    kolm_max1 = np.max(np.abs(np.cumsum(z1)-np.cumsum(q)))
    kolm_max2 = np.max(np.abs(np.cumsum(z2)-np.cumsum(q)))
    kolm_max = np.max(np.array([kolm_max1, kolm_max2]))
    kolm_ceiled = kolm_raw / kolm_max

    return 1 - kolm_ceiled

max_stars_repo_name: dmayo/brain-score

max_issues_repo_name: dmayo/brain-score

max_forks_repo_name: dmayo/brain-score


text:
import mne
import numpy as np
import scipy
import sys
import os.path
import matplotlib.pyplot as plt
import meet
import helper_functions

s_rate = 1000 # sampling rate of the EEG (1000 samples per sec)
data_folder = sys.argv[1]
N_subjects = 21 #here: total number of subjects
result_folder = sys.argv[3]

if not os.path.exists(result_folder):
    os.mkdir(result_folder)

#for subject in range(1, N_subjects + 1, 1):
#for now only for first subject
subject = 1
try:
    with np.load(os.path.join(data_folder, 'S%02d' % subject,
            'clean_data.npz'), 'r') as f:
        artifact_mask = f['artifact_mask']
        clean_data = f['clean_data']
        #clean_data = f['clean_data'][:, artifact_mask] #DONT exclude artifacts, otherwise sample numbers wont match later on
except:
    print('Warning: Subject %02d could not be loaded!' %i)

if os.path.exists(os.path.join(
    data_folder, 'S%02d' % subject,
    'S{:02d}_eeg_all_files.vmrk'.format(subject)
    )):
    marker_fname = os.path.join(
            data_folder, 'S%02d' % subject,
            'S{:02d}_eeg_all_files.vmrk'.format(subject))
else:
    marker_fname = os.path.join(data_folder, 'S%02d' % subject,
        'S%02d_eeg.vmrk' % subject)

eeg_clocks = helper_functions.getSessionClocks(marker_fname)
eeg_clocks = [c for c in eeg_clocks if len(c) > 100]

# read the channel names
channames = meet.sphere.getChannelNames('channels.txt')
chancoords = meet.sphere.getStandardCoordinates(channames)
chancoords = meet.sphere.projectCoordsOnSphere(chancoords)
chancoords_2d = meet.sphere.projectSphereOnCircle(chancoords,
        projection='stereographic')
n_channels = clean_data.shape[0]

# convert our data to raw format (io: https://mne.tools/stable/auto_examples/io/plot_objects_from_arrays.html#sphx-glr-auto-examples-io-plot-objects-from-arrays-py)
montage = mne.channels.make_dig_montage(ch_pos=dict(zip(channames, chancoords)),
                                        coord_frame='head')
info = mne.create_info(ch_names=channames, montage=montage,
        sfreq=s_rate, ch_types=np.repeat('eeg',n_channels))
raw = mne.io.RawArray(clean_data, info)

#plot to check
scalings = 'auto'  # Could also pass a dictionary with some value == 'auto'
#raw.plot(n_channels=n_channels, scalings=scalings,
#        title='Auto-scaled Data from arrays',show=True, block=True)
#raw.plot_psd(fmin=1., fmax=45., tmax=60., average=False);
raw.info

#https://mne.tools/stable/auto_tutorials/source-modeling/plot_mne_dspm_source_localization.html#sphx-glr-auto-tutorials-source-modeling-plot-mne-dspm-source-localization-py
# triggers are all duple and triple beats, see read_aif.py
# müssen aus eeg rohdaten tick von sync clock (siehe vmrk datei wie in zb FFtcSpoc)
# siehe zb prepareFFtcSpoc.py: eeg_clocks enthält sync ticks
# syncMusicToEeg gibt uns drum schläge in eeg daten [in sample nummer]
#--> brauchen read_aif, helper funtions

#a) events are only Cues in bar 4
with np.load(os.path.join(result_folder, 'S%02d' % subject,
    'behavioural_results.npz'), 'r', allow_pickle=True, encoding='latin1') as f:
    snareCue_nearestClock = f['snareCue_nearestClock']
    snareCue_DevToClock = f['snareCue_DevToClock']
    wdBlkCue_nearestClock = f['wdBlkCue_nearestClock']
    wdBlkCue_DevToClock = f['wdBlkCue_DevToClock']
    bar_duration = f['bar_duration']

snareCue_events = helper_functions.SyncMusicToEEG(eeg_clocks,
        snareCue_nearestClock,
        snareCue_DevToClock, s_rate=s_rate)
snareCue_events = [np.array([e, 0, 0]) for e in snareCue_events]

wdBlkCue_events = helper_functions.SyncMusicToEEG(eeg_clocks,
        wdBlkCue_nearestClock,
        wdBlkCue_DevToClock, s_rate=s_rate)
wdBlkCue_events = [np.array([e, 0, 1]) for e in wdBlkCue_events]

allCues_events = np.concatenate((
        wdBlkCue_events,snareCue_events))
allCues_events = allCues_events[np.argsort(allCues_events[:,0])]
allCues_events[:, 0] = allCues_events[:, 0]-allCues_events[0,0]
# shows when which event occured over time
#mne.viz.plot_events(allCues_events, raw.info['sfreq'], raw.first_samp);

event_id = {"snare": 0, "wdBlk": 1}
tmin = -0.0  # start of each epoch (a) takt 4
tmax = bar_duration  # end of each epoch a) one bar takes around 1.71 s
epochs = mne.Epochs(raw, allCues_events, event_id, tmin, tmax,
        baseline=None, proj=True)

# volt of different electrodes over time and their location on sculp
evoked = epochs.average()
evoked.plot(show=False,spatial_colors=True);
# plot scalp polarities over times
evoked.plot_topomap(show=False,times=np.linspace(0., 0.3, 6));
#both together
evoked.plot_joint(show=False,times=[0.105, 0.130, 0.180]); #TODO give times of all beats

#where does it come from in the brain?
evoked_faces = epochs['snare'].average()
evoked_faces.plot_topomap(show=False,times=[0.095], size=2)
evoked_faces = epochs['wdBlk'].average()
evoked_faces.plot_topomap(show=False,times=[0.095], size=2)

#b) track listening (bars 1-3): event is first beat of each trial

# get events. differentietae between snare and wdblk?
# first beat of trial is cue - 3 bars (in sample time)
bar_samples = int(s_rate * bar_duration) # number of samples per bar
allStarts_events = allCues_events
allStarts_events[:,0] = np.array([allCues_events[:,0]- 3*bar_samples])

tmin_b = -0.1  # start of each epoch (0.1 seconds before trial)
tmax_b = bar_duration*3  # listening period
epochs_b = mne.Epochs(raw, allStarts_events, event_id, tmin_b, tmax_b,
        baseline=None, proj=True)

# volt of different electrodes over time and their location on sculp
evoked_b = epochs_b.average()
evoked_b.plot(show=False,spatial_colors=True);
# plot scalp polarities over times -.-
fig, axes = plt.subplots(ncols=6, nrows=3)
for ax,i in zip(axes,range(3)):
    evoked_b.plot_topomap(axes=ax,show=False, colorbar=False,
                times=np.linspace(i*bar_duration, (i+1)*bar_duration, 6))
    fig.suptitle('Bars 1-3 (Listening)')
plt.show()

max_stars_repo_name: neurophysics/DrumsAndBrains

max_issues_repo_name: neurophysics/DrumsAndBrains

max_forks_repo_name: neurophysics/DrumsAndBrains

reason: import numpy,import scipy


text:
import hmms
import pytest
import numpy
import scipy.linalg

from common import *

@pytest.fixture
def cthmm():
    """Parameters for cthmm of 3 hidden states and 3  output variables"""
    Q = numpy.array( [[-0.375,0.125,0.25],[0.25,-0.5,0.25],[0.25,0.125,-0.375]] )
    B = numpy.array( [[0.8,0.05,0.15],[0.05,0.9,0.05],[0.2,0.05,0.75]] )
    Pi = numpy.array( [0.6,0,0.4] )
    return hmms.CtHMM(Q,B,Pi)

@pytest.fixture
def dthmm( cthmm ):
    """The discrete model, created so it behaves identical to the given continuous."""
    return hmms.DtHMM( *cthmm.get_dthmm_params() )

@pytest.fixture
def cthmm_zeros():
    """Parameters for cthmm of 3 hidden states and 3  output variables, with zeros is Q"""
    Q = numpy.array( [[-0.125,0.125,0.0],[0.25,-0.5,0.25],[0.0,0.125,-0.125]] )
    B = numpy.array( [[0.8,0.05,0.15],[0.05,0.9,0.05],[0.2,0.05,0.75]] )
    Pi = numpy.array( [0.6,0,0.4] )
    return hmms.CtHMM(Q,B,Pi)

def test_zeros( cthmm_zeros ):

    t = numpy.array([ [0,5,8,9,14,19],[0,3,6,7,12,13],[0,5,6,11,14,19] ])
    e = numpy.array([ [0,0,1,0,1,0],[0,1,2,0,1,0],[2,2,0,1,0,2] ])

    cthmm_zeros.baum_welch( t, e, 10 ,method="soft" )

    result = hmms.CtHMM( numpy.array( [[-0.21327285,  0.21327285,  0.        ],
                                       [ 0.42250406, -0.6310883 ,  0.20858425],
                                       [ 0.        ,  0.33834679, -0.33834679]]),
                         numpy.array([[ 0.5696601 ,  0.20239348,  0.22794642],
                                      [ 0.28613018,  0.71024895,  0.00362086],
                                      [ 0.56025733,  0.03147241,  0.40827027]]),
                         numpy.array([ 0.41600342,  0.        ,  0.58399658])
    )

    assert compare_parameters_no_sort( result, cthmm_zeros)

@pytest.mark.parametrize(
    ['data_num', 'data_len'],
    [(1, 100),
     (3, 100),
     (20, 20),
     (40, 2), ],
)
def test_compare_state_probs_with_discrete( data_num, data_len,dthmm ):
    """Test will run algorithms for counting state probability, determinically with the same initialization for both models"""
    t,_,e = dthmm.generate_data( (data_num,data_len), times=True )

    ct = hmms.CtHMM.random(3,3)
    dt = hmms.DtHMM( *ct.get_dthmm_params() )

    assert compare_parameters_no_sort( dt,  hmms.DtHMM( *ct.get_dthmm_params() ) )

    row = e[0]
    trow = t[0]

    #ct
    alpha = ct.forward ( trow, row )
    beta =  ct.backward( trow, row )
    gamma = ct.single_state_prob( alpha, beta )
    ksi = ct.double_state_prob( alpha, beta, trow, row )
    #dt
    d_alpha = dt.forward ( row )
    d_beta =  dt.backward( row )
    d_gamma = dt.single_state_prob( d_alpha, d_beta )
    d_ksi = dt.double_state_prob( d_alpha, d_beta, row )

    assert float_equal_mat( gamma, d_gamma  )
    assert float_equal_mat( ksi[0], d_ksi  )

@pytest.mark.parametrize(
    ['data_num', 'data_len'],
    [(1, 100),
     (3, 100),
     (20, 20),
     (40, 2), ],
)
def test_estimate( data_num, data_len,dthmm ):
    """Test will run algorithms for counting state probability, determinically with the same initialization for both models"""
    t,s,e = dthmm.generate_data( (data_num,data_len), times=True )

    ct = hmms.CtHMM.random(3,3)
    dt = hmms.DtHMM( *ct.get_dthmm_params() )

    cte = ct.estimate(s[0],t[0],e[0])
    dte = dt.estimate(s[0],e[0])

    assert ( cte == dte )

@pytest.mark.parametrize("t,e,num", [
    (numpy.array([ [0,5,8,9,14,19],[0,3,6,7,12,13],[0,5,6,11,14,19] ]),
     numpy.array([ [0,0,1,0,1,0],[0,1,2,0,1,0],[2,2,0,1,0,2] ]),
     3
    ),
    (numpy.array([ [0,1,8,16,19,29],[0,2,60,77,120,133],[0,50,61,70,77,79] ]),
     numpy.array([ [0,0,1,0,1,0],[0,1,2,0,1,0],[2,2,0,1,0,2] ]),
     13
    ),
    (numpy.array([ [0,1,8,16,19,29],[0,2,60,77,120,133],[0,50,61,70,77,79] ]),
     numpy.array([ [0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0] ]),
     13
    ),
    (numpy.array([ [0,1,8,16,19,29],[0,2,60,77,120,133],[0,50,61,70,77,79] ]),
     numpy.array([ [1,1,1,1,1,0],[1,1,0,0,0,0],[1,1,0,0,0,0] ]),   #impossibility of init model
     13
    )

])
def test_time_intervals_mapping( t,e,num, cthmm ):
    """test if the time intervals compress to uniq intervals correctly.
    Training data of various time intervals.
    Data are created in the way so it encompass zero probability -> -inf logprobability.
    """
    cthmm.baum_welch( t, e , 3)

    assert cthmm.time_n == num

@pytest.fixture
def train_data():
    data = ( numpy.array([ [ 0,  1,  2,  3,  6,  8, 13, 14, 15, 16],
                           [ 0,  1,  2,  3,  5,  8, 10, 14, 17, 20],
                           [ 0,  6,  9, 14, 17, 21, 22, 25, 29, 30],
                           [ 0,  1,  2,  5,  8, 10, 11, 14, 16, 17],
                           [ 0,  1,  3,  5,  7,  9, 12, 15, 16, 22 ] ] ),
             numpy.array([ [0, 0, 0, 0, 2, 1, 1, 1, 2, 0],
                           [0, 0, 0, 1, 0, 0, 0, 0, 0, 2],
                           [0, 0, 1, 0, 2, 0, 0, 2, 2, 2],
                           [0, 0, 0, 2, 1, 2, 2, 2, 2, 0],
                           [2, 0, 0, 2, 0, 2, 2, 0, 0, 0] ] )
           )
    return data

@pytest.fixture
def out_params():
    """Parameters obtained from cthmm after train at """
    Q = numpy.array( [[-0.248751,0.090448,0.158303],
	                  [0.173265,-0.738227,0.564962],
                      [0.210508,0.165507,-0.376015]] )
    B = numpy.array( [[0.883138,0.015511,0.101351],
                      [0.005337,0.872273,0.122390],
                      [0.207833,0.015863,0.776304]] )
    Pi = numpy.array( [0.999943,0.000000,0.000057] )
    return hmms.CtHMM(Q,B,Pi)

@pytest.mark.parametrize(
    ['h_states', 'o_symbols', 'data_num', 'data_len'],
    [(3, 3, 20, 20),
     (2, 1, 20, 20),
     (2, 10, 20, 20),
     (10, 1, 20, 20),
     (5, 5, 1, 50),
     (5, 7, 3, 25),
     (5, 3, 3, 25),
     (5, 5, 25, 2),
     (10, 10, 5, 5),
    ],
)
def test_growing_likelihood(h_states, o_symbols, data_num, data_len):
    """The likelihood in the EM algorithm had always to grow"""

    cthmm = hmms.CtHMM.random( h_states, o_symbols )
    t, e = cthmm.generate_data( ( data_num, data_len ) )

    cthmm_t = hmms.CtHMM.random( h_states, o_symbols )

    dl = cthmm_t.baum_welch( t,e,15, est=True )

    assert float_equal_mat( dl, numpy.sort( dl )  )

@pytest.mark.parametrize(
    ['h_states', 'o_symbols', 'data_num', 'data_len'],
    [(3, 3, 20, 20),
     (2, 1, 20, 20),
     (2, 10, 20, 20),
     (10, 1, 20, 20),
     (5, 5, 1, 50),
     (5, 7, 3, 25),
     (5, 3, 3, 25),
     (5, 5, 25, 2),
     (10, 10, 5, 5),
    ],
)
def test_growing_likelihood_mle(h_states, o_symbols, data_num, data_len):
    """The likelihood in the EM algorithm had always to grow"""

    chmm = hmms.CtHMM.random( h_states, o_symbols )
    t_seqs, s_seqs, e_seqs = chmm.generate_data( (data_num,data_len), states=True )

    chmm_r = hmms.CtHMM.random(h_states,o_symbols)
    graph = chmm_r.maximum_likelihood_estimation(s_seqs,t_seqs,e_seqs,15,est=True )

    assert float_equal_mat( graph, numpy.sort( graph )  )

def test_mle_uneven_sequences_length():
    """Test if different inputs methods hold the same results"""
    chmm = hmms.CtHMM.random( 5, 7 )
    t_seqs, s_seqs, e_seqs = chmm.generate_data( (10,10), states=True )
    t_seqs2 = numpy_to_list( t_seqs )
    s_seqs2 = numpy_to_list( s_seqs )
    e_seqs2 = numpy_to_list( e_seqs )

    chmm_r = hmms.CtHMM.random(5,7)
    chmm_r2 = hmms.CtHMM( * chmm_r.params )
    graph = chmm_r.maximum_likelihood_estimation(s_seqs,t_seqs,e_seqs,15,est=True )
    graph2 = chmm_r2.maximum_likelihood_estimation(s_seqs2,t_seqs2,e_seqs2,15,est=True )

    assert float_equal_mat( graph, graph2  )

def test_bw_uneven_sequences_length():
    """The likelihood in the EM algorithm had always to grow"""

    cthmm = hmms.CtHMM.random( 7, 5 )
    t, e = cthmm.generate_data( ( 10, 8 ) )
    t2 = numpy_to_list(t)
    e2 = numpy_to_list(e)

    cthmm_t = hmms.CtHMM.random( 7, 5 )
    cthmm_t2 = hmms.CtHMM( * cthmm_t.params  )
    dl = cthmm_t.baum_welch( t,e,15, est=True )
    dl2 = cthmm_t2.baum_welch( t2,e2,15, est=True )

    assert float_equal_mat( dl, dl2  )

def test_baum_welch( train_data, cthmm, out_params ):
    """This is just the consistency test, do not ensure right computations"""
    t,e = train_data

    cthmm.baum_welch( t,e,20 )

    assert compare_parameters_no_sort( out_params,  cthmm, 1e-5 )

max_stars_repo_name: denklewer/HMMs

max_issues_repo_name: denklewer/HMMs

max_forks_repo_name: denklewer/HMMs


text:
import os
import numpy as np
from functools import partial
from easydict import EasyDict
import copy
import time
from tensorboardX import SummaryWriter

from core.envs import SimpleCarlaEnv, CarlaEnvWrapper
from core.utils.others.tcp_helper import parse_carla_tcp
from core.eval import SerialEvaluator, SingleCarlaEvaluator, CarlaBenchmarkEvaluator
from ding.envs import SyncSubprocessEnvManager, BaseEnvManager
from ding.policy import SACPolicy
from ding.worker import BaseLearner, SampleCollector, NaiveReplayBuffer
from ding.utils import set_pkg_seed

from demo.simple_rl.env_wrapper import DiscreteBenchmarkEnvWrapper, ContinuousBenchmarkEnvWrapper, ContinuousRgbBenchmarkEnvWrapper, ContinuousRgbCarlaEnvWrapper
from core.utils.data_utils.bev_utils import unpack_birdview
from core.utils.others.ding_utils import compile_config
from demo.simple_rl.model import CSACRGBRLModel

train_config = dict(
    exp_name='sac2_bev32_buf2e5_lr1e4_bs128_ns3000_update4_train_ft',
    env=dict(
        collector_env_num=7,
        evaluator_env_num=1,
        simulator=dict(
            town='Town01',
            disable_two_wheels=True,
            verbose=False,
            waypoint_num=32,
            planner=dict(
                type='behavior',
                resolution=0.1,
                min_distance = 3
            ),
            obs=(
                dict(
                    name='rgb',
                    type='rgb',
                    size=[320, 180],
                    position=[2.0,0,1.4],
                    rotataion=[0,0,0]
                ),
            )
        ),
        col_is_failure=True,
        stuck_is_failure=True,
        off_road_is_failure=True,
        off_route_is_failure=True,
        ignore_light=True,
        finish_reward=300,
        visualize=dict(
            type='rgb',
            outputs=['video'],
            show_text=True,
            save_dir='/home/qhzhang/video'
        ),
        manager=dict(
            collect=dict(
                auto_reset=True,
                shared_memory=False,
                context='spawn',
                max_retry=3,
                reset_timeout=1000,
                step_timeout=1000
            ),
            eval=dict(
                shared_memory=False,
                auto_reset=False,
                context='spawn',
                max_retry=3,
                reset_timeout=1000,
                step_timeout=1000
                )
        ),
        wrapper=dict(
            # Collect and eval suites for training
            collect=dict(suite='NoCrashTown01-v1', ),
            eval=dict(suite='NoCrashTown01-v1', ),
        ),
    ),
    server=[
        dict(carla_host='localhost', carla_ports=[9000, 9016, 2]),
    ],
    policy=dict(
        cuda=True,
        model=dict(
            action_shape=2,
            twin_critic=True,
            task_pretrained=False,
            img_pretrained=True,
            fix_perception=False,
            normalization=None,
        ),
        learn=dict(
            batch_size=256,
            learning_rate_q=1e-4,
            learning_rate_policy=1e-4,
            learning_rate_value=1e-4,
            learning_rate_alpha=1e-4,
            weight_decay=0.0001,
            learner=dict(
                hook=dict(
                    load_ckpt_before_run='',
                ),
            ),
        ),
        collect=dict(
            n_sample=3000,
            noise_sigma=0.1,
            collector=dict(
                collect_print_freq=1000,
                deepcopy_obs=True,
                transform_obs=True,
            ),
        ),
        eval=dict(
            evaluator=dict(
                suite='NoCrashTown01-v1',
                episodes_per_suite=50,
                eval_freq=5000,
                n_episode=3,
                stop_rate=0.7,
                transform_obs=True,
                render=True
            ),
        ),
        other=dict(
            replay_buffer=dict(
                replay_buffer_size=400000,
                replay_buffer_start_size=10000,
                max_use=16,
                monitor=dict(
                    sampled_data_attr=dict(
                        print_freq=10000,  # times
                    ),
                    periodic_thruput=dict(
                        seconds=120000000,
                    ),
                ),
            ),
        ),
    ),
)

main_config = EasyDict(train_config)

def wrapped_env(env_cfg, wrapper_cfg, host, port, tm_port=None):
    return ContinuousRgbBenchmarkEnvWrapper(SimpleCarlaEnv(env_cfg, host, port, tm_port), wrapper_cfg)

def single_wrapped_env_for_benchmark(env_cfg, host, port, tm_port=None):
    return ContinuousRgbCarlaEnvWrapper(SimpleCarlaEnv(env_cfg, host, port, tm_port))

def main(cfg, seed=0):
    cfg = compile_config(
        cfg,
        SyncSubprocessEnvManager,
        SACPolicy,
        BaseLearner,
        SampleCollector,
        buffer=NaiveReplayBuffer,
    )
    tcp_list = parse_carla_tcp(cfg.server)
    collector_env_num, evaluator_env_num = cfg.env.collector_env_num, cfg.env.evaluator_env_num
    assert len(tcp_list) >= collector_env_num + evaluator_env_num, \
        "Carla server not enough! Need {} servers but only found {}.".format(
            collector_env_num + evaluator_env_num, len(tcp_list)
    )

    collector_env = SyncSubprocessEnvManager(
        env_fn=[partial(wrapped_env, cfg.env, cfg.env.wrapper.collect, *tcp_list[i]) for i in range(collector_env_num)],
        cfg=cfg.env.manager.collect,
    )
     #evaluate_env = BaseEnvManager(
     #    env_fn=[partial(wrapped_env, cfg.env, cfg.env.wrapper.eval, *tcp_list[collector_env_num + i]) for i in range(evaluator_env_num)],
     #    cfg=cfg.env.manager.eval,
     #)

    vis_eval_single_env = wrapped_env(cfg.env, cfg.env.wrapper.eval, *tcp_list[collector_env_num])

    # Uncomment this to add save replay when evaluation
    # evaluate_env.enable_save_replay(cfg.env.replay_path)
    collector_env.seed(seed)
    vis_eval_single_env.seed(seed)
    # evaluate_env.seed(seed)
    set_pkg_seed(seed)

    model = CSACRGBRLModel(**cfg.policy.model)
    policy = SACPolicy(cfg.policy, model=model)

    tb_logger = SummaryWriter('./log/{}/'.format(cfg.exp_name))
    learner = BaseLearner(cfg.policy.learn.learner, policy.learn_mode, tb_logger, exp_name=cfg.exp_name)
    collector = SampleCollector(cfg.policy.collect.collector, collector_env, policy.collect_mode, tb_logger, exp_name=cfg.exp_name)
    # evaluator = SerialEvaluator(cfg.policy.eval.evaluator, evaluate_env, policy.eval_mode, tb_logger, exp_name=cfg.exp_name)
    replay_buffer = NaiveReplayBuffer(cfg.policy.other.replay_buffer, tb_logger, exp_name=cfg.exp_name)

    wandb_notes='task pretrain='+str(cfg.policy.model.task_pretrained) + '_img_pretrained='+str(cfg.policy.model.img_pretrained)+ '_fix-weight=' + str(cfg.policy.model.fix_perception)
    vis_evaluator = SingleCarlaEvaluator(wandb_notes, cfg.policy.eval.evaluator, vis_eval_single_env, policy.eval_mode)

    learner.call_hook('before_run')

    new_data = collector.collect(n_sample=10000, train_iter=learner.train_iter)
    replay_buffer.push(new_data, cur_collector_envstep=collector.envstep)

    while True:
        print('eval')
        vis_evaluator.eval()
        # if evaluator.should_eval(learner.train_iter):
        #     stop, rate = evaluator.eval(learner.save_checkpoint, learner.train_iter, collector.envstep)
        #     if stop:
        #         break
        # Sampling data from environments
        new_data = collector.collect(train_iter=learner.train_iter)
        update_per_collect = len(new_data) // cfg.policy.learn.batch_size * 4
        replay_buffer.push(new_data, cur_collector_envstep=collector.envstep)
        # Training
        for i in range(update_per_collect):
            train_data = replay_buffer.sample(cfg.policy.learn.batch_size, learner.train_iter)
            if train_data is not None:
                train_data = copy.deepcopy(train_data)
                unpack_birdview(train_data)
                learner.train(train_data, collector.envstep)

    learner.call_hook('after_run')

    collector.close()
    evaluator.close()
    learner.close()
    replay_buffer.close()

if __name__ == '__main__':
    main(main_config)


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
import numpy as np
import os
from astropy.tests.helper import raises
from astropy.utils.data import get_pkg_data_filename
from .. import Pydlspec2dException
from ..spec1d import (HMF, findspec, spec_append, spec_path, template_metadata,
                      wavevector)

class TestSpec1d(object):
    """Test the functions in pydl.pydlspec2d.spec1d.
    """

    def setup(self):
        self.env = {'BOSS_SPECTRO_REDUX': '/boss/spectro/redux',
                    'SPECTRO_REDUX': '/sdss/spectro/redux',
                    'RUN2D': 'v1_2_3',
                    'RUN1D': 'v1_2_3'}
        self.original_env = dict()
        for key in self.env:
            if key in os.environ:
                self.original_env[key] = os.environ[key]
            else:
                self.original_env[key] = None
            os.environ[key] = self.env[key]

    def teardown(self):
        for key in self.original_env:
            if self.original_env[key] is None:
                del os.environ[key]
            else:
                os.environ[key] = self.original_env[key]

    def test_findspec(self):
        """This is just a placeholder for now.
        """
        # slist = findspec(infile='file.in', sdss=True)
        assert True

    def test_hmf_init(self):
        """Test initialization of HMF object
        """
        spec = np.random.random((20, 100))
        invvar = np.random.random((20, 100))
        hmf = HMF(spec, invvar)
        assert hmf.K == 4
        assert hmf.log.level == 20  # INFO
        hmf = HMF(spec, invvar, K=6, verbose=True)
        assert hmf.K == 6
        assert hmf.log.level == 10  # DEBUG

    def test_spec_append(self):
        spec1 = np.array([[1, 1, 1, 1, 1],
                          [1, 1, 1, 1, 1]])
        spec2 = np.array([[2, 2, 2, 2, 2],
                          [2, 2, 2, 2, 2]])
        s = spec_append(spec1, spec2)
        assert (s == np.array([[1, 1, 1, 1, 1],
                               [1, 1, 1, 1, 1],
                               [2, 2, 2, 2, 2],
                               [2, 2, 2, 2, 2]])).all()
        spec2 = np.array([[2, 2, 2, 2],
                          [2, 2, 2, 2]])
        s = spec_append(spec1, spec2)
        assert (s == np.array([[1, 1, 1, 1, 1],
                               [1, 1, 1, 1, 1],
                               [2, 2, 2, 2, 0],
                               [2, 2, 2, 2, 0]])).all()
        s = spec_append(spec1, spec2, 1)
        assert (s == np.array([[1, 1, 1, 1, 1],
                               [1, 1, 1, 1, 1],
                               [0, 2, 2, 2, 2],
                               [0, 2, 2, 2, 2]])).all()
        spec1 = np.array([[1, 1, 1],
                          [1, 1, 1]])
        spec2 = np.array([[2, 2, 2, 2, 2],
                          [2, 2, 2, 2, 2]])
        s = spec_append(spec1, spec2, -2)
        assert (s == np.array([[0, 0, 1, 1, 1],
                               [0, 0, 1, 1, 1],
                               [2, 2, 2, 2, 2],
                               [2, 2, 2, 2, 2]])).all()

    def test_spec_path(self):
        bsr = self.env['BOSS_SPECTRO_REDUX']
        run2d = self.env['RUN2D']
        p = spec_path(123)
        assert p[0] == os.path.join(bsr, run2d, '0123')
        p = spec_path(1234)
        assert p[0] == os.path.join(bsr, run2d, '1234')
        p = spec_path(1234, topdir=bsr, run2d=run2d)
        assert p[0] == os.path.join(bsr, run2d, '1234')
        p = spec_path(np.array([1234, 5678]), topdir=bsr, run2d=run2d)
        assert p[0] == os.path.join(bsr, run2d, '1234')
        assert p[1] == os.path.join(bsr, run2d, '5678')
        p = spec_path(1234, path=bsr)
        assert p[0] == bsr

    def test_template_metadata(self):
        with raises(Pydlspec2dException):
            slist, metadata = template_metadata('/no/such/file.par')
        inputfile = get_pkg_data_filename('t/test_template_metadata.par')
        slist, metadata = template_metadata(inputfile)
        assert metadata['object'] == 'gal'
        assert not metadata['nonnegative']

    def test_wavevector(self):
        l = wavevector(3, 4, binsz=0.1)
        ll = np.array([3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0])
        assert np.allclose(l, ll)
        l = wavevector(3, 4, wavemin=3, binsz=0.1)
        ll = np.array([3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0])
        assert np.allclose(l, ll)

max_stars_repo_name: jhennawi/pydl

max_issues_repo_name: jhennawi/pydl

max_forks_repo_name: jhennawi/pydl

reason: import numpy,from astropy


text:
# import _init_paths
import argparse
import os
import copy
import random
import numpy as np
from PIL import Image
import scipy.io as scio
import scipy.misc
import numpy.ma as ma
import math
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import torch.nn.functional as F
from torch.autograd import Variable
# from datasets.ycb.dataset import PoseDataset
# sys.path.insert(0, "/media/aditya/A69AFABA9AFA85D9/Cruzr/code/DenseFusion")
from lib_d.network import PoseNet, PoseRefineNet
from lib_d.transformations import euler_matrix, quaternion_matrix, quaternion_from_matrix

# parser = argparse.ArgumentParser()
# parser.add_argument('--dataset_root', type=str, default = '', help='dataset root dir')
# parser.add_argument('--model', type=str, default = '',  help='resume PoseNet model')
# parser.add_argument('--refine_model', type=str, default = '',  help='resume PoseRefineNet model')
# opt = parser.parse_args()

norm = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
border_list = [-1, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400, 440, 480, 520, 560, 600, 640, 680]
xmap = np.array([[j for i in range(640)] for j in range(480)]) #height
ymap = np.array([[i for i in range(640)] for j in range(480)]) #width
# cam_cx = 312.9869
# cam_cy = 241.3109
# cam_fx = 1066.778
# cam_fy = 1067.487
# cam_scale = 10000.0
num_obj = 21
img_width = 480
img_length = 640
num_points = 1000
num_points_mesh = 500
iteration = 2
bs = 1
dataset_config_dir = 'datasets/ycb/dataset_config'
ycb_toolbox_dir = 'YCB_Video_toolbox'
result_wo_refine_dir = 'experiments/eval_result/ycb/Densefusion_wo_refine_result'
result_refine_dir = 'experiments/eval_result/ycb/Densefusion_iterative_result'

# def get_bbox(posecnn_rois):
#     rmin = int(posecnn_rois[idx][3]) + 1
#     rmax = int(posecnn_rois[idx][5]) - 1
#     cmin = int(posecnn_rois[idx][2]) + 1
#     cmax = int(posecnn_rois[idx][4]) - 1
#     r_b = rmax - rmin
#     for tt in range(len(border_list)):
#         if r_b > border_list[tt] and r_b < border_list[tt + 1]:
#             r_b = border_list[tt + 1]
#             break
#     c_b = cmax - cmin
#     for tt in range(len(border_list)):
#         if c_b > border_list[tt] and c_b < border_list[tt + 1]:
#             c_b = border_list[tt + 1]
#             break
#     center = [int((rmin + rmax) / 2), int((cmin + cmax) / 2)]
#     rmin = center[0] - int(r_b / 2)
#     rmax = center[0] + int(r_b / 2)
#     cmin = center[1] - int(c_b / 2)
#     cmax = center[1] + int(c_b / 2)
#     if rmin < 0:
#         delt = -rmin
#         rmin = 0
#         rmax += delt
#     if cmin < 0:
#         delt = -cmin
#         cmin = 0
#         cmax += delt
#     if rmax > img_width:
#         delt = rmax - img_width
#         rmax = img_width
#         rmin -= delt
#     if cmax > img_length:
#         delt = cmax - img_length
#         cmax = img_length
#         cmin -= delt
#     return rmin, rmax, cmin, cmax

estimator = PoseNet(num_points = num_points, num_obj = num_obj)
estimator.cuda()
estimator.load_state_dict(torch.load("/media/aditya/A69AFABA9AFA85D9/Cruzr/code/DenseFusion/trained_checkpoints/ycb/pose_model_26_0.012863246640872631.pth"))
estimator.eval()

refiner = PoseRefineNet(num_points = num_points, num_obj = num_obj)
refiner.cuda()
refiner.load_state_dict(torch.load("/media/aditya/A69AFABA9AFA85D9/Cruzr/code/DenseFusion/trained_checkpoints/ycb/pose_refine_model_69_0.009449292959118935.pth"))
refiner.eval()

# testlist = []
# input_file = open('{0}/test_data_list.txt'.format(dataset_config_dir))
# while 1:
#     input_line = input_file.readline()
#     if not input_line:
#         break
#     if input_line[-1:] == '\n':
#         input_line = input_line[:-1]
#     testlist.append(input_line)
# input_file.close()
# print(len(testlist))

# class_file = open('{0}/classes.txt'.format(dataset_config_dir))
# class_id = 1
# cld = {}
# while 1:
#     class_input = class_file.readline()
#     if not class_input:
#         break
#     class_input = class_input[:-1]

#     input_file = open('{0}/models/{1}/points.xyz'.format(opt.dataset_root, class_input))
#     cld[class_id] = []
#     while 1:
#         input_line = input_file.readline()
#         if not input_line:
#             break
#         input_line = input_line[:-1]
#         input_line = input_line.split(' ')
#         cld[class_id].append([float(input_line[0]), float(input_line[1]), float(input_line[2])])
#     input_file.close()
#     cld[class_id] = np.array(cld[class_id])
#     class_id += 1

def run_densefusion_image(color_img_path,
                 depth_img_path,
                 mask_img_path,
                 cam_intrinsics,
                 cam_scale,
                 bbox):
    img = Image.open(color_img_path)
    depth = np.array(Image.open(depth_img_path))
    mask_depth = ma.getmaskarray(ma.masked_not_equal(depth, 0))
    mask_label = np.array(Image.open(mask_img_path))
    mask = mask_label * mask_depth

    cmin, rmin, cmax, rmax = bbox
    print([cmin, rmin, cmax, rmax])
    choose = mask[rmin:rmax, cmin:cmax].flatten().nonzero()[0]
    if len(choose) > num_points:
        c_mask = np.zeros(len(choose), dtype=int)
        c_mask[:num_points] = 1
        np.random.shuffle(c_mask)
        choose = choose[c_mask.nonzero()]
    else:
        choose = np.pad(choose, (0, num_points - len(choose)), 'wrap')
    choose = np.array([choose])

    depth_masked = depth[rmin:rmax, cmin:cmax].flatten()[choose][:, np.newaxis].astype(np.float32)
    xmap_masked = xmap[rmin:rmax, cmin:cmax].flatten()[choose][:, np.newaxis].astype(np.float32)
    ymap_masked = ymap[rmin:rmax, cmin:cmax].flatten()[choose][:, np.newaxis].astype(np.float32)

    # cam_scale = 100.0
    # cam_intrinsics = np.loadtxt('{0}/{1}/rgb_camera_intrinsics.txt'.format("conveyor_dataset_v1", "sugar_1"))
    # print(cam_intrinsics)

    cam_fx =  cam_intrinsics[0,0]
    cam_fy =  cam_intrinsics[1,1]
    cam_cx =  cam_intrinsics[0,2]
    cam_cy =  cam_intrinsics[1,2]
    itemid = 5

    pt2 = depth_masked / cam_scale
    pt0 = (ymap_masked - cam_cx) * pt2 / cam_fx
    pt1 = (xmap_masked - cam_cy) * pt2 / cam_fy
    cloud = np.concatenate((pt0, pt1, pt2), axis=1)

    img_masked = np.array(img)[:, :, :3]
    img_masked = np.transpose(img_masked, (2, 0, 1))
    img_masked = img_masked[:, rmin:rmax, cmin:cmax]

    cloud = torch.from_numpy(cloud.astype(np.float32))
    choose = torch.LongTensor(choose.astype(np.int32))
    img_masked = norm(torch.from_numpy(img_masked.astype(np.float32)))
    index = torch.LongTensor([itemid - 1])

    cloud = Variable(cloud).cuda()
    choose = Variable(choose).cuda()
    img_masked = Variable(img_masked).cuda()
    index = Variable(index).cuda()

    cloud = cloud.view(1, num_points, 3)
    img_masked = img_masked.view(1, 3, img_masked.size()[1], img_masked.size()[2])

    my_result_wo_refine = []
    my_result = []

    pred_r, pred_t, pred_c, emb = estimator(img_masked, cloud, choose, index)
    pred_r = pred_r / torch.norm(pred_r, dim=2).view(1, num_points, 1)

    pred_c = pred_c.view(bs, num_points)
    how_max, which_max = torch.max(pred_c, 1)
    print(how_max)
    pred_t = pred_t.view(bs * num_points, 1, 3)
    points = cloud.view(bs * num_points, 1, 3)

    my_r = pred_r[0][which_max[0]].view(-1).cpu().data.numpy()
    my_t = (points + pred_t)[which_max[0]].view(-1).cpu().data.numpy()
    my_pred_wo_refine = np.append(my_r, my_t)
    my_result_wo_refine.append(my_pred_wo_refine.tolist())
    # print([pred_r, pred_t, pred_c, emb])
    print(my_pred_wo_refine)

    for ite in range(0, iteration):
        T = Variable(torch.from_numpy(my_t.astype(np.float32))).cuda().view(1, 3).repeat(num_points, 1).contiguous().view(1, num_points, 3)
        my_mat = quaternion_matrix(my_r)
        R = Variable(torch.from_numpy(my_mat[:3, :3].astype(np.float32))).cuda().view(1, 3, 3)
        my_mat[0:3, 3] = my_t

        new_cloud = torch.bmm((cloud - T), R).contiguous()
        pred_r, pred_t = refiner(new_cloud, emb, index)
        pred_r = pred_r.view(1, 1, -1)
        pred_r = pred_r / (torch.norm(pred_r, dim=2).view(1, 1, 1))
        my_r_2 = pred_r.view(-1).cpu().data.numpy()
        my_t_2 = pred_t.view(-1).cpu().data.numpy()
        my_mat_2 = quaternion_matrix(my_r_2)

        my_mat_2[0:3, 3] = my_t_2

        my_mat_final = np.dot(my_mat, my_mat_2)
        my_r_final = copy.deepcopy(my_mat_final)
        my_r_final[0:3, 3] = 0
        my_r_final = quaternion_from_matrix(my_r_final, True)
        my_t_final = np.array([my_mat_final[0][3], my_mat_final[1][3], my_mat_final[2][3]])

        my_pred = np.append(my_r_final, my_t_final)
        my_r = my_r_final
        my_t = my_t_final

    my_result.append(my_pred.tolist())
    print(my_result)
    return my_pred_wo_refine, my_pred

# for now in range(0, 0):
#     img = Image.open('{0}/{1}-color.png'.format(opt.dataset_root, testlist[now]))
#     depth = np.array(Image.open('{0}/{1}-depth.png'.format(opt.dataset_root, testlist[now])))
#     posecnn_meta = scio.loadmat('{0}/results_PoseCNN_RSS2018/{1}.mat'.format(ycb_toolbox_dir, '%06d' % now))
#     label = np.array(posecnn_meta['labels'])
#     posecnn_rois = np.array(posecnn_meta['rois'])

#     lst = posecnn_rois[:, 1:2].flatten()
#     my_result_wo_refine = []
#     my_result = []

#     for idx in range(len(lst)):
#         itemid = lst[idx]
#         try:
#             rmin, rmax, cmin, cmax = get_bbox(posecnn_rois)

#             mask_depth = ma.getmaskarray(ma.masked_not_equal(depth, 0))
#             mask_label = ma.getmaskarray(ma.masked_equal(label, itemid))
#             mask = mask_label * mask_depth

#             choose = mask[rmin:rmax, cmin:cmax].flatten().nonzero()[0]
#             if len(choose) > num_points:
#                 c_mask = np.zeros(len(choose), dtype=int)
#                 c_mask[:num_points] = 1
#                 np.random.shuffle(c_mask)
#                 choose = choose[c_mask.nonzero()]
#             else:
#                 choose = np.pad(choose, (0, num_points - len(choose)), 'wrap')

#             depth_masked = depth[rmin:rmax, cmin:cmax].flatten()[choose][:, np.newaxis].astype(np.float32)
#             xmap_masked = xmap[rmin:rmax, cmin:cmax].flatten()[choose][:, np.newaxis].astype(np.float32)
#             ymap_masked = ymap[rmin:rmax, cmin:cmax].flatten()[choose][:, np.newaxis].astype(np.float32)
#             choose = np.array([choose])

#             pt2 = depth_masked / cam_scale
#             pt0 = (ymap_masked - cam_cx) * pt2 / cam_fx
#             pt1 = (xmap_masked - cam_cy) * pt2 / cam_fy
#             cloud = np.concatenate((pt0, pt1, pt2), axis=1)

#             img_masked = np.array(img)[:, :, :3]
#             img_masked = np.transpose(img_masked, (2, 0, 1))
#             img_masked = img_masked[:, rmin:rmax, cmin:cmax]

#             cloud = torch.from_numpy(cloud.astype(np.float32))
#             choose = torch.LongTensor(choose.astype(np.int32))
#             img_masked = norm(torch.from_numpy(img_masked.astype(np.float32)))
#             index = torch.LongTensor([itemid - 1])

#             cloud = Variable(cloud).cuda()
#             choose = Variable(choose).cuda()
#             img_masked = Variable(img_masked).cuda()
#             index = Variable(index).cuda()

#             cloud = cloud.view(1, num_points, 3)
#             img_masked = img_masked.view(1, 3, img_masked.size()[1], img_masked.size()[2])

#             pred_r, pred_t, pred_c, emb = estimator(img_masked, cloud, choose, index)
#             pred_r = pred_r / torch.norm(pred_r, dim=2).view(1, num_points, 1)

#             pred_c = pred_c.view(bs, num_points)
#             how_max, which_max = torch.max(pred_c, 1)
#             pred_t = pred_t.view(bs * num_points, 1, 3)
#             points = cloud.view(bs * num_points, 1, 3)

#             my_r = pred_r[0][which_max[0]].view(-1).cpu().data.numpy()
#             my_t = (points + pred_t)[which_max[0]].view(-1).cpu().data.numpy()
#             my_pred = np.append(my_r, my_t)
#             my_result_wo_refine.append(my_pred.tolist())

#             for ite in range(0, iteration):
#                 T = Variable(torch.from_numpy(my_t.astype(np.float32))).cuda().view(1, 3).repeat(num_points, 1).contiguous().view(1, num_points, 3)
#                 my_mat = quaternion_matrix(my_r)
#                 R = Variable(torch.from_numpy(my_mat[:3, :3].astype(np.float32))).cuda().view(1, 3, 3)
#                 my_mat[0:3, 3] = my_t

#                 new_cloud = torch.bmm((cloud - T), R).contiguous()
#                 pred_r, pred_t = refiner(new_cloud, emb, index)
#                 pred_r = pred_r.view(1, 1, -1)
#                 pred_r = pred_r / (torch.norm(pred_r, dim=2).view(1, 1, 1))
#                 my_r_2 = pred_r.view(-1).cpu().data.numpy()
#                 my_t_2 = pred_t.view(-1).cpu().data.numpy()
#                 my_mat_2 = quaternion_matrix(my_r_2)

#                 my_mat_2[0:3, 3] = my_t_2

#                 my_mat_final = np.dot(my_mat, my_mat_2)
#                 my_r_final = copy.deepcopy(my_mat_final)
#                 my_r_final[0:3, 3] = 0
#                 my_r_final = quaternion_from_matrix(my_r_final, True)
#                 my_t_final = np.array([my_mat_final[0][3], my_mat_final[1][3], my_mat_final[2][3]])

#                 my_pred = np.append(my_r_final, my_t_final)
#                 my_r = my_r_final
#                 my_t = my_t_final

#             # Here 'my_pred' is the final pose estimation result after refinement ('my_r': quaternion, 'my_t': translation)

#             my_result.append(my_pred.tolist())
#         except ZeroDivisionError:
#             print("PoseCNN Detector Lost {0} at No.{1} keyframe".format(itemid, now))
#             my_result_wo_refine.append([0.0 for i in range(7)])
#             my_result.append([0.0 for i in range(7)])

#     scio.savemat('{0}/{1}.mat'.format(result_wo_refine_dir, '%04d' % now), {'poses':my_result_wo_refine})
#     scio.savemat('{0}/{1}.mat'.format(result_refine_dir, '%04d' % now), {'poses':my_result})
#     print("Finish No.{0} keyframe".format(now))

max_stars_repo_name: Tacha-S/perception

max_issues_repo_name: Tacha-S/perception

max_forks_repo_name: Tacha-S/perception


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np              # Numerical library
from scipy import *             # Load the scipy functions
from control.matlab import *    # Load the controls systems library
from matplotlib import pyplot as plt
from scipy import arange
from scipy.linalg import expm

def drange(begin, end, step):
    n = begin
    while n + step < end:
     yield n
     n += step

def main():
  # system
  A = np.array([[0, 1],
                [-5, -6]])
  B = np.array([[0],
                [1]])

  C = np.array([[1, 0]])

  x0 = np.array([[1, 0]])

  sys = ss(A, B, C, 0);

  i = 0
  x1 = []
  x2 = []
  time  = []

  for n in drange(0.0, 6.0, 0.1):
      i = i + 1
      x = expm(sys.A * n) * x0
      x1.append(x[0][0])
      x2.append(x[1][0])
      time.append(n)
  plt.plot(time, x1, time, x2)

  plt.grid(which='major',color='gray',linestyle='-')
  plt.show()

if __name__ == "__main__":
  main()

reason: import numpy,from scipy


text:
import sys
import tensorflow as tf
import numpy as np
from src.Utilities import Utilities
from src.Autoencoder import InputLayer
from src.Autoencoder import HiddenLayer
from src.Autoencoder import OutputLayer

# Number of epochs to run
numEpochs = 50
# Size of each batch
batchSize = 10
# Scaling factor for sparsity cost function
alpha = 0.01
# Sparsity parameter
rho = 0.05
# Scaling factor for l2 regularization cost function
beta = 0.01

# Read in data from csv file
Utilities.progress(1, 7, status='Reading in data        ')
inputArray = Utilities.readData()
dictFeeder = Utilities.batchBuilder(inputArray, batchSize)

# Build input layer
Utilities.progress(2, 7, status='Building input layer   ')
with tf.variable_scope("input"):
    iLayer = InputLayer(len(inputArray[1]))

# Build hidden layer 1
Utilities.progress(3, 7, status='Building hidden layer 1')
with tf.variable_scope("hidden1"):
    hidden1 = HiddenLayer(100, iLayer.inputLayer)
    hidden1.buildTrainer(alpha, beta, rho)

# Build hidden layer 2
Utilities.progress(4, 7, status='Building hidden layer 2')
with tf.variable_scope("hidden2"):
    hidden2 = HiddenLayer(50, hidden1.y1)
    hidden2.buildTrainer(alpha, beta, rho)

# Build hidden layer 3
Utilities.progress(5, 7, status='Building hidden layer 3')
with tf.variable_scope("hidden3"):
    hidden3 = HiddenLayer(16, hidden2.y1)
    hidden3.buildTrainer(alpha, beta, rho)

# Build output layer
Utilities.progress(6, 7, status='Building output layer  ')
with tf.variable_scope("output"):
    oLayer = OutputLayer(2, hidden3.y1)
    oLayer.buildTrainer()

Utilities.progress(7, 7, status='Starting session       ')
sess = Utilities.startSession()

# Print the shape of each layer
iLayer.printLayerShape()
hidden1.printLayerShape()
hidden2.printLayerShape()
hidden3.printLayerShape()
oLayer.printLayerShape()

# Training the hidden layers
testCase = Utilities.numpyReshape(inputArray[0])

for i in range(numEpochs):
    Utilities.progress(i + 1, numEpochs, status='Training Layer 1 ')
    np.random.shuffle(dictFeeder)
    for j in range(len(dictFeeder)):
        sess.run(hidden1.trainStep, feed_dict = {iLayer.inputLayer: dictFeeder[j]})
    Utilities.dataRecorder(1, sess.run(hidden1.squareDifference, feed_dict = {iLayer.inputLayer: testCase}), i)

for i in range(numEpochs):
    Utilities.progress(i + 1, numEpochs, status='Training Layer 2 ')
    np.random.shuffle(dictFeeder)
    for j in range(len(dictFeeder)):
        sess.run(hidden2.trainStep, feed_dict = {iLayer.inputLayer: dictFeeder[j]})
    Utilities.dataRecorder(2, sess.run(hidden2.squareDifference, feed_dict = {iLayer.inputLayer: testCase}), i)

for i in range(numEpochs):
    Utilities.progress(i + 1, numEpochs, status='Training Layer 3 ')
    np.random.shuffle(dictFeeder)
    for j in range(len(dictFeeder)):
        sess.run(hidden3.trainStep, feed_dict = {iLayer.inputLayer: dictFeeder[j]})
    Utilities.dataRecorder(3, sess.run(hidden3.squareDifference, feed_dict = {iLayer.inputLayer: testCase}), i)

outputList = []
for i in range(len(inputArray)):
    Utilities.progress(i + 1, len(inputArray), status='Gathering Output')
    outputList.append(sess.run(hidden3.y1, feed_dict = {iLayer.inputLayer: Utilities.numpyReshape(inputArray[i])}))

writeStream = open('results.csv', 'w')
for i in outputList:
    outputString = str(i[0][0])
    count = 0
    for j in range(len(i[0])):
        if count != 0:
            outputString += "," + str(i[0][j])
        else:
            count = 1
    outputString += "\n"
    writeStream.write(outputString)
writeStream.close()

# Training the output layer
# for i in range(numEpochs):
    # Utilities.progress(i + 1, numEpochs, status='Training Ouput Layer')
    # logits = Utilities.batchBuilder(inputArray, batchSize)
    # labels = []
    # for i in logits:
        # if i[0] > 0.5:
            # labels.append([1.0, 0.0])
        # else:
            # labels.append([0.0, 1.0])
    # sess.run(oLayer.trainStep, feed_dict = {iLayer.inputLayer: logits, oLayer.labelTensor: labels})

# Gathers the results for analysis
# for i in range(len(inputArray)):
    # Utilities.progress(i + 1, len(inputArray), status='Gathering Output')
    # outputList.append(sess.run(oLayer.yo,
                      # feed_dict = {iLayer.inputLayer: Utilities.numpyReshape(inputArray[i])}))

num1 = 0
num2 = 0

for i in outputList:
    if i[0][0] > i[0][1]:
        num1 += 1
    else:
        num2 += 1

# Output results
print("\nNumber of 1: ", num1)
print("Number of 2: ", num2)

print("\nHidden Layer 1:")
print("Squared Difference: ", sess.run(hidden1.squareDifference, feed_dict = {iLayer.inputLayer: testCase}))
print("\nHidden Layer 2:")
print("Squared Difference: ", sess.run(hidden2.squareDifference, feed_dict = {iLayer.inputLayer: testCase}))
print("\nHidden Layer 3:")
print("Squared Difference: ", sess.run(hidden3.squareDifference, feed_dict = {iLayer.inputLayer: testCase}))

max_stars_repo_name: amarrforever/Eunomia

max_issues_repo_name: amarrforever/Eunomia

max_forks_repo_name: amarrforever/Eunomia


text:
#!/usr/bin/env python

# ApplePy - an Apple ][ emulator in Python
# James Tauber / http://jtauber.com/
# originally written 2001, updated 2011

import numpy
import pygame
import select
import socket
import struct
import subprocess
import sys
import time
import wave

class Display:

    characters = [
        [0b00000, 0b01110, 0b10001, 0b10101, 0b10111, 0b10110, 0b10000, 0b01111],
        [0b00000, 0b00100, 0b01010, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001],
        [0b00000, 0b11110, 0b10001, 0b10001, 0b11110, 0b10001, 0b10001, 0b11110],
        [0b00000, 0b01110, 0b10001, 0b10000, 0b10000, 0b10000, 0b10001, 0b01110],
        [0b00000, 0b11110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11110],
        [0b00000, 0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b11111],
        [0b00000, 0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000],
        [0b00000, 0b01111, 0b10000, 0b10000, 0b10000, 0b10011, 0b10001, 0b01111],
        [0b00000, 0b10001, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001],
        [0b00000, 0b01110, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110],
        [0b00000, 0b00001, 0b00001, 0b00001, 0b00001, 0b00001, 0b10001, 0b01110],
        [0b00000, 0b10001, 0b10010, 0b10100, 0b11000, 0b10100, 0b10010, 0b10001],
        [0b00000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b11111],
        [0b00000, 0b10001, 0b11011, 0b10101, 0b10101, 0b10001, 0b10001, 0b10001],
        [0b00000, 0b10001, 0b10001, 0b11001, 0b10101, 0b10011, 0b10001, 0b10001],
        [0b00000, 0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110],
        [0b00000, 0b11110, 0b10001, 0b10001, 0b11110, 0b10000, 0b10000, 0b10000],
        [0b00000, 0b01110, 0b10001, 0b10001, 0b10001, 0b10101, 0b10010, 0b01101],
        [0b00000, 0b11110, 0b10001, 0b10001, 0b11110, 0b10100, 0b10010, 0b10001],
        [0b00000, 0b01110, 0b10001, 0b10000, 0b01110, 0b00001, 0b10001, 0b01110],
        [0b00000, 0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100],
        [0b00000, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110],
        [0b00000, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01010, 0b00100],
        [0b00000, 0b10001, 0b10001, 0b10001, 0b10101, 0b10101, 0b11011, 0b10001],
        [0b00000, 0b10001, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b10001],
        [0b00000, 0b10001, 0b10001, 0b01010, 0b00100, 0b00100, 0b00100, 0b00100],
        [0b00000, 0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111],
        [0b00000, 0b11111, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11111],
        [0b00000, 0b00000, 0b10000, 0b01000, 0b00100, 0b00010, 0b00001, 0b00000],
        [0b00000, 0b11111, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b11111],
        [0b00000, 0b00000, 0b00000, 0b00100, 0b01010, 0b10001, 0b00000, 0b00000],
        [0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111],
        [0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000],
        [0b00000, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00000, 0b00100],
        [0b00000, 0b01010, 0b01010, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000],
        [0b00000, 0b01010, 0b01010, 0b11111, 0b01010, 0b11111, 0b01010, 0b01010],
        [0b00000, 0b00100, 0b01111, 0b10100, 0b01110, 0b00101, 0b11110, 0b00100],
        [0b00000, 0b11000, 0b11001, 0b00010, 0b00100, 0b01000, 0b10011, 0b00011],
        [0b00000, 0b01000, 0b10100, 0b10100, 0b01000, 0b10101, 0b10010, 0b01101],
        [0b00000, 0b00100, 0b00100, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000],
        [0b00000, 0b00100, 0b01000, 0b10000, 0b10000, 0b10000, 0b01000, 0b00100],
        [0b00000, 0b00100, 0b00010, 0b00001, 0b00001, 0b00001, 0b00010, 0b00100],
        [0b00000, 0b00100, 0b10101, 0b01110, 0b00100, 0b01110, 0b10101, 0b00100],
        [0b00000, 0b00000, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0b00000],
        [0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00100, 0b00100, 0b01000],
        [0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b00000, 0b00000, 0b00000],
        [0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00100],
        [0b00000, 0b00000, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b00000],
        [0b00000, 0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110],
        [0b00000, 0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110],
        [0b00000, 0b01110, 0b10001, 0b00001, 0b00110, 0b01000, 0b10000, 0b11111],
        [0b00000, 0b11111, 0b00001, 0b00010, 0b00110, 0b00001, 0b10001, 0b01110],
        [0b00000, 0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010],
        [0b00000, 0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110],
        [0b00000, 0b00111, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110],
        [0b00000, 0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000],
        [0b00000, 0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110],
        [0b00000, 0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b11100],
        [0b00000, 0b00000, 0b00000, 0b00100, 0b00000, 0b00100, 0b00000, 0b00000],
        [0b00000, 0b00000, 0b00000, 0b00100, 0b00000, 0b00100, 0b00100, 0b01000],
        [0b00000, 0b00010, 0b00100, 0b01000, 0b10000, 0b01000, 0b00100, 0b00010],
        [0b00000, 0b00000, 0b00000, 0b11111, 0b00000, 0b11111, 0b00000, 0b00000],
        [0b00000, 0b01000, 0b00100, 0b00010, 0b00001, 0b00010, 0b00100, 0b01000],
        [0b00000, 0b01110, 0b10001, 0b00010, 0b00100, 0b00100, 0b00000, 0b00100]
    ]

    lores_colours = [
        (0, 0, 0),  # black
        (208, 0, 48),  # magenta / dark red
        (0, 0, 128),  # dark blue
        (255, 0, 255),  # purple / violet
        (0, 128, 0),  # dark green
        (128, 128, 128),  # gray 1
        (0, 0, 255),  # medium blue / blue
        (96, 160, 255),  # light blue
        (128, 80, 0),  # brown / dark orange
        (255, 128, 0),  # orange
        (192, 192, 192),  # gray 2
        (255, 144, 128),  # pink / light red
        (0, 255, 0),  # light green / green
        (255, 255, 0),  # yellow / light orange
        (64, 255, 144),  # aquamarine / light green
        (255, 255, 255),  # white
    ]

    def __init__(self):
        self.screen = pygame.display.set_mode((560, 384))
        pygame.display.set_caption("ApplePy")
        self.mix = False
        self.flash_time = time.time()
        self.flash_on = False
        self.flash_chars = [[0] * 0x400] * 2

        self.page = 1
        self.text = True
        self.colour = False

        self.chargen = []
        for c in self.characters:
            chars = [[pygame.Surface((14, 16)), pygame.Surface((14, 16))],
                     [pygame.Surface((14, 16)), pygame.Surface((14, 16))]]
            for colour in (0, 1):
                hue = (255, 255, 255) if colour else (0, 200, 0)
                for inv in (0, 1):
                    pixels = pygame.PixelArray(chars[colour][inv])
                    off = hue if inv else (0, 0, 0)
                    on = (0, 0, 0) if inv else hue
                    for row in range(8):
                        b = c[row] << 1
                        for col in range(7):
                            bit = (b >> (6 - col)) & 1
                            pixels[2 * col][2 * row] = on if bit else off
                            pixels[2 * col + 1][2 * row] = on if bit else off
                    del pixels
            self.chargen.append(chars)

    def txtclr(self):
        self.text = False

    def txtset(self):
        self.text = True
        self.colour = False

    def mixclr(self):
        self.mix = False

    def mixset(self):
        self.mix = True
        self.colour = True

    def lowscr(self):
        self.page = 1

    def hiscr(self):
        self.page = 2

    def lores(self):
        self.high_res = False

    def hires(self):
        self.high_res = True

    def update(self, address, value):
        if self.page == 1:
            start_text = 0x400
            start_hires = 0x2000
        elif self.page == 2:
            start_text = 0x800
            start_hires = 0x4000
        else:
            return

        if start_text <= address <= start_text + 0x3FF:
            base = address - start_text
            self.flash_chars[self.page - 1][base] = value
            hi, lo = divmod(base, 0x80)
            row_group, column = divmod(lo, 0x28)
            row = hi + 8 * row_group

            if row_group == 3:
                return

            if self.text or not self.mix or not row < 20:
                mode, ch = divmod(value, 0x40)

                if mode == 0:
                    inv = True
                elif mode == 1:
                    inv = self.flash_on
                else:
                    inv = False

                self.screen.blit(self.chargen[ch][self.colour][inv], (2 * (column * 7), 2 * (row * 8)))
            else:
                pixels = pygame.PixelArray(self.screen)
                if not self.high_res:
                    lower, upper = divmod(value, 0x10)

                    for dx in range(14):
                        for dy in range(8):
                            x = column * 14 + dx
                            y = row * 16 + dy
                            pixels[x][y] = self.lores_colours[upper]
                        for dy in range(8, 16):
                            x = column * 14 + dx
                            y = row * 16 + dy
                            pixels[x][y] = self.lores_colours[lower]
                del pixels

        elif start_hires <= address <= start_hires + 0x1FFF:
            if self.high_res:
                base = address - start_hires
                row8, b = divmod(base, 0x400)
                hi, lo = divmod(b, 0x80)
                row_group, column = divmod(lo, 0x28)
                row = 8 * (hi + 8 * row_group) + row8

                if self.mix and row >= 160:
                    return

                if row < 192 and column < 40:

                    pixels = pygame.PixelArray(self.screen)
                    msb = value // 0x80

                    for b in range(7):
                        c = value & (1 << b)
                        xx = (column * 7 + b)
                        x = 2 * xx
                        y = 2 * row

                        if msb:
                            if xx % 2:
                                pixels[x][y] = (0, 0, 0)
                                # orange
                                pixels[x][y] = (255, 192, 0) if c else (0, 0, 0)  # @@@
                                pixels[x + 1][y] = (255, 192, 0) if c else (0, 0, 0)
                            else:
                                # blue
                                pixels[x][y] = (0, 192, 255) if c else (0, 0, 0)
                                pixels[x + 1][y] = (0, 0, 0)
                                pixels[x + 1][y] = (0, 192, 255) if c else (0, 0, 0)  # @@@
                        else:
                            if xx % 2:
                                pixels[x][y] = (0, 0, 0)
                                # green
                                pixels[x][y] = (0, 255, 0) if c else (0, 0, 0)  # @@@
                                pixels[x + 1][y] = (0, 255, 0) if c else (0, 0, 0)
                            else:
                                # violet
                                pixels[x][y] = (255, 0, 255) if c else (0, 0, 0)
                                pixels[x + 1][y] = (0, 0, 0)
                                pixels[x + 1][y] = (255, 0, 255) if c else (0, 0, 0)  # @@@

                        pixels[x][y + 1] = (0, 0, 0)
                        pixels[x + 1][y + 1] = (0, 0, 0)

                    del pixels

    def flash(self):
        if time.time() - self.flash_time >= 0.5:
            self.flash_on = not self.flash_on
            for offset, char in enumerate(self.flash_chars[self.page - 1]):
                if (char & 0xC0) == 0x40:
                    self.update(0x400 + offset, char)
            self.flash_time = time.time()

class Speaker:

    CPU_CYCLES_PER_SAMPLE = 60
    CHECK_INTERVAL = 1000

    def __init__(self):
        pygame.mixer.pre_init(11025, -16, 1)
        pygame.init()
        self.reset()

    def toggle(self, cycle):
        if self.last_toggle is not None:
            l = (cycle - self.last_toggle) / Speaker.CPU_CYCLES_PER_SAMPLE
            self.buffer.extend([0, 26000] if self.polarity else [0, -2600])
            self.buffer.extend((l - 2) * [16384] if self.polarity else [-16384])
            self.polarity = not self.polarity
        self.last_toggle = cycle

    def reset(self):
        self.last_toggle = None
        self.buffer = []
        self.polarity = False

    def play(self):
        sample_array = numpy.int16(self.buffer)
        sound = pygame.sndarray.make_sound(sample_array)
        sound.play()
        self.reset()

    def update(self, cycle):
        if self.buffer and (cycle - self.last_toggle) > self.CHECK_INTERVAL:
            self.play()

class Cassette:

    def __init__(self, fn):
        wav = wave.open(fn, "r")
        self.raw = wav.readframes(wav.getnframes())
        self.start_cycle = 0
        self.start_offset = 0

        for i, b in enumerate(self.raw):
            if ord(b) > 0xA0:
                self.start_offset = i
                break

    def read_byte(self, cycle):
        if self.start_cycle == 0:
            self.start_cycle = cycle
        offset = self.start_offset + (cycle - self.start_cycle) * 22000 / 1000000
        return ord(self.raw[offset]) if offset < len(self.raw) else 0x80

class SoftSwitches:

    def __init__(self, display, speaker, cassette):
        self.kbd = 0x00
        self.display = display
        self.speaker = speaker
        self.cassette = cassette

    def read_byte(self, cycle, address):
        assert 0xC000 <= address <= 0xCFFF
        if address == 0xC000:
            return self.kbd
        elif address == 0xC010:
            self.kbd = self.kbd & 0x7F
        elif address == 0xC030:
            if self.speaker:
                self.speaker.toggle(cycle)
        elif address == 0xC050:
            self.display.txtclr()
        elif address == 0xC051:
            self.display.txtset()
        elif address == 0xC052:
            self.display.mixclr()
        elif address == 0xC053:
            self.display.mixset()
        elif address == 0xC054:
            self.display.lowscr()
        elif address == 0xC055:
            self.display.hiscr()
        elif address == 0xC056:
            self.display.lores()
        elif address == 0xC057:
            self.display.hires()
        elif address == 0xC060:
            if self.cassette:
                return self.cassette.read_byte(cycle)
        else:
            pass  # print "%04X" % address
        return 0x00

class Apple2:

    def __init__(self, options, display, speaker, cassette):
        self.display = display
        self.speaker = speaker
        self.softswitches = SoftSwitches(display, speaker, cassette)

        listener = socket.socket()
        listener.bind(("127.0.0.1", 0))
        listener.listen(0)

        args = [
            sys.executable,
            "cpu6502.py",
            "--bus", str(listener.getsockname()[1]),
            "--rom", options.rom,
        ]
        if options.ram:
            args.extend([
                "--ram", options.ram,
            ])
        if options.pc is not None:
            args.extend([
                "--pc", str(options.pc),
            ])
        self.core = subprocess.Popen(args)

        rs, _, _ = select.select([listener], [], [], 2)
        if not rs:
            print >>sys.stderr, "CPU module did not start"
            sys.exit(1)
        self.cpu, _ = listener.accept()

    def run(self):
        update_cycle = 0
        quit = False
        while not quit:
            op = self.cpu.recv(8)
            if len(op) == 0:
                break
            cycle, rw, addr, val = struct.unpack("<IBHB", op)
            if rw == 0:
                self.cpu.send(chr(self.softswitches.read_byte(cycle, addr)))
            elif rw == 1:
                self.display.update(addr, val)
            else:
                break

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit = True

                if event.type == pygame.KEYDOWN:
                    key = ord(event.unicode) if event.unicode else 0
                    if event.key == pygame.K_LEFT:
                        key = 0x08
                    if event.key == pygame.K_RIGHT:
                        key = 0x15
                    if key:
                        if key == 0x7F:
                            key = 0x08
                        self.softswitches.kbd = 0x80 + (key & 0x7F)

            update_cycle += 1
            if update_cycle >= 1024:
                self.display.flash()
                pygame.display.flip()
                if self.speaker:
                    self.speaker.update(cycle)
                update_cycle = 0

def usage():
    print >>sys.stderr, "ApplePy - an Apple ][ emulator in Python"
    print >>sys.stderr, "James Tauber / http://jtauber.com/"
    print >>sys.stderr
    print >>sys.stderr, "Usage: applepy.py [options]"
    print >>sys.stderr
    print >>sys.stderr, "    -c, --cassette Cassette wav file to load"
    print >>sys.stderr, "    -R, --rom      ROM file to use (default A2ROM.BIN)"
    print >>sys.stderr, "    -r, --ram      RAM file to load (default none)"
    print >>sys.stderr, "    -p, --pc       Initial PC value"
    print >>sys.stderr, "    -q, --quiet    Quiet mode, no sounds (default sounds)"
    sys.exit(1)

def get_options():
    class Options:
        def __init__(self):
            self.cassette = None
            self.rom = "A2ROM.BIN"
            self.ram = None
            self.pc = None
            self.quiet = False

    options = Options()
    a = 1
    while a < len(sys.argv):
        if sys.argv[a].startswith("-"):
            if sys.argv[a] in ("-c", "--cassette"):
                a += 1
                options.cassette = sys.argv[a]
            elif sys.argv[a] in ("-R", "--rom"):
                a += 1
                options.rom = sys.argv[a]
            elif sys.argv[a] in ("-r", "--ram"):
                a += 1
                options.ram = sys.argv[a]
            elif sys.argv[a] in ("-p", "--pc"):
                a += 1
                options.pc = int(sys.argv[a])
            elif sys.argv[a] in ("-q", "--quiet"):
                options.quiet = True
            else:
                usage()
        else:
            usage()
        a += 1

    return options

if __name__ == "__main__":
    options = get_options()
    display = Display()
    speaker = None if options.quiet else Speaker()
    cassette = Cassette(options.cassette) if options.cassette else None

    apple = Apple2(options, display, speaker, cassette)
    apple.run()

max_stars_repo_name: brpollock/applepy

max_issues_repo_name: brpollock/applepy

max_forks_repo_name: brpollock/applepy