# import logging

# import azure.functions as func

# def main(req: func.HttpRequest) -> func.HttpResponse:
#     logging.info('Python HTTP trigger function processed a request.')

#     name = req.params.get('name')
#     if not name:
#         try:
#             req_body = req.get_json()
#         except ValueError:
#             pass
#         else:
#             name = req_body.get('name')

#     if name:
#         return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
#     else:
#         return func.HttpResponse(
#              "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
#              status_code=200
#         )

import logging

import azure.functions as func
from textblob import TextBlob
import nltk
import os
import json

import urllib
import pickle
import speech_recognition as sr
import os
import json
from moviepy.editor import *
import requests

r= sr.Recognizer()

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    try:
        req_body = req.get_json()
    except ValueError:
        pass

    extractedURL =req_body['url']
    outputSentiment ="Nothing"
    try:
        sentiment,extractedText = urlToSentimentConverter(urls = extractedURL)
        print("final sentiment "+str(sentiment))
        return func.HttpResponse(json.dumps(sentiment), mimetype='application/json' )
    except:
        sentiment,extractedText = "error in urlToSentiment","error in urlToSentiment"

def urlToSentimentConverter(urls):
    def textToSentiment(text):
        blob = TextBlob(text)
        polarity = blob.sentiment.polarity
        outputSentiment ="Nothing"
        if(polarity < -0.1):
            outputSentiment ="NEGATIVE, score =  " +str(polarity)
        elif(-0.1<=polarity<=0.1):
            outputSentiment ="NEUTRAL, score =  " +str(polarity)
        elif(polarity>0.1):
            outputSentiment ="POSITIVE, score =  " +str(polarity)
        print("output sentiment = "+str(outputSentiment))
        return(outputSentiment)
    def startConvertion(path='/tmp/convertedAudio.wav', lang='en-IN'):
        with sr.AudioFile(path) as source:
            print('Fetching File')
            audio_text = r.listen(source)
            # recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
            try:

                # using google speech recognition
                print('Converting audio transcripts into text ...')
                text = r.recognize_google(audio_text)
                print('Converted is '+str(text))
                return text

            except:
                print('unable to convert audio to text')
                return "error in text conversion"

    print('Downloading video')
    urllib.request.urlretrieve(urls, '/tmp/downloadedVideo.mp4')

    print('Download completed')

    print("reading video file")
    # my_clip = mp.VideoFileClip(r'/tmp/downloadedVideo.mp4')

    # my_clip.audio.write_audiofile(r"/tmp/my_result.wav")
    # print('writting complete')
    sound = AudioFileClip("/tmp/downloadedVideo.mp4")
    print('writing audio file')
    sound.write_audiofile("/tmp/convertedAudio.wav")
    print('writting complete')

    text = startConvertion(path='/tmp/convertedAudio.wav', lang='en-IN')
    # text = 'that was very nice very very nice keep doing it its great'

    if (text == "error in text conversion"):
        interResult = ["Audio to text failed", ""]
        print(interResult)
        print(type(interResult))
        return str(interResult[0]), text

    else:
        interResult = textToSentiment(text)
        print(interResult)
        print(type(interResult))
        return str(interResult), text


import struct
from spinnman.model.enums import P2PTableRoute

_ONE_WORD = struct.Struct("<I")

class P2PTable(object):
    """ Represents a P2P routing table read from the machine.
    """
    __slots__ = [
        "_height",
        "_routes",
        "_width"]

    def __init__(self, width, height, column_data):
        """
        :param int width:
        :param int height:
        :param bytes column_data:
        """
        self._routes = dict()
        self._width = width
        self._height = height
        for x in range(len(column_data)):
            y = 0
            pos = 0
            while y < height:
                data, offset = column_data[x]
                next_word, = _ONE_WORD.unpack_from(data, offset + (pos * 4))
                pos += 1
                for entry in range(min(8, height - y)):
                    route = P2PTableRoute((next_word >> (3 * entry)) & 0b111)
                    if route is not P2PTableRoute.NONE:
                        self._routes[x, y] = route
                    y += 1

    @staticmethod
    def get_n_column_bytes(height):
        """ Get the number of bytes to be read for each column of the table.

        :param int height: The height of the machine
        """
        return ((height + 7) // 8) * 4

    @staticmethod
    def get_column_offset(column):
        """ Get the offset of the next column in the table from the P2P base\
            address.

        :param int column: The column to be read
        """
        return (((256 * column) // 8) * 4)

    @property
    def width(self):
        """ The width of the machine that this table represents.

        :rtype: int
        """
        return self._width

    @property
    def height(self):
        """ The height of the machine that this table represents.

        :rtype: int
        """
        return self._height

    def iterchips(self):
        """ Get an iterator of tuples of (x, y) coordinates in the table

        :rtype: iterable(tuple(int,int))
        """
        return iter(self._routes.keys())

    def is_route(self, x, y):
        """ Determines if there is a route in the P2P table to the given chip.

        :param int x: The x-coordinate of the chip to look up
        :param int y: The y-coordinate of the chip to look up
        :rtype: bool
        """
        return (
            (x, y) in self._routes and
            self._routes[(x, y)] != P2PTableRoute.NONE)

    def get_route(self, x, y):
        """ Get the route to follow from this chip to the given chip.

        :param int x: The x-coordinate of the chip to find the route to
        :param int y: The y-coordinate of the chip to find the route to
        :rtype: P2PTableRoute
        """
        if (x, y) in self._routes:
            return self._routes[x, y]
        return P2PTableRoute.NONE


"""
Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

Example 1:

Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:

Input: s = "axc", t = "ahbgdc"
Output: false
"""

class Solution:
    def checkIncrement(self, ch) :
        asc = ord(ch)
        if ch == 'z':
            return 'a'
        else:
            return chr(asc + 1)
    def canMakeSubsequence(self, str1: str, str2: str) -> bool:
        ind = 0
        for ch in str1:
            if ch == str2[ind] or self.checkIncrement(ch) == str2[ind]:
                ind += 1
            if ind == len(str2):
                return True
        return False

2825. Make String a Subsequence Using Cyclic Increments.py


"""
Linear classification on the Default data set
"""
# Here we import all we will need
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import statsmodels.api as sm
import statsmodels.formula.api as smf

from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report, precision_score, roc_curve, auc
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import label_binarize

path_to_data = 'https://raw.githubusercontent.com/jeshan49/eemp2/master/Default.csv'

# Let us import the default data

default = pd.read_csv(path_to_data, index_col=0)

# Let us see the summary statistics

default.describe()

# Looks like we need to create dummies for 'default' and 'student'

default['d_default'] = pd.get_dummies(default['default'], drop_first=True)

default['d_student'] = pd.get_dummies(default['student'], drop_first=True)

default['d_student2'] = pd.get_dummies(default['student']).loc[:,'Yes']

default['d_student3'] = default['student'].apply(lambda val: 1 if val == 'Yes' else 0)

default.head()

default.describe()

##########################################################################
##### Here we construct the scatter and box plots from the lecture #######
##########################################################################
fig, axs = plt.subplots(1, 3, gridspec_kw = {'width_ratios':[2, 1, 1]})

# Take a fraction of the samples where target value (default) is 'no'
df_no = default[default['d_default'] == 0].sample(frac=0.15)
# Take all samples  where target value is 'yes'
df_yes = default[default['d_default'] == 1]
# Join the samples in one dataframe
df_ = df_no.append(df_yes)

# Let us construct the scatter plot
axs[0].scatter(df_[df_.default == 'Yes'].balance, df_[df_.default == 'Yes'].income,
   s=40, c='orange', marker='+', linewidths=1, label='Default = Yes')
axs[0].scatter(df_[df_.default == 'No'].balance, df_[df_.default == 'No'].income,
   s=40, marker='o', linewidths='1', edgecolors='lightblue', facecolors='white',
   alpha=.6, label = 'Default = No')

axs[0].set_ylim(ymin=0)
axs[0].set_ylabel('Income')
axs[0].set_xlim(xmin=-100)
axs[0].set_xlabel('Balance')
axs[0].legend()

# Let us now construct the two boxplots
c_palette = {'No':'lightblue', 'Yes':'orange'}
sns.boxplot('default', 'balance', data=default, orient='v', ax=axs[1],
            palette=c_palette)
sns.boxplot('default', 'income', data=default, orient='v', ax=axs[2],
            palette=c_palette)
##########################################################################
##########################################################################

##########################################################################
####### Here we construct the regression plots from the lecture ##########
##########################################################################
fig, axs = plt.subplots(2, 1)
sns.regplot('balance', 'd_default', data=default,
            scatter_kws={'color':'orange'}, line_kws={'color':'blue'}, ax=axs[0])

sns.regplot('balance', 'd_default', data=default, ci=None, logistic=True,
            scatter_kws={'color':'orange'}, line_kws={'color':'blue'}, ax=axs[1])

axs[0].set_title('Linear Regression')
axs[1].set_title('Logistic Regression')

for ax in axs:
    ax.set_ylabel('Prob of Default')
    ax.set_xlabel('Balance')
##########################################################################
##########################################################################

# Let us perform linear regression with statsmodels

y = default['d_default']

X = default[['balance', 'income', 'd_student']]

linreg = sm.OLS(y, X).fit()

linreg.summary()

print(linreg.summary().tables[1])

X = sm.add_constant(X)

linreg2 = sm.OLS(y, X).fit()

linreg2.summary()

# Let us perform logistic regression with statmodels

logreg = sm.Logit(y, X[['const', 'balance']]).fit()

logreg.summary()

print(logreg.summary().tables[1])

logreg2 = sm.Logit(y, X).fit()

logreg2.summary()

# Let us perform the regressions with scikit-learn

X = default[['balance', 'income', 'd_student']]

sk_linreg = LinearRegression().fit(X, y)

print(linreg2.summary().tables[1])
sk_linreg.coef_
sk_linreg.intercept_

sk_logreg = LogisticRegression().fit(X, y)

print(logreg2.summary().tables[1])
sk_logreg.coef_
sk_logreg.intercept_

# Let us try a different algorithm
X = default[['balance', 'income', 'd_student']]
sk_logreg2 = LogisticRegression(solver='newton-cg').fit(X, y)

print(logreg2.summary().tables[1])
sk_logreg2.coef_
sk_logreg2.intercept_

# Let us now perform LDA on the data
lda = LinearDiscriminantAnalysis().fit(X, default['default'])

# We can check the possible classes:
lda.classes_
# We can check the priors, i.e. our estimates of Pr(Y=k):
print(pd.DataFrame(lda.priors_, index=['No', 'Yes'], columns=['Pr(Y)']))
# We can check our estimates of mu_k:
print(pd.DataFrame(lda.means_, index = ['No', 'Yes'],
                   columns = X.columns))

# Let us reproduce the confusion matrix
y_hat = lda.predict(X)

print(pd.DataFrame(confusion_matrix(default['default'], y_hat).T,
                                    index = ['No', 'Yes'], columns = ['No', 'Yes']))

fal_pos_rate = confusion_matrix(default['default'], y_hat).T[1,0]/np.sum(confusion_matrix(default['default'], y_hat).T[:,0])
print(fal_pos_rate)
fal_neg_rate = confusion_matrix(default['default'], y_hat).T[0,1]/np.sum(confusion_matrix(default['default'], y_hat).T[:,1])
print(fal_neg_rate)

# We can also print a classification report:
print(classification_report(default['default'], y_hat))

# Note: Precision is the rate of prediction that corresponds to the truth
## Here: we predict 9645+254=9899 no defaults from which 9645 were no defaults
## Here: we predict 22+79=101 defaults from which 79 where defaults
# Note: Recall is one minus the false positive and false negative rate, respectively

# Let us try a different threshold than 50%, e.g. 20%

prob_hat = lda.predict_proba(X)[:,1]
new_y_hat = []
for i in prob_hat:
    if i>0.2:
        new_y_hat.append('Yes')
    else:
        new_y_hat.append('No')
new_y_hat = np.array(new_y_hat)

print(pd.DataFrame(confusion_matrix(default['default'], new_y_hat).T,
                                    index = ['No', 'Yes'], columns = ['No', 'Yes']))

print(classification_report(default['default'], new_y_hat))

##########################################################################
########## Here we construct the ROC curve from the lecture ##############
##########################################################################
y_score = lda.fit(X, y).decision_function(X)

fpr, tpr, _ = roc_curve(y, y_score)
roc_auc = auc(fpr, tpr)

plt.figure()
plt.plot(fpr, tpr, color='orange',
         label='ROC curve (area = {:0.2f})'.format(roc_auc))
plt.plot([0, 1], [0, 1], color='blue', linestyle='--')
plt.xlim([-0.05, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve for our LDA')
plt.legend(loc="lower right")
##########################################################################
##########################################################################

# Let us turn to QDA now

qda = QuadraticDiscriminantAnalysis().fit(X, default['default'])

print(pd.DataFrame(qda.priors_, index=['No', 'Yes'], columns=['Pr(Y)']))

print(pd.DataFrame(qda.means_, index = ['No', 'Yes'],
                   columns = X.columns))

y_hat = qda.predict(X)

print(pd.DataFrame(confusion_matrix(default['default'], y_hat).T,
                                    index = ['No', 'Yes'], columns = ['No', 'Yes']))

print(classification_report(default['default'], y_hat))

# Now, let us split the data into a training and test data set

X_train, X_test, y_train, y_test = train_test_split(X, default['default'],
                                                    test_size=0.3, random_state=181)

logreg = LogisticRegression(solver='newton-cg').fit(X_train, y_train)
logreg_y_hat = logreg.predict(X_test)
precision_score(y_test, logreg_y_hat, labels=['No', 'Yes'], pos_label='No')

lda = LinearDiscriminantAnalysis().fit(X_train, y_train)
lda_y_hat = lda.predict(X_test)
precision_score(y_test, lda_y_hat, labels=['No', 'Yes'], pos_label='No')

qda = QuadraticDiscriminantAnalysis().fit(X_train, y_train)
qda_y_hat = qda.predict(X_test)
precision_score(y_test, qda_y_hat, labels=['No', 'Yes'], pos_label='No')

knn = KNeighborsClassifier(n_neighbors=3).fit(X_train, y_train)
knn_y_hat = knn.predict(X_test)
precision_score(y_test, knn_y_hat, labels=['No', 'Yes'], pos_label='No')


from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response

from item.models import Item, Review
from  item.serializers import (ItemSerializer)

@api_view(["GET", 'POST'])
def item_list(request):
    """
        list of all items  or create new item
        """

    if request.method == 'GET':
        items = Item.objects.all()
        serializer = ItemSerializer(items, many=True)
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    elif request.method == 'POST':
        serializer = ItemSerializer(data=request.data)
        data = request.data
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


""" Napisati kod koji za date katete a i b (a<b) pravouglog trougla računa površinu i
zapreminu tijela koje se dobija rotacijom trougla oko manje katete.
"""
import math
"""
Prilikom rotacije pravouglog trougla dobijamo pravu kupu, gdje je a poluporecnik kupe, a stranica b je kruzni isjecak s
"""
a=1
b=2
c = math.sqrt(a*a+b*b)
p = a*(a+c)*3.14
v = (a*a*3.14*b)*1/3

print(c)
print(p)
print(v)


# 자동차 경주 게임 클래스
import random
from time import sleep

class Car:
    def __init__(self, n='Formula', c='black', s=200):
        self.name = n
        self.color = c
        self.maxSpeed = s
        self.distance = 0

    def printCarInfo(self):
        print(f'Car is {self.name}')
        print(f'Color is {self.color}')
        print(f'Max Speed is {self.maxSpeed}')

    def controlSpeed(self):
        return random.randint(0, self.maxSpeed)

    def getDistanceForHour(self):
        return self.controlSpeed() * 1

class CarRacing:
    def __init__(self):
        self.cars = []

    def startRacing(self):
        for i in range(10):
            print(f'Racing : {i + 1}바퀴')
            for car in self.cars:
                car.distance += car.getDistanceForHour()
                sleep(0.3) #딜레이
            self.printCurrentCarDistance()
            print()

    def printCurrentCarDistance(self):
        for car in self.cars:
            print(f'{car.name} : {car.distance} \t\t ', end='')

    def addCar(self, car):
        self.cars.append(car)

car1 = Car()
car2 = Car('Grand', 'green', 300)
car3 = Car('Sports', 'red', 250)
car4 = Car('Stock', 'gray', 150)
car5 = Car('Touring', 'blue', 280)

racing = CarRacing()
racing.addCar(car1)
racing.addCar(car2)
racing.addCar(car3)
racing.addCar(car4)
racing.addCar(car5)

racing.startRacing()


from functions import *

path = 'data/robustness_analysis/'
flow_idfrs = ['.3001 > crab-VirtualBox', '.3002 > crab', '.3003 > crab']
exps = ["delay0/", "delay20/", "delay50/", "delay100/", "delay200/", "delay500/"]
exp_names = ["1 ms", "20 ms", "50 ms", "100 ms", "200 ms", "500 ms"]
td = 50
flow1 = []
flow2 = []
for i in range(len(exps)):
  p = path+exps[i]
  flows = parse_log(p+'ifb0.txt', flow_idfrs, bg=False)
  cum_flows, tm, mbps,_ = cummulate(flows)
  fd = flow_download(cum_flows)
  flow1.append(fd[0]/(fd[0]+fd[1]))
  flow2.append(fd[1]/(fd[0]+fd[1]))

plt.figure(figsize=(5,4))
plt.bar(exp_names, flow1, width=0.3, label="flow 1", color="navy")
plt.bar(exp_names, flow2, bottom=flow1, width=0.3, label="flow 2", color="royalblue")
x1,x2,y1,y2 = plt.axis()
plt.axis([x1,x2,y1,y2])
plt.ylabel("%age of downloaded data")
plt.xlabel("RTT of flow 2")
plt.legend()
plt.savefig("rttunfairness.pdf", bbox_inches="tight")


#!/usr/bin/python3

print('---------------------------------------------------');
print('You entered NuPhy.py, merger of calc-react and srimbuster');
print("""
Three MODES:
  nuphy.py  react  -i h2,c12  -o h1  -e 19  -a 11
  nuphy.py  srim  -i h1 -m si -e 28 -w 5500um -n 100
  nuphy.py  srim  -i he4 -m c  -e 5.804 -w 100ug -n 100
STORE in file:
  nuphy.py  srim  -i he4 -m c  -e 5.804 -w 100ug -n 100 -S ~/srim_h1h2
 LIST:
  nuphy.py  store -S ~/srim_h1h2
 PLOT
  nuphy.py  store -S ~/srim_h1h2,0,1
  nuphy.py  store -S ~/srim_h1h2,0,1 -f yz
  nuphy.py  store -S ~/srim_h1h2,0,1 -f x
  nuphy.py  store -S ~/srim_h1h2,0,1 -f cos
  nuphy.py  store -S ~/srim_h1h2,0,1 -f cosy
  nuphy.py  store -S ~/srim_h1h2,0,1 -f 0.100
 RRATE:
  nuphy.py  rrate -n 10nA  -t 10ug          -m c   -xs 0.1
  nuphy.py  rrate -n 10uA  -t 10um   -d 1.8 -m c   -xs 0.1
  nuphy.py  rrate -n 1uA   -t 100ug         -m h2  -xs 0.1
  ...  ... current Nb/t;  rho_S;  Na/Mm ;  100mb=> R
""")
print('---------------------------------------------------');
#time.sleep(1)
#http://stackoverflow.com/questions/7427101/dead-simple-argparse-example-wanted-1-argument-3-results
import argparse

import numpy as np
import pandas as pd
import NuPhyPy.db.ReadNubase  as db
import NuPhyPy.Reactions.Kinematics as kin

import re
################# SRIM
#import NuPhyPy.srim as sr
import NuPhyPy.srim.srim as sr

from NuPhyPy.db.ReadNubase import gas,densities,elements,molarweights
#import NuPhyPy.db.ReadNubase as db
#from NuPhyPy.db.ReadNubase import gas,densities,elements

import NuPhyPy.srim.compounds as srcomp

######################################
# look for compounds/elements/isotopes : return tabulated density
#
######################################
def material_density( matnam ):
    #print('i... testing if ',matnam,'is in compounds ')
    #print( 'D...  keys=',srcomp.material_text.keys()  )
    mm_amu=1.0  # NONSENSE
    if matnam.title() in srcomp.material_text.keys():
        print('F... FOUND in compounds')
        dens=srcomp.get_density( matnam.title()  )
        print('i... FOUND density from compounds=',dens)  # mm_amu ???
        # mm_amu determined probably somewhere in SRIM.... # i dont need
    elif matnam.title() in elements:
        print('F...  ',matnam,' FOUND in elements ')
        CC=matnam.title()
        #print(CC,type(CC))
        zzz=elements.index(  matnam.title() )
        #print(zzz)
        dens=densities[ elements.index( matnam.title() ) ]
        mm_amu=molarweights[  elements.index( matnam.title() ) ]  # Mm for mix of isotopes
        print('i... element ',matnam.title() ,'found, density is set to:', dens)
    else:
        print('i... element NOT found, maybe it is an isotope?')
        isotope=db.isotope( matnam.title() )
        dens=isotope.isodensity
        mm_amu=isotope.amu   # This is for cross sections  Mm pure
        print('i...  isotope: {} found;  density is set to: {:.4f} g/cm3'.format(isotope.name,dens) )
    return dens,mm_amu

#
#################### PARSE

parser=argparse.ArgumentParser(description="""NuPhy.py

10um of Si:

./nuphy.py srim  -i he4 -m si -e 5.804 -n 100 -t 10um

with density:
./nuphy.py srim  -i he4 -m si -e 5.804 -n 100 -t 10um  -d 1.2

gas:
./nuphy.py srim  -i he4 -m air -e 5.804 -n 100 -t 10000um  -P 150 -T 293

store:
./nuphy.py store

""");

parser.add_argument('mode' ,help='react; srim; store')
parser.add_argument('-i','--incomming',  default="h2,c12" , help='REACT')
parser.add_argument('-o','--outgoing',  default="h2,c12"  , help='REACT')
parser.add_argument('-a','--angle',  default=0 , help='REACT')

parser.add_argument('-e','--energy', default=5.804 , help='REACT+SRIM')

parser.add_argument('-t','--thickness',  default="100ug" , help='SRIM')
parser.add_argument('-m','--material',   default="c12",help='SRIM')
parser.add_argument('-n','--number',  default=100 , help='SRIM')
parser.add_argument('-d','--density',  default="0" , help='SRIM')
parser.add_argument('-P','--Pressure',  default=1013.25e+3 ,type=float, help='pressure in Pascals e.g.1013250')
parser.add_argument('-T','--Temperature',  default=273.15 , type=float,help='SRIM')

parser.add_argument('-s','--silent', action="store_true",   help='SRIM')

parser.add_argument('-xs','--xsection', default=1.0,   help='XS in barns')

parser.add_argument('-S','--Store', default="",   help='Determine the STORE file; When reading- tell spectrum # -S file,0,1')

parser.add_argument('-f','--fwhm', default=0.,   help='with STORE:  convolute with a detector resolution (FWHM) in [MeV]; BUT ALSO  -f yz ... plot Y vs. Z scatter;  -f cos ... plot radians scatter; -f view ... view')

#parser.add_argument('-t','--thickness',  default="4" , help='SRIM')
args=parser.parse_args()

############################################
#
#  reaction
#
##########################################
if args.mode=='react':
    print(args.mode,' incomming=',args.incomming, 'outgoing=',args.outgoing)
    ip=args.incomming.split(',')
    op=args.outgoing.split(',')
    nu1=db.isotope( ip[0] )
    nu2=db.isotope( ip[1] )
    nu3=db.isotope( op[0] )
    if len(op)==1:
        print( 'i...',nu1.A +  nu2.A - nu3.A )
        print('i...', nu1.Z +  nu2.Z - nu3.Z )
        nu4=db.isotope( nu1.A +  nu2.A - nu3.A, nu1.Z +  nu2.Z - nu3.Z )
    else:
        nu4=db.isotope( op[1] )
    TKE=float( args.energy)
    th=float( args.angle)
    res=kin.react( nu1,nu2,nu3,nu4, TKE=TKE, theta=th,silent=0)

###############################################################  SRIM
#
#  srim    -  first functions -
#
###############################

##################
#  question on GAS = we use 2x
#################
def isitGas( material ):
    if material.title() in srcomp.material_gas:
        return 1
    if material.title() in elements:
        eZ=elements.index(material.title())
        if gas[ eZ ]==1:
            return 1
    try:
        isotope=db.isotope( material , silent=True  )
    except:
        return 0
    if isotope==None:
        eZ=isotope.Z
        if gas[ eZ ]==1:
            return 1
    return 0

#####################################
# get_thick_rho  ......  I extract the part
#         return   thick [ug/cm2] and rho g/cm3
#####################################
def get_thick_rho( material,  thick, rho ):   #convert properly um and find rho
    print('D... in  get_thick_rho ::: DUPLICITE CODE:', material, thick, rho, '-------------')
    rho=float(rho)
    if rho==0:
        rho,mm_amu=material_density(material) # compound/element/isot = ALL
    # GASEOUS DENSITY
    # 1/ if compound - rho from function
    # 2/ element:
    rho2=rho # to keep if solid phase
    #========test gas phase =========
    if material.title() in srcomp.material_gas:
        """
        trim assumes the target as STP before 1982
        T=273.15 K
        p=1013.25 kPa
        SRIMHelp/helpTR23.rtf
        """
        R=1013.25e+3/273.15/rho
        rho2=args.Pressure/R/args.Temperature
        print('i...rho at STD (0deg,1013kPa)=',rho,' NEW now=',rho2)
    elif material.title() in elements:
        #print("D... rho - elements")
        eZ=elements.index(material.title())
        print("D... rho - elements  eZ={:d}".format(eZ))
        #print(gas)
        if gas[ eZ ]==1:
            R=1013.25e+3/273.15/rho
            rho2=args.Pressure/R/args.Temperature
            print('i...rho at STD (0deg,1013kPa)=',rho,' NEW now=',rho2)
    else: # could be also a gaseous  isotope
        print('D... maybe gaseous isotope?')
        try:
            isotope=db.isotope( material , silent=True )
        except:
            isotope=None
        if isotope==None:
            print('D... not a gaseous isotope')
        else:
            eZ=isotope.Z
            if gas[ eZ ]==1:
                R=1013.25e+3/273.15/rho
                rho2=args.Pressure/R/args.Temperature
                print('i...rho at STD (0deg,1013kPa)=',rho,' NEW now=',rho2)
    rho=rho2
    print("D... rho WAS deduced")
    # THICKNESS TO mgcm2:    ##### MY UNIT WILL BE mg/cm2
    thickok=False
    if thick.find('ug')>0:
        thick=float(thick.split('ug')[0])/1000
        thickok=True
    elif thick.find('mg')>0:
        thick=float( thick.split('mg')[0] )
        thickok=True
    elif thick.find('um')>0:
        print('!... um ! I need rho=',rho)
        thick=float(thick.split('um')[0])
        thick=sr.get_mgcm2( thick,  rho ) # um in, mgcm2 out
        thickok=True
    elif thick.find('mm')>0:
        print('!... mm ! I need rho=',rho)
        thick=float(thick.split('mm')[0])
        thick=sr.get_mgcm2( thick*1000,  rho ) # um in, mgcm2 out
        thickok=True
    if not(thickok):
        print('!...  thicknesses must be in ug,mg or um,mm')
        quit()
    print('i... {} thickness {:.6f} mg/cm2 for rho={:.3f} ... {:.0f} A = {:.2f}um'.format( material.title(),thick,
                                                rho,1000*thick/rho/1e-2  ,   1000*thick/rho/1e+2 ) )
    return thick, rho, mm_amu

######################################
#PREPARE TRIMIN
#  prepare single layer, return one TRIM.IN line
########################################
def prepare_trimin( material,  thick,  rho  ):
    '''
    Here I prepare materials:  single layers
    '''
    print('D... preparing TRIMIN:', material, thick, rho, '-------------')
    #print('D... PV/T density:')
    rho=float(rho)
    if rho==0:
        rho,mm_amu=material_density(material) # compound/element/isot = ALL
    # GASEOUS DENSITY
    # 1/ if compound - rho from function
    # 2/ element:
    rho2=rho # to keep if solid phase
    if material.title() in srcomp.material_gas:
        """
        trim assumes the target as STP before 1982
        T=273.15 K
        p=1013.25 kPa
        SRIMHelp/helpTR23.rtf
        """
        R=1013.25e+3/273.15/rho
        rho2=args.Pressure/R/args.Temperature
        print('i...rho at STD (0deg,1013kPa)=',rho,' NEW now=',rho2)
    elif material.title() in elements:
        print("D... rho - elements")
        eZ=elements.index(material.title())
        print("D... rho - elements  eZ=",eZ)
        if gas[ eZ ]==1:
            R=1013.25e+3/273.15/rho
            rho2=args.Pressure/R/args.Temperature
            print('i...rho at STD (0deg,1013kPa)=',rho,' now=',rho2)
    else: # could be also a gaseous  isotope
        print('D... maybe gaseous isotope?')
        try:
            isotope=db.isotope( material , silent=True )
        except:
            isotope=None
        if isotope==None:
            print('D... not a gaseous isotope')
        else:
            eZ=isotope.Z
            if gas[ eZ ]==1:
                R=1013.25e+3/273.15/rho
                rho2=args.Pressure/R/args.Temperature
                print('i...rho at STD (0deg,1013kPa)=',rho,' now=',rho2)
    rho=rho2
    print("D... rho deduced")
    # THICKNESS TO mgcm2:    ##### MY UNIT WILL BE mg/cm2
    thickok=False
    if thick.find('ug')>0:
        thick=float(thick.split('ug')[0])/1000
        thickok=True
    elif thick.find('mg')>0:
        thick=float( thick.split('mg')[0] )
        thickok=True
    elif thick.find('um')>0:
        print('!... um ! I need rho=',rho)
        thick=float(thick.split('um')[0])
        thick=sr.get_mgcm2( thick,  rho ) # um in, mgcm2 out
        thickok=True
    elif thick.find('mm')>0:
        print('!... mm ! I need rho=',rho)
        thick=float(thick.split('mm')[0])
        thick=sr.get_mgcm2( thick*1000,  rho ) # um in, mgcm2 out
        thickok=True
    if not(thickok):
        print('!...  thicknesses must be in ug,mg or um, mm')
        quit()
    print('i... {} thickness {:.6f} mg/cm2 for rho={:.3f} ... {:.0f} A = {:.2f}um'.format( material.title(),thick,
                                                rho,1000*thick/rho/1e-2  ,   1000*thick/rho/1e+2 ) )

    # AT THIN MOMENT I HAVE A GOOD rho an thick in mgcm2

    #print('D... goto TRIMIN MULTI')
    TRIMIN=sr.PrepSrimFile( ion=n0, energy=Eini, angle=0., number=number,
                            mater=material, thick=thick, dens=rho  )

    return TRIMIN
############# END OF PREPARE TRIMIN

###############################
#  SRIM MODE
#        here i try to prepare also multilayer  materials
#
##############################

if args.mode=='srim':
    print('i...',args.mode)
    ipath=sr.CheckSrimFiles()

    n0=db.isotope(args.incomming )
    Eini=float( args.energy )
    number=int(args.number)
    rho=args.density
    thick=args.thickness
    material=args.material.title()  # this will get complicated with layers
    nmats=len(material.split(','))
    print("D... counting number of layers",nmats)
    if nmats>1:
        print('!... ',nmats,'materials - TEST REGIME:')
        if nmats!=len(thick.split(',')):
            print('!... NOT THE SAME NUMBER OF THICKNESSES')
            quit()
        if nmats!=len(rho.split(',')):
            if float(rho)!=0.:
                print('!... NOT THE SAME NUMBER OF densities')
                quit()
            else:
                rho=','.join( map(str,[0]*nmats) )
        print('i... PREPARING ANALYSIS  mat, thick, rho:')
        TRILIST=[]
        for imat in range(nmats):
            print(imat,'... =========', material.split(',')[imat].title(), thick.split(',')[imat],  rho.split(',')[imat],
                  "================================"  )
            TRILIST.append( prepare_trimin(  material.split(',')[imat].title(), thick.split(',')[imat],  rho.split(',')[imat]   )   )
        print('i... I GOT ALL TRIM.IN files. Now somebody must merge....')
        ############################   MERGING  LAYERS ##############
        print('D--------------')
        for xi,xii in enumerate(TRILIST):  print( xi, xii,'\n')          # PRINT
        TRIMIN="==> SRIM-2013.00 This file controls TRIM Calculations.\r\n"
        #TRIMIN= TRIMIN + TRILIST[0].split("\n")[0].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[1].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[2].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[3].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[4].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[5].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[6].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[7].rstrip()+'\r\n'   # target material  num ele and layers
        #TRIMIN=TRIMIN+"            8 \r\n"

        #print(TRIMIN)
        #quit()
        #############################################################
#        with open('TRIM.IN.ALL','w') as f:
#            for imat in range(nmats):
#                f.write(TRILIST[imat])
        line8=[]      # Target material+1
        TRILISTTOT=[] # totallist for Atom
        lineLay=[]    # Layer Layer long line
        layerList=[]
        for imat in range(nmats):
            listOfLines=TRILIST[imat].split('\n')
            if listOfLines[7].find('Target material')<0:
                print('!... Target material line not detected...quit')
                quit()
            # define line8 (7: Target material)
            line8.append(   re.findall(r'".+"|[\w\.]+',  listOfLines[8] )  )
            TRILISTTOT.extend(  listOfLines  )
            # lineLay...
            liLa=[ i for i in listOfLines if re.match('^Layer\s+Layer\s+Name',i)  ]
            lineLay.extend(liLa)  # line with columns for layers.
            #---- duplicate: but i find #line of Layer Layer +2
            for j,v in enumerate( listOfLines ):
                if v.find('Layer')>=0 and v.find('Density')>0:
                    print(imat,'LAYER LINE= #',j+2+1) # starts with 0
                    layerList.append( re.findall(r'".+"|[\w\.\-]+', listOfLines[j+2] ) )
        #print(line8)
        layname='...'.join( [ i[0].strip("\"").rstrip() for i in line8]  )
        layname='"'+layname+'"'
        nelems=sum( map(int, [i[1] for i in line8] ) )
        nlayers=sum( map(int, [i[2] for i in line8] ) )
        print(layname,nelems,nlayers)
        TRIMIN=TRIMIN+"{} {} {}\r\n".format(layname,nelems,nlayers)
        #TRIMIN= TRIMIN + TRILIST[0].split("\n")[8].rstrip()+'\r\n'  # He 4  into C  - nedavat
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[9].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[10].rstrip()+'\r\n'
        TRIMIN= TRIMIN + TRILIST[0].split("\n")[11].rstrip()+'\r\n'

        # NOW I need to get all Atom .. = ... = lines
        #print(TRILISTTOT)
        #regex = re.compile('^Atom\s\d+\s=\s')
        atoms=[i for i in TRILISTTOT if re.match('^Atom\s\d+\s=\s',i) ]
        if len(atoms)!=nelems:
            print('!... Atoms lines and # elements differ!')
            quit()
        #        atoms=[m.group(1) for l in TRILISTTOT for m in [regex.search(l)] if m]
        #atoms=re.findall(r'Atom\s\d+\s=\s', TRILISTTOT)
        for i,v in enumerate(atoms):
            atoms[i]=re.sub( 'Atom\s(\d+)\s','Atom '+str(i+1)+' ' , v ).rstrip()
        print( '\n'.join( atoms ) )
        TRIMIN=TRIMIN+ '\r\n'.join(atoms)+'\r\n'
        # NOW I need Layer Layer
        preLayer=re.sub(r'^(.+Density).+$',r'\1', lineLay[0] )
        for i,v in enumerate(lineLay):
            lineLay[i]=re.sub('^.+?Density ','', v).strip()
        print( preLayer,'  '.join(lineLay) )
        TRIMIN=TRIMIN+preLayer+' '+'  '.join(lineLay)+'\r\n'
        # NOW I need the same with next "stoich stoich stoich...."
        lineStoich="Numb.   Description                (Ang) (g/cm3)   "+" Stoich "*nelems
        print(lineStoich)
        TRIMIN=TRIMIN+lineStoich+'\r\n'
        # NOW I need 1   "Layer 1";  2  "Layer 2"
        zeroes=0
        for i,v in enumerate(layerList):
            zstring=' 0   '*zeroes
            zpost=' 0   '*(nelems-zeroes -  int(line8[i][1]) )
            prepart='  '.join(v[1:4])
            postpart='  '.join(v[4:])
            print( "{} {} {} {} {}".format(i+1,prepart,zstring,postpart, zpost) )
            TRIMIN=TRIMIN+ "{} {} {} {} {}\r\n".format(i+1,prepart,zstring,postpart, zpost)
            zeroes=zeroes+int(line8[i][1])
            # 16 fields;
        # NOW I need to copy lines with GAS.....
        lineGas="0  Target layer phases (0=Solid, 1=Gas)"
        print(lineGas)
        TRIMIN=TRIMIN+lineGas+'\r\n'
        for i,v in enumerate(layerList):
            ###print( 'material====',v )
            print(  isitGas(v[1]),' '   , end=' ')
            TRIMIN=TRIMIN+ str(isitGas(v[1]) )+' '
        print()
        TRIMIN=TRIMIN+'\r\n'
        ######## RAGG
        lineBragg="Target Compound Corrections (Bragg)"
        print(lineBragg)  # i want to put all lines  12 ( i think)
        TRIMIN=TRIMIN+lineBragg+'\r\n'
        for imat in range(nmats):
            listOfLines=TRILIST[imat].split('\n')
            for jmat in range( len(listOfLines) ):
                if listOfLines[jmat].find('Target Compound Corrections')>=0:
                    #print( listOfLines[jmat].rstrip() )
                    print( listOfLines[jmat+1].rstrip()  , end=' ')
                    TRIMIN=TRIMIN+ listOfLines[jmat+1].rstrip() +' '
                    break
            if jmat==len(listOfLines)-1:
                print('!... Bragg line not found')
                quit()
        print()
        TRIMIN=TRIMIN+'\r\n'
        #Individual target atom displacement energies (eV)
        lineAtomDisp="Individual target atom displacement energies (eV)"
        print(lineAtomDisp)
        TRIMIN=TRIMIN+lineAtomDisp+'\r\n'
        for imat in range(nmats):
            listOfLines=TRILIST[imat].split('\n')
            for jmat in range( len(listOfLines) ):
                if listOfLines[jmat].find('Individual target atom displacement energies')>=0:
                    #print( listOfLines[jmat].rstrip() )
                    print( listOfLines[jmat+1].rstrip()  , end=' ')
                    TRIMIN=TRIMIN+ listOfLines[jmat+1].rstrip() +' '
                    break
            if jmat==len(listOfLines)-1:
                print('!... Displacement line not found')
                quit()
        print()
        TRIMIN=TRIMIN+'\r\n'

        #Individual target atom displacement energies (eV)
        lineAtomDisp="Individual target lattice binding energies (eV)"
        print(lineAtomDisp)
        TRIMIN=TRIMIN+lineAtomDisp+'\r\n'
        for imat in range(nmats):
            listOfLines=TRILIST[imat].split('\n')
            for jmat in range( len(listOfLines) ):
                if listOfLines[jmat].find('Individual target atom lattice binding energies')>=0:
                    #print( listOfLines[jmat].rstrip() )
                    print( listOfLines[jmat+1].rstrip()  , end=' ')
                    TRIMIN=TRIMIN+ listOfLines[jmat+1].rstrip() +' '
                    break
            if jmat==len(listOfLines)-1:
                print('!... Lattice binding line not found')
                quit()
        print()
        TRIMIN=TRIMIN+'\r\n'
        #Individual target atom displacement energies (eV)
        lineAtomDisp="Individual target atom surface binding energies (eV)"
        print(lineAtomDisp)
        TRIMIN=TRIMIN+lineAtomDisp+'\r\n'
        for imat in range(nmats):
            listOfLines=TRILIST[imat].split('\n')
            for jmat in range( len(listOfLines) ):
                if listOfLines[jmat].find('Individual target atom surface binding energies')>=0:
                    #print( listOfLines[jmat].rstrip() )
                    print( listOfLines[jmat+1].rstrip()  , end=' ')
                    TRIMIN=TRIMIN+ listOfLines[jmat+1].rstrip() +' '
                    break
            if jmat==len(listOfLines)-1:
                print('!... Surface binding line not found')
                quit()
        print()
        TRIMIN=TRIMIN+'\r\n'
        print('Stopping Power Version (1=2011, 0=2011)')
        TRIMIN=TRIMIN+'Stopping Power Version (1=2011, 0=2011)\r\n'
        print(' 0 ')
        TRIMIN=TRIMIN+' 0\r\n'

        print('\n\n\n',TRIMIN,"\n\n\n")
        #quit()

    else:
        print("D... one material - preparing TRIMIN")
        TRIMIN=prepare_trimin(  material, thick,  rho   )
    #########################################
    # PREPARE FILE
    ##########################################
#    print('D... goto TRIMIN')
#    TRIMIN=sr.PrepSrimFile( ion=n0, energy=Eini, angle=0., number=number,
#                            mater=material, thick=thick, dens=rho  )

    # RUN ############################
    if args.silent:
        tmpp=sr.run_srim(ipath, TRIMIN,  silent=True, nmax=number)
    else:
        tmpp=sr.run_srim(ipath, TRIMIN,  silent=False, nmax=number)
    print(tmpp[-5:])
    if 'e' in tmpp:
        deint=tmpp['e'].max()-tmpp['e'].min()
        sigma=tmpp['e'].std()
        mean=tmpp['e'].mean()
        median=tmpp['e'].median()
        print()
        print( "{:.3f} MeV (median {:.3f}) +- {:.3f}  hi-lo={:.3f}  Eloss={:.3} MeV".format( mean, median,  sigma , deint ,  Eini-mean)   )
    else:
        #print(tmpp)
        print()
        print('{:.3f} +- {:.4f} um implanted depth'.format( tmpp['x'].mean(), tmpp['x'].std() )  )
        mean=0.0
        median=0.0
        deint=0.0
        sigma=0.0
        #print( tmpp['e'].max(),tmpp['e'].min(), de  )
    #plt.hist( tmpp['e'], 20 , facecolor='red', alpha=0.25)
    #    print("R...    E mean +- std")
    #    print(tmpp['e'].mean(), '  ' ,tmpp['e'].std() )
    #   print(tmpp['e'].mean(), '  ' ,tmpp['e'].std() )

    ### MAYBE - I WILL NUMBER ALREADY FROM HERE
    if args.Store!="":
        store = pd.HDFStore( args.Store )
        existing=len(store.keys())
        print('D... already existing is', existing )
        #print(store)
        if material.title() in srcomp.material_gas:
            pt='P{}_T{}'.format( args.Pressure, args.Temperature )
        else:
            pt=""

        fname='n{:03d}_{}_in_{:_<6s}_w{:_<6s}_r{:3.1f}_{}_n{:04d}_e{:06.3f}_ef{:06.3f}'.format( existing,
                            args.incomming, args.material, args.thickness, float(args.density),
                             pt, int(args.number), float(args.energy), mean )
        fname=fname.replace('.','_')
        store[fname] = tmpp
        print(store)
        store.close()

# RUN SRIM ENDED

###############################################################
#
#   Reaction Rate
#   rate=Nb/t; Nt/A ; sigma==cross section==xs
#####################################
if args.mode=='rrate':
    Nb=args.number
    if Nb[-2:]=='nA':   Nb=float(Nb[:-2])*1e-9/1.602176e-19
    elif Nb[-2:]=='uA': Nb=float(Nb[:-2])*1e-6/1.602176e-19
    elif Nb[-2:]=='mA': Nb=float(Nb[:-2])*1e-3/1.602176e-19
    else: Nb=float(Nb)

    sigma=  float(args.xsection)  # give it in barns= cm2

    rho_S,just_rho,Mm_amu=get_thick_rho( args.material , args.thickness, args.density )
    rho_S = rho_S  /1e+3 # mg -> g/cm2
    Na=6.24+23   # i think - Avogadro at/ mol   or 1e+26/kmol
    Mm=Mm_amu    # molar weight -  isotopic should be

    rrate=Nb * rho_S *Na /Mm *sigma
    print("Beam {:.4g} cps; XS={:.4g} barns; rho_S={:.4g} mg/cm2  Mm={:.3f} g/mol".format( Nb, sigma, 1000*rho_S, Mm) )
    print("Reaction rate: {:.4g} cps".format(rrate))
    print("Reaction rate: {:.4g} cps/sr".format(rrate/4/3.1415926 ))

###############################################################  STORE
#
#   (UN) store  and   plot
#
#####################################
if args.mode=='store':
    import matplotlib.pyplot as plt
    if args.Store!="":
        stor=args.Store.split(',')
        store = pd.HDFStore( stor[0] )
        if len(stor)>1:
            plots=[]
            #========= i want to plot dE histogram with convolution fwhm
            for inx in range( len(stor)-1 ):
                dfname=sorted(store.keys())[ int(stor[inx+1])]
                print('o... openning: ', dfname )
                df=store[dfname]
                plotme=False
                #==================  if fwhm .... CONVOLUTE;  yz ... scatter;  cosy cosz ...angles
                if args.fwhm=="x": #======= implantation plot
                    plt.hist( df['x'], 20, ec='k',alpha=0.3,label=dfname+' implant [um]')
                    plotme=True
                if args.fwhm=="y": #======= implantation plot
                    plt.hist( df['y'], 20, ec='k',alpha=0.3,label=dfname+' implant [um]')
                    plotme=True
                if args.fwhm=="z": #======= implantation plot
                    plt.hist( df['z'], 20, ec='k',alpha=0.3,label=dfname+' implant [um]')
                    plotme=True

                    #plt.scatter( df['z']/1e+4, df['y']/1e+4,alpha=0.3,label=dfname+' [um]' )
                if args.fwhm=="yz": #======= scatter plot  Y Z
                    plt.scatter( df['z'], df['y'],alpha=0.3,label=dfname+' [um]' )
                    plotme=True
                if args.fwhm=="cos" and  'e' in df.keys(): #======= scatter plot cosy cosz
                    plt.scatter( df['cosz'], df['cosy'], alpha=0.3, label=dfname+' [rad]' )
                    plotme=True

                if args.fwhm=="cosy" and  'e' in df.keys(): #======= scatter plot cosy cosz
                    plt.hist( df['cosy'], 20, ec='k',alpha=0.3,label=dfname+' implant [rad]')
                    plotme=True
                if args.fwhm=="cosz" and  'e' in df.keys(): #======= scatter plot cosy cosz
                    plt.hist( df['cosz'], 20, ec='k',alpha=0.3,label=dfname+' implant [rad]')
                    plotme=True
                if args.fwhm=="view": #======= VIEW DATA
                    print(df)
                try:
                    floatfwh=float(args.fwhm)
                except:
                    floatfwh=-1
                if floatfwh>=0.: #====== GENERATE GAUSS ======== fwhm=2.355sigma
                    if 'e' in df.keys():
                        print("...... MEAN ENERGY MODE")
                        print('i...  mean before convolution: {:.3f} {:.4f}'.format(df['e'].mean(),df['e'].std() ))
                        df['fwhm']=np.random.normal( 0.0, float(args.fwhm)/2.355 ,  len(df) )
                        df['e']=df['e']+df['fwhm']
                        print('i...  mean with   convolution: {:.3f} {:.4f}'.format(df['e'].mean(),df['e'].std() ))
                        plt.hist( df['e'], 20, ec='k',alpha=0.3,label=dfname)
                        plotme=True
                    elif floatfwh>0:
                        print("...... MEAN ENERGY MODE FOR IMPLANT")
                        df['fwhm']=np.random.normal( 0.0, float(args.fwhm)/2.355 ,  len(df) )
                        ENE=dfname.split('_')[-4]+'.'+dfname.split('_')[-3]
                        ENE=float(ENE[1:])
                        df['e']=ENE+df['fwhm']
                        print('i...  mean with   convolution: {:.3f} {:.4f}'.format(df['e'].mean(),df['e'].std() ))
                        plt.hist( df['e'], 20, ec='k',alpha=0.3,label=dfname)
                        plotme=True

                    else:
                        print("....... IMPLANT MODE")
                        plt.hist( df['x'], 20, ec='k',alpha=0.3,label=dfname+' [um]')
                        plotme=True

            if plotme:
                plt.legend( loc=4 , fontsize="x-small" )
                plt.show()
        else:
            for i,v in enumerate(sorted(store.keys())):print("{:03d} {}".format(i,v) )

        store.close()
    else:
        print('!... filename missing: use -S; open an item by specifying  line number after comma')


# https://codeup.kr/problem.php?id=1420
# 1420: 3등 찾기

num = int(input())

list = []
for i in range(num):
    tmp = []
    name, score = input().split()
    tmp.append(name)
    tmp.append(int(score))
    list.append(tmp)

sorted_list = sorted(list, key = lambda x: x[1], reverse=True)
print(sorted_list[2][0])


from BinaryTree import Tree

def iterativePreorder(tree):
    if not tree.root:
        print("Tree is Empty")
        return

    stack = []

    current = tree.root
    while len(stack) > 0 or current:
        if current:
            print(current.data, end=" ")
            stack.append(current)
            current = current.left
        else:
            current = stack.pop()
            current = current.right
    print()

def iterativeInorder(tree):
    if not tree.root:
        print("Tree is Empty")
        return

    stack = []
    current = tree.root

    while(current or len(stack) > 0):
        if current:
            stack.append(current)
            current = current.left
        else:
            current = stack.pop()
            print(current.data, end=" ")
            current = current.right

    print()

def iterativePostorderWith1Stack(tree):
    if not tree.root:
        return []
    prev = None
    stack = [tree.root]
    result = []

    while stack:
        current = stack[-1]
        # Case 1
        if prev == None or prev.left == current or prev.right == current:

            if current.left:
                stack.append(current.left)
            elif current.right:
                stack.append(current.right)
            else:
                result.append(current.data)
                stack.pop()
        # Case 2
        elif prev == current.left:
            if current.right:
                stack.append(current.right)
        # Case 3
        else:
            result.append(current.data)
            stack.pop()

        prev = current

    print(*result)

def iterativePostorderWith2Stacks(tree):
    if not tree.root:
        return []

    stack1 = [tree.root]
    stack2 = []

    while stack1:
        current = stack1.pop()
        stack2.append(current.data)

        if current.left:
            stack1.append(current.left)
        if current.right:
            stack1.append(current.right)

    print(*stack2[::-1])

def main():
    inputArray = input().split()
    tree = Tree(inputArray)

    print("Iterative Preorder: ")
    iterativePreorder(tree)

    print("Iterative Inorder: ")
    iterativeInorder(tree)

    print("Iterative Postorder with 2 stacks: ")
    iterativePostorderWith2Stacks(tree)

    print("Iterative Postorder with 1 stack: ")
    iterativePostorderWith1Stack(tree)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-

# Looks for musical samples in arbitrary audio
# Adapted from: https://github.com/kylemcdonald/AudioNotebooks/blob/master/Multisamples%20to%20Samples.ipynb
# Usage:
    # python audio_to_samples.py -plot 1
    # python audio_to_samples.py -save 1
    # python audio_to_samples.py -in "../audio/downloads/birds/*.mp3" -saved 1
    # python audio_to_samples.py -in "../data/usergen/saved_birds.json" -plot 1

import argparse
import csv
import glob
import json
import librosa
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
import os
from os.path import join
import numpy as np
from pprint import pprint
import sys
from utils import getAudioSamples, readFiles

# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILES", default="../audio/sample/*.mp3", help="Input file pattern")
parser.add_argument('-adir', dest="INPUT_AUDIO_DIR", default="../audio/downloads/birds/%s.mp3", help="Input audio directory (if INPUT_FILES is .json file)")
parser.add_argument('-samples', dest="SAMPLES", default=8, type=int, help="Max samples to produce, -1 for all")
parser.add_argument('-min', dest="MIN_DUR", default=0.05, type=float, help="Minimum sample duration in seconds")
parser.add_argument('-max', dest="MAX_DUR", default=1.00, type=float, help="Maximum sample duration in seconds")
parser.add_argument('-amp', dest="AMP_THESHOLD", default=-1, type=float, help="Amplitude theshold, -1 for default")
parser.add_argument('-save', dest="SAVE", default=0, type=int, help="Save files?")
parser.add_argument('-plot', dest="PLOT", default=0, type=int, help="Show plot?")
parser.add_argument('-saved', dest="SAVE_DATA", default=0, type=int, help="Save data files?")
parser.add_argument('-plotdir', dest="PLOT_DIR", default="../data/output/plot/%s.png", help="Output dir for plot images")
parser.add_argument('-dir', dest="SAMPLE_DIR", default="../audio/output/birds", help="Output dir")
parser.add_argument('-out', dest="OUTPUT_FILE", default="../data/output/birds_audio_samples.csv", help="CSV output file")
parser.add_argument('-overwrite', dest="OVERWRITE", default=0, type=int, help="Overwrite existing audio/data?")
args = parser.parse_args()

# Parse arguments
INPUT_FILES = args.INPUT_FILES
INPUT_AUDIO_DIR = args.INPUT_AUDIO_DIR
SAMPLES = args.SAMPLES
MIN_DUR = args.MIN_DUR
MAX_DUR = args.MAX_DUR
AMP_THESHOLD = args.AMP_THESHOLD
SAVE = args.SAVE > 0
PLOT = args.PLOT > 0
SAVE_DATA = args.SAVE_DATA > 0
PLOT_DIR = args.PLOT_DIR
SAMPLE_DIR = args.SAMPLE_DIR
OUTPUT_FILE = args.OUTPUT_FILE
OVERWRITE = args.OVERWRITE > 0

if SAMPLES <= 0:
    SAMPLES = None

# Audio config
FFT = 2048
HOP_LEN = FFT/4

# Read files
fileGroups, files = readFiles(INPUT_FILES, INPUT_AUDIO_DIR)
fileCount = len(files)
hasGroups = len(fileGroups) > 1
print("Found %s files" % fileCount)

# Make sure output dirs exist
outDirs = [SAMPLE_DIR, os.path.dirname(OUTPUT_FILE)]
if PLOT:
    outDirs.append(os.path.dirname(PLOT_DIR))
for outDir in outDirs:
    if not os.path.exists(outDir):
        os.makedirs(outDir)

progress = 0
# files = files[:1]

def makeSamples(fn):
    global progress
    global hasGroups

    sampleData = []
    basename = os.path.splitext(os.path.basename(fn))[0]
    path, groupName = os.path.split(os.path.dirname(fn))
    groupName = "" if not hasGroups else groupName
    plotfilename = PLOT_DIR % basename

    if SAVE_DATA or SAVE or PLOT and (OVERWRITE or not os.path.isfile(plotfilename)):

        sampleData, ysamples, y, sr = getAudioSamples(fn, min_dur=MIN_DUR, max_dur=MAX_DUR, fft=FFT, hop_length=HOP_LEN, amp_threshold=AMP_THESHOLD, plot=PLOT, plotfilename=plotfilename, groupName=groupName)

        # if too many samples, take the ones with the most power
        if SAMPLES is not None and len(sampleData) > SAMPLES and (SAVE or SAVE_DATA):
            sampleData = sorted(sampleData, key=lambda k: k['power'], reverse=True)
            sampleData = sampleData[:SAMPLES]

        if SAVE:
            for d in sampleData:
                ysample = ysamples[d["index"]]
                outFn = join(SAMPLE_DIR, d["filename"])
                if OVERWRITE or not os.path.isfile(outFn):
                    sample = np.copy(ysample)
                    sample /= np.abs(sample).max()
                    librosa.output.write_wav(outFn, sample, sr)

        # sort chronologically
        if SAVE or SAVE_DATA:
            sampleData = sorted(sampleData, key=lambda k: k['start'])

    progress += 1
    sys.stdout.write('\r')
    sys.stdout.write("%s%%" % round(1.0*progress/fileCount*100,1))
    sys.stdout.flush()

    return sampleData

if PLOT:
    for fn in files:
        makeSamples(fn)

else:
    pool = ThreadPool()
    data = pool.map(makeSamples, files)
    pool.close()
    pool.join()

if SAVE_DATA:
    print("Writing data to file...")
    headings = ["parent", "group", "start", "dur", "power", "hz", "note", "octave"]
    rowCount = 0
    with open(OUTPUT_FILE, 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(headings)
        for sdata in data:
            for entry in sdata:
                writer.writerow([entry[key] for key in headings])
                rowCount += 1
    print("Wrote %s rows to %s" % (rowCount, OUTPUT_FILE))


"""
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once.
For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?
"""

from itertools import permutations

def isprime(x):
    for i in range(2, x):
        if x % i == 0:
            return False
    return True

listofallpandigitalnumbers = list()
listofallpandigitalprimes = list()
for i in [4,7]:#other n-digit pantigital numbers cannot be prime because they are divisible by 3
    list1 = list(permutations(list(range(1, i + 1))))
    for j in list1:
        number = 0
        for k in j:
            number *= 10
            number += k
        if number%2!=0:
            listofallpandigitalnumbers.append(number)
for i in listofallpandigitalnumbers:
    if isprime(i):
        listofallpandigitalprimes.append(i)
print(max(listofallpandigitalprimes))


from __future__ import absolute_import

import os
import stat
import socket
from select import select
from .socket import Connection, Server, Client, Socket
from ..utility import socket_pair

class UnixConnection(Connection):
    def __init__(self, socket):
        self._socket = socket
        self._isalive = True

    def send(self, data):
        self._socket.sendall(data)

    def recv(self, size=2**16):
        data = self._socket.recv(size)
        if not data:
            self._isalive = False

        return data

    def isalive(self):
        return self._isalive

    def shutdown(self):
        self._isalive = False
        try:
            self._socket.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass

    def close(self):
        self._socket.close()

class UnixClient(UnixConnection, Client):
    def __init__(self, address='/tmp/pydaplink/socket', timeout=None):
        self.address = address
        self._isalive = False
        self._timeout = timeout

    def open(self):
        conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        conn.settimeout(self._timeout)
        conn.connect(self.address)

        UnixConnection.__init__(self, conn)

class UnixServer(Server):
    def __init__(self, address='/tmp/pydaplink/socket', timeout=None):
        self.address = address
        self._isalive = False
        self._timeout = timeout

    def open(self):
        # First make sure path to socket exists
        try:
            os.makedirs(os.path.dirname(self.address))
        except OSError:
            pass

        # Create internal socket so we can interrupt our own accept call
        self._shutdown_pipe = socket_pair()

        # Socket can get left if previous server failed to exit cleanly
        try:
            if (stat.S_ISSOCK(os.stat(self.address).st_mode) and
                os.access(self.address, os.W_OK)):
                os.unlink(self.address)
        except OSError:
            pass

        # Create the server socket
        self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self._socket.settimeout(self._timeout)
        self._socket.bind(self.address)
        self._socket.listen(socket.SOMAXCONN)

        self._isalive = True

    def accept(self):
        select([self._socket, self._shutdown_pipe[1]], [], [])

        if not self._isalive:
            return None

        conn, _ = self._socket.accept()
        conn.settimeout(self._timeout)
        return UnixConnection(conn)

    def isalive(self):
        return self._isalive

    def shutdown(self):
        self._isalive = False
        # Use pipe to interrupt accept call
        self._shutdown_pipe[0].sendall('shutdown')

    def close(self):
        self._socket.close()
        self._shutdown_pipe[0].close()
        self._shutdown_pipe[1].close()

        try:
            os.unlink(self.address)
        except OSError:
            pass

class UnixSocket(Socket):
    name = 'unix'
    available = hasattr(socket, 'AF_UNIX')

    @staticmethod
    def addrisvalid(address):
        return os.access(os.path.dirname(address), os.W_OK)

    Client = UnixClient
    Server = UnixServer


from functools import partial

import torch
import torch.nn as nn

from .basic import ConvUnit, BaseMultiReturn

# https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py
# return _resnet(Bottleneck, [3, 4, 6, 3], weights, progress, **kwargs)
# return _resnet(Bottleneck, [3, 8, 36, 3], weights, progress, **kwargs)

class Bottleneck(nn.Module):

    def __init__(self, cin, width, stride, bn):
        super().__init__()
        w = width
        cout = w * 4
        self.u1 = ConvUnit(cin, w, 1, 1, 0, 'relu', bn)
        self.u2 = ConvUnit(w, w, 3, stride, 1, 'relu', bn)
        self.u3 = ConvUnit(w, cout, 1, 1, 0, 'relu', bn)
        if stride > 1 or cin != cout:
            self.downsample = ConvUnit(cin, cout, 1, stride, 0, activ=None, bn=bn)

    def forward(self, x):
        y = x if not hasattr(self, 'downsample') else self.downsample(x)
        x = self.u1(x)
        x = self.u2(x)
        x = self.u3(x, add=y)
        return x

class ResNet(BaseMultiReturn):

    def get_res_layer(self, cin, cout, stride, bn, count):
        blocks = [Bottleneck(cin, cout, stride, bn)]
        blocks.extend([Bottleneck(cout * 4, cout, 1, bn) for _ in range(1, count)])
        return nn.Sequential(*blocks)

    def __init__(self, block_counts, retidx=[1, 2, 3, 4], bn=1e-05, num_freeze=None):
        super().__init__(retidx)
        self.layers = nn.Sequential(
            nn.Sequential(
                ConvUnit(3, 64, k=7, s=2, p=3, activ='relu', bn=bn), # input_size/2
                nn.MaxPool2d(kernel_size=3, stride=2, padding=1)     # input_size/4
            ),
            self.get_res_layer(64, 64, 1, bn, block_counts[0]),   # C2 (input_size/4)
            self.get_res_layer(256, 128, 2, bn, block_counts[1]), # C3 (input_size/8)
            self.get_res_layer(512, 256, 2, bn, block_counts[2]), # C4 (input_size/16)
            self.get_res_layer(1024, 512, 2, bn, block_counts[3]) # C5 (imput_size/32)
        )
        if num_freeze:
            super().freeze(num_freeze)

#def ResNet50(retidx, bn=1e-05):  return ResNet([3, 4, 6, 3], retidx, bn)
#def ResNet152(retidx, bn=1e-05): return ResNet([3, 8, 36, 3], retidx, bn)
ResNet50 = partial(ResNet, block_counts=[3, 4, 6, 3])
ResNet152 = partial(ResNet, block_counts=[3, 8, 36, 3])


#Lab2 Question 7
#Alan Vuong
#Description of Program: This program accepts user input wholesale prices to calulate retail prices, where the markup is 2.5 times the wholesale price. Validation will be used to check if the inputted value is out of range and displays a message to let the user know when an error is encountered. Validation: Wholesale price !< 0

#Initialize variables
add_item = 'Y'

while add_item == 'Y' or add_item == 'y':
	#Input data by user
	wholesale_price = float(input("Enter the item's wholesale cost: "))

	while wholesale_price < 0:			# Validation check for user input
		print("ERROR: the cost cannot be negative")
		wholesale_price = float(input("Enter the correct wholesale cost: "))

	#Constants
	MARKUP = 2.5

	#Processing
	retail_price = wholesale_price * MARKUP

	#User Output
	print("Retail price is $" + '{:.2f}'.format(retail_price))

	add_item = input("Do you have another item? (Enter y for yes): ")

##Test Run 1 for Q7
## Enter the item's wholesale cost: -.50
## ERROR: the cost cannot be negative
## Enter the correct wholesale cost: .50
## Retail price is $1.25
## Do you have another item? (Enter y for yes): n

## Test Run 2 for Q7
## Enter the item's wholesale cost: .75
## Retail price is $1.88
## Do you have another item? (Enter y for yes): Y
## Enter the item's wholesale cost: .50
## Retail price is $1.25
## Do you have another item? (Enter y for yes): n

## Test Run 3 for Q7
## Enter the item's wholesale cost: 1.50
## Retail price is $3.75
## Do you have another item? (Enter y for yes): y
## Enter the item's wholesale cost: .35
## Retail price is $0.88
## Do you have another item? (Enter y for yes): y
## Enter the item's wholesale cost: -0.1
## ERROR: the cost cannot be negative
## Enter the correct wholesale cost: 0.42
## Retail price is $1.05
## Do you have another item? (Enter y for yes): n


"""
Read robustness.json

@author: lukasfischer

"""

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import warnings; warnings.simplefilter('ignore')
import yaml
import json
import seaborn as sns
sns.set_style('white')
import os
with open('./loc_settings.yaml', 'r') as f:
    content = yaml.load(f)

def roi_robustness_calculation(fig_datasets, fname, fformat='png', subfolder=[]):

    avg_robustness = []
    avg_robustness_openloop = []

    # run through every dataset and append ROIs to matrices
    for r in figure_datasets:
        print(r)
        mouse = r[0]
        session = r[1]
        ol_session = r[2]

        robustness_short = []
        robustness_short_openloop = []
        robustness_long = []
        robustness_long_openloop = []

        with open(content['figure_output_path'] + mouse+session + os.sep + 'roi_classification.json') as f:
            roi_classification = json.load(f)
        with open(content['figure_output_path'] + mouse+ol_session + os.sep + 'roi_classification.json') as f:
            roi_classification_openloop = json.load(f)

        with open(content['figure_output_path'] + mouse+session + os.sep + 'roi_robustness.json') as f:
            roi_robustness = json.load(f)
        with open(content['figure_output_path'] + mouse+ol_session + os.sep + 'roi_robustness.json') as f:
            roi_robustness_openloop = json.load(f)

        for i in roi_classification['task_engaged_short']:
            robustness_short.append(roi_robustness['robustness_short'][i])
            robustness_short_openloop.append(roi_robustness_openloop['robustness_short'][i])

        for i in roi_classification['task_engaged_long']:
            robustness_long.append(roi_robustness['robustness_long'][i])
            robustness_long_openloop.append(roi_robustness_openloop['robustness_long'][i])

        # robustness_long = roi_robustness['robustness_long']
        # robustness_long_openloop = roi_robustness_openloop['robustness_long']
        avg_robustness.append(np.nanmean(robustness_short))
        avg_robustness.append(np.nanmean(robustness_long))
        avg_robustness_openloop.append(np.nanmean(robustness_short_openloop))
        avg_robustness_openloop.append(np.nanmean(robustness_long_openloop))

    avg_robustness_mean = np.nanmean(avg_robustness)
    avg_robustness_sem = np.nanstd(avg_robustness)/np.sqrt(len(avg_robustness))

    avg_robustness_openloop_mean = np.nanmean(robustness_short_openloop)
    avg_robustness_openloop_sem = np.nanstd(robustness_short_openloop)/np.sqrt(len(robustness_short_openloop))

    print(avg_robustness_mean,avg_robustness_openloop_mean)
    print(stats.ttest_ind(avg_robustness,avg_robustness_openloop))

    fig = plt.figure(figsize=(4,6))
    ax1 = plt.subplot(111)
    ax1.set_xlim([0,3.5])
    ax1.set_ylim([0.2,0.5])
    bar_width = 0.5

    ax1.bar([1-bar_width,2.5-bar_width],[avg_robustness_mean,avg_robustness_openloop_mean], bar_width, color='k', yerr=[avg_robustness_sem,avg_robustness_openloop_sem], ecolor='0.5')

    ax1.set_xticks([0.5,1.7])
    ax1.set_xticklabels(['navigation', 'openloop movie'], rotation=45, fontsize=20)

    ax1.spines['top'].set_visible(False)
    ax1.spines['right'].set_visible(False)

    fig.tight_layout()
    #
    if not os.path.isdir(content['figure_output_path'] + subfolder):
        os.mkdir(content['figure_output_path'] + subfolder)
    fname = content['figure_output_path'] + subfolder + os.sep + fname + '.' + fformat
    print(fname)
    try:
        fig.savefig(fname, format=fformat)
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type, exc_value, exc_traceback,
                              limit=2, file=sys.stdout)

if __name__ == "__main__":

    figure_datasets = [['LF170110_2','Day20170331','Day20170331_openloop',87],['LF170222_1','Day20170615','Day20170615_openloop',96],
    ['LF170420_1','Day20170719','Day20170719_openloop',95],['LF170421_2','Day20170719','Day20170719_openloop',68],['LF170421_2','Day20170720','Day20170720_openloop',45],['LF170613_1','Day201784','Day201784_openloop',77]]

    #figure_datasets = [['LF170110_2','Day20170331','Day20170331_openloop',87]]

    #figure output parameters
    subfolder = 'roi_fractions'
    fname = 'roi_robustness_all'
    fformat = 'svg'

    print('ALL:')
    roi_robustness_calculation(figure_datasets, fname, fformat, subfolder)

    figure_datasets = [['LF170110_2','Day20170331','Day20170331_openloop',87],['LF170613_1','Day201784','Day201784_openloop',77]]

    #figure output parameters
    subfolder = 'roi_fractions'
    fname = 'roi_robustness_l23'
    fformat = 'svg'

    print('L2/3:')
    roi_robustness_calculation(figure_datasets, fname, fformat, subfolder)

    figure_datasets = [['LF170222_1','Day20170615','Day20170615_openloop',96],['LF170420_1','Day20170719','Day20170719_openloop',95],['LF170421_2','Day20170719','Day20170719_openloop',68],['LF170421_2','Day20170720','Day20170720_openloop',45]]

    #figure output parameters
    subfolder = 'roi_fractions'
    fname = 'roi_robustness_l5'
    fformat = 'svg'

    print('5:')
    roi_robustness_calculation(figure_datasets, fname, fformat, subfolder)

    figure_datasets = [['LF170214_1','Day201777','Day201777_openloop',112],['LF170214_1','Day2017714','Day2017714_openloop',165]]

    subfolder = 'roi_fractions'
    fname = 'roi_robustness_V1'
    fformat = 'svg'

    roi_robustness_calculation(figure_datasets, fname, fformat, subfolder)


### dense model

from keras import Model
from keras.layers import Dense, Dropout, BatchNormalization, Reshape, Embedding, Reshape, Concatenate, Activation
import numpy as np
from sklearn.model_selection import StratifiedKFold
from metrics import keras_auc,keras_precision,keras_recall,keras_f1,keras_fb
from common import prep, read_data
from keras.callbacks import EarlyStopping
import tensorflow as tf
from keras.optimizers import SGD
from tqdm import tqdm

from keras.constraints import MaxNorm

class SimpleMLP(Model):

    def __init__(self, input_dim, embedding_dim = [128,128], use_bn=False, use_dp=False):
        super(SimpleMLP, self).__init__(name='mlp')
        self.use_bn = use_bn
        self.use_dp = use_dp

        if not isinstance(embedding_dim,list):
            embedding_dim = [embedding_dim] * 2

        constraint = MaxNorm(1,axis=1)

        self.e1 = Embedding(input_dim[0],embedding_dim[0],embeddings_constraint=constraint)
        self.e2 = Embedding(input_dim[1],embedding_dim[1],embeddings_constraint=constraint)
        self.m = Concatenate()

        self.rate = 0.2

        self.ls = [Concatenate(),
                   Dense(32),
                   Dropout(self.rate),
                   BatchNormalization(axis=-1),
                   Activation('relu'),
                   Dense(1,activation='sigmoid'),
                   Reshape((-1,))]

        if self.use_dp:
            self.dp = Dropout(self.rate)
        if self.use_bn:
            self.bn = BatchNormalization(axis=-1)

    def call(self, inputs):
        x1,x2 = inputs[:,0],inputs[:,1]

        x1 = self.e1(x1)
        x2 = self.e2(x2)

        if self.use_dp:
            x1 = self.dp(x1)
            x2 = self.dp(x2)

        if self.use_bn:
            x1 = self.bn(x1)
            x2 = self.bn(x2)

        x = [x1,x2]

        for l in self.ls:
            x = l(x)

        return x

def main(cv=False, verbose = 1, num_cl=10):
    Etr = read_data('./data/endpoints_train.txt')
    Ete = read_data('./data/endpoints_test.txt')

    Cg = read_data('./data/chemical_graph.txt')
    Tg = read_data('./data/taxonomy_graph.txt')

    C = set.union(*[set([a,b]) for a,_,b,_ in Cg])
    T = set.union(*[set([a,b]) for a,_,b,_ in Tg])

    Xc = set([k for k,_,_,_ in Etr if k in C]) | set([k for k,_,_,_ in Ete if k in C])
    Xt = set([k for _,_,k,_ in Etr if k in T]) | set([k for _,_,k,_ in Ete if k in T])
    N = len(Xc)
    M = len(Xt)
    mapping_c = {c:i for c,i in zip(Xc,range(N))}
    mapping_t = {c:i for c,i in zip(Xt,range(M))}

    Xtr = []
    Ytr = []
    Xte = []
    Yte = []

    for c,p,t,e in Etr:
        try:
            tmp = [mapping_c[c],mapping_t[t]]
            if tmp in Xtr: continue
            e = float(e)
            Xtr.append(tmp)
            Ytr.append(e)
        except KeyError:
            pass

    for c,p,t,e in Ete:
        try:
            tmp = [mapping_c[c],mapping_t[t]]
            if tmp in Xte: continue
            e = float(e)
            Xte.append(tmp)
            Yte.append(e)
        except KeyError:
            pass

    Xtr = np.asarray(Xtr)
    Ytr = np.asarray(Ytr)
    Xte = np.asarray(Xte)
    Yte = np.asarray(Yte)

    metrics = ['accuracy', keras_precision, keras_recall, keras_auc,keras_f1, keras_fb]
    callbacks = [EarlyStopping(monitor='loss', mode='min', patience=5, restore_best_weights=True)]
    kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
    cvscores = []
    epochs = 1000
    if cv:
        with tqdm(total=10,desc='CV') as pbar:
            for train,test in kfold.split(Xtr,Ytr):
                model = SimpleMLP(input_dim=[N,M], embedding_dim=16, use_bn=True,use_dp=True)
                # Compile model
                model.compile(loss='binary_crossentropy', optimizer='adagrad', metrics=metrics)
                # Fit the model
                model.fit(Xtr[train], Ytr[train], epochs=epochs, batch_size=len(Ytr[train]), verbose=verbose, callbacks = callbacks)
                # evaluate the model
                scores = model.evaluate(Xtr[test], Ytr[test], verbose=0,batch_size=len(Ytr[test]))
                cvscores.append(scores)
                pbar.update(1)

            cvscores = np.asarray(cvscores)

        for i,n in enumerate(model.metrics_names):
            print(n,"%.2f (+/- %.2f)" % (np.mean(cvscores[:,i]), np.std(cvscores[:,i])))

    cvscores = []
    for i in tqdm(range(num_cl)):
        model = SimpleMLP(input_dim=[N,M], embedding_dim=16, use_bn=True,use_dp=True)
        model.compile('adagrad', loss='binary_crossentropy',metrics=metrics)
        model.fit(Xtr, Ytr, batch_size = len(Ytr), epochs = epochs, validation_split=0.0, shuffle=True, verbose = verbose, callbacks=callbacks)

        scores = model.evaluate(Xte, Yte, batch_size=len(Yte), verbose=0)
        cvscores.append(scores)

        p = model.predict(Xte)
        with open('./results/MLP/'+str(i)+'.txt', 'w') as f:
            for a,b in zip(Yte,p):
                f.write(str(a)+','+str(b[-1])+'\n')

    cvscores = np.asarray(cvscores)
    for i,n in enumerate(model.metrics_names):
        print(n,"%.2f (+/- %.2f)" % (np.mean(cvscores[:,i]), np.std(cvscores[:,i])))

if __name__ == '__main__':
    main(cv=True,verbose=0,num_cl=10)


import numpy as np
import pylab

from uxdconverter.ui.controller import Controller
from uxdconverter.plugin.stress.fit import CorrelatedTwoPeakFit
from uxdconverter.plugin.stress.psi_fit import LinearPsiFit
from uxdconverter.measurement import Measurements
from uxdconverter.operation import MultiMerger, MeasurementMerger, ErrorCalculation

from collections import defaultdict
from skipi.function import Function, NullFunction

NAME = 'stress'

class StressAnalysis(object):
    def __init__(self, controller: Controller):
        self._c = controller
        self._fitters = {}

    def group_measurements_by_psi(self, measurements):

        mss = measurements.get_measurements()

        grouped = defaultdict(list)

        for ms in mss:
            grouped[round(ms.get_psi(), 2)].append(ms)

        return grouped

    def merge_group(self, group):
        multi_merge = MultiMerger(MeasurementMerger())
        merged = {}
        for psi, mss in group.items():
            merge = multi_merge.merge(mss)
            merge = ErrorCalculation().manipulate(merge, self._c._settings_controller.get_measurement_context())
            theta, dtheta, counts, dcounts = zip(*merge.get_data())
            theta = np.array(theta)
            dx = Function.to_function(theta, dtheta)
            dy = Function.to_function(theta, dcounts)
            merged[psi] = Function.to_function(theta, counts, dx=dx, dy=dy)

        return merged

    def analyze(self):
        measurements = self._c.measurements # type: uxdconverter.measurement.Measurements
        psi_measurement_dict = self.merge_group(self.group_measurements_by_psi(measurements))

        wavelength_k1 = 0.70931724
        wavelength_k2 = 0.71360728

        psis = []
        thetas = []

        for psi, fctn in psi_measurement_dict.items():
            if not( psi in self._fitters.keys()):
                self._fitters[psi] = CorrelatedTwoPeakFit(fctn, wavelength_k1, wavelength_k2)

            fitter = self._fitters[psi]

            fit = fitter.get_fit()
            fitter.plot()
            print(fit.fit_report(show_correl=False))
            if fit.redchi > 1000:
                continue

            psis.append(psi)
            thetas.append(fitter.get_peak_position())

        pylab.show()
        pylab.figure()
        pylab.xlabel("$sin^2(\psi)$")
        pylab.ylabel("lattice spacing $[\AA]$")

        f = Function.to_function(psis, np.array(thetas))
        trafo_to_lattice = lambda psi, theta: wavelength_k1 / (2 * np.sin(np.deg2rad(theta)))
        dy_traf = lambda psi, theta: np.abs(trafo_to_lattice(psi, theta) / np.tan(np.deg2rad(theta)))
        dy_scale = f.transform(dy_traf)
        dy_traf = lambda psi, dtheta: dtheta * dy_scale(psi)

        f = f.transform(trafo_to_lattice, trafo_dy=dy_traf)

        if f.get_dom().min() < 0:
            fneg = self._to_sin_sqr(f.vremesh((None, 0)))
        else:
            fneg = NullFunction(f.get_dom())

        if f.get_dom().max() > 0:
            fpos = self._to_sin_sqr(f.vremesh((0, None), dstart=-1))
        else:
            fpos = NullFunction(f.get_dom())

        fpos.plot(label='$\psi \geq 0$')
        fneg.plot(label='$\psi < 0$')

        self.analyze_psi(fpos)
        self.analyze_psi(fneg)

        pylab.show()

        return f

    def _to_sin_sqr(self, fctn):
        psi = fctn.get_domain()
        theta = fctn.eval()
        sinsqrd = np.fromiter(map(lambda x: np.sin(np.deg2rad(x)) ** 2, psi), float)
        dy = fctn.dy
        if not dy.is_null():
            dy = Function.to_function(sinsqrd, dy.eval())

        return Function.to_function(sinsqrd, theta, dy=dy)

    def plot_lin_fit(self, fit, function):
        comps = fit.eval_components(x=function.get_domain())
        pylab.plot(function.get_domain(), comps['Lin_'])

    def analyze_psi(self, f):
        if f.is_null():
            return

        fit = LinearPsiFit(f).get_fit()
        print(fit.fit_report(show_correl=False))
        self.plot_lin_fit(fit, f)


#!/usr/bin/env python3

import numpy as np
import cv2 as cv
import glob
import pickle
import argparse

#program derived from Nico Nielsen at https://raw.githubusercontent.com/niconielsen32/ComputerVision/master/cameraCalibration.py

################ FIND CHESSBOARD CORNERS - OBJECT POINTS AND IMAGE POINTS #############################

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required=False, default='./',
        help="path to parameter file. default is ./")
ap.add_argument("-v", "--video", required=True, type=str,
        help="file name for video to undistort")
ap.add_argument("-w", "--watch", required=False, default=0, type=int,
        help="whether (1) or not (0) to watch video while writing. Default = 1")
ap.add_argument("-d", "--delay", required=False, default=0, type=float,
        help="delay time between frames for slo-mo")
ap.add_argument("-pre", "--prefix", required=True, type=str,
        help="prefix name for parameters file ending in .p")
ap.add_argument("-o", "--outfile", required=True, type=str,
        help="outfile for video name not including extension")
ap.add_argument ('-fr', '--frameSize', nargs=2, type=int, action = 'append', required=True,
        help="need to specify frame size of video e.g. -c 640 480")

args = vars(ap.parse_args())
delay = args["delay"]
watch = args["watch"]
video = args["video"]
prefix = args["prefix"]
outfile = args["outfile"]
dir_path = args["path"]
frameSize = args["frameSize"]
frameSize = tuple(frameSize[0])

########## READ PICKLE PARAM FILE########################
infile = dir_path + prefix + ".p"
print("Opening infile ", infile)
calib_result_pickle = pickle.load( open(infile, "rb" ) )

cameraMatrix = calib_result_pickle["cameraMatrix"]
dist = calib_result_pickle["dist"]
rvecs = calib_result_pickle["rvecs"]
tvecs = calib_result_pickle["tvecs"]

############## UNDISTORTION #####################################################
def undistort(img, cameraMatrix, dist) :
    h,  w = img.shape[:2]
    newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(cameraMatrix, dist, (w,h), 1, (w,h))

    # Undistort
    dst = cv.undistort(img, cameraMatrix, dist, None, newCameraMatrix)

    ## crop the image to remove black sections of the image
    #x, y, w, h = roi
    #dst = dst[y:y+h, x:x+w]

    ## Undistort with Remapping
    #This is a different algorithm for remapping -- not sure of the resulting differences though
    #mapx, mapy = cv.initUndistortRectifyMap(cameraMatrix, dist, None, newCameraMatrix, (w,h), 5)
    #dst = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)

    ## crop the image
    #x, y, w, h = roi
    #dst = dst[y:y+h, x:x+w]
    return(dst)

#*******************
# Open video to undistort and save
cap = cv.VideoCapture(video)

#should check here to make sure video files contains 3 letter extension
vidoutfile = outfile + "_undis.mkv"

out = cv.VideoWriter(vidoutfile,cv.VideoWriter_fourcc('H','2','6','4'), 30, frameSize)

print("Undistorting fish eye video and writing to " + vidoutfile)
_img_shape = None

while(cap.isOpened()):
    succes, frame = cap.read()

    if succes == True:
        if _img_shape == None:
            _img_shape = frame.shape[:2]
        else:
            assert _img_shape == frame.shape[:2], "All images must share the same size."

        # Undistort and rectify images

        uframe = undistort(frame, cameraMatrix, dist)

        #If watch variable is true
        # Show the frames
        if watch == 1:
          cv.imshow("frame", uframe)
          # Hit "q" to close the window
          if cv.waitKey(1) & 0xFF == ord('q'):
              break

        #write the frame to outfile
        out.write(uframe)

    else:
        break
# Release and destroy all windows before termination
cap.release()
cv.destroyAllWindows()