text:
#!/usr/bin/env python3

""" Copyright © 2020 Borys Olifirov

Functions for cell detecting and ROI extraction.
Optimysed for confocal images of the individual HEK 293 cells.
(mYFP-HPCA project).

"""

import os
import logging

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

from skimage.external import tifffile
from skimage import filters
from skimage import measure

from scipy.ndimage import measurements as msr
from scipy import signal

def cellMass(img):
    """ Calculating of the center of mass coordinate using threshold mask
    for already detected cell.

    Treshold function use modifyed Hessian filter.
    This method optimysed for confocal image of HEK 293 cells with fluorecent-
    labelled protein who located into the membrane.

    Results of this method for fluorecent microscop images
    or fluorecent-labelled proteins with cytoplasmic localization
    may by unpredictable and incorrect.

    """

    mass_mask = filters.hessian(img, sigmas=range(20, 28, 1))
    mass_cntr = msr.center_of_mass(mass_mask)
    mass_coord = [np.int(mass_cntr[1]), np.int(mass_cntr[0])]

    logging.info("Image center of mass: %s" % mass_coord)

    return mass_coord

def backCon(img, edge_lim=20, dim=3):
    """ Background extraction in TIFF series

    For confocal Z-stacks only!
    dem = 2 for one frame, 3 for z-stack

    """

    if dim == 3:
        edge_stack = img[:,:edge_lim,:edge_lim]
        mean_back = np.mean(edge_stack)

        logging.debug('Mean background, {} px region: {:.3f}'.format(edge_lim, mean_back))

        img_out = np.copy(img)
        img_out = img_out - mean_back
        img_out[img_out < 0] = 0

        return img_out
    elif dim == 2:
        edge_fragment = img[:edge_lim,:edge_lim]
        mean_back = np.mean(edge_fragment)

        logging.debug('Mean background, %s px region: %s' % (edge_lim, mean_back))

        img = np.copy(img)
        img = img - mean_back
        img[img < 0] = 0

        return img

def hystLow(img, img_gauss, sd, mean, diff=40, init_low=0.05, gen_high=0.8):
    """ Lower treshold calculations for hysteresis membrane detection function hystMemb.

    diff - int, difference (in px number) between hysteresis mask and img without greater values
    delta_diff - int, tolerance level (in px number) for diff value
    gen_high, sd, mean - see hystMemb

    """
    masks = {'2sd': ma.masked_greater_equal(img, 2*sd),  # values greater then 2 noise sd
             'mean': ma.masked_greater(img, mean)}         # values greater then mean cytoplasm intensity

    low_val = {}
    control_diff = False
    for mask_name in masks:
        mask_img = masks[mask_name]

        logging.info('Mask {} lower treshold fitting in progress'.format(mask_name))

        mask_hyst = filters.apply_hysteresis_threshold(img_gauss,
                                                      low=init_low*np.max(img_gauss),
                                                      high=gen_high*np.max(img_gauss))
        diff_mask = np.sum(ma.masked_where(~mask_hyst, mask_img) > 0)

        if diff_mask < diff:
            raise ValueError('Initial lower threshold is too low!')
        logging.info('Initial masks difference {}'.format(diff_mask))

        low = init_low

        i = 0
        control_diff = 1
        while diff_mask >= diff:
            mask_hyst = filters.apply_hysteresis_threshold(img_gauss,
                                                          low=low*np.max(img_gauss),
                                                          high=gen_high*np.max(img_gauss))
            diff_mask = np.sum(ma.masked_where(~mask_hyst, mask_img) > 0)

            low += 0.01

            i += 1
            # is cytoplasm mean mask at initial lower threshold value closed? prevent infinit cycle
            if i == 75:
                logging.fatal('Lower treshold for {} mask {:.2f}, control difference {}px'.format(mask_name, low, control_diff))
                raise RuntimeError('Membrane in mean mask doesn`t detected at initial lower threshold value!')

        # is cytoplasm mask at setted up difference value closed?
        if mask_name == 'mean':
            control_diff = np.all((segmentation.flood(mask_hyst, (0, 0)) + mask_hyst))
            if control_diff == True:
                logging.fatal('Lower treshold for {} mask {:.2f}, masks difference {}px'.format(mask_name, low, diff_mask))
                raise ValueError('Membrane in mean mask doesn`t closed, mebrane unlocated at this diff value (too low)!')

        low_val.update({mask_name : low})
    logging.info('Lower tresholds {}'.format(low_val))
    print(low_val)

    return low_val

def hystMemb(img, roi_center, roi_size=30, noise_size=20, low_diff=40, gen_high=0.8, sigma=3):
    """ Function for membrane region detection with hysteresis threshold algorithm.
    Outdide edge - >= 2sd noise
    Inside edge - >= cytoplasm mean intensity

    Require hystLow function for lower hysteresis threshold calculations.

    img - imput z-stack frame;
    roi_center - list of int [x, y], coordinates of center of the cytoplasmic ROI for cytoplasm mean intensity calculation;
    roi_size - int, cutoplasmic ROI side size in px (ROI is a square area);
    noise_size - int, size in px of region for noise sd calculation (square area witf start in 0,0 coordinates);
    sd_low - float, hysteresis algorithm lower threshold for outside cell edge detection,
             > 2sd of noise (percentage of maximum frame intensity);
    mean_low - float, hysteresis algorithm lower threshold for inside cell edge detection,
             > cytoplasmic ROI mean intensity (percentage of maximum frame intensity);
    gen_high - float,  general upper threshold for hysteresis algorithm (percentage of maximum frame intensity);
    sigma - int, sd for gaussian filter.

    Returts membrane region boolean mask for input frame.

    """
    img = backCon(img, dim=2)
    img_gauss = filters.gaussian(img, sigma=sigma)

    noise_sd = np.std(img[:noise_size, :noise_size])
    logging.info('Frame noise SD={:.3f}'.format(noise_sd))

    roi_mean = np.mean(img[roi_center[0] - roi_size//2:roi_center[0] + roi_size//2, \
                           roi_center[1] - roi_size//2:roi_center[1] + roi_size//2])  # cutoplasmic ROI mean celculation
    logging.info('Cytoplasm ROI mean intensity {:.3f}'.format(roi_mean))

    low_val = hystLow(img, img_gauss, sd=noise_sd, mean=roi_mean, diff=low_diff, gen_high=gen_high)

    mask_2sd = filters.apply_hysteresis_threshold(img_gauss,
                                                  low=low_val['2sd']*np.max(img_gauss),
                                                  high=gen_high*np.max(img_gauss))
    mask_roi_mean = filters.apply_hysteresis_threshold(img_gauss,
                                                      low=low_val['mean']*np.max(img_gauss),
                                                      high=gen_high*np.max(img_gauss))
    # filling external space and create cytoplasmic mask
    mask_cytoplasm = mask_roi_mean + segmentation.flood(mask_roi_mean, (0, 0))

    return mask_2sd, mask_roi_mean, ma.masked_where(~mask_cytoplasm, mask_2sd)

if __name__=="__main__":
    pass

# That's all!


text:
from typing import Tuple, Any

import numpy as np
import torch
from torch import nn
import gym

from typing import List

from wordrl.experience import SequenceReplay, Experience
from wordrl.envs.wordle_env_v2_visualized import WORDS

class Agent:
    def __init__(self,
                 net: nn.Module,
                 action_space: Any):
        """
        Args:
            env: training environment
            replay_buffer: replay buffer storing experiences
        """
        self.net = net
        self.action_space = action_space

    def get_action(self, state, epsilon: float, device: str) -> int:
        """Using the given network, decide what action to carry out using an epsilon-greedy policy.

        Args:
            net: DQN network
            epsilon: value to determine likelihood of taking a random action
            device: current device

        Returns:
            action
        """
        if np.random.random() < epsilon:
            action = self.action_space.sample()
        else:
            state = torch.tensor([state]).to(device)
            q_values = self.net(state)
            _, action = torch.max(q_values, dim=1)
            action = int(action.item())

        return action

class A2CEmbeddingChars(nn.Module):
    def __init__(self, obs_size, agent_config):
        """
        Args:
            obs_size: observation/state size of the environment
            hidden_size: size of hidden layers
        """
        super().__init__()
        word_width = 26*5
        emb_size = 8
        self.embedding_layer = nn.Embedding(obs_size, emb_size)
        self.f0 = nn.Sequential(
            nn.Linear(obs_size*emb_size, agent_config["hidden_size"]),
            nn.ReLU(),
            nn.Linear(agent_config["hidden_size"],
                      agent_config["hidden_size"]),
            nn.ReLU(),
            nn.Linear(agent_config["hidden_size"], word_width),
        )
        word_array = np.zeros((word_width, len(word_list)))
        for i, word in enumerate(word_list):
            for j, c in enumerate(word):
                word_array[j*26 + (ord(c) - ord('A')), i] = 1
        self.words = torch.Tensor(word_array)

    def forward(self, x):
        emb = self.embedding_layer(x.int())
        y = self.f0(emb.view(x.shape[0], x.shape[1]
                    * self.embedding_layer.embedding_dim))
        z = torch.tensordot(y, self.words.to(
            self.get_device(x)), dims=[(1,), (0,)])
        return nn.Softmax(dim=1)(z)

class A2CSumChars(nn.Module):
    def __init__(self, obs_size: int, word_list: List[str], n_hidden: int = 1, hidden_size: int = 256):
        """
        Args:
            obs_size: observation/state size of the environment
            n_actions: number of discrete actions available in the environment
            hidden_size: size of hidden layers
        """
        super().__init__()
        word_width = 26*5
        layers = [
            nn.Linear(obs_size, hidden_size),
            nn.ReLU(),
        ]
        for _ in range(n_hidden):
            layers.append(nn.Linear(hidden_size, hidden_size))
            layers.append(nn.ReLU())
        layers.append(nn.Linear(hidden_size, word_width))
        layers.append(nn.ReLU())

        self.f0 = nn.Sequential(*layers)
        word_array = np.zeros((word_width, len(word_list)))
        for i, word in enumerate(word_list):
            for j, c in enumerate(word):
                word_array[j*26 + (ord(c) - ord('A')), i] = 1
        self.words = torch.Tensor(word_array)

        self.actor_head = nn.Linear(word_width, word_width)
        self.critic_head = nn.Linear(word_width, 1)

    def forward(self, x):
        y = self.f0(x.float())
        a = torch.log_softmax(
            torch.tensordot(self.actor_head(y),
                            self.words.to(self.get_device(y)),
                            dims=((1,), (0,))),
            dim=-1)
        c = self.critic_head(y)
        return a, c

    def get_device(self, batch) -> str:
        """Retrieve device currently being used by minibatch."""
        return batch[0].device.index

class ActorCriticAgent:
    """Actor-Critic based agent that returns an action based on the networks policy."""

    def __init__(self, net):
        self.net = net

    def __call__(self, states: torch.Tensor, device: str) -> List[int]:
        """Takes in the current state and returns the action based on the agents policy.
        Args:
            states: current state of the environment
            device: the device used for the current batch
        Returns:
            action defined by policy
        """
        logprobs, _ = self.net(torch.tensor([states], device=device))
        probabilities = logprobs.exp().squeeze(dim=-1)
        prob_np = probabilities.data.cpu().numpy()

        # take the numpy values and randomly select action based on prob distribution
        # Note that this is much faster than numpy.random.choice
        cdf = np.cumsum(prob_np, axis=1)
        cdf[:, -1] = 1.  # Ensure cumsum adds to 1
        select = np.random.random(cdf.shape[0])
        actions = [
            np.searchsorted(cdf[row, :], select[row])
            for row in range(cdf.shape[0])
        ]

        return actions

class GreedyActorCriticAgent:
    def __init__(self, net):
        self.net = net

    def __call__(self, states: torch.Tensor, device: str) -> List[int]:
        """Takes in the current state and returns the action based on the agents policy.
        Args:
            states: current state of the environment
            device: the device used for the current batch
        Returns:
            action defined by policy
        """
        logprobs, _ = self.net(torch.tensor([states], device=device))
        probabilities = logprobs.exp().squeeze(dim=-1)
        prob_np = probabilities.data.cpu().numpy()

        actions = np.argmax(prob_np, axis=1)

        return list(actions)

class DQNEmbeddingChars(nn.Module):
    def __init__(self,
                 obs_size: int,
                 word_list: List[str],
                 n_hidden: int = 1,
                 hidden_size: int = 256,
                 n_emb: int = 32,
                 ):
        """
        Args:
            obs_size: observation/state size of the environment
            n_actions: number of discrete actions available in the environment
            hidden_size: size of hidden layers
        """
        super().__init__()
        word_width = 26*5
        self.n_emb = n_emb

        layers = [
            nn.Linear(obs_size, hidden_size),
            nn.ReLU(),
        ]
        for _ in range(n_hidden):
            layers.append(nn.Linear(hidden_size, hidden_size))
            layers.append(nn.ReLU())
        layers.append(nn.Linear(hidden_size, self.n_emb))

        self.f_state = nn.Sequential(*layers)

        self.actor_head = nn.Linear(self.n_emb, self.n_emb)
        self.critic_head = nn.Linear(self.n_emb, 1)

        word_array = np.zeros((len(word_list), word_width))
        for i, word in enumerate(word_list):
            for j, c in enumerate(word):
                word_array[i, j*26 + (ord(c) - ord('A'))] = 1
        self.words = torch.Tensor(word_array)

        # W x word_width -> W x emb
        self.f_word = nn.Sequential(
            nn.Linear(word_width, 64),
            nn.ReLU(),
            nn.Linear(64, self.n_emb),
        )

    def forward(self, x):
        fs = self.f_state(x.float())
        fw = self.f_word(
            self.words.to(self.get_device(x)),
        ).transpose(0, 1)

        a = torch.log_softmax(
            torch.tensordot(self.actor_head(fs), fw,
                            dims=((1,), (0,))),
            dim=-1)
        c = self.critic_head(fs)
        return a, c

    def get_device(self, batch) -> str:
        """Retrieve device currently being used by minibatch."""
        return batch[0].device.index

class DQNSumChars(nn.Module):
    def __init__(self, obs_size, agent_config):
        """
        Args:
            obs_size: observation/state size of the environment
            hidden_size: size of hidden layers
        """
        # , word_list: List[str], hidden_size: int = 256
        super().__init__()
        word_width = 26*5
        word_list = WORDS
        self.f0 = nn.Sequential(
            nn.Linear(obs_size, agent_config["hidden_size"]),
            nn.ReLU(),
            nn.Linear(agent_config["hidden_size"],
                      agent_config["hidden_size"]),
            nn.ReLU(),
            nn.Linear(agent_config["hidden_size"], word_width),
        )
        word_array = np.zeros((word_width, len(word_list)))
        for i, word in enumerate(word_list):
            for j, c in enumerate(word):
                word_array[j*26 + (ord(c) - ord('A')), i] = 1
        self.words = torch.Tensor(word_array)

    def forward(self, x):
        y = self.f0(x.float())
        return torch.tensordot(y, self.words, dims=((1,), (0,)))

def get_net(obs_size, n_actions, agent_config):
    if agent_config["type"] == "SumChars":
        return SumChars(obs_size, agent_config)
    elif agent_config["type"] == "EmbeddingChars":
        return EmbeddingChars(obs_size, agent_config)
    else:
        raise ValueError("Network not configured!")

ext: py

max_stars_repo_name: VictorButoi/WordRL

max_issues_repo_name: VictorButoi/WordRL

max_forks_repo_name: VictorButoi/WordRL

reason: import numpy


text:
import pickle
import numpy as np
from pathlib import Path
from typing import Dict, Tuple, cast
from functools import cache
from nomenklatura.util import DATA_PATH
from sklearn.pipeline import Pipeline  # type: ignore

from nomenklatura import __version__
from nomenklatura.entity import CompositeEntity as Entity
from nomenklatura.matching.features import FEATURES, encode_pair
from nomenklatura.matching.types import FeatureDocs, MatchingResult

BASE_URL = "https://github.com/opensanctions/nomenklatura/blob/%s/nomenklatura/%s#L%s"
MODEL_PATH = DATA_PATH.joinpath("match-regression.pkl")
CODE_PATH = DATA_PATH.joinpath("..").resolve()

def save_matcher(pipe: Pipeline, coefficients: Dict[str, float]) -> None:
    """Store a classification pipeline after training."""
    mdl = pickle.dumps({"pipe": pipe, "coefficients": coefficients})
    with open(MODEL_PATH, "wb") as fh:
        fh.write(mdl)
    load_matcher.cache_clear()
    explain_matcher.cache_clear()

@cache
def load_matcher() -> Tuple[Pipeline, Dict[str, float]]:
    """Load a pre-trained classification pipeline for ad-hoc use."""
    with open(MODEL_PATH, "rb") as fh:
        matcher = pickle.loads(fh.read())
    pipe = cast(Pipeline, matcher["pipe"])
    coefficients = cast(Dict[str, float], matcher["coefficients"])
    current = [f.__name__ for f in FEATURES]
    if list(coefficients.keys()) != current:
        raise RuntimeError("Model was not trained on identical features!")
    return pipe, coefficients

@cache
def explain_matcher() -> FeatureDocs:
    """Return an explanation of the features and their coefficients."""
    features: FeatureDocs = {}
    _, coefficients = load_matcher()
    for func in FEATURES:
        name = func.__name__
        code_path = Path(func.__code__.co_filename).relative_to(CODE_PATH)
        line_no = func.__code__.co_firstlineno
        url = BASE_URL % (__version__, code_path, line_no)
        features[name] = {
            "description": func.__doc__,
            "coefficient": coefficients[name],
            "url": url,
        }
    return features

def compare_scored(left: Entity, right: Entity) -> MatchingResult:
    """Encode a comparison of the two entities, apply the model and return a score."""
    pipe, _ = load_matcher()
    encoded = encode_pair(left, right)
    npfeat = np.array([encoded])
    pred = pipe.predict_proba(npfeat)
    score = cast(float, pred[0][1])
    features = {f.__name__: c for f, c in zip(FEATURES, encoded)}
    return {"score": score, "features": features}

max_stars_repo_name: opensanctions/nomenklatura

max_issues_repo_name: opensanctions/nomenklatura

max_forks_repo_name: opensanctions/nomenklatura


text:
import numpy as np
import time
import RPi.GPIO as GPIO
from sh import gphoto2 as gp
import numpy as np
import os

#Status LED
ReadyPin = 37 # This Led will glow, when we are ready to take a photo
PhotoPin = 35 # This Led glows when we taking the phot
SwitchPin = 33 # Switch to take a photo

def setup():
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    GPIO.setup(ReadyPin, GPIO.OUT)    # input mode
    GPIO.setup(PhotoPin, GPIO.OUT)
    GPIO.setup(SwitchPin,GPIO.IN, pull_up_down = GPIO.PUD_UP)
    GPIO.output(PhotoPin, False)
    GPIO.output(ReadyPin,True)
    return 0

def take_a_photo(max_file_number):
    GPIO.output(PhotoPin, True)
    GPIO.output(ReadyPin,False)

    max_file_number += 1
    np.savetxt('filenumber.txt', [max_file_number])

    gp( '--capture-image-and-download' ) #takeing a photo
    print('rename')

    os.rename( 'capt0000.jpg', 'photobox_' + str(int(max_file_number)) + '.jpg') #rename file
    print('photo saved')

    GPIO.output(PhotoPin, False)
    GPIO.output(ReadyPin,True)
    return 0

def destroy ():
    GPIO.cleanup()

##Change Folder
os.chdir("/home/pi/rasberry/photobox/photo_folder")

max_file_number = 0

with open('filenumber.txt', 'r') as f:
    max_file_number = float(f.read())

print( type(max_file_number), max_file_number)
## Let's take some pictures!!
setup()
try:
    take_a_photo(max_file_number)
except:
    destroy()

max_stars_repo_name: beckstev/rasberry

max_issues_repo_name: beckstev/rasberry

max_forks_repo_name: beckstev/rasberry


text:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  7 06:51:03 2018

@author: Adam
"""

import math
import numpy as np
from numpy import asarray
from scipy.special import rel_entr

def get_cosine(vec1, vec2):
     numerator = np.dot(vec1, vec2)
     sum1 = sum([vec1[x-1]**2 for x in range(len(vec1))])
     sum2 = sum([vec2[x-1]**2 for x in range(len(vec1))])
     denominator = math.sqrt(sum1) * math.sqrt(sum2)
     if not denominator:
         return 0.0
     else:
         return float(numerator) / denominator

def get_kl(pk, qk):

    pk = asarray(pk)
    pk = 1.0*pk / np.sum(pk, axis=0)
    qk = asarray(qk)
    qk = 1.0*qk / np.sum(qk, axis=0)
    vec = rel_entr(pk, qk)
    S = np.sum(vec, axis=0)
    return S

reason: import numpy,from numpy,from scipy


text:
import numpy as np
from .SharedTrees import cstr, get_fmt, Description, get_data_kind, get_recording_mode, AmplifierState_v9, UserParamDescrType, LockInParams_v9

class TraceRecord(Description):
    def __init__(self, n=1):
        super(TraceRecord, self).__init__(n)

        self.description = [

            ("TrMark",                  "i"),                       # (* INT32 *)
            ("TrLabel",                 "32s",       cstr),         # (* String32Type *)
            ("TrTraceID",               "i"),                       # (* INT32 *)
            ("TrData",                  "i"),                       # (* INT32 *)
            ("TrDataPoints",            "i"),                       # (* INT32 *)
            ("TrInternalSolution",      "i"),                       # (* INT32 *)
            ("TrAverageCount",          "i"),                       # (* INT32 *)
            ("TrLeakID",                "i"),                       # (* INT32 *)
            ("TrLeakTraces",            "i"),                       # (* INT32 *)
            ("TrDataKind",              "h",     get_data_kind),    # (* SET16 *)
            ("TrFiller1",               "h"),
            ("TrRecordingMode",         "b",     get_recording_mode),  # (* BYTE *)
            ("TrAmplIndex",             "c"),                       # (* CHAR *)
            ("TrDataFormat",            "b"),                       # (* BYTE *)
            ("TrDataAbscissa",          "b"),                       # (* BYTE *)
            ("TrDataScaler",            "d"),                       # (* LONGREAL *)
            ("TrTimeOffset",            "d"),                       # (* LONGREAL *)
            ("TrZeroData",              "d"),                       # (* LONGREAL *)
            ("TrYUnit",                 "8s",        cstr),         # (* String8Type *)
            ("TrXInterval",             "d"),                       # (* LONGREAL *)
            ("TrXStart",                "d"),                       # (* LONGREAL *)
            ("TrXUnit",                 "8s",        cstr),         # (* String8Type *)
            ("TrYRange",                "d"),                       # (* LONGREAL *)
            ("TrYOffset",               "d"),                       # (* LONGREAL *)
            ("TrBandwidth",             "d"),                       # (* LONGREAL *)
            ("TrPipetteResistance",     "d"),                       # (* LONGREAL *)
            ("TrCellPotential",         "d"),                       # (* LONGREAL *)
            ("TrSealResistance",        "d"),                       # (* LONGREAL *)
            ("TrCSlow",                 "d"),                       # (* LONGREAL *)
            ("TrGSeries",               "d"),                       # (* LONGREAL *)
            ("TrRsValue",               "d"),                       # (* LONGREAL *)
            ("TrGLeak",                 "d"),                       # (* LONGREAL *)
            ("TrMConductance",          "d"),                       # (* LONGREAL *)
            ("TrLinkDAChannel",         "i"),                       # (* INT32 *)
            ("TrValidYrange",           "?"),                       # (* BOOLEAN *)
            ("TrAdcMode",               "b"),                       # (* CHAR *)        # "c" is not read properly
            ("TrAdcChannel",            "h"),                       # (* INT16 *)
            ("TrYmin",                  "d"),                       # (* LONGREAL *)
            ("TrYmax",                  "d"),                       # (* LONGREAL *)
            ("TrSourceChannel",         "i"),                       # (* INT32 *)
            ("TrExternalSolution",      "i"),                       # (* INT32 *)
            ("TrCM",                    "d"),                       # (* LONGREAL *)
            ("TrGM",                    "d"),                       # (* LONGREAL *)
            ("TrPhase",                 "d"),                       # (* LONGREAL *)
            ("TrDataCRC",               "I"),                       # (* CARD32 *)
            ("TrCRC",                   "I"),                       # (* CARD32 *)
            ("TrGS",                    "d"),                       # (* LONGREAL *)
            ("TrSelfChannel",           "i"),                       # (* INT32 *)

            # Sigtool added the below 15.08.2012
            ("TrInterleaveSize",        "i"),                       # (* INT32 *)
            ("TrInterleaveSkip",        "i"),                       # (* INT32 *)
            ("TrImageIndex",            "i"),                       # (* INT32 *)
            ("TrTrMarkers",             "10d"),                     # (* ARRAY[0..9] OF LONGREAL *)
            ("TrSECM_X",                "d"),                       # (* LONGREAL *)
            ("TrSECM_Y",                "d"),                       # (* LONGREAL *)
            ("TrSECM_Z",                "d"),                       # (* LONGREAL *)
        ]

        self.size = 408

class SweepRecord(Description):
    def __init__(self, n=1):
        super(SweepRecord, self).__init__(n)

        self.description = [

            ("SwMark",                  "i"),               # (* INT32 *)
            ("SwLabel",                 "32s",      cstr),  # (* String32Type *)
            ("SwAuxDataFileOffset",     "i"),               # (* INT32 *)
            ("SwStimCount",             "i"),               # (* INT32 *)
            ("SwSweepCount",            "i"),               # (* INT32 *)
            ("SwTime",                  "d"),               # (* LONGREAL *)
            ("SwTimer",                 "d"),               # (* LONGREAL *)
            ("SwSwUserParams",          "4d"),              # (* ARRAY[0..3] OF LONGREAL *)
            ("SwTemperature",           "d"),               # (* LONGREAL *)
            ("SwOldIntSol",             "i"),               # (* INT32 *)
            ("SwOldExtSol",             "i"),               # (* INT32 *)
            ("SwDigitalIn",             "h"),               # (* SET16 *)
            ("SwSweepKind",             "h"),               # (* SET16 *)
            ("SwFiller1",               "i"),               # Int32 in older versions, int16 in newer
            ("SwSwMarkers",             "4d"),              # (* ARRAY[0..3] OF LONGREAL, see SwMarkersNo *)
            ("SwFiller2",               "i"),               # (* INT32 *)
            ("SwCRC",                   "I"),               # (* CARD32 *)
        ]
        self.size = 160

class PulSeriesRecord(Description):
    def __init__(self, n=1):
        super(PulSeriesRecord, self).__init__(n)

        self.description = [

            ("SeMark",                                "i"),                                             # (* INT32 *)
            ("SeLabel",                               "32s",                   cstr),                   # (* String32Type *)
            ("SeComment",                             "80s",                   cstr),                   # (* String80Type *)
            ("SeSeriesCount",                         "i"),                                             # (* INT32 *)
            ("SeNumberSweeps",                        "i"),                                             # (* INT32 *)
            ("SeAmplStateOffset",                     "i"),                                             # (* INT32 *)
            ("SeAmplStateSeries",                     "i"),                                             # (* INT32 *)
            ("SeMethodTag",                           "i"),                                             # (* INT32 *)
            ("SeTime",                                "d"),                                             # * LONGREAL *)
            ("SePageWidth",                           "d"),                                             # * LONGREAL *)
            ("SeSwUserParamDescr",                    "160s",                  UserParamDescrType(4)),  # (* ARRAY[0..3] OF UserParamDescrType = 4*40 *)
            ("SeMethodName",                          "32s",                   cstr),                   # (* String32Type *)
            ("SeSeUserParams1",                       "4d"),                                            # (* ARRAY[0..3] OF LONGREAL *)
            ("SeLockInParams",                        "96s",                   LockInParams_v9()),      # (* SeLockInSize = 96, see "Pulsed.de" *)
            ("SeAmplifierState",                      "400s",                  AmplifierState_v9()),    # (* AmplifierStateSize = 400 *)
            ("SeUsername",                            "80s",                   cstr),                   # (* String80Type *)
            ("SeSeUserParamDescr1",                   "160s",                  UserParamDescrType(4)),  # (* ARRAY[0..3] OF UserParamDescrType = 4*40 *)
            ("SeFiller1",                             "i"),                                             # (* INT32 *)
            ("SeCRC",                                 "I"),                                             # (* CARD32 *)
            ("SeSeUserParams2",                       "4d"),                                            # (* ARRAY[0..3] OF LONGREAL *)
            ("SeSeUserParamDescr2",                   "160s",                  UserParamDescrType(4)),  # (* ARRAY[0..3] OF UserParamDescrType = 4*40 *)
            ("SeScanParams",                          "96s",                   cstr),                   # (* ScanParamsSize = 96 *)
        ]
        self.size = 1408

class GroupRecord(Description):
    def __init__(self, n=1):
        super(GroupRecord, self).__init__(n)

        self.description = [

            ("GrMark",                           "i"),                   # (* INT32 *)
            ("GrLabel",                          "32s",         cstr),   # (* String32Size *)
            ("GrText",                           "80s",         cstr),   # (* String32Size *)
            ("GrExperimentNumber",               "i"),                   # (* INT32 *)
            ("GrGroupCount",                     "i"),                   # (* INT32 *)
            ("GrCRC",                            "I"),                   # (* CARD32 *)
        ]
        self.size = 128

class PulseRootRecord(Description):
    def __init__(self, n=1):
        super(PulseRootRecord, self).__init__(n)

        self.description = [
            ("RoVersion",           "i"),                    # (* INT32 *)
            ("RoMark",              "i"),                    # (* INT32 *)
            ("RoVersionName",       "32s",       cstr),      # (* String32Type *)
            ("RoAuxFileName",       "80s",       cstr),      # (* String80Type *)
            ("RoRootText",          "400s",      cstr),      # (* String400Type *)
            ("RoStartTime",         "d"),                    # (* LONGREAL *)
            ("RoMaxSamples",        "i"),                    # (* INT32 *)
            ("RoCRC",               "I"),                    # (* CARD32 *)
            ("RoFeatures",          "h"),                    # (* SET16 *)
            ("RoFiller1",           "h"),                    # (* INT16 *)
            ("RoFiller2",           "i"),                    # (* INT32 *)
        ]
        self.size = 544

max_stars_repo_name: easy-electrophysiology/load-heka-python

max_issues_repo_name: easy-electrophysiology/load-heka-python

max_forks_repo_name: easy-electrophysiology/load-heka-python


text:
"""
utils.py

Contains helper functions.
"""

from collections import OrderedDict
import copy
import math
import os

import numpy as np

from skimage.transform import resize

import torch
import torch.nn as nn

from torchvision import models

from tqdm import tqdm

from resnet_caffe import load_resnet50

VOC_CLASSES = np.array([
    'aeroplane',
    'bicycle',
    'bird',
    'boat',
    'bottle',
    'bus',
    'car',
    'cat',
    'chair',
    'cow',
    'diningtable',
    'dog',
    'horse',
    'motorbike',
    'person',
    'pottedplant',
    'sheep',
    'sofa',
    'train',
    'tvmonitor',
], dtype=str)
COCO_CATEGORY_IDS = np.loadtxt(os.path.join(os.path.dirname(__file__), 'data/coco_category_ids.txt'), dtype=int)

class RISE(nn.Module):
    def __init__(self, model, input_size, device, gpu_batch=100):
        super(RISE, self).__init__()
        self.model = model
        assert(isinstance(input_size, int))
        self.input_size = input_size
        self.gpu_batch = gpu_batch
        self.sigmoid = nn.Sigmoid()
        self.device = device

    def generate_masks(self, N, s, p1, savepath='masks.npy'):
        cell_size = np.ceil(self.input_size / s)
        up_size = (s + 1) * cell_size

        grid = np.random.rand(N, s, s) < p1
        grid = grid.astype(np.float32)

        self.masks = np.empty((N, self.input_size, self.input_size))

        for i in tqdm(range(N), desc='Generating filters'):
            # Random shifts
            x, y = np.random.randint(0, cell_size, 2)
            # Linear upsampling and cropping
            self.masks[i, :, :] = resize(grid[i],
                                         (up_size, up_size),
                                         order=1,
                                         mode='reflect',
                                         anti_aliasing=False)[x:x + self.input_size, y:y + self.input_size]
        self.masks = self.masks.reshape(-1, 1, self.input_size, self.input_size)
        np.save(savepath, self.masks)
        self.masks = torch.from_numpy(self.masks).float()
        self.N = N

    def load_masks(self, filepath='masks.npy'):
        masks_np = np.load(filepath)
        self.masks = torch.from_numpy(masks_np).float()
        self.N = self.masks.shape[0]

    def update_input_size(self, input_size):
        self.input_size = input_size
        mask_temp = np.transpose(self.masks[:,0].cpu().data.numpy(),
                                 (1, 2, 0))
        mask_temp = resize(mask_temp,
                           self.input_size,
                           order=1,
                           mode='reflect',
                           anti_aliasing=False)
        mask_temp = np.transpose(mask_temp, (2, 0, 1))
        self.masks = torch.from_numpy(mask_temp).float().unsqueeze(1)

    def forward(self, x):
        N = self.N
        _, _, H, W = x.size()

        # print('msk shape', self.masks.shape)
        # print('x shape', x.shape)
        p = []
        for i in range(0, N, self.gpu_batch):
            with torch.no_grad():
                x_new = torch.mul(self.masks[i:min(i + self.gpu_batch, N)],
                                  x.cpu().data).to(self.device)
                p.append(self.sigmoid(self.model(x_new)).cpu())
        p = torch.cat(p)

        # Number of classes.
        CL = p.size(1)
        # print(CL)
        # print(p.shape)

        if len(p.shape) == 4:
            assert(p.shape[2] == 1)
            assert(p.shape[3] == 1)
            p = p[:,:,0,0]

        assert(len(p.shape) == 2)

        sal = torch.matmul(p.data.transpose(0, 1),
                           self.masks.view(N, H * W))
        sal = sal.view((CL, H, W))
        sal = sal / N
        return sal

class FromVOCToOneHotEncoding(object):
    def __init__(self, num_classes=20, class_to_idx=None):
        self.num_classes = num_classes
        if class_to_idx is None:
            self.class_to_idx = {c: i for i, c in enumerate(VOC_CLASSES)}
        else:
            self.class_to_idx = class_to_idx
        assert(self.num_classes == len(self.class_to_idx))

    def __call__(self, d):
        assert('annotation' in d)
        assert('object' in d['annotation'])
        objs = d['annotation']['object']
        if isinstance(objs, list):
            classes = [obj['name'] for obj in objs]
        else:
            classes = [objs['name']]

        class_idx = [self.class_to_idx[c] for c in classes]
        label = np.zeros(self.num_classes, dtype=np.float32)
        label[class_idx] = 1
        return label

class FromCocoToOneHotEncoding(object):
    def __init__(self, num_classes=80, class_to_idx=None):
        self.num_classes = num_classes
        if class_to_idx is None:
            self.class_to_idx = {c: i for i, c in enumerate(COCO_CATEGORY_IDS)}
        else:
            self.class_to_idx = class_to_idx
        assert(self.num_classes == len(self.class_to_idx))

    def __call__(self, anns):
        class_idx = []
        label = np.zeros(self.num_classes, dtype=np.float32)
        for ann in anns:
            assert('category_id' in ann)
            class_idx.append(self.class_to_idx[ann['category_id']])
        label[class_idx] = 1
        return label

def blur_input_tensor(tensor, kernel_size=11, sigma=5.0):
    """Blur tensor with a 2D gaussian blur.

    Args:
        tensor: torch.Tensor, 3 or 4D tensor to blur.
        kernel_size: int, size of 2D kernel.
        sigma: float, standard deviation of gaussian kernel.

    Returns:
        4D torch.Tensor that has been smoothed with gaussian kernel.
    """
    ndim = len(tensor.shape)
    if ndim == 3:
        tensor = tensor.unsqueeze(0)
    assert ndim == 4
    num_channels = tensor.shape[1]
    device = tensor.device

    # Create a x, y coordinate grid of shape (kernel_size, kernel_size, 2)
    x_cord = torch.arange(kernel_size)
    x_grid = x_cord.repeat(kernel_size).view(kernel_size, kernel_size)
    y_grid = x_grid.t()
    xy_grid = torch.stack([x_grid, y_grid], dim=-1).float()

    mean = (kernel_size - 1)/2.
    variance = sigma**2.

    # Calculate the 2-dimensional gaussian kernel which is
    # the product of two gaussian distributions for two different
    # variables (in this case called x and y)
    gaussian_kernel = (1./(2.*math.pi*variance)) * torch.exp(
        -1*torch.sum((xy_grid - mean)**2., dim=-1) /
        (2.*variance)
    )

    # Make sure sum of values in gaussian kernel equals 1.
    gaussian_kernel = gaussian_kernel / torch.sum(gaussian_kernel)

    # Reshape to 2d depthwise convolutional weight
    gaussian_kernel = gaussian_kernel.view(1, 1, kernel_size, kernel_size)
    gaussian_kernel = gaussian_kernel.repeat(num_channels, 1, 1, 1)
    gaussian_kernel = gaussian_kernel.to(device)

    padding = nn.ReflectionPad2d(int(mean)).to(device)
    gaussian_filter = nn.Conv2d(in_channels=num_channels,
                                out_channels=num_channels,
                                kernel_size=kernel_size,
                                groups=num_channels,
                                bias=False).to(device)

    gaussian_filter.weight.data = gaussian_kernel
    gaussian_filter.weight.requires_grad = False

    smoothed_tensor = gaussian_filter(padding(tensor))

    return smoothed_tensor

class SimpleToTensor(object):
    def __call__(self, x):
        return torch.from_numpy(x)

class GoogLeNetNormalize(object):
    """Preprocess input as done in caffe for GoogLeNet."""
    def __call__(self, x):
        assert(len(x.shape) == 3)
        x_ch0 = torch.unsqueeze(x[0], 0) * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
        x_ch1 = torch.unsqueeze(x[1], 0) * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
        x_ch2 = torch.unsqueeze(x[2], 0) * (0.225 / 0.5) + (0.406 - 0.5) / 0.5
        x = torch.cat((x_ch0, x_ch1, x_ch2), 0)
        return x

class CaffeNetWrapper(nn.Module):
    def __init__(self, model, key):
        self.model = model
        self.key = key
        self.model.verbose = False
        self.model.phase = 'TEST'

    def forward(self, x):
        blobs = self.model(x)
        return blobs[self.key]

def get_finetune_model(arch='vgg16',
                       dataset='voc_2007',
                       checkpoint_path=None,
                       convert_to_fully_convolutional=False,
                       final_gap_layer=False,
                       converted_caffe=False,
                       torchvision_path='/users/ruthfong/pytorch/vision'):
    # Set number of classes in dataset.
    if 'voc' in dataset:
        num_classes = 20
    elif 'coco' in dataset:
        num_classes = 80
    elif 'imnet' in dataset:
        num_classes = 1000
    else:
        assert(False)

    # Load pre-trained model.
    # Handle GoogLeNet specially because it's not in the stable release of torchvision yet.
    if arch == 'googlenet':
        import importlib.util
        googlenet_path = os.path.join(torchvision_path,
                                      'torchvision/models/googlenet.py')
        spec = importlib.util.spec_from_file_location('googlenet',
                                                      googlenet_path)
        googlenet = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(googlenet)

        model = googlenet.googlenet(pretrained=checkpoint_path is None, transform_input=False)
        model.aux_logits = False
    else:
        model = models.__dict__[arch](pretrained=checkpoint_path is None)
    if arch == 'inception_v3':
        model.aux_logits = False

    # Only fine-tune last layer.
    for p in model.parameters():
        p.requires_grad = False

    # Get the last layer.
    if 'imnet' not in dataset:
        last_name, last_module = list(model.named_modules())[-1]

        # Construct new last layer.
        if isinstance(last_module, nn.Linear):
            in_features = last_module.in_features
            bias = last_module.bias is not None
            new_layer_module = nn.Linear(in_features, num_classes, bias=bias)
        else:
            assert(False)

        # Replace last layer.
        model = replace_module(model, last_name.split('.'), new_layer_module)

    # Load weights, if provided.
    if checkpoint_path is not None:
        if converted_caffe:
            checkpoint = torch.load(checkpoint_path, map_location='cpu')
            if arch == 'vgg16':
                classifier_keys = [k for k in checkpoint.keys()
                                   if 'classifier' in k]
                index_remapping = {0:0, 2:3, 4:6}
                for k in classifier_keys:
                    # Get original key.
                    parent_module, index, weight_name = k.split('.')
                    new_index = str(index_remapping[int(index)])
                    new_k = '.'.join([parent_module, new_index, weight_name])

                    # Reshape weights if necessary.
                    weights = checkpoint[k]
                    if weight_name == 'weight':
                        checkpoint[new_k] = weights.reshape(weights.shape[0], -1)
                    elif weight_name == 'bias':
                        checkpoint[new_k] = weights
                    else:
                        assert(False)

                    # Delete old key-value pair.
                    if new_k != k:
                        del checkpoint[k]
                model.load_state_dict(checkpoint)
            elif arch == 'resnet50':
                # Load custom ResNet50 architecture.
                model = load_resnet50(checkpoint_path)
            else:
                assert(False)
        else:
            checkpoint = torch.load(checkpoint_path, map_location='cpu')
            model.load_state_dict(checkpoint['state_dict'])

    # Convert model to fully convolutional one.
    if convert_to_fully_convolutional:
        model = make_fully_convolutional(model,
                                         final_gap_layer=final_gap_layer)

    # Set model to evaluation mode.
    model.eval()

    return model

def make_fully_convolutional(model, final_gap_layer=False):
    if isinstance(model, models.VGG):
        new_model_layers = list(model.features.children())

        # Get fully-connected layers.
        classifier = list(model.classifier.children())

        # Especially handle first fully-connected layer.
        first_fc = classifier[0].state_dict()
        in_ch = 512
        kernel_size = 7
        out_ch = first_fc['weight'].shape[0]
        orig_in_ch = in_ch * kernel_size * kernel_size
        assert(first_fc['weight'].shape[1] == orig_in_ch)
        first_conv = nn.Conv2d(in_ch, out_ch, (kernel_size, kernel_size))
        first_conv.load_state_dict({
            'weight': first_fc['weight'].view(out_ch,
                                              in_ch,
                                              kernel_size,
                                              kernel_size),
            'bias': first_fc['bias']
        })
        new_model_layers.append(first_conv)

        # Handle subsequent layers.
        for layer in classifier[1:]:
            if isinstance(layer, nn.Linear):
                fc = layer.state_dict()
                out_ch, in_ch = fc['weight'].shape
                conv = nn.Conv2d(in_ch, out_ch, (1, 1))
                conv.load_state_dict({
                    'weight': fc['weight'].view(out_ch, in_ch, 1, 1),
                    'bias': fc['bias']
                })
                new_model_layers.append(conv)
            else:
                new_model_layers.append(layer)
    elif isinstance(model, models.ResNet):
        new_model_layers = list(model.children())[:-1]

        first_fc = model.fc.state_dict()
        out_ch, in_ch = first_fc['weight'].shape
        first_conv = nn.Conv2d(in_ch, out_ch, (1, 1))
        first_conv.load_state_dict({
            'weight': first_fc['weight'].view(out_ch, in_ch, 1, 1),
            'bias': first_fc['bias']
        })

        new_model_layers.append(first_conv)
    # TODO(ruthfong): Handle this better once GoogLeNet is in stable release.
    elif type(model).__name__ == 'GoogLeNet':
        # Exclude InceptionAux, dropout, and last FC layer.
        new_model_layers = [m for m in list(model.children())[:-2]
                            if type(m).__name__ != 'InceptionAux']

        first_fc = model.fc.state_dict()
        out_ch, in_ch = first_fc['weight'].shape
        first_conv = nn.Conv2d(in_ch, out_ch, (1, 1))
        first_conv.load_state_dict({
            'weight': first_fc['weight'].view(out_ch, in_ch, 1, 1),
            'bias': first_fc['bias']
        })

        new_model_layers.append(first_conv)
    else:
        assert(False)

    # Add final global average pooling layer.
    if (final_gap_layer
        and not isinstance(model, models.ResNet)
        and not type(model).__name__ == 'GoogLeNet'):
        new_model_layers.append(nn.AdaptiveAvgPool2d((1, 1)))

    new_model = nn.Sequential(*new_model_layers)

    return new_model

def create_dir_if_necessary(path, is_dir=False):
    """Create directory to path if necessary."""
    parent_dir = get_parent_dir(path) if not is_dir else path
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)

def get_parent_dir(path):
    """Return parent directory of path."""
    return os.path.abspath(os.path.join(path, os.pardir))

def str2bool(value):
    """Converts string to bool."""
    value = value.lower()
    if value in ('yes', 'true', 't', '1'):
        return True
    if value in ('no', 'false', 'f', '0'):
        return False
    raise ValueError('Boolean argument needs to be true or false. '
                     'Instead, it is %s.' % value)

def get_device():
    """Return torch.device based on if cuda is available."""
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    return device

def get_pytorch_module(net, layer_name):
    """Return PyTorch module."""
    modules = layer_name.split('.')
    if len(modules) == 1:
        return net._modules.get(layer_name)
    else:
        curr_m = net
        for m in modules:
            curr_m = curr_m._modules.get(m)
        return curr_m

def replace_module(parent_module, module_path, replacement_module):
    """Replace a PyTorch module with a replacement module."""
    if isinstance(parent_module, nn.Sequential):
        module_dict = OrderedDict()
    elif isinstance(parent_module, nn.Module):
        new_parent_module = copy.deepcopy(parent_module)
    for (k, v) in parent_module._modules.items():
        if k == module_path[0]:
            if len(module_path) == 1:
                child_module = replacement_module
            else:
                child_module = replace_module(v, module_path[1:],
                                              replacement_module)
        else:
            child_module = v

        if isinstance(parent_module, nn.Sequential):
            module_dict[k] = child_module
        elif isinstance(parent_module, nn.Module):
            setattr(new_parent_module, k, child_module)
        else:
            assert False

    if isinstance(parent_module, nn.Sequential):
        return nn.Sequential(module_dict)
    elif isinstance(parent_module, nn.Module):
        return new_parent_module
    else:
        assert False

def register_hook_on_module(curr_module,
                            module_type,
                            hook_func,
                            hook_direction='backward'):
    """Register hook on all modules of a given type."""
    if isinstance(curr_module, module_type):
        if hook_direction == 'forward':
            curr_module.register_forward_hook(hook_func)
        elif hook_direction == 'backward':
            curr_module.register_backward_hook(hook_func)
        else:
            raise NotImplementedError('Only "forward" and "backward" are '
                                      'supported, not %s.' % hook_direction)
    for m in curr_module.children():
        register_hook_on_module(m,
                                module_type,
                                hook_func,
                                hook_direction=hook_direction)

activations = []

def hook_acts(module, input, output):
    """Forward hook function for saving activations."""
    activations.append(output)

def get_acts(model, input, second_input=None, clone=True):
    """Returns activations saved using existing hooks."""
    del activations[:]
    if second_input is not None:
        _ = model(input, second_input)
    else:
        _ = model(input)
    if clone:
        return [a.clone() for a in activations]
    else:
        return activations

def hook_get_acts(model, layer_names, input, second_input=None, clone=True):
    """Returns activations at specified layers."""
    hooks = []
    for i in range(len(layer_names)):
        hooks.append(
            get_pytorch_module(model, layer_names[i]).register_forward_hook(
                hook_acts))

    acts_res = [a for a in
                get_acts(model, input, second_input=second_input, clone=clone)]

    for h in hooks:
        h.remove()

    return acts_res

def set_gpu(gpu=None):
    """Set visible gpu(s). This function should be called once at beginning.

    Args:
        gpu (NoneType, int, or list of ints): the gpu(s) (zero-indexed) to use;
            None if no gpus should be used.
    Return:
        bool: True if using at least 1 gpu; otherwise False.
    """
    # Check type of gpu.
    if isinstance(gpu, list):
        if gpu:
            for gpu_i in gpu:
                if not isinstance(gpu_i, int):
                    raise ValueError('gpu should be of type NoneType, int, or '
                                     'list of ints. Instead, gpu[%d] is of '
                                     'type %s.' % type(gpu_i))
    elif isinstance(gpu, int):
        pass
    elif gpu is None:
        pass
    else:
        raise ValueError('gpu should be of type NoneType, int, or list of '
                         'ints. Instead, gpu is of type %s.' % type(gpu))

    # Set if gpu usage (i.e., cuda) is enabled.
    if gpu is None:
        cuda = False
    elif isinstance(gpu, list) and not gpu:
        cuda = False
    else:
        cuda = True

    # Set CUDA_VISIBLE_DEVICES environmental variable.
    gpu_params = ''
    if cuda:
        if isinstance(gpu, list):
            gpu_params = str(gpu).strip('[').strip(']')
        else:
            gpu_params = str(gpu)
    os.environ['CUDA_VISIBLE_DEVICES'] = gpu_params

    # Check type of framework.
    num_visible_gpus = torch.cuda.device_count()

    # Check number of visible gpus.
    if isinstance(gpu, list):
        if num_visible_gpus != len(gpu):
            raise ValueError('The following %d gpu(s) should be visible: %s; '
                             'instead, %d gpu(s) are visible.'
                             % (len(gpu), str(gpu), num_visible_gpus))
    elif gpu is None:
        if num_visible_gpus != 0:
            raise ValueError('0 gpus should be visible; instead, %d gpu(s) '
                             'are visible.' % num_visible_gpus)
    else:
        if num_visible_gpus != 1:
            raise ValueError('1 gpu should be visible; instead %d gpu(s) '
                             'are visible.' % num_visible_gpus)
        assert num_visible_gpus == 1

    print("%d GPU(s) being used at the following index(es): %s" % (
        num_visible_gpus, gpu_params))
    return cuda


text:
"""
Script generates predictions, splitting original images into tiles, and assembling prediction back together
"""
import argparse
from prepare_train_val import get_split
from dataset import RoboticsDataset
import cv2
from models import UNet16, LinkNet34, UNet11, UNet, AlbuNet
import torch
from pathlib import Path
from tqdm import tqdm
import numpy as np
import utils
import prepare_data
from torch.utils.data import DataLoader
from torch.nn import functional as F
from prepare_data import (original_height,
                          original_width,
                          h_start, w_start
                          )
from albumentations import Compose, Normalize

def img_transform(p=1):
    return Compose([
        Normalize(p=1)
    ], p=p)

def get_model(model_path, model_type='UNet11', problem_type='binary'):
    """

    :param model_path:
    :param model_type: 'UNet', 'UNet16', 'UNet11', 'LinkNet34', 'AlbuNet'
    :param problem_type: 'binary', 'parts', 'instruments'
    :return:
    """
    if problem_type == 'binary':
        num_classes = 1
    elif problem_type == 'parts':
        num_classes = 4
    elif problem_type == 'instruments':
        num_classes = 8

    if model_type == 'UNet16':
        model = UNet16(num_classes=num_classes)
    elif model_type == 'UNet11':
        model = UNet11(num_classes=num_classes)
    elif model_type == 'LinkNet34':
        model = LinkNet34(num_classes=num_classes)
    elif model_type == 'AlbuNet':
        model = AlbuNet(num_classes=num_classes)
    elif model_type == 'UNet':
        model = UNet(num_classes=num_classes)

    state = torch.load(str(model_path), map_location='cpu')
    state = {key.replace('module.', ''): value for key, value in state['model'].items()}
    model.load_state_dict(state)

    if torch.cuda.is_available():
        return model.cuda()

    model.eval()

    return model

def predict(model, from_file_names, batch_size, to_path, problem_type, img_transform):
    loader = DataLoader(
        dataset=RoboticsDataset(from_file_names, transform=img_transform, mode='predict', problem_type=problem_type),
        shuffle=False,
        batch_size=batch_size,
        num_workers=args.workers,
        pin_memory=torch.cuda.is_available()
    )

    with torch.no_grad():
        for batch_num, (inputs, paths) in enumerate(tqdm(loader, desc='Predict')):
            inputs = utils.cuda(inputs)

            outputs = model(inputs)

            for i, image_name in enumerate(paths):
                if problem_type == 'binary':
                    factor = prepare_data.binary_factor
                    t_mask = (F.sigmoid(outputs[i, 0]).data.cpu().numpy() * factor).astype(np.uint8)
                elif problem_type == 'parts':
                    factor = prepare_data.parts_factor
                    t_mask = (outputs[i].data.cpu().numpy().argmax(axis=0) * factor).astype(np.uint8)
                elif problem_type == 'instruments':
                    factor = prepare_data.instrument_factor
                    t_mask = (outputs[i].data.cpu().numpy().argmax(axis=0) * factor).astype(np.uint8)

                h, w = t_mask.shape

                full_mask = np.zeros((original_height, original_width))
                full_mask[h_start:h_start + h, w_start:w_start + w] = t_mask

                instrument_folder = Path(paths[i]).parent.parent.name

                (to_path / instrument_folder).mkdir(exist_ok=True, parents=True)

                cv2.imwrite(str(to_path / instrument_folder / (Path(paths[i]).stem + '.png')), full_mask)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg('--model_path', type=str, default='data/models/UNet', help='path to model folder')
    arg('--model_type', type=str, default='UNet', help='network architecture',
        choices=['UNet', 'UNet11', 'UNet16', 'LinkNet34', 'AlbuNet'])
    arg('--output_path', type=str, help='path to save images', default='1')
    arg('--batch-size', type=int, default=4)
    arg('--fold', type=int, default=-1, choices=[0, 1, 2, 3, -1], help='-1: all folds')
    arg('--problem_type', type=str, default='binary', choices=['binary', 'parts', 'instruments'])
    arg('--workers', type=int, default=12)

    args = parser.parse_args()

    if args.fold == -1:
        for fold in [0, 1, 2, 3]:
            _, file_names = get_split(fold)
            model = get_model(str(Path(args.model_path).joinpath('model_{fold}.pt'.format(fold=fold))),
                              model_type=args.model_type, problem_type=args.problem_type)

            print('num file_names = {}'.format(len(file_names)))

            output_path = Path(args.output_path)
            output_path.mkdir(exist_ok=True, parents=True)

            predict(model, file_names, args.batch_size, output_path, problem_type=args.problem_type,
                    img_transform=img_transform(p=1))
    else:
        _, file_names = get_split(args.fold)
        model = get_model(str(Path(args.model_path).joinpath('model_{fold}.pt'.format(fold=args.fold))),
                          model_type=args.model_type, problem_type=args.problem_type)

        print('num file_names = {}'.format(len(file_names)))

        output_path = Path(args.output_path)
        output_path.mkdir(exist_ok=True, parents=True)

        predict(model, file_names, args.batch_size, output_path, problem_type=args.problem_type,
                img_transform=img_transform(p=1))


text:
import math
import os
import pathlib
import itertools

import matplotlib
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt

LINES = ('-', '--', ':', '-.', '-', '--')
MARKER = ('o', 's', '^', '*', '>', '<', '8', 'p')
CMAP = 'viridis'
COLORS = sns.color_palette('colorblind').as_hex()

def savefig(fig, filepath, **kwargs):
    filepath = os.path.join('figs', filepath)
    dir_name = os.path.dirname(filepath)
    pathlib.Path(dir_name).mkdir(parents=True, exist_ok=True)
    fig.savefig(filepath, **kwargs)

def latexify(fig_width=None, fig_height=None, columns=3):
    """Set up matplotlib's RC params for LaTeX plotting.
    Call this before plotting a figure.

    Code is adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples.
    Width and max height in inches for IEEE journals taken from https://computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf.

    Parameters
    ----------
    fig_width : float, optional, inches
    fig_height : float,  optional, inches
    columns : {1, 2}
    """

    assert(columns in [1,2,3,4])

    # width in inches
    if fig_width is None:
        if columns==1:
            fig_width = 6.9
        elif columns==2:
            fig_width = 3.39
        elif columns==3:
            fig_width = 2.2
        else:
            fig_width = 1.7

    if fig_height is None:
        golden_mean = (math.sqrt(5)-1.0)/2.0    # Aesthetic ratio
        fig_height = fig_width*golden_mean # height in inches

    MAX_HEIGHT_INCHES = 8.0
    if fig_height > MAX_HEIGHT_INCHES:
        print("WARNING: fig_height too large:" + fig_height +
              "so will reduce to" + MAX_HEIGHT_INCHES + "inches.")
        fig_height = MAX_HEIGHT_INCHES

    fig_size = [fig_width,fig_height]

    params = {
            'backend': 'pgf',
            'pgf.preamble': [
                r'\usepackage[utf8x]{inputenc}',
                r'\usepackage[T1]{fontenc}',
                r'\usepackage{gensymb}',
            ],
            'axes.labelsize': 8, # fontsize for x and y labels (was 10)
            'axes.titlesize': 8,
            #'font.fontsize': 8, # was 10
            'legend.fontsize': 8, # was 10
            'xtick.labelsize': 8,
            'ytick.labelsize': 8,
            'text.usetex': True,
            'figure.figsize': fig_size,
            'font.family': 'sans-serif',

            'legend.markerscale': .9,
            'legend.numpoints': 1,
            'legend.handlelength': 2,
            'legend.scatterpoints': 1,
            'legend.labelspacing': 0.5,
            'legend.facecolor': '#eff0f1',
            # 'legend.edgecolor': 'none',
            'legend.handletextpad': 0.5,  # pad between handle and text
            'legend.borderaxespad': 0.5,  # pad between legend and axes
            'legend.borderpad': 0.5,  # pad between legend and legend content
            'legend.columnspacing': 1,  # pad between each legend column
            'axes.spines.left': True,
            'axes.spines.top': True,
            'axes.titlesize': 'medium',
            'axes.spines.bottom': True,
            'axes.spines.right': True,
            'axes.axisbelow': True,
            'axes.grid': True,
            'grid.linewidth': 0.5,
            'grid.linestyle': '-',
            'grid.alpha': .6,
            'lines.linewidth': 1,
            'lines.markersize': 4,
            'lines.markeredgewidth': 1,

            # Force white to avoid transparent turning into black in latex.
            'axes.facecolor': 'white',
            'savefig.facecolor': 'white',

            # 'axes.prop_cycle': cycler('linestyle', LINES),

        # 'path.simplify': True,
        # 'path.simplify_threshold': 0.1,
        # 'pgf.preamble': [
        #     r'\usepackage[utf8x]{inputenc}',
        #     r'\usepackage[T1]{fontenc}',
        #     #rf'\usepackage{{{typeface}}}'
        # ],
        # Colors
    }

    matplotlib.rcParams.update(params)
    from matplotlib import rc

    sns.set_style("whitegrid")
    sns.set_palette(COLORS)

    rc('font', **{'family': 'serif', 'serif': 'cm'})
    rc('text', usetex=True)

    matplotlib.rcParams.update({
            'patch.linewidth': 0.5,
            'legend.markerscale': .9,
            'legend.numpoints': 1,
            'legend.handlelength': 2,
            'legend.scatterpoints': 1,
            'legend.labelspacing': 0.5,
            'legend.facecolor': '#ffffff',
            'legend.fancybox': False,
            # 'legend.edgecolor': 'none',
            'legend.handletextpad': 0.5,  # pad between handle and text
            'legend.borderaxespad': 0.6,  # pad between legend and axes
            'legend.borderpad': 0.6,  # pad between legend and legend content
            'legend.columnspacing': 1,  # pad between each legend column
    })

    matplotlib.rcParams.update({'image.cmap': CMAP})


text:
import cv2 as cv
import numpy as np

font = cv.FONT_HERSHEY_SIMPLEX

cap = cv.VideoCapture(0)

while(1):

    _, frame = cap.read()

    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    blur = cv.GaussianBlur(hsv, (5, 5), 0)

    lowerGreen = np.array([70, 100, 100])
    upperGreen = np.array([90, 255, 255])

    maskGreen = cv.inRange(hsv, lowerGreen, upperGreen)

    kernel = np.ones((5, 5), np.uint8)
    dilate = cv.dilate(maskGreen, kernel, iterations=2)

    ret, thresh = cv.threshold(dilate, 15, 255, cv.THRESH_BINARY)

    _, contours, _ = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

    #draw contours
    cv.drawContours(frame, contours, -1, (0, 255, 0), 3)
    cv.drawContours(thresh, contours, -1, (0, 255, 0), 3)

    if len(contours) > 0:
        #outputs a dictionary of all moments calculated like area, centroid, etc
        #TENNIS BALL 1 Centroid
        M = cv.moments(contours[0])

        if M['m00'] > 0:
            #calculation of centroid
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            #draw centroid
            centroid = cv.circle(frame, (cx, cy), 5, (0, 0, 255), -1)
            cv.putText(frame, "Centroid", (cx - 25, cy -25), font, 1, (255, 255, 255), 2, cv.LINE_AA)

            centroid = cv.circle(thresh, (cx, cy), 5, (0, 0, 255), -1)
            cv.putText(thresh, "Centroid", (cx - 25, cy -25), font, 1, (255, 255, 255), 2, cv.LINE_AA)

        else:
            cx, cy = 700, 700

        #TENNIS BALL 2 Centroid
        M = cv.moments(contours[1])

        if M['m00'] > 0:
            #calculation of centroid
            cx2 = int(M['m10'] / M['m00'])
            cy2 = int(M['m01'] / M['m00'])
            #draw centroid
            centroid = cv.circle(frame, (cx2, cy2), 5, (0, 0, 255), -1)
            cv.putText(frame, "Centroid2", (cx2 - 25, cy2 -25), font, 1, (255, 255, 255), 2, cv.LINE_AA)

            centroid = cv.circle(thresh, (cx2, cy2), 5, (0, 0, 255), -1)
            cv.putText(thresh, "Centroid2", (cx2 - 25, cy2 -25), font, 1, (255, 255, 255), 2, cv.LINE_AA)

        else:
            cx2, cy2 = 700, 700

    else:
        cx, cy = 700, 700
        cx2, cy2 = 700, 700

    #draw line from centroids
    cv.line(frame, (cx, cy), (cx2, cy2), (255, 0 , 0), 5)

    cv.line(thresh, (cx, cy), (cx2, cy2), (255, 0 , 0), 5)

    #draw a horizontal line across the center of the screen to represent bar
    cv.line(frame, (0, 250), (500, 250), (140, 50, 80), 5)

    cv.line(thresh, (0, 250), (500, 250), (140, 50, 80), 5)

    cv.imshow("Thresh", thresh)
    cv.imshow("Dilate", dilate)
    cv.imshow("Frame", frame)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()


text:
# coding: utf-8

# In[1]:

'''
* Author           : Anjana Tiha
* Assignment No    : #5
* Course           : Natural Language Processing (COMP 8780)
* Semester         : Spring 2018
* Course Instructor: Professor Visali Rus
* University       : University of Memphis
* Deadline         : Due April 12, 2018.
*
* Description      : 1. From the SnapshotBROWN.pos.all.txt file extracted all word types and their frequencies.
*                    2. Sorted the list of word types in decreasing order based on their frequency.
*                    3. Drew a chart showing the relationship between the rank in the ordered list and the frequency (Zipf's Law).
*                       (Do not stem but do ignore punctuation.)
*                    4. Generated a Bigram Grammar from the above file.
*                    5. Performed add-one smoothing.
*                    6. Showed the grammar before and after smoothing for the sentence "A similar resolution passed in the Senate".
* Comments         : Work under progress for above description.
* Tools Requirement: Anaconda, Python
* Current Version  : v1.0.0.3
* Version History  : v1.0.0.0
*                    v1.0.0.1
*                    v1.0.0.2
*                    v1.0.0.3
*                    v1.0.0.4
*                    v1.0.0.5
* Last Update      : 04.07.2018 (Time : 05:45 P.M)

'''

# In[2]:

'''
Question

ASSIGNMENT #5: DUE: April 12!
==============

Goal: Familiarize with statistical methods and properties of language.

Remember the homework is due by midnight on the due date via email to the TA!
_________________________________________________________________

1. [10 points] From the SnapshotBROWN.pos.all.txt file extract all
   word types and their frequencies. Sort the list of word types in
   decreasing order based on their frequency. Draw a chart showing the
   relationship between the rank in the ordered list and the frequency
   (Zipf's Law). Do not stem but do ignore punctuation.

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

2. [20 points] Generate a Bigram Grammar from the above file. Perform
   add-one smoothing. Show the grammar before and after smoothing for
   the sentence "A similar resolution passed in the Senate".

*****************************************************************

'''

# In[3]:

import re
import os
import operator
from collections import OrderedDict
import pickle
import matplotlib.pyplot as plt
import numpy as np

# In[6]:

# read file line by line/ splits by line
def read_file_line_by_line(filename):
	with open(filename, 'r', encoding="utf8") as content_file:
		content = content_file.readlines()
	return content

# removes file
def remove_file(path):
	try:
		os.remove(path)
		return 1
	except OSError:
	#except WindowsError:
		print("failed removing: " + path)
		return 0

# converts text to lowercase
def to_lower(text):
	return text.lower()

# remove all the characters except alphabetical
# removes special characters and numerical charcharters
def remove_non_alpha(text):
    return re.sub(r'[^a-zA-Z0-9]', '', text)

# remove all the characters except alphabetical
# removes special characters and numerical charcharters
def remove_non_alpha_no_space(text):
    return re.sub(r'[^a-zA-Z0-9]', ' ', text)

# sort dictionary by key or value
def sort_dict(dict_x, type_s):
    if type_s == "key":
        return sorted(dict_x.items(), key=operator.itemgetter(0))

    elif type_s == "val":
        return OrderedDict(sorted(dict_x.items(), key=operator.itemgetter(1), reverse=True))

    else:
        return OrderedDict(sorted(dict_x.items(), key=operator.itemgetter(0), reverse=True))

# read annotated file with pos
# clean file and save text in pos: word format
def get_pos_word(text, output_file):
	blacklist = ["-NONE-", "-LRB-", "-RRB-"]
	eof_txt = "(TOP END_OF_TEXT_UNIT)"
	i = 0
	current_line = ""
	remove_file(output_file)

	with open(output_file, 'a') as the_file:

		for line in text:

			if eof_txt in line:
				current_line = current_line.strip()

				if current_line != "":
					the_file.write(current_line + "\n")

				current_line = ""
				continue

			else:
				temp = line.rsplit('(')
				temp = temp[len(temp)-1]
				temp = temp.rsplit(')')
				temp = temp[0]
				#temp = temp.strip()
				pass_iter = 0

				for item in blacklist:
					if item in temp:
						pass_iter = 1
						break

				if pass_iter == 1:
					continue
				else:
					#temp_test = re.sub(r'[^a-zA-Z0-9]', ' ', temp)
					#temp_test = remove_all_space(temp_test)
					temp_test  = temp

					if temp_test == "":
						continue
					else:
						temp_vocab = temp.split()

						if len(temp_vocab) == 2:
							temp = temp.strip()
							current_line += temp + " "

# get frequency of each word in a document
def get_word_freq(text):
    pos = ""
    word_freq = {}

    for line in text:
        line = line.strip()
        temp = line.split(" ")

        for i in range(len(temp)):
            if temp[i] == "\n":
                pos = ""
                continue

            if i%2 == 0:
                pass

            else:
                vocab = temp[i]
                vocab = remove_non_alpha(vocab)
                vocab = vocab.strip()

                if vocab.strip() == "" or vocab.strip() == '':
                    pass

                else:
                    vocab = to_lower(vocab)

                    if vocab in word_freq:
                        word_freq[vocab] +=1

                    else:
                        word_freq[vocab] = 1
    return word_freq

# get frequency of each word in a document
def get_bigram(text):
    bi_gram = {}

    first = ""
    second = ""

    w_c = 0
    c = 0

    for line in text:
        line = line.strip()
        temp = line.split(" ")

        for i in range(len(temp)):
            if temp[i] == "\n":
                first = ""
                second = ""
                pass

            else:
                if i%2==1:
                    vocab = temp[i]
                    vocab = remove_non_alpha(vocab)
                    vocab = vocab.strip()
                    vocab = to_lower(vocab)

                    if vocab.strip()!="":
                        if first == "":
                            first = vocab
                            second = ""

                        elif second =="":
                            second = vocab

                            if (first, second) in bi_gram:
                                bi_gram [first, second] += 1
                            else:
                                bi_gram [first, second] = 1

                            first = ""
                            second = ""

                        else:
                            first = vocab
                            second = ""

    return bi_gram

# get smoothed frequency for bigram in a document
def get_smoothed_bigram(word_pos, bi_gram):
    smoothed_bigram = {}
    voc_size = len(word_pos)

    for bi_gram_u in bi_gram:
        smoothed_bigram[bi_gram_u] = (bi_gram[bi_gram_u]+1)/(word_pos[bi_gram_u[0]]+voc_size)

    return smoothed_bigram

# print bigram
def print_bigram(bi_gram, smoothed_bigram, count):
    i = 0

    for key in bi_gram:
        i +=1
        print("------------------------------------------------------------------")
        print("%s: Count - %s, Smoothed Count - %.4f" %(key, bi_gram[key], smoothed_bigram[key]))

        if i > count and count !=-1:
            break
    print("------------------------------------------------------------------")

# draw frequency distribution and assimilate with zip_f
def draw_zip_f(word_pos, title, x_label, y_label, image_name):
    freq = []
    i=0

    rnk = np.arange(0, len(word_pos), 1)

    for word in word_pos:
        freq.append(word_pos[word])

    fig = plt.figure()
    plt.plot(rnk, freq)
    fig.suptitle(title, fontsize=15)
    plt.xlabel(x_label, fontsize=12)
    plt.ylabel(y_label, fontsize=10)
    fig.savefig(image_name)
    plt.show()

# print max frequent words
def max_freq_dict(dict_, max_count):
    items_=[]
    i = 0

    for key in dict_:
        i +=1
        print("---------------------------------")
        print("%s - %d"%(key, dict_[key]))
        items_.append(key)

        if i > max_count and max_count != -1:
            break

    print("---------------------------------")
    return items_

# take slice of dictionary for count
def slice_dict(dict_, max_count):
    dict_f = {}
    i=0

    for key in dict_:
        i +=1
        dict_f[key] = dict_[key]

        if i> max_count:
            break

    return dict_f

# get bigram for string
def get_bigram_str(text):
    text = to_lower(text)
    text = remove_non_alpha_no_space(text)
    text_splits = text.split(" ")

    bigram_str = {}
    for i in range(len(text_splits)-1):
        if (text_splits[i], text_splits[i+1]) in bigram_str:
            bigram_str[text_splits[i], text_splits[i+1]] +=1
        else:
            bigram_str[text_splits[i], text_splits[i+1]] = 1

    return bigram_str

# get bigram for string
def get_big_gram_compare_str(bigram_str,bi_gram, word_pos):
    bigram_dict = {}
    bigram_dict_smoothed = {}
    vocabulary_size = len(word_pos)

    for pair in bigram_str:
        if pair in bi_gram:
            bigram_dict[pair] = bi_gram[pair]/word_pos[pair[0]]
            bigram_dict_smoothed[pair] = (bi_gram[pair]+1)/(word_pos[pair[0]]+vocabulary_size)
        else:
            if pair[0] in word_pos:
                bigram_dict[pair] = 0
                bigram_dict_smoothed[pair] = 2/(word_pos[pair[0]]+vocabulary_size)
            else:
                bigram_dict[pair] = 0
                bigram_dict_smoothed[pair] = 2/(vocabulary_size)

    return bigram_dict, bigram_dict_smoothed

# compare bigram for string
def comapare_bigram_un_and_smoothed(bigram_dict, bigram_dict_smoothed):
    for pair in bigram_dict:
        print("%25s - Raw: %0.4f, Smoothed: %0.4f"% (pair, bigram_dict[pair], bigram_dict_smoothed[pair]))

# main function for assignment
def assignment5(input_file, output_file):
    print("  Processing File: \"%s\"%s %s" % (input_file, "......................................", "\n"))

    text = read_file_line_by_line(input_file)

    get_pos_word(text, output_file)

    text = read_file_line_by_line(output_file)

    word_pos = get_word_freq(text)
    word_pos= sort_dict(word_pos, "val")

    bi_gram = get_bigram(text)
    bi_gram = sort_dict(bi_gram, "val")

    smoothed_bigram = get_smoothed_bigram(word_pos, bi_gram)

    return word_pos, bi_gram, smoothed_bigram

# main result function
def assignment5_result_main(input_file, output_file, text, max_count_bigram, max_count_word):
    title = "Frequency Distribution of Words"
    x_label = "Word Rank"
    y_label = "Frequency"
    image_name = "out/zip_f.png"
    print("\n\n")
    print("*********************************************************************************************")
    print("                                  Assignment 5                                               ")
    print("*********************************************************************************************")
    print("\n\n")

    word_pos, bi_gram, smoothed_bigram = assignment5(input_file, output_file)

    draw_zip_f(word_pos, title, x_label, y_label, image_name)
    #print("---------------------------------")
    #print(" %s Most Frequent Words "%(max_count_word))
    #print("---------------------------------")
    #max_freq_dict(word_pos, max_count_word)
    #print_bigram(bi_gram, smoothed_bigram, max_count_bigram)

    print("\n\n\n")
    print("------------------------------------------------------------------")
    print(" Bigrams Grammer Before and After Smoothing ")
    print("------------------------------------------------------------------")
    bigram_str = get_bigram_str(text)
    bigram_dict, bigram_dict_smoothed = get_big_gram_compare_str(bigram_str,bi_gram, word_pos)
    comapare_bigram_un_and_smoothed(bigram_dict, bigram_dict_smoothed)
    print("------------------------------------------------------------------")
    print("\n")
    print("****************************************Thank You**********************************************")
    return word_pos, bi_gram, smoothed_bigram

# In[8]:

# main function call

max_count_bigram = -1
max_count_word = 10

text = "A similar resolution passed in the Senate"

input_file = "data/SnapshotBROWN.pos.all.txt"
output_file = "out/SnapshotBROWN.pos.all_clean.txt"
word_pos, bi_gram, smoothed_bigram = assignment5_result_main(input_file, output_file, text, max_count_bigram, max_count_word)