import cv2
import mediapipe as mp
import time

mp_hand = mp.solutions.hands
mp_draw = mp.solutions.drawing_utils

cap = cv2.VideoCapture(0)
with mp_hand.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5) as hand:
    while cap.isOpened():
        success, frame = cap.read()
        start = time.time()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame.flags.writeable = False
        results = hand.process(frame)
        frame.flags.writeable = True
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        position = []
        if results.multi_hand_landmarks:
            for detection in results.multi_hand_landmarks:
                mp_draw.draw_landmarks(frame,detection,mp_hand.HAND_CONNECTIONS)
                w,h,c = frame.shape
                for id,lm in enumerate(detection.landmark):
                    cx,cy = round(lm.x*w), round(lm.y*h)
                    position.append((id,cx,cy))
        figerCounter = []
        if len(position) != 0:
            mat = [4,8,12,16,20]

            for point in mat:
                if point == 4 :

                    if position[0][1] < position[4][1]:#right hand
                        if position[point][1]<position[point-1][1]:
                            figerCounter.append(0)
                        else:
                            figerCounter.append(1)
                    elif position[0][1] > position[4][0]:#left hand
                         if position[point][1]<position[point-1][1]:
                            figerCounter.append(1)
                         else:
                            figerCounter.append(0)
                else:
                     if position[point][2]<position[point-2][2]:
                        figerCounter.append(1)

                     else:
                        figerCounter.append(0)
        totalFinger = figerCounter.count(1)

        end =time.time()
        fps = 1.0/(end-start)
        cv2.putText(frame,f'{int(totalFinger)}',(20,400),cv2.FONT_HERSHEY_COMPLEX,5,(0,255,0),5)
        cv2.putText(frame, f'FPS:{int(fps)}',(20,70),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
        cv2.imshow("HAND_DETECTION",frame)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()


import os
import cv2
import numpy as np
import pandas as pd

class SceneDetector:
    baseDirectory: str
    scanInputDirectories: list
    baseOutputDirectory: str
    sceneDetectionOutputDirectory: str
    currentScanOutDirectory: str
    _currentSceneDirectory: str
    _currentScanInputDirectory: str
    _currentFramePath: str

    infoDicts: list = []
    _sceneDict: dict

    _duplicatesPaths: list = []
    _erroneousPaths: list = []

    _scanNo: int = -1
    _sceneNo: int = -1
    _scanFrameNo: int = -1
    _sceneFrameCounter: int = -1
    _accumulatedDuplicates: int = 0
    _accumulatedErroneous: int = 0
    _inputStartFrame: int = 0
    _diffs: list = []
    _lastDiffs: list = []

    def __init__(self, projectDirectory):
        self.baseDirectory = projectDirectory
        self.scanInputDirectories = [subdir.path for subdir in os.scandir(self.baseDirectory) if subdir.is_dir()]
        self.baseOutputDirectory = os.path.join(self.baseDirectory, 'output')
        self.sceneDetectionOutputDirectory = os.path.join(self.baseOutputDirectory, 'scanned_scenes')

    def detect_scenes(self):
        self.printStartInformation()

        os.makedirs(self.baseOutputDirectory)

        for scan_index in range(0, len(self.scanInputDirectories)):
            self._scanNo = scan_index
            self._currentScanInputDirectory = self.scanInputDirectories[scan_index]
            self.currentScanOutDirectory = os.path.join(self.sceneDetectionOutputDirectory, f'scan_{scan_index}')
            print(f'Started scene detection for scan {self._currentScanInputDirectory}')

            frames = self.getFramesForCurrentScan()

            if not len(frames) == 0:
                self._currentFramePath = frames[0]
                self.newScene(np.nan)
                lastFrame = np.zeros(cv2.imread(frames[0]).shape)
            for frame_index in range(0, len(frames)):
                self._scanFrameNo = frame_index
                self._sceneFrameCounter += 1
                self._currentFramePath = frames[self._scanFrameNo]
                frame = cv2.imread(self._currentFramePath)

                diff, frameDup, frameOK = self.analyzeFrame(frame, lastFrame, frames)

                if frameOK:
                    self._diffs.append(diff)
                    self._lastDiffs.append(diff)
                    if len(self._lastDiffs) > 36:
                        self._lastDiffs = self._lastDiffs[1:]
                    lastFrame = frame
                    append = ''
                elif frameDup:
                    append = '_duplicate'
                else:
                    append = '_erroneous'

                frameFileName = f'frame_{str(self._sceneFrameCounter).zfill(5)}{append}.jpg'
                frameOutPath = os.path.join(self._currentSceneDirectory, frameFileName)
                cv2.imwrite(frameOutPath, frame)
                if frameDup:
                    self._duplicatesPaths.append(frameOutPath)
                elif not frameOK:
                    self._erroneousPaths.append(frameOutPath)

        print(f'duplicates:', '\n'.join(self._duplicatesPaths))
        print(f'erroneous:', '\n'.join(self._erroneousPaths))
        self.writeInfoDictsToCSV()

    def printStartInformation(self):
        print('Starting scene detection')
        print(f'Project root is {self.baseDirectory}')
        print(f'Found {len(self.scanInputDirectories)} scans:')
        print('\n'.join(self.scanInputDirectories))
        print(f'writing output to {self.baseOutputDirectory}')

    def getFramesForCurrentScan(self):
        frames = []
        for dirpath, dirnames, filenames in os.walk(self._currentScanInputDirectory):
            frames += [os.path.join(dirpath, filename)
                       for filename in [f for f in filenames if f.endswith(".jpg")]]
        frames.sort()
        print(f'Found and sorted {len(frames)} frames.')
        return frames

    def analyzeFrame(self, frame, lastFrame, frames):
        diff = np.mean(np.sqrt((frame - lastFrame) ** 2))
        frameOK = True
        frameDup = False
        if diff == 0:
            print(f'duplicate frame {self._currentFramePath}')
            frameOK = False
            frameDup = True
        elif len(self._lastDiffs) > 5:
            diffThreshold = 8.0
            significantDiff = diff > diffThreshold
            nextFrameAvailable = self._scanFrameNo + 1 < len(frames)

            if significantDiff and nextFrameAvailable:
                nextFrame = cv2.imread(frames[self._scanFrameNo + 1])
                diffNext = np.mean(np.sqrt((nextFrame - lastFrame) ** 2))

                if diffNext > diffThreshold:
                    self.newScene(diff)
                else:
                    print(f'something seems wrong with frame {self._currentFramePath}.')
                    frameOK = False
        return diff, frameDup, frameOK

    def newScene(self, diff,):
        if self._sceneNo >= 0:
            newDuplicates = len(self._duplicatesPaths) - self._accumulatedDuplicates
            newErroneous = len(self._erroneousPaths) - self._accumulatedErroneous
            oldSceneDict = {**self._sceneDict,
                            'length': self._sceneFrameCounter,
                            'duplicates': newDuplicates,
                            'erroneous': newErroneous,
                            'end_frame': self._scanFrameNo - 1}
            self.infoDicts.append(oldSceneDict)
        self._sceneNo += 1
        self._sceneFrameCounter = 0
        self._accumulatedDuplicates = len(self._duplicatesPaths)
        self._accumulatedErroneous = len(self._erroneousPaths)
        self._currentSceneDirectory = os.path.join(self.currentScanOutDirectory, f'scene_{str(self._sceneNo).zfill(3)}')
        os.makedirs(self._currentSceneDirectory)
        self._sceneDict = {
            'scan': self._scanNo,
            'scene': self._sceneNo,
            'start_frame': self._scanFrameNo,
            'rmse': diff,
            'rolling_mean_rmse': np.mean(self._lastDiffs),
            'folder': self._currentSceneDirectory
        }
        print(f'new scene at frame {self._currentFramePath}. '
              f'Diff: {diff}, '
              f'avg{len(self._lastDiffs)}: {np.mean(self._lastDiffs)}')
        self._lastDiffs = []

    def writeInfoDictsToCSV(self):
        (pd.DataFrame(self.infoDicts)
            .set_index(['scene', 'scan'])
            .sort_index()
            .to_csv(
            os.path.join(self.baseOutputDirectory, 'info.csv')))


# coding: utf-8

# In[1]:

from splinter import Browser
from bs4 import BeautifulSoup as bs

# In[2]:

executable_path = {"executable_path": "chromedriver"}
browser = Browser("chrome", **executable_path, headless=False)

# In[3]:

# Visit the Nasa Mars site
url = "https://mars.nasa.gov/news/?page=0&per_page=40&order=publish_date+desc%2Ccreated_at+desc&search=&category=19%2C165%2C184%2C204&blank_scope=Latest"
browser.visit(url)

# In[4]:

# Scrape page into soup
html = browser.html
soup = bs(html, "html.parser")

    # Scrape the NASA Mars News Site and collect the latest News Title and Paragraph Text.
    # Assign the text to variables that you can reference later.Find today's forecast

MarsNews = soup.find("div", class_="content_title")

MarsNewsTitle = MarsNews.find('a').text

print(MarsNewsTitle)

MarsDict={"Article_Title": MarsNewsTitle}

print(MarsDict)

# In[5]:

MarsTease = soup.find("div", class_="article_teaser_body").text

print(MarsTease)
MarsDict["Article_Tease"] = MarsTease

# In[6]:

# Use splinter to navigate the site and find the image url for the current Featured Mars Image and assign the url string to a
# variable called featured_image_url.
# Make sure to find the image url to the full size .jpg image.
# Make sure to save a complete url string for this image.
url = 'https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars'
browser.visit(url)

# In[7]:

html = browser.html
soup = bs(html, 'html.parser')

# In[8]:

carousels = soup.find_all('article', class_='carousel_item')

# In[9]:

for carousel in carousels:
    article = carousel["style"]
    img_url = article.lstrip()
    img_url = img_url.split()
    img_url = img_url[1]
    img = img_url[5:57]
print(img)

print(url[:24])

# In[10]:

main_img_url = url[:24] + img

print (main_img_url)

MarsDict["RecentMarsPic"] = main_img_url

# In[11]:

#Scrape Mars Weather Tweet
url = "https://twitter.com/marswxreport?lang=en"
browser.visit(url)

# In[12]:

# Scrape page into soup
html = browser.html
soup = bs(html, "html.parser")

MarsWTweet = soup.find("p", class_="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text").text

print(MarsWTweet)
print(type(MarsWTweet))

MarsDict["Mars Weather"] = MarsWTweet

print(MarsDict)

# In[13]:

# Visit the Mars Facts webpage here and use Pandas to scrape the table containing facts about the planet including Diameter,
#Mass, etc.
# Use Pandas to convert the data to a HTML table string.
import pandas as pd
url = 'https://space-facts.com/mars/'

tables = pd.read_html(url)
tables

# In[14]:

df =tables[0]

df.columns = ["", "values"]

# In[15]:

df.set_index("", inplace=True)

df.head()

# In[17]:

html_table = pd.DataFrame.to_html(df)

print(type(html_table))
html_table

MarsDict["Html_Table"]=html_table

MarsDict

# In[18]:

# Visit the USGS Astrogeology site to obtain high resolution images for each of Mar's hemispheres.
# You will need to click each of the links to the hemispheres in order to find the image url to the full resolution image.
# Save both the image url string for the full resolution hemisphere image, and the Hemisphere title containing the
# hemisphere name. Use a Python dictionary to store the data using the keys img_url and title.
# Append the dictionary with the image url string and the hemisphere title to a list. This list will contain one dictionary
# for each hemisphere.
url = "https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars"

mars_hemi = "Cerberus Hemisphere Enhanced"

browser.visit(url)

# In[19]:

browser.click_link_by_partial_text(mars_hemi)

# In[20]:

html = browser.html
soup = bs(html, "html.parser")

picdownload = soup.find('div', class_="downloads")

picdownload

# In[21]:

hemi_pic = picdownload.find('li')

hemi=hemi_pic.a["href"]
hemi

# In[22]:

image_url =[]
hemi_dict={}
hemi_dict["tile"]=mars_hemi
hemi_dict["img_url"]=hemi

image_url.append(hemi_dict)

image_url

# In[23]:

mars_hemis = ["Cerberus Hemisphere Enhanced","Schiaparelli Hemisphere Enhanced", "Syrtis Major Hemisphere Enhanced","Valles Marineris Hemisphere Enhanced"]
url = "https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars"

hemisphere_image_urls=[]

for mars_hemi in mars_hemis:

    browser.visit(url)

    browser.click_link_by_partial_text(mars_hemi)

    html = browser.html
    soup = bs(html, "html.parser")

    picdwnload = soup.find('div',class_="downloads" )

    hemi_url =picdwnload.find('li').a["href"]

    hemi_dict={}

    hemi_dict["title"]=mars_hemi

    hemi_dict["img_url"]=hemi_url

    hemisphere_image_urls.append(hemi_dict)

hemisphere_image_urls

# hemisphere_image_urls = [
#     {"title": "", "img_url": "http://astropedia.astrogeology.usgs.gov/download/Mars/Viking/cerberus_enhanced.tif"},
#     {"title": , "img_url": "http://astropedia.astrogeology.usgs.gov/download/Mars/Viking/schiaparelli_enhanced.tif"},
#     {"title": , "img_url": "http://astropedia.astrogeology.usgs.gov/download/Mars/Viking/syrtis_major_enhanced.tif"},
#     {"title": , "img_url": "http://astropedia.astrogeology.usgs.gov/download/Mars/Viking/valles_marineris_enhanced.tif"},
# ]

# In[140]:

MarsDict["Mars_Hemispheres"]=hemisphere_image_urls

MarsDict


from collections import Counter
import pandas as pd
import argparse
import random
import os
random.seed(0)

month_days = {'Apr': '30', 'May': '31', 'Jun': '30', 'Jul': '31', 'Aug': '31', 'Sep': '30', 'Oct': '31', 'Nov': '30', 'Dec': '31', 'Jan': '31', 'Feb': '28'}
month_list = ['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb']
time_zone_dict = {}
for i in range(3, 9):
    time_zone_dict[str(i).zfill(2)] = '0'
for i in range(9, 15):
    time_zone_dict[str(i).zfill(2)] = '1'
for i in range(15, 21):
    time_zone_dict[str(i).zfill(2)] = '2'
for i in list(range(21, 24)) + list(range(0, 3)):
    time_zone_dict[str(i).zfill(2)] = '3'

def now_time_zone(times, offset=9):
    m, d, h = times
    d, h = int(d), int(h)
    if offset > 0:
        if h + offset > 23:
            if d + 1 <= int(month_days[m]):
                d = d + 1
            else:
                d = 1
                m = month_list[month_list.index(m)+1]
            h = (h + offset) % 24
        else:
            h = h + offset
    else:
        if h + offset < 0:
            if d > 1:
                d = d - 1
            else:
                m = month_list[month_list.index(m)-1]
                d = month_days[m]
            h = 24 + h + offset
        else:
            h = h + offset
    d, h = str(d).zfill(2), str(h).zfill(2)
    zone = time_zone_dict[h]
    if h in ['00','01','02']:
        if d == '01':
            m = month_list[month_list.index(m)-1]
            d = month_days[m]
        else:
            d = str(int(d)-1).zfill(2)
    return int(zone), m + '#' + d

def load_user_visit(data):
    visit_dict = {}
    for index, row in data.iterrows():
        user = row['userId']
        tag = row['venueCategory']
        time = row['utcTimestamp']
        location = row['venueId']
        x, y = row['latitude'], row['longitude']
        if location not in visit_dict:
            visit_dict[location] = {'user':[], 'loc': [], 'time': []}
        visit_dict[location]['user'].append(user)
        visit_dict[location]['time'].append(time[4:13].split(' '))
        visit_dict[location]['loc'].append((x,y,tag))

def update_user_visit_with_time(visit_dict):
    visit_dict_new = {}
    for k, v in visit_dict.items():
        if len(set(v['user'])) >= 4:
            visit_dict_new[k] = [set() for _ in range(4)]
            for time, user in zip(v['time'], v['user']):
                zone, md = now_time_zone(time)
                visit_dict_new[k][zone].add(md + '#' + str(user))
    # print(len(visit_dict_new), len(visit_dict))
    return visit_dict_new

def build_relationship(visit_dict_new):
    # basic correlations
    relation = [[] for i in range(4)]
    for k1, v1 in visit_dict_new.items():
        for k2, v2 in visit_dict_new.items():
            if k1 == k2:
                continue
            for i in range(4):
                v12 = v1[i] & v2[i]
                if len(v12) > 2:
                    relation[i] += [(k1, k2, len(v12))]

    # processing location nodes
    node_set = set()
    for rr in relation:
        for u, v, n in rr:
            if n < 4: continue
            node_set.add(u)
            node_set.add(v)
    poi_dict = {}
    for k in node_set:
        poi_dict[k] = {}
        xy_tag = visit_dict[k]['loc']
        xy = list(set([(x,y) for x,y,t in xy_tag]))
        tag = list(set([t for x,y,t in xy_tag]))
        xy_count = Counter([(x,y) for x,y,t in xy_tag])
        tag_count = Counter([t for x,y,t in xy_tag])
        xy_count = sorted(xy_count.items(), key=lambda x:x[1], reverse=True)
        tag_count = sorted(tag_count.items(), key=lambda x:x[1], reverse=True)
        if len(xy_count) == 1:
            poi_dict[k]['coord'] = xy_count[0][0]
        else:
            poi_dict[k]['coord'] = xy_count[0][0]
            pass
        if len(tag_count) == 1:
            poi_dict[k]['tag'] = tag_count[0][0]
        else:
            poi_dict[k]['tag'] = tag_count[0][0]

    threshold = 4
    all_pair_set_dict = {'time'+str(i+1): set() for i in range(4)}
    for i in range(4):
        for u, v, n in relation[i]:
            if n < threshold:
                continue
            uv = v+'#'+u if u > v else u+'#'+v
            all_pair_set_dict['time'+str(i+1)].add(uv)
    for i in range(4):
        for j in range(i+1, 4):
            s1 = all_pair_set_dict['time'+str(i+1)]
            s2 = all_pair_set_dict['time'+str(j+1)]
            print(len(s1), len(s2), str(i+1)+'#'+str(j+1), 1.0*len(s1&s2)/len(s1|s2), 1.0*len(s1&s2)/min(len(s1),len(s2)))
    all_pair_set = set()
    all_pair_list_d = {}
    for k, v in all_pair_set_dict.items():
        all_pair_set = all_pair_set | v
        all_pair_list_d[k] = list(v)

    return poi_dict, relation, all_pair_list_d

def build_dataset(node_dict, relation, all_pair_list_d):
    all_pair_list_d['time0'] = [x for x in all_pair_list_d['time4']]
    ratio = 0.2
    test_val_pair_dict = {'time'+str(i+1): set() for i in range(4)}
    threshold = 4
    relation_g = dict()
    for i in range(4):
        for u, v, n in relation[i]:
            if n < threshold:
                continue
            if v not in relation_g:
                relation_g[v] = {'time1': {}, 'time2': {}, 'time3': {}, 'time4': {}}
            relation_g[v]['time'+str(i+1)][u] = n

    relation_g_skip = dict()
    for k1, v in relation_g.items():
        relation_g_skip[k1] = {}
        for t, v2 in v.items():
            if len(v2) == 0:
                relation_g_skip[k1][t] = ''
                continue
            k2_list = [k2 for k2 in v2]
            ind = random.randint(0,len(k2_list)-1)
            k2 = k2_list[ind]
            relation_g_skip[k1][t] = k2

    for i in range(4):
        i = str(i+1)
        lll = all_pair_list_d['time'+i]
        num_test = int(len(lll) * ratio)
        while len(test_val_pair_dict['time'+i]) < num_test:
            ind = random.randint(0,len(lll)-1)
            if lll[ind] in test_val_pair_dict['time'+i]:
                continue
            b1, b2 = lll[ind].split('#')
            conflict = False
            for j in range(int(i), 5):
                if len(relation_g[b1]['time'+str(j)]) == 1 or len(relation_g[b2]['time'+str(j)]) == 1:
                    conflict = True
                    break
            if conflict: continue
            for j in range(int(i), 5):
                if b2 not in relation_g[b1]['time'+str(j)]:
                    continue
                test_val_pair_dict['time'+str(j)].add(lll[ind])
                relation_g[b1]['time'+str(j)].pop(b2)
                relation_g[b2]['time'+str(j)].pop(b1)

    train_pair_dict, test_pair_dict, valid_pair_dict = {'time0':[]}, {'time0':[]}, {'time0':[]}
    for i in range(1, 5):
        time = 'time'+str(i)
        ll = list(test_val_pair_dict[time])
        random.shuffle(ll)

        times = ['morning','midday','night','minight']
        test_pair_dict[time] = [x+'#'+times[i-1] for x in ll[:int(len(ll)/2)]]
        valid_pair_dict[time] = [x+'#'+times[i-1] for x in ll[int(len(ll)/2):]]
        train_pair_dict[time] = [x+'#'+times[i-1] for x in list(set(all_pair_list_d[time])-set(ll))]
        test_pair_dict['time0'] += test_pair_dict[time]
        train_pair_dict['time0'] += train_pair_dict[time]
        valid_pair_dict['time0'] += valid_pair_dict[time]

    node_dict = dict()
    for rr in relation:
        for u, v, n in rr:
            if n < threshold: continue
            if u not in node_dict:
                node_dict[u] = node_dict[u]
            if v not in node_dict:
                node_dict[v] = node_dict[v]

    return node_dict, train_pair_dict, valid_pair_dict, test_pair_dict

def save_relationship_dataset(output_path, node_dict, train_pair_dict, valid_pair_dict, test_pair_dict):
    times = ['morning','midday','night','minight']
    with open('%s/entities.dict' % output_path, 'w') as f:
        for i, (bid, value) in enumerate(node_dict.items()):
            f.write(str(i) + '\t' + bid + '\n')
    with open('%s/coords.dict' % output_path, 'w') as f:
        for i, (bid, value) in enumerate(node_dict.items()):
            f.write(bid + '\t' + str(value['coord'][0]) + '\t' + str(value['coord'][1]) + '\n')

    relation_types = ['competitive', 'complementary']
    idx = 0
    with open('%s/relations.dict' % output_path, 'w') as f:
        for r in relation_types:
            for t in times:
                f.write(str(idx) + '\t' + '_'.join([r, t]) + '\n')
                idx += 1
    f1 = open('%s/train.txt' % output_path, 'w')
    f2 = open('%s/valid.txt' % output_path, 'w')
    f3 = open('%s/test.txt' % output_path, 'w')
    cnt1, cnt2, cnt3 = 0, 0, 0
    for uv in train_pair_dict['time0']:
        u, v, t = uv.split('#')
        diff_tag = int(node_dict[u]['tag'] != node_dict[v]['tag'])
        cnt1 += diff_tag
        rel = relation_types[diff_tag]
        f1.write(u + '\t' + '_at_'.join([rel, t]) + '\t' + v + '\n')
        f1.write(v + '\t' + '_at_'.join([rel, t]) + '\t' + u + '\n')
    for uv in valid_pair_dict['time0']:
        u, v, t = uv.split('#')
        diff_tag = int(node_dict[u]['tag'] != node_dict[v]['tag'])
        cnt2 += diff_tag
        rel = relation_types[diff_tag]
        f2.write(u + '\t' + '_at_'.join([rel, t]) + '\t' + v + '\n')
        f2.write(v + '\t' + '_at_'.join([rel, t]) + '\t' + u + '\n')
    for uv in test_pair_dict['time0']:
        u, v, t = uv.split('#')
        diff_tag = int(node_dict[u]['tag'] != node_dict[v]['tag'])
        cnt3 += diff_tag
        rel = relation_types[diff_tag]
        f3.write(u + '\t' + '_at_'.join([rel, t]) + '\t' + v + '\n')
        f3.write(v + '\t' + '_at_'.join([rel, t]) + '\t' + u + '\n')
    f1.close()
    f2.close()
    f3.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--input_data', type=str, default='./data/raw/dataset_TSMC2014_TKY.csv')
    parser.add_argument('--output_path', type=str, default='./data/tokyo/')
    args = parser.parse_args()
    if not os.path.exists(args.output_path):
        os.mkdir(args.output_path)

    data = pd.read_csv(args.input_data)
    visit_dict = load_user_visit(data)
    visit_dict_time = update_user_visit_with_time(visit_dict)
    node_dict, relation, all_pair_list_d = build_relationship(visit_dict_time)
    node_dict, train, valid, test = build_dataset(node_dict, relation, all_pair_list_d)

    save_relationship_dataset(args.output_path, node_dict, train, valid, test)


# Training a logistic regression classifier to predict whether a flower is iris virginica or not
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from matplotlib import pyplot as plt
import numpy as np
# loading datasets

iris = datasets.load_iris()
X = iris['data'][:,3:]
y = (iris["target"] == 2).astype(np.int)

clf =  LogisticRegression()

clf.fit(X,y)

X_New = np.linspace(0,4,1000).reshape(-1,1)
print(X_New)

y_Prob = clf.predict_proba(X_New)
plt.title('Virginica')
plt.plot(X_New, y_Prob[:,1], "C1", label="virginica")
plt.show()


import utils.controller as controller
import utils.tool as tool
from model.contact import Contact
import utils.mysql as db

# 菜单
def menu():
    print("---" * 10 + 'Contact by Ackerven' + '---' * 10)
    print("1. 添加联系人")
    print("2. 删除联系人")
    print("3. 修改联系人")
    print("4. 查找联系人")
    print("5. 导入联系人")
    print("6. 导出联系人")
    print("7. 列出所有联系人")
    print("8. 同步数据")
    print("0. 退出")

# 添加联系人
def addContact(data):
    name = input("请输入姓名: ")
    while True:
        print("1. Male\n2. Female")
        choose = eval(input("请选择性别: "))
        if choose == 0:
            return
        elif choose == 1:
            gender = 'Male'
            break
        elif choose == 2:
            gender = 'Female'
            break
        else:
            print("输入错误, 取消添加请输入0!")

    while True:
        phone = input("请输入手机号码: ")
        if phone == '0':
            return
        elif not tool.isPhone(phone):
            print("格式错误，取消添加请输入0!")
        else:
            break
    wx_code = input("请输入微信号: ")
    tmp = Contact(name, gender, phone, wx_code)
    controller.add(data, tmp)
    print("成功添加联系人" + name)

# 删除联系人
def delContact(data):
    cid = eval(input("请输入需要删除的联系人的id: "))
    tmp = controller.queryID(data, cid)
    if tmp is None:
        print("联系人不存在!")
    else:
        if controller.delete(data, cid):
            print("删除" + tmp.name + '成功!')
        else:
            print("删除失败!")

# 修改字段
def mod_field(field):
    mod = input("是否修改" + field + "(y/n): ")
    if mod == 'y':
        tmp = input("请输入新的" + field + ": ")
        return tmp
    else:
        return None

# 修改联系人
def modifyContact(data):
    cid = eval(input("请输入需要修改的联系人的id: "))
    tmp = controller.queryID(data, cid)
    if tmp is None:
        print("联系人不存在!")
    else:
        # 姓名 性别 电话 微信号
        name = mod_field('姓名')
        if name is None: name = tmp.name
        mod = input("是否修改性别(y/n): ")
        gender = tmp.gender
        if mod == 'y':
            while True:
                print("1. Male\n2. Female")
                choose = eval(input("请选择新的性别(取消修改请输入0): "))
                if choose == 1:
                    gender = 'Male'
                    break
                elif choose == 2:
                    gender = 'Female'
                    break
                elif choose == 0:
                    break
        mod = input("是否修改手机号(y/n): ")
        phone = tmp.phone
        if mod == 'y':
            while True:
                temp = input("请输入新手机号码: ")
                if temp == '0':
                    break
                elif not tool.isPhone(temp):
                    print("格式错误，取消修改请输入0!")
                else:
                    phone = temp
                    break
        wx_code = mod_field('微信号')
        if wx_code is None: wx_code = tmp.wx_code
        if controller.modify(data, cid, name, gender, phone, wx_code):
            print('修改成功')
        else:
            print('修改失败')

# 查找联系人
def searchContact(data):
    key = input("请输入查找的关键字: ")
    isField = input("是否指定字段(y/n): ")
    field = ''
    if isField == 'y':
        field = input("请输入需要查找的字段: ")
    isFuzzy = input("是否开启模糊搜索(y/n): ")
    result = []
    if isFuzzy == 'y' and isField == 'y':
        result = controller.search(data, key, field=field, fuzzy=True)
    elif isFuzzy == 'n' and isField == 'y':
        result = controller.search(data, key, field=field)
    elif isFuzzy == 'y' and isField == 'n':
        result = controller.search(data, key, fuzzy=True)
    else:
        result = controller.search(data, key)

    if result:
        print("共找到{}条数据: ".format(len(result)))
        for i in result:
            print(i)
    else:
        print("共找到0条数据.")

# 到处联系人
def exportContact(data):
    try:
        while True:
            filePath = input("请输入导出文件路径(支持csv, xlsx): ")
            if filePath == '0':
                return
            else:
                fileType = filePath.split('.')[1]
                if fileType != 'csv' and fileType != 'xlsx':
                    print("文件类型错误! 取消请输入0.")
                else:
                    break
    except:
        print("文件路径错误!")
        return
    controller.export(data, filePath, fileType)
    print("导出文件" + filePath + "成功!")

# 导入联系人
def importContact(data):
    try:
        while True:
            filePath = input("请输入导入文件路径(支持csv, xlsx)")
            if filePath == '0':
                return
            else:
                fileType = filePath.split('.')[-1]
                if fileType != 'csv' and fileType != 'xlsx':
                    print("文件类型错误! 取消请输入0.")
                else:
                    break
    except:
        print("文件路径错误!")
        return
    overlay = input("是否覆盖原数据(y/n): ")
    try:
        if overlay == 'y':
            controller.importData(data, filePath, fileType, overlay=True)
        else:
            controller.importData(data, filePath, fileType)
        print("导入文件" + filePath + "成功!")
    except:
        print("导入文件" + filePath + "失败!")

# 显示所有联系人
def showData(data):
    if data is None:
        print()
    else:
        for i in data:
            print(i)

# 同步数据到磁盘
def snyc(data):
    controller.save(data)

def console():
    data = controller.init()
    while True:
        menu()
        choose = eval(input("请选择: "))
        if choose == 1:
            addContact(data)
        elif choose == 2:
            delContact(data)
        elif choose == 3:
            modifyContact(data)
        elif choose == 4:
            searchContact(data)
        elif choose == 5:
            importContact(data)
        elif choose == 6:
            exportContact(data)
        elif choose == 7:
            showData(data)
        elif choose == 8:
            snyc(data)
        elif choose == 0:
            break
        else:
            print("输入错误！")
    controller.save(data)


"""

:synopsis: :mod:`ptp` library entry point that exposes the public API.

.. moduleauthor:: Tao Sauvage

"""

from .libptp.exceptions import NotSupportedToolError, NotSupportedVersionError
from .libptp.constants import UNKNOWN, RANKING_SCALE
from .tools.arachni.parser import ArachniXMLParser, ArachniJSONParser
from .tools.skipfish.parser import SkipfishJSParser
from .tools.w3af.parser import W3AFXMLParser
from .tools.wapiti.parser import WapitiXMLParser, Wapiti221XMLParser
from .tools.metasploit.parser import MetasploitParser
from .tools.dirbuster.parser import DirbusterParser
from .tools.nmap.parser import NmapXMLParser
from .tools.owasp.cm008.parser import OWASPCM008Parser
from .tools.robots.parser import RobotsParser
from .tools.burpsuite.parser import BurpXMLParser
from .tools.hoppy.parser import HoppyParser

class PTP(object):

    """:class:`PTP` class exposing :mod:`ptp`'s public API.

    Example::

        ptp = PTP(pathname='my/path')
        ptp.parse()

    """

    #: :class:`dict` -- Supported tools and their parser(s).
    supported = {
        'arachni': [ArachniXMLParser, ArachniJSONParser],
        'skipfish': [SkipfishJSParser],
        'w3af': [W3AFXMLParser],
        'wapiti': [WapitiXMLParser, Wapiti221XMLParser],
        'metasploit': [MetasploitParser],
        'dirbuster': [DirbusterParser],
        'nmap': [NmapXMLParser],
        'owasp-cm-008': [OWASPCM008Parser],
        'robots': [RobotsParser],
        'burpsuite': [BurpXMLParser],
        'hoppy': [HoppyParser]}

    def __init__(self, tool_name='', cumulative=False):
        """Initialize :class:`PTP`.

        :param str tool_name: help :mod:`ptp` by specifying the name of the tool that has generated the target report.
        :param bool cumulative: `True` to cumulate the vulns across multiple tools. `False` to reset them.

        :raises: :class:`ptp.libptp.exceptions.NotSupportedToolError` when ``tool_name`` is not in the PTP's supported
            tools list.

        """
        if tool_name and tool_name not in self.supported:
            raise NotSupportedToolError("The tool '%s' is not supported by PTP." % tool_name)
        #: :class:`str` -- Name of the tool that generated the report.
        self.tool_name = tool_name
        #: :class:`libptp.AbstractParser` -- Parser used on the report.
        self._parser = None
        #: :class:`list` -- Vulnerabilities that are listed in the report.
        self.vulns = []
        #: :class:`dict` -- Metadata from the report.
        self.metadata = {}
        #: :class:`bool` -- Check if user wants vulns to be re-intialised for each run ot not.
        self.cumulative = cumulative
        # Parser was chosen automatically.
        self._auto = True

    def _init_parser(self, *args, **kwargs):
        """Find and initialize the parser automatically.

        :param list \*args: Arguments that are needed by the parser.
        :param dict \*\*kwargs: Arguments that are needed by the parser.

        """
        if self._auto:
            if not self.tool_name:
                # Since no tool name has been specified by the user, try all parsers.
                try:
                    supported = self.supported.itervalues()
                except AttributeError:  # Python3 then.
                    supported = self.supported.values()
            else:
                supported = [self.supported.get(self.tool_name)]
            supported = [parser for parsers in supported for parser in parsers]
            for parser in supported:
                try:
                    if parser.is_mine(*args, **kwargs):
                        self._parser = parser
                        break
                except TypeError:  # Mismatch in terms of arguments, therefore must not be the correct parser.
                    pass
                except NotSupportedVersionError:  # Mismatch in terms of supported version.
                    pass
                except (IOError, OSError):  # Mismatch in terms of report files.
                    pass
        # Check if instantiated.
        if self._parser and not hasattr(self._parser, 'stream'):
            self._parser = self._parser(*args, **kwargs)

    def parse(self, *args, **kwargs):
        """Parse a tool report.

        :param list \*args: Arguments that are needed by the parser.
        :param dict \*\*kwargs: Arguments that are needed by the parser.

        :raises: :class:`NotSupportedToolError` if the tool that has generated the report is not supported by PTP.

        :return: The list of dictionaries of the results found in the report.
        :rtype: list

        """
        # Ensure that parser is properly initialized.
        self._init_parser(*args, **kwargs)
        # PTP could not automatically detect the parser or manually initialize it.
        if self._parser is None:
            raise NotSupportedToolError('This tool is not supported by PTP. No parser matched `ToolParser(%s, %s)`\n\n' % (args, kwargs))

        self.tool_name = self._parser.__tool__
        self.metadata = self._parser.parse_metadata()
        if self.cumulative:
            self.vulns.extend(self._parser.parse_report())
        else:
            self.vulns = self._parser.parse_report()
        return self.vulns

    @property
    def highest_ranking(self):
        """Return the highest ranking of the report.

        :return: the risk id of the highest ranked vulnerability referenced in the report.
        :rtype: int

        .. note::

            The ranking starts from `0` to `n` where `n` represents the most critical risk.
            (See :mod:`ptp.libptp.constants`).

        """
        if not self.vulns:
            return UNKNOWN
        return max(RANKING_SCALE.get(vuln.get('ranking'), UNKNOWN) for vuln in self.vulns)

    @property
    def parser(self):
        return self._parser

    @parser.setter
    def parser(self, parser):
        """Set the parser that will be used by PTP, while keeping internal state consistent."""
        self._auto = False  # Manual mode
        self._parser = parser


"""Functions used in system tests"""

from datetime import datetime

def templete_used(expected_templates, captured_templates):
    """Check if a list of templates have been used during the system tests."""

    assert expected_templates == [
        template.name for template, context in captured_templates
    ]

def convert_date_to_string(
    data: dict, data_input: str, data_format: str = "%Y-%m-%d"
) -> dict:
    """Convert input to string."""

    if data.get(data_input):
        value: datetime = data[data_input]
        data[data_input] = value.strftime(data_format)
    return data


month= int(input("What numerical month are you in? "))
day= int(input("What numerical day are you in? "))
ends_in_31 = [1,3,5,7,8,10,12]
ends_in_30 = [4,6,9,11]
february = 2

def moon_31(day):
    if day <= 3:
        print ("There is a New Moon that day")
    elif day > 3 and day < 7:
        print ("Waning Crescent")
    elif day >= 7 and day <= 11:
        print("Third Qtr")
    elif day > 11 and day <=15:
        print("Full Moon, Beware The Werewolves Are Out!!!")
    elif day > 15 and day <= 18:
        print("Waxing Gibbons")
    elif day > 18 and day <= 22:
        print("First Quarter")
    elif day > 22 and day <= 25:
        print("Waxing Crescent")
    else :
        print("New Moon")

def moon_30(day):
    if day <= 2:
        print("New Moon")
    elif day > 2 and day < 6:
        print("Waning Crescent ")
    elif day >= 6 and day <=10:
        print("Third Qtr")
    elif day > 10 and day <= 14:
        print("Full Moon, Beware The Werewolves Are Out!!!")
    elif day > 14 and day <= 17:
        print("Waxing Gibbons")
    elif day > 17 and day <= 21:
        print("First Quarter")
    elif day > 21 and day <= 24:
        print("Waxing Crescent")

def moon_feb(day):
    if  day > 3 and day < 7:
        print("Waning Crescent")
    elif day >= 7 and day <=9:
        print("Third Qtr")
    elif day > 9 and day <= 13:
        print("Full Moon, Beware The Werewolves Are Out!!!")
    elif day > 13 and day <= 16:
        print("Waxing Gibbons")
    elif day > 16 and day <= 20:
        print("First Quarter")
    elif day > 20 and day <= 23:
        print("Waxing Crescent")

if day == ends_in_31:
    moon_31(day)
elif day == ends_in_30:
    moon_30(day)
else:
    moon_feb(day)


from tkinter import*
from tkinter.ttk import Combobox
from tkcalendar import*
from PIL import*
root=Tk()
root.title('Bus Ticket Booking System V 2.0.7.7.0.3')
w=root.winfo_screenwidth()
h=root.winfo_screenwidth()
root.geometry("%dx%d+0+0"%(w,h))
#=====================top 3 common frames=====================

frame1=Frame(root,width=(w),height=100,bd=10,relief="raise")
frame1.pack(side=TOP,fill=BOTH)
frame2=Frame(root,width=(w),height=50,bd=5,relief="raise")
frame2.pack(side=TOP,fill=BOTH)
frame3=Frame(root,width=(w),height=500,bd=10,relief="raise")
frame3.pack(side=TOP,fill=BOTH)
userdasboard_frame=Frame(frame3,width=500,height=300)
userdasboard_frame.pack(side=LEFT,fill=BOTH)
userdaspic_frame=Frame(frame3,width=500,height=300,bd=10)
userdaspic_frame.pack(side=RIGHT)
#===================frame1-label================================
frame1_label=Label(frame1,text=" Bus Ticket Booking System\t\t\t",font=('Arial',40,'bold'),bg='green',fg='white').pack()
#===============frame2-buttons===============
frame2_button1=Button(frame2,text='Dasboard',font=('Arial',15,'bold'),bg='green',fg='white').grid(row=0,column=0)
frame2_button2=Button(frame2,text='Search Bus',font=('Arial',15,'bold'),bg='green',fg='white').grid(row=0,column=1)
frame2_button3=Button(frame2,text='My Booking',font=('Arial',15,'bold'),bg='green',fg='white').grid(row=0,column=2)
frame2_button4=Button(frame2,text='Logout',font=('Arial',15,'bold'),bg='green',fg='white').grid(row=0,column=3)
frame2_button5=Button(frame2,text='Contact',font=('Arial',15,'bold'),bg='green',fg='white').grid(row=0,column=4)

#=============search bus============
search_city_label=Label(userdasboard_frame,text='Search Bus',font=('Arial',20,'bold')).grid(row=0,column=0)
from_city_label=Label(userdasboard_frame,text='From city',font=('Arial',15,'bold')).grid(row=1,column=0,sticky=W)
to_city_label=Label(userdasboard_frame,text='To city',font=('Arial',15,'bold')).grid(row=2,column=0,sticky=W)
travel_city_datelabel=Label(userdasboard_frame,text='Travel Date',font=('Arial',15,'bold')).grid(row=3,column=0,sticky=W)
fbusdestination_cobobox=Combobox(userdasboard_frame,width=20,state='readonly',font=('arial',11,'bold'))
fbusdestination_cobobox['value']=('Birgunj','Kathmandu','Pokhara','Butwal','Kalaiya')
fbusdestination_cobobox.grid(row=1,column=1)
fbusdestination_cobobox.current(0)
tbusdestination_cobobox=Combobox(userdasboard_frame,width=20,state='readonly',font=('arial',11,'bold'))
tbusdestination_cobobox['value']=('Birgunj','Kathmandu','Pokhara','Butwal','Kalaiya')
tbusdestination_cobobox.grid(row=2,column=1)
tbusdestination_cobobox.current(1)

user_image=PhotoImage(file='avatar-3637561_640.png')
user_img_label=Label(userdaspic_frame,image=user_image).pack()

#==============calander============

def grab_date():
    my_label.config(text=cal.get_date())
my_button=Button(userdasboard_frame,text='Conform',padx=10,pady=5,font=("arial",10,"bold"),command=grab_date)
my_button.grid(row=4)
my_label=Label(userdasboard_frame,text="")
my_label.grid(row=5)
cal=Calendar(userdasboard_frame,selectmode='day',year=2020,month=6,command=grab_date)
cal.grid(row=3,column=1,sticky=W)

root.mainloop()


import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('localhost', 10001)
while True:
    message = input('Enter your message:\n')
    b_message = message.encode()

    try:
        print('sending "%s"' % message)
        sent = sock.sendto(b_message, server_address)

        print('waiting to receive')
        data, server = sock.recvfrom(4096)
        s_data = data.decode()
        print('from %s received:\n%s' % (server, s_data))

    finally:
        print('closing socket')
        sock.close


#!/usr/bin/env python3
#
#   hebinode.py
#
#   Create the HEBI node.  This sets up the HEBI connection, then
#   passes then HEBI feedback to the /joint_states topic, while
#   transfering the /joint_commands topic to the HEBIs.
#
#   It maps the HEBI actuator names to joint names according to the
#   ROS parameters:
#
#     family    Name of the HEBI actuator family ('robotlab')
#     motors    List of motor names (['9.3', '9.6', ...])
#     joints    List of joint names (['base', 'joint2', 'eblow', ...])
#     rate      HEBI Feedback rate in Hertz
#     lifetime  HEBI command lifetime in milliseconds
#
import hebi
import numpy as np
import rclpy

from time import sleep
from traceback import print_exc

from rclpy.node import Node
from rclpy.parameter import Parameter
from rcl_interfaces.msg import ParameterDescriptor
from rcl_interfaces.msg import ParameterType

from sensor_msgs.msg import JointState

#
#   Definitions
#
FAMILY = "robotlab"
RATE = 100.0  # Hertz
LIFETIME = 50.0  # ms

WATCHDOGDT = 0.2  # Watchdog Time step (seconds)

#
#   HEBI Node Class
#
class HebiNode(Node):
    # Initialization.
    def __init__(self, name):
        # Initialize the node, naming it as specified
        super().__init__(name)

    # Startup
    def startup(self):
        # Locate any HEBIs. Then grab the ROS parameters to confirm
        # which actuators to use for which joints.  Connect to them.
        # And finally set the feedback rate and command lifetime.
        self.hebilookup()
        self.readparameters()
        self.hebiconnect()
        self.hebirate(self.rate)
        self.hebilifetime(self.lifetime)

        # Feedback: Create a feedback message, a publisher to send the
        # joint states, and a HEBI feeedback handler to drive this.
        self.fbktime = self.get_clock().now()
        self.fbkmsg = JointState()
        self.fbkmsg.name = self.joints  # Set to the joint names

        self.pub = self.create_publisher(JointState, "/joint_states", 10)

        self.group.add_feedback_handler(self.feedbackCB)

        # Commands: Create a HEBI command structure and subscribe to
        # the joint command messages to drive this.
        self.cmd = hebi.GroupCommand(self.group.size)
        self.nans = np.full(self.N, np.nan)

        self.sub = self.create_subscription(
            JointState, "/joint_commands", self.commandCB, 10
        )

        # Finally create a watchdog timer to check the HEBI connection.
        self.watchdog = self.create_timer(WATCHDOGDT, self.watchdogCB)

        # raise Exception("End") # Test: end without spinning

    # Shutdown
    def shutdown(self):
        # Clear the HEBI feedback handler.  Then destroy the watchdog
        # timer, then shut down the node.
        self.group.clear_feedback_handlers()
        self.destroy_timer(self.watchdog)
        self.destroy_node()

    # Grab the ROS parameters.
    def readparameters(self):
        # Declare the parameters.
        STRING = ParameterDescriptor(type=ParameterType.PARAMETER_STRING)
        STRINGARRAY = ParameterDescriptor(type=ParameterType.PARAMETER_STRING_ARRAY)
        DOUBLE = ParameterDescriptor(type=ParameterType.PARAMETER_DOUBLE)
        self.declare_parameter("family", descriptor=STRING, value=FAMILY)
        self.declare_parameter("motors", descriptor=STRINGARRAY)
        self.declare_parameter("joints", descriptor=STRINGARRAY)
        self.declare_parameter("rate", descriptor=DOUBLE, value=RATE)
        self.declare_parameter("lifetime", descriptor=DOUBLE, value=LIFETIME)

        # Get the parameters.
        self.family = self.get_parameter("family").get_parameter_value().string_value
        self.motors = (
            self.get_parameter("motors").get_parameter_value().string_array_value
        )
        self.joints = (
            self.get_parameter("joints").get_parameter_value().string_array_value
        )
        self.rate = self.get_parameter("rate").get_parameter_value().double_value
        self.lifetime = (
            self.get_parameter("lifetime").get_parameter_value().double_value
        )
        self.N = len(self.joints)

        # Check the parameters for consistency.
        if len(self.motors) == 0:
            self.get_logger().error("No motors specified!")
            raise Exception("Inconsistent ROS parameters")
        if len(self.joints) == 0:
            self.get_logger().error("No joints specified!")
            raise Exception("Inconsistent ROS parameters")
        if len(self.motors) != len(self.joints):
            self.get_logger().error("Unequal number of joints/motors specified!")
            raise Exception("Inconsistent ROS parameters")

        # Report the parameters.
        self.get_logger().info("Selecting...")
        self.get_logger().info("HEBI family  '%s'" % self.family)
        for i in range(self.N):
            self.get_logger().info(
                "HEBI motor %ld '%s' = joint '%s'" % (i, self.motors[i], self.joints[i])
            )
        self.get_logger().info("HEBI update rate %3.0fHz" % self.rate)
        self.get_logger().info("HEBI command lifetime %3.0fms" % self.lifetime)

    # Locate the HEBI actuators
    def hebilookup(self):
        # Locate HEBI actuators on the network, waiting 1s for discovery.
        self.get_logger().info("Locating HEBIs...")
        self.lookup = hebi.Lookup()
        sleep(1)

        # Report the connected HEBIs.
        if sum(1 for _ in self.lookup.entrylist) == 0:
            self.get_logger().info("No HEBIs located.")
        else:
            for entry in self.lookup.entrylist:
                self.get_logger().info(
                    "Located family '%s' name '%s' at address %s"
                    % (entry.family, entry.name, entry.mac_address)
                )

    # Connect to the HEBI group to send commands/receive feedback.
    def hebiconnect(self):
        # Use the HEBI lookup to create the actuator group.
        self.group = self.lookup.get_group_from_names([self.family], self.motors)
        if self.group is None:
            self.get_logger().error("Unable to connect to selected motors!")
            self.get_logger().error("Make sure the motors are powered and connected.")
            self.get_logger().error("(for example using the Scope application)")
            raise Exception("No Motor Connection")
        else:
            self.get_logger().info("Connected to HEBIs.")

    # Set the HEBI command lifetime (in milliseconds).
    def hebilifetime(self, lifetime):
        if lifetime > 0.0:
            self.group.command_lifetime = lifetime

    # Set the HEBI feedback frequency (in Hertz).
    def hebirate(self, rate):
        if rate > 0.0:
            self.group.feedback_frequency = rate

    # HEBI feedback callback - send feedback on ROS message.
    def feedbackCB(self, fbk):
        # Grab the current time.
        self.fbktime = self.get_clock().now()

        # Build up the message and publish.  The joint names are preset.
        self.fbkmsg.header.stamp = self.fbktime.to_msg()
        self.fbkmsg.position = fbk.position.tolist()
        self.fbkmsg.velocity = fbk.velocity.tolist()
        self.fbkmsg.effort = fbk.effort.tolist()
        self.pub.publish(self.fbkmsg)

        # Reset the watchdog.
        self.watchdog.reset()

    # JointState message callback - send commands to HEBI.
    def commandCB(self, cmdmsg):
        # Check the message names matching the joint names.
        if not (cmdmsg.name == self.joints):
            self.get_logger().warn("Joint commands not matching joint names!")
            return

        # Check the command dimensions.
        l = self.N
        lp = len(cmdmsg.position)
        lv = len(cmdmsg.velocity)
        le = len(cmdmsg.effort)
        if (
            not (lp == 0 or lp == l)
            or not (lv == 0 or lv == l)
            or not (le == 0 or le == l)
        ):
            self.get_logger().warn("Illegal length of pos/vel/eff commands!")
            return

        # Copy the commands.
        self.cmd.position = self.nans if (lp == 0) else np.array(cmdmsg.position)
        self.cmd.velocity = self.nans if (lv == 0) else np.array(cmdmsg.velocity)
        self.cmd.effort = self.nans if (le == 0) else np.array(cmdmsg.effort)

        # And send.
        self.group.send_command(self.cmd)

    # Watchdog callback
    def watchdogCB(self):
        self.get_logger().warn("Not getting HEBI feedback - check connection")

#
#   Main Code
#
def main(args=None):
    # Initialize ROS.
    rclpy.init(args=args)

    # Instantiate the HEBI node.
    node = HebiNode("hebinode")

    # Run the startup and spin the node until interrupted.
    try:
        node.startup()
        rclpy.spin(node)
    except BaseException as ex:
        print("Ending due to exception: %s" % repr(ex))
        print_exc()

    # Shutdown the node and ROS.
    node.shutdown()
    rclpy.shutdown()

if __name__ == "__main__":
    main()


"""
Several specific types of lattices.
"""

from operator import le

from .constraints import is_antichain, is_connected, is_cover, is_partition
from .lattice import Lattice
from .orderings import antichain_le, refinement_le
from .utils import powerset

__all__ = [
    'powerset_lattice',
    'partition_lattice',
    'free_distributive_lattice',
    'dependency_lattice',
    'dependency_antichain_lattice',
    'partition_antichain_lattice',
    'free_modular_lattice',
    'M3',
    'N5',
]

def powerset_lattice(elements):
    """
    Construct the powerset lattice, representing all subsets of `elements`
    ordered by inclusion.

    Parameters
    ----------
    elements : collection
        The elements to use to construct the lattice.

    Returns
    -------
    lattice : Lattice
        The corresponding lattice.
    """
    return Lattice(powerset(elements), le)

def partition_lattice(elements):
    """
    Construct the partition lattice, representing all partitions of `elements`
    ordered by refinement.

    Parameters
    ----------
    elements : collection
        The elements to use to construct the lattice.

    Returns
    -------
    lattice : Lattice
        The corresponding lattice.
    """
    partitions = [part for part in powerset(powerset(elements, 1), 1) if is_partition(part, elements)]
    return Lattice(partitions, refinement_le(), symbols='|')

def free_distributive_lattice(elements):
    """
    Construct the free distributive lattice over `elements`, that is the lattice
    of antichains of the powerset of `elements`, ordered by containment.

    Parameters
    ----------
    elements : collection
        The elements to use to construct the lattice.

    Returns
    -------
    lattice : Lattice
        The corresponding lattice.
    """
    antichains = [ac for ac in powerset(powerset(elements, 1), 1) if is_antichain(ac)]
    return Lattice(antichains, antichain_le())

def dependency_lattice(elements, cover=True, connected=False):
    """
    Construct the lattice of antichains of the powerset of `elements`, ordered
    by refinement.

    Parameters
    ----------
    elements : collection
        The elements to use to construct the lattice.
    cover : bool
        Whether the antichains should be covers. Defaults to True.
    connected : bool
        Whether the antichains should represent a connected component. Defaults
        to False.

    Returns
    -------
    lattice : Lattice
        The corresponding lattice.
    """
    dependencies = [dep for dep in powerset(powerset(elements, 1)) if is_antichain(dep)]
    if cover:
        dependencies = [dep for dep in dependencies if is_cover(dep, elements)]
    if connected:
        dependencies = [dep for dep in dependencies if is_connected(dep)]
    return Lattice(dependencies, refinement_le(), '•꞉⋮')

def dependency_antichain_lattice(elements, cover=True, connected=False):
    """
    Construct the lattice of antichains of dependencies of the powerset of
    `elements`, ordered by containment.

    Parameters
    ----------
    elements : collection
        The elements to use to construct the lattice.
    cover : bool
        Whether the dependencies should be covers. Defaults to True.
    connected : bool
        Whether the dependencies should represent a connected component. Defaults
        to False.

    Returns
    -------
    lattice : Lattice
        The corresponding lattice.
    """
    dependencies = [dep for dep in powerset(powerset(elements, 1)) if is_antichain(dep)]
    if cover:
        dependencies = [dep for dep in dependencies if is_cover(dep, elements)]
    if connected:
        dependencies = [dep for dep in dependencies if is_connected(dep)]
    dependency_acs = [dep_ac for dep_ac in powerset(dependencies, 1) if is_antichain(dep_ac, refinement_le())]
    return Lattice(dependency_acs, antichain_le(refinement_le()))

def partition_antichain_lattice(elements):
    """
    Construct the lattice of antichains of partitions of the powerset of
    `elements`, ordered by refinement.

    Parameters
    ----------
    elements : collection
        The elements to use to construct the lattice.

    Returns
    -------
    lattice : Lattice
        The corresponding lattice.
    """
    partitions = [part for part in powerset(powerset(elements, 1), 1) if is_partition(part, elements)]
    partitions_acs = [part_ac for part_ac in powerset(partitions, 1) if is_antichain(part_ac, refinement_le())]
    return Lattice(partitions_acs, antichain_le(refinement_le()))

def free_modular_lattice(elements):
    """
    """
    raise NotImplementedError

################################################################################
# Some special lattices

nodes = {frozenset([0]),
         frozenset([1]),
         frozenset(['a']),
         frozenset(['b']),
         frozenset(['c']),
         }

def m3_order(a, b):
    """
    The smallest non-distributive lattice.
    """
    if a == {0} or b == {1}:
        return True
    else:
        return False

M3 = Lattice(nodes, m3_order)

def n5_order(a, b):
    """
    The smallest non-modular lattice.
    """
    if a == {0} or b == {1}:
        return True
    elif a == {'a'} and b == {'b'}:
        return True
    else:
        return False

N5 = Lattice(nodes, n5_order)


####################################
# Date : 2020-11-05                #
# mk01071@snu.ac.kr                #
####################################
# This is for drawing density of states.
import shutil, os, sys, subprocess, yaml
from module_log import *
from module_vasprun import *
from module_dos import *
from input_conf import set_on_off
from _version import __version__
code_data = 'Version '+__version__+'. Modified at 2020-11-05'

# Set input
dir = sys.argv[1]

inp_file = sys.argv[2]
with open(inp_file,'r') as f:
	inp_yaml = yaml.safe_load(f)
ERROR_path = inp_yaml['directory']['error']
src_path = inp_yaml['directory']['src_path']
vasp_std = inp_yaml['program']['vasp_std']
vasp_gam = inp_yaml['program']['vasp_gam']
vasp_ncl = inp_yaml['program']['vasp_ncl']
mpi = inp_yaml['program']['mpi_command']
gnuplot = inp_yaml['program']['gnuplot']
npar = inp_yaml['vasp_parallel']['npar']
kpar = inp_yaml['vasp_parallel']['kpar']
inp_dos = inp_yaml['density_of_states']

node = node_simple(sys.argv[3])
nproc = sys.argv[4]

pot_type = sys.argv[5]
if pot_type == 'LDA':
	POT = 'LDA'
else:
	POT = 'GGA'

# Set directory for input structure and INCAR
dir_dos = dir+'/dos_'+pot_type

# Check existing data
if os.path.isdir(dir_dos) and os.path.isfile(dir_dos+'/Pdos_dat/Tot_dos.dat') :
	make_amp2_log_default(dir,src_path,'DOS calculation with '+pot_type+' potential.',node,code_data)
	make_amp2_log(dir,'DOS calculation is already done.\n')
	print(1)
	sys.exit()

if not os.path.isdir(dir_dos):
	os.mkdir(dir_dos,0o755)

os.chdir(dir_dos)

make_amp2_log_default(dir_dos,src_path,'DOS calculation with '+pot_type+' potential.',node,code_data)

# check band or relax calculation
no_rlx = 0
if not os.path.isdir(dir+'/relax_'+pot_type):
	make_amp2_log(dir_dos,'Relax directory does not exist.')
	no_rlx = 1
else:
	if not os.path.isfile(dir+'/relax_'+pot_type+'/CONTCAR'):
		make_amp2_log(dir_dos,'CONTCAR file in relaxation does not exist.')
		no_rlx = 1
	elif count_line(dir+'/relax_'+pot_type+'/CONTCAR') < 9:
		make_amp2_log(dir_dos,'CONTCAR file in relaxation is invalid.')
		no_rlx = 1

if no_rlx == 1 and set_on_off(inp_dos['relax_check']) == 1:
	print(0)
	sys.exit()

dir_band = dir+'/band_'+pot_type

# Potential type
if pot_type == 'HSE':
	if no_rlx == 1:
		copy_input(dir+'/INPUT0',dir_dos,POT)
	else:
		copy_input_cont(dir+'/relax_'+pot_type,dir_dos)
	# make INCAR for CHGCAR
	incar_from_yaml(dir_dos,inp_dos['incar'])
	if no_rlx == 1:
		mag_on = 2
	else:
		mag_on = check_magnet(dir+'/relax_'+pot_type,inp_yaml['magnetic_ordering']['minimum_moment'])
	vasprun = make_incar_for_ncl(dir_dos,mag_on,kpar,npar,vasp_std,vasp_gam,vasp_ncl)

	wincar(dir_dos+'/INCAR',dir_dos+'/INCAR',[['NEDOS','3001'],['ISMEAR','-5'],['SIGMA','0.1']],[])

	incar_from_yaml(dir_dos,inp_dos['incar'])

	gam_option = 'gamma'
	if bool(inp_dos['incar']) and 'ISMEAR' in list(inp_dos['incar'].keys()) and inp_dos['incar']['ISMEAR'] > -3.5:
		gam_option =''
	with open(dir+'/INPUT0/sym','r') as symf:
		sym = int(symf.readline().split()[0])

	while 1:
		make_multiple_kpts(dir+'/kptest/kpoint.log',dir_dos+'/KPOINTS',dir_dos+'/POSCAR',inp_dos['kp_multiplier'],sym,gam_option)

		incar_for_hse(dir_dos+'/INCAR')
		hse_algo = pygrep('ALGO',dir+'/relax_'+pot_type+'/INCAR',0,0).split()[2]
		wincar(dir_dos+'/INCAR',dir_dos+'/INCAR',[['NSW','0'],['ALGO',hse_algo]],[])
		vasprun = vasp_std
		# VASP calculation
		out = run_vasp(dir_dos,nproc,vasprun,mpi)
		if out == 0:
			break
		if out == 1:  # error in vasp calculation
			if os.path.isfile(dir_dos+'/IBZKPT'):
				with open(dir_dos+'/IBZKPT','r') as f:
					num_kpts = int(f.readlines()[1].split()[0])
				if num_kpts < 4:
					inp_dos['kp_multiplier'] = inp_dos['kp_multiplier'] + 1
					make_amp2_log(dir_dos,'Tetrahedron method requires NKPT >= 4. AMP2 increases kp_multiplier to '+str(inp_dos['kp_multiplier'])+'.')
				else:
					print(0)
					sys.exit()
			else:
				print(0)
				sys.exit()

	out = electronic_step_convergence_check(dir_dos)
	while out == 1:
		make_amp2_log(dir_dos,'Calculation options are changed. New calculation starts.')
		out = run_vasp(dir_dos,nproc,vasprun,mpi)
		if out == 1:  # error in vasp calculation
			print(0)
			sys.exit()
		out = electronic_step_convergence_check(dir_dos)

	if out == 2:  # electronic step is not converged. (algo = normal)
		make_amp2_log(dir_dos,'The calculation stops but electronic step is not converged.')
		print(0)
		sys.exit()

else:
	# check CHGCAR
	if os.path.isfile(dir_dos+'/CHGCAR') and os.path.getsize(dir_dos+'/CHGCAR') > 0 :
		make_amp2_log(dir_dos,'DOS calculation is performed by using existing CHGCAR file.')
	elif os.path.isdir(dir_band) and os.path.isfile(dir_band+'/CHGCAR') and os.path.getsize(dir_band+'/CHGCAR') > 0 :
		make_amp2_log(dir_dos,'DOS calculation is performed by using existing CHGCAR file for band calculation.')
		# Copy input data and write CHGCAR
		if no_rlx == 1:
			copy_input(dir+'/INPUT0',dir_dos,POT)
		else:
			copy_input_cont(dir+'/relax_'+pot_type,dir_dos)
			# make INCAR for dos
			incar_from_yaml(dir_dos,inp_dos['incar'])
			mag_on = check_magnet(dir+'/relax_'+pot_type,inp_yaml['magnetic_ordering']['minimum_moment'])
			vasprun = make_incar_for_ncl(dir_dos,mag_on,kpar,npar,vasp_std,vasp_gam,vasp_ncl)

		subprocess.call(['cp',dir_band+'/CHGCAR',dir_dos+'/.'])
	else:
		make_amp2_log(dir_dos,'VASP calculation for CHGCAR file.')
		# Copy input data and write CHGCAR
		if no_rlx == 1:
			copy_input(dir+'/INPUT0',dir_dos,POT)
		else:
			copy_input_cont(dir+'/relax_'+pot_type,dir_dos)
		# make INCAR for CHGCAR
		incar_from_yaml(dir_dos,inp_dos['incar'])
		if no_rlx == 1:
			mag_on = 2
		else:
			mag_on = check_magnet(dir+'/relax_'+pot_type,inp_yaml['magnetic_ordering']['minimum_moment'])
		vasprun = make_incar_for_ncl(dir_dos,mag_on,kpar,npar,vasp_std,vasp_gam,vasp_ncl)
		wincar(dir_dos+'/INCAR',dir_dos+'/INCAR',[['NSW','0'],['LCHARG','.T.']],[])

		# VASP calculation for CHGCAR
		out = run_vasp(dir_dos,nproc,vasprun,mpi)
		if out == 1:  # error in vasp calculation
			print(0)
			sys.exit()
		make_amp2_log(dir_dos,'CHGCAR file is generated successfully.')

	wincar(dir_dos+'/INCAR',dir_dos+'/INCAR',[['NEDOS','3001'],['ISMEAR','-5'],['SIGMA','0.1'],["ALGO","Normal"]],[])

	incar_from_yaml(dir_dos,inp_dos['incar'])

	gam_option = 'gamma'
	if bool(inp_dos['incar']) and 'ISMEAR' in list(inp_dos['incar'].keys()) and inp_dos['incar']['ISMEAR'] > -3.5:
		gam_option =''
	with open(dir+'/INPUT0/sym','r') as symf:
		sym = int(symf.readline().split()[0])

	while 1:
		make_multiple_kpts(dir+'/kptest/kpoint.log',dir_dos+'/KPOINTS',dir_dos+'/POSCAR',inp_dos['kp_multiplier'],sym,gam_option)

		wincar(dir_dos+'/INCAR',dir_dos+'/INCAR',[['NSW','0'],['ICHARG','11'],['LCHARG','.F.']],[])
		if no_rlx == 1:
			mag_on = 2
		else:
			mag_on = check_magnet(dir+'/relax_'+pot_type,inp_yaml['magnetic_ordering']['minimum_moment'])
		vasprun = make_incar_for_ncl(dir_dos,mag_on,kpar,npar,vasp_std,vasp_gam,vasp_ncl)
		# VASP calculation
		out = run_vasp(dir_dos,nproc,vasprun,mpi)
		if out == 0:
			break
		if out == 1:  # error in vasp calculation
			if os.path.isfile(dir_dos+'/IBZKPT'):
				with open(dir_dos+'/IBZKPT','r') as f:
					num_kpts = int(f.readlines()[1].split()[0])
				if num_kpts < 4:
					inp_dos['kp_multiplier'] = inp_dos['kp_multiplier'] + 1
					make_amp2_log(dir_dos,'Tetrahedron method requires NKPT >= 4. AMP2 increases kp_multiplier to '+str(inp_dos['kp_multiplier'])+'.')
				else:
					print(0)
					sys.exit()
			else:
				print(0)
				sys.exit()

	out = electronic_step_convergence_check(dir_dos)
	while out == 1:
		make_amp2_log(dir_dos,'Calculation options are changed. New calculation starts.')
		out = run_vasp(dir_dos,nproc,vasprun,mpi)
		if out == 1:  # error in vasp calculation
			print(0)
			sys.exit()
		out = electronic_step_convergence_check(dir_dos)

	if out == 2:  # electronic step is not converged. (algo = normal)
		make_amp2_log(dir_dos,'The calculation stops but electronic step is not converged.')
		print(0)
		sys.exit()

# set fermi level
fermi = float(pyhead(dir_dos+'/DOSCAR',6).splitlines()[-1].split()[3])
gap = 0
if os.path.isdir(dir_band) and os.path.isfile(dir_band+'/Band_gap.log'):
	if not pyhead(dir_band+'/Band_gap.log',1,).split()[2] == 'is' :
		fermi = float(pygrep('VBM',dir_band+'/Band_gap.log',0,0).splitlines()[0].split()[-2])
		gap = round(float(pyhead(dir_band+'/Band_gap.log',1).split()[2]))
spin = pygrep('ISPIN',dir_dos+'/OUTCAR',0,0).split()[2]
ncl = pygrep('NONCOL',dir_dos+'/OUTCAR',0,0).split()[2]
# read atom information from poscar
[atom_name,atom_num] = poscar_to_atom_inform(dir_dos+'/POSCAR')
# read dos information from doscar
[Ene,Tot_dos,par_dos] = make_dos_dat(dir_dos+'/DOSCAR',spin,atom_num,ncl)

# write dos dat files
write_tot_dos(Ene,Tot_dos,fermi,dir_dos)
write_par_dos(Ene,par_dos,atom_name,fermi,dir_dos)

make_dos_in(dir_dos,atom_name,spin,len(par_dos[0][0]),[inp_dos['y_min'],inp_dos['y_max']+gap])

if not os.path.isfile(inp_yaml['program']['gnuplot']):
    make_amp2_log(dir_dos,'If you want to draw figure, please check the path of gnuplot.')
if inp_yaml['calculation']['plot'] == 1:
	os.chdir(dir_dos+'/Pdos_dat')
	try:
		subprocess.call([gnuplot,dir_dos+'/Pdos_dat/dos.in'])
	except:
		make_amp2_log(dir_dos,'Error occured drawing figure. Please check gnuplot.')

make_amp2_log(dir_dos,'DOS calculation is done.')

with open(dir_dos+'/amp2.log','r') as amp2_log:
	with open(dir+'/amp2.log','a') as amp2_log_tot:
		amp2_log_tot.write(amp2_log.read())
print(1)


import cv2
import numpy as np
from math import sqrt
image = cv2.imread("swirl.jpg")

'''
gx = cv2.Sobel(image, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(image, cv2.CV_32F, 0, 1)
weight, angle = cv2.cartToPolar(gx, gy)

print(weight[100])
print(angle[100])

sum_angle  = np.sum(angle)
sum_weight = np.sum(weight)

print(sum_angle)
print(sum_weight)
'''
rows, cols, ch = image.shape
swirl = image.copy()
swirl_y = int(rows/2)
swirl_x = int(cols/2)

for k in range(ch):
	for y in range(rows):
		for x in range(cols):
			pixel_x 	= x - swirl_x
			pixel_y 	= y - swirl_y
			pixel_dist 	= sqrt(pixel_x**2 + pixel_y**2)


import os

def read_input(input_filename):
    with open(input_filename) as input_file:
        input_split_lines = [int(item.rstrip()) for item in input_file.readlines()]
    return input_split_lines

def main(input_lines):
    counter = 0
    prev_sum = sum(input_lines[0:3]) + 1
    for item1, item2, item3 in zip(input_lines[:-2],
                                   input_lines[1:-1],
                                   input_lines[2:]):
        if sum([item1, item2, item3]) > prev_sum:
            counter += 1
        prev_sum = sum([item1, item2, item3])

    return counter

def functional(input_lines):
    counter = sum([item for item in map(lambda x, y: x < y,
                                        input_lines[:-3],
                                        input_lines[3:])])
    return counter

if __name__ == "__main__":
    input_split_by_lines = read_input(os.path.join(os.path.dirname(__file__), 'input.txt'))
    print(main(input_lines=input_split_by_lines))
    print(functional(input_lines=input_split_by_lines))


import json
from datetime import datetime
from http import HTTPStatus
from time import sleep

import pytest
from exchange.config import settings
from exchange.data_base import create_session
from exchange.models import User
from flask import Response
from flask.testing import FlaskClient

def test_add_user(client: FlaskClient, user_login):
    response: Response = client.post(
        '/users/',
        data=json.dumps(dict(login=user_login)),
        content_type='application/json',
    )
    assert response.status_code == HTTPStatus.CREATED
    with create_session() as session:
        res: User = session.query(User).filter(User.id == 1).first()
        assert res.money == settings.start_money
        assert res.login == user_login

@pytest.mark.usefixtures('_add_user')
def test_get_user(client: FlaskClient, user_login):
    response: Response = client.get('/users/1/')
    assert response.status_code == HTTPStatus.OK
    assert response.json['login'] == user_login

@pytest.mark.usefixtures('_add_user', '_add_currency')
def test_buy_currency(client: FlaskClient):
    sleep(1)
    response: Response = client.post(
        '/users/1/currencies/',
        data=json.dumps(
            dict(
                currency_id=1,
                operation='BUY',
                amount='1',
                time=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),
            )
        ),
    )
    assert response.status_code == HTTPStatus.CREATED
    assert response.json['amount'] == '1.00000'

@pytest.fixture()
def _buy_currency(client):
    sleep(1)
    client.post(
        '/users/1/currencies/',
        data=json.dumps(
            dict(
                currency_id=1,
                operation='BUY',
                amount='1',
                time=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),
            )
        ),
    )

@pytest.mark.usefixtures('_add_user', '_add_currency', '_buy_currency')
def test_get_user_operations(client: FlaskClient):
    response: Response = client.get('/users/1/operations/')
    assert response.status_code == HTTPStatus.OK
    response_json = response.json
    assert len(response_json) == 1
    assert response_json[0]['amount'] == '1.00000'
    assert response_json[0]['operation_type'] == 'BUY'

@pytest.mark.usefixtures('_add_user', '_add_currency', '_buy_currency')
def test_get_user_currencies(client: FlaskClient):
    response: Response = client.get('/users/1/currencies/')
    assert response.status_code == HTTPStatus.OK
    response_json = response.json
    assert len(response_json) == 1
    assert response_json[0]['currency_id'] == 1
    assert response_json[0]['amount'] == '1.00000'


import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
from filters import sav_golay
import numpy as np
from numpy.testing import assert_array_equal
import pytest

@pytest.fixture
def my_data():
	data = np.array([[100, 1000, 2000, 3000],
				[200, 1500, 500, 2500],
				[300, 1700, 200, 2200],
				[400, 1800, 100, 2000],
				[500, 1900, 20, 1700]
	])
	return data

# Checks that a change in the window parameter of the golay filter changes the result
def test_golay_params(my_data):
	given = sav_golay.golay(my_data, 3, 1)
	expected = sav_golay.golay(my_data, 5, 1)
	with pytest.raises(AssertionError):
		assert_array_equal(given, expected)

# Checks that the input and output data are of the same length
def test_golay_length(my_data):
	given = len(my_data)
	expected = len(sav_golay.golay(my_data, 3, 1))
	assert given == expected

# Checks that if you input an odd integer for window lenght it raises an error
def test_golay_parmerrors(my_data):
	with pytest.raises(Exception):
		result = sav_golay.golay(my_data, 2, 1)

# Checks that if you input an integer for the polynomial parameter equal or larger than the window lenght ir raises
# an error
def test_golay_parmerrors(my_data):
	with pytest.raises(Exception):
		result = sav_golay.golay(my_data, 3, 3)


import re
from bs4 import BeautifulSoup

def main():
	with open('test-html.html') as html_f:
		soup = BeautifulSoup(html_f, 'lxml')
		for element in soup.find_all('a', text=re.compile(
            r"staff|faculty|directory|employee", re.IGNORECASE)):
			try:
				print(element['href'])
			except KeyError:
				print('Error: href attribute not found')

if __name__ == "__main__":
    main()


import math

class UncertaintyProcessor:
    def __init__(self, ens, h, w, data):
        self.ens = ens
        self.h = h
        self.w = w
        self.data = data

    def ProcessUnVolume(self, x, y, level):
        unResult = {}
        unResult['mean'] = 0
        unResult['var'] = 0

        level = pow(2, level) / 2

        left = int(x - level < 0 and 0 or (x - level))
        right = int(x + level >= self.w and (self.w - 1) or (x + level))
        bottom = int(y - level < 0 and 0 or (y - level))
        top = int(y + level >= self.h and (self.h - 1) or (y + level))

        for h in range(bottom, top + 1):
            for w in range(left, right + 1):
                for ens in range(0, self.ens):
                    unResult['mean'] += self.data[ens][h][w]
        nodeCount = (right - left + 1) * (top - bottom + 1) * self.ens
        unResult['mean'] /= nodeCount

        for h in range(bottom, top + 1):
            for w in range(left, right + 1):
                for ens in range(0, self.ens):
                    unResult['var'] += math.pow(self.data[ens][h][w] - unResult['mean'], 2)
        unResult['var'] = math.sqrt(unResult['var'] / (nodeCount * self.ens))
        return unResult


import sys
import importlib
import time
import json
from typing import List
from dataclasses import dataclass

@dataclass
class ChatGPTConfig:
    model: str = 'gpt-3.5-turbo'
    api_key: str = ''
    completion_tokens: int = 0
    prompt_tokens: int = 0
    total_tokens: int = 0

class ChatGPTForConvLM:

    def __init__(self, config: ChatGPTConfig) -> None:
        self.config = config
        self.last_response = None

        name = 'openai'
        if name in sys.modules:
            self.openai = importlib.reload(sys.modules[name])
        else:
            self.openai = importlib.import_module(name)
        self.openai.api_key = self.config.api_key

    def __str__(self) -> str:
        return f"{self.__class__.__name__}({str(self.config)})"

    def __repr__(self) -> str:
        return self.__str__()

    # https://platform.openai.com/docs/guides/gpt/chat-completions-api
    def generate(self,
                 conv:list,
                 system:str='',
                 **kwargs
                 )->List[str]:
        messages = []
        if system:
            messages.append({'role': 'system', 'content': system})
        for i, turn in enumerate(conv):
            role = 'user' if i % 2 == 0 else 'assistant'
            messages.append({'role': role, 'content': turn})

        response :dict = self.openai.Completion.create(
            model=self.config.model,
            messages=messages,
            **kwargs
        )
        self.last_response = response

        # save usage
        self.config.completion_tokens += response['usage']['completion_tokens']
        self.config.prompt_tokens += response['usage']['prompt_tokens']
        self.config.total_tokens += response['usage']['total_tokens']

        replys = []
        for choice in response['choices']:
            reason = choice['finish_reason']
            # skip abnormal completion
            if reason not in ('stop', 'length', 'function_call'):
                continue

            # index = choice['index']
            content = choice['message']['content']
            replys.append(content)

        return replys

    def generate_retry(self,
                        max_tries=3,
                        delay=5,
                        **generate_kwargs):
        ret = []
        for trial in range(max_tries):
            try:
                ret = self.generate(**generate_kwargs)
            except Exception as e:
                if 'RateRateLimitError' in repr(e):
                    time.sleep(delay)
                    continue
                else:
                    raise e
            else:
                # abnormally no response
                if not ret:
                    break

        return ret

    def save_json(self, save_path:str):
        with open(save_path, 'w', encoding='utf-8') as f:
            json.dump(self.config, f, ensure_ascii=False, indent=4)

    @staticmethod
    def from_json(json_path:str):
        with open(json_path, 'r', encoding='utf-8') as f:
            config = json.load(f)
        obj = ChatGPTForConvLM(config=config)
        return obj


import pygame

from random import randint
from src.util.constants import SLIME
from src.component.obstacle.obstacle import Obstacle

class Slime(Obstacle):
    def __init__(self):
        images = SLIME
        index = randint(0, len(images)-1)
        super().__init__(images, index)
        self.rect.y = 330
        self.sprite_index = 0

    def draw(self, display):
        self.image = self.images[self.sprite_index % len(self.images)].convert_alpha()
        self.image = pygame.transform.scale(self.image, (50, 50))
        self.obstacle_mask = pygame.mask.from_surface(self.image)
        self.mask = self.obstacle_mask.to_surface(unsetcolor=(0, 0, 0, 0))
        display.blit(self.image, self.rect)
        self.sprite_index += 1


# this file provides functions for drawing chaos tokens.
# coding=utf-8

from json import load
from random import choice
campaign_settings = load(open('campaignsettings.json'))

class Token():
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return "Token({})".format(self.value)

    def __str__(self):
        if type(self.value) is int:
            return str(self.value)
        else:
            return Token.to_symbol(self.value)

    def __eq__(self, other):
        return self.value == other.value if isinstance(other, Token) else False

    @staticmethod
    def to_symbol(string):
        dictionary = {
            'skull':'💀',
            'cult':'🧛',
            'tentacles':'🦑',
            'stone':'🍞',
            'old one':'🐲',
            'elder sign':'🌟'
        }
        string = string.lower()
        if string in dictionary:
            return dictionary[string]
        else:
            return string

class Bag():
    def __init__(self, campaign=None, difficulty=None, tokens=None):
        if difficulty == None or campaign == None:
            if tokens == None:
                tokens = []
            self.tokens = tokens
        else:
            campaign, difficulty = campaign.lower(), difficulty.lower()
            if campaign not in campaign_settings:
                raise Error("invalid campaign")
            elif difficulty not in ["easy", "standard", "hard", "expert"]:
                raise Error("invalid difficulty")
            else:
                self.tokens = [Token(value) for value in campaign_settings[campaign][difficulty]]

    def __repr__(self):
        return "Bag({})".format(repr(self.tokens))

    def __str__(self):
        return "Bag({})".format(",".join([str(token) for token in self.tokens]))

    def draw(self):
        return(str(choice(self.tokens)))

    def add(self, value):
        self.tokens.append(Token(value))

    def remove(self, value):
        self.tokens.remove(Token(value))

if __name__ == '__main__':
    # run some tests:
    bag = Bag()
    print("Empty bag: " + str(bag))
    bag = Bag([])
    print("Empty bag: " + str(bag))
    bag = Bag("night of the zealot", "easy")
    print("Night of the Zealot easy bag: " + str(bag))
    bag = Bag("the dunwich legacy", "standard")
    print("The Dunwich Legacy standard bag: " + str(bag))
    bag.add('old one')
    bag.remove('tentacles')
    print("with old one but not tentacles: " + str(bag))


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
get_ipython().run_line_magic('matplotlib', 'inline')

# ## 1. Exploring data

# df = pd.read_csv('my_data.csv')
#
# df.head()
#
# df.info()
#
# df.columns
#
# df.describe()
#
# df.column.value_counts()
#
# df.column.plot('hist')

# In[2]:

import zipfile

with zipfile.ZipFile("dob_job_application_filings_subset.csv.zip") as z:
    with z.open("dob_job_application_filings_subset.csv") as f:
# Read the file into a DataFrame: df
        df = pd.read_csv(f, low_memory=False)

# Print the shape of df
print(df.shape)

# Print the columns of df
print(df.columns)

# In[3]:

# Print the info of df
print(df.info())

# In[4]:

# Print the value counts for 'Borough'
print(df['Borough'].value_counts(dropna=False))

# In[5]:

# Describe the column
df['Existing Zoning Sqft'].describe()

# In[6]:

# Plot the histogram
df['Existing Zoning Sqft'].plot(kind='hist', rot=70, logx=True, logy=True)

# Display the histogram
plt.show()

# ## 2. Tidying data for analysis

# In[7]:

airquality = pd.read_csv('airquality.csv')
airquality.head()

# In[8]:

airquality_melt = pd.melt(airquality, id_vars=['Month', 'Day'], var_name='measurement', value_name='reading')
airquality_melt.head()

# In[9]:

airquality_pivot = airquality_melt.pivot_table(index=['Month','Day'], columns='measurement', values='reading')
airquality_pivot.head()

# In[10]:

airquality_pivot = airquality_melt.pivot_table(index=['Month', 'Day'], columns='measurement', values='reading', aggfunc=np.mean)

# Reset the index of airquality_pivot
airquality_pivot = airquality_pivot.reset_index()

# Print the head of airquality_pivot
print(airquality_pivot.head())

# In[11]:

tb = pd.read_csv('tb.csv')
# Melt tb: tb_melt
tb_melt = pd.melt(frame = tb, id_vars=['country','year'])

# Create the 'gender' column
tb_melt['gender'] = tb_melt.variable.str[0]

# Create the 'age_group' column
tb_melt['age_group'] = tb_melt.variable.str[1:]

# Print the head of tb_melt
print(tb_melt.head())

# In[12]:

ebola = pd.read_csv('ebola.csv')

# In[13]:

# Melt ebola: ebola_melt
ebola_melt = pd.melt(ebola, id_vars=['Date', 'Day'], var_name='type_country', value_name='counts')

# Create the 'str_split' column
ebola_melt['str_split'] = ebola_melt.type_country.str.split('_')

# Create the 'type' column
ebola_melt['type'] = ebola_melt['str_split'].str.get(0)

# Create the 'country' column
ebola_melt['country'] =ebola_melt['str_split'].str.get(1)

# Print the head of ebola_melt
print(ebola_melt.head())

# ## 3. Combining data for analysis
# pd.merge(df1, df2, ...)
#
# pd.concat([df1, df2, df3, ...])

# In[14]:

# Write the pattern: pattern
pattern = '*.csv'

# Save all file matches: csv_files
csv_files = glob.glob(pattern)

# Print the file names
print(csv_files)

# Load the second file into a DataFrame: csv2
csv2 = pd.read_csv(csv_files[1])

# Print the head of csv2
print(csv2.head())

# In[15]:

# Create an empty list: frames
frames = []

#  Iterate over csv_files
for csv in csv_files:

    #  Read csv into a DataFrame: df
    df = pd.read_csv(csv)

    # Append df to frames
    frames.append(df)

# Concatenate frames into a single DataFrame: uber
uber = pd.concat(frames, sort=False)

# Print the shape of uber
print(uber.shape)

# Print the head of uber
print(uber.head())

# ## 4. Cleaning data for analysis
# def cleaning_function(row_data):
#
#     data cleaning steps
#
#     return ...
#
# df.apply(cleaning_function, axis=1)
#
# assert (df.column_data > 0).all()

# In[16]:

tips = pd.read_csv('tips.csv')
tips.sample(5)

# ### Converting data types
# df.dtypes
#
# df['column'] = df['column'].to_numeric()
#
# df['column'] = df['column'].astype(str)

# In[17]:

# Convert the sex column to type 'category'
tips.sex = tips.sex.astype('category')

# Convert the smoker column to type 'category'
tips.smoker = tips.smoker.astype('category')

# Print the info of tips
print(tips.info())

# In[18]:

# Convert 'total_bill' to a numeric dtype
tips['total_bill'] = pd.to_numeric(tips['total_bill'], errors='coerce')

# Convert 'tip' to a numeric dtype
tips['tip'] = pd.to_numeric(tips['tip'], errors = 'coerce')

# Print the info of tips
print(tips.info())

# ### apply

# In[19]:

# Define recode_gender()
def recode_gender(gender):

    # Return 0 if gender is 'Female'
    if gender == 'Female':
        return 0

    # Return 1 if gender is 'Male'
    elif gender == 'Male':
        return 1

    # Return np.nan
    else:
        return np.nan

# Apply the function to the sex column
tips['recode'] = tips['sex'].apply(recode_gender)

# Print the first five rows of tips
tips.head()

# #### Write the lambda function using replace
# tips['total_dollar_replace'] = tips.total_dollar.apply(lambda x: x.replace('$', ''))
#
# #### Write the lambda function using regular expressions
# tips['total_dollar_re'] = tips.total_dollar.apply(lambda x: re.findall('\d+\.\d+', x)[0])

# ### re

# In[20]:

# Import the regular expression module
import re

# Compile the pattern: prog
prog = re.compile('\d{3}-\d{3}-\d{4}')

# See if the pattern matches
result = prog.match('123-456-7890')
print(bool(result))

# See if the pattern matches
result2 = prog.match('1123-456-7890')
print(bool(result2))

# In[21]:

# Import the regular expression module
import re

# Find the numeric values: matches
matches = re.findall('\d+', 'the recipe calls for 10 strawberries and 1 banana')

# Print the matches
print(matches)

# In[22]:

# Write the first pattern
pattern1 = bool(re.match(pattern='\d{3}-\d{3}-\d{4}', string='123-456-7890'))
print(pattern1)

# Write the second pattern
pattern2 = bool(re.match(pattern='\$\d*\.\d{2}', string='$123.45'))
print(pattern2)

# Write the third pattern
pattern3 = bool(re.match(pattern='[A-Z]\w*', string='Australia'))
print(pattern3)

# ### Dropping duplicate data

# #### Create the new DataFrame: tracks
# tracks = billboard[['year', 'artist', 'track', 'time']]
#
# #### Drop the duplicates: tracks_no_duplicates
# tracks_no_duplicates = tracks.drop_duplicates()

# ### Filling missing data

# In[23]:

# Calculate the mean of the Ozone column: oz_mean
oz_mean = airquality['Ozone'].mean()

# Replace all the missing values in the Ozone column with the mean
airquality['Ozone'] = airquality['Ozone'].fillna(oz_mean)

# Print the info of airquality
print(airquality.info())

# ### Testing your data with asserts

# #### Assert that there are no missing values
# assert ebola.notnull().all().all()
#
# #### Assert that all values are >= 0
# assert (ebola >= 0).all().all()

# ## 5. Case Study

# In[24]:

gapminder = pd.read_csv('gapminder.csv')
gapminder.drop('Unnamed: 0', axis = 1, inplace = True)
gapminder.head()

# In[25]:

# Melt gapminder: gapminder_melt
gapminder_melt = pd.melt(gapminder, id_vars = 'Life expectancy')

# # Rename the columns
gapminder_melt.columns = ['country', 'year', 'life_expectancy']

# Print the head of gapminder_melt
print(gapminder_melt.head())

# In[26]:

# Convert the year column to numeric
gapminder_melt.year = pd.to_numeric(gapminder_melt.year)

# Test if country is of type object
assert gapminder_melt.country.dtypes == np.object

# Test if year is of type int64
assert gapminder_melt.year.dtypes == np.int64

# Test if life_expectancy is of type float64
assert gapminder_melt.life_expectancy.dtypes == np.float64

# In[27]:

# Create the series of countries: countries
countries = gapminder_melt.country

# Drop all the duplicates from countries
countries = countries.drop_duplicates()

# Write the regular expression: pattern
pattern = '^[A-Za-z\.\s]*$'

# Create the Boolean vector: mask
mask = countries.str.contains(pattern)

# Invert the mask: mask_inverse
mask_inverse = ~mask

# Subset countries using mask_inverse: invalid_countries
invalid_countries = countries.loc[mask_inverse]

# Print invalid_countries
print(invalid_countries)

# In[28]:

# Assert that country does not contain any missing values
assert pd.notnull(gapminder_melt.country).all()

# Assert that year does not contain any missing values
assert pd.notnull(gapminder_melt.year).all().all()

# Drop the missing values
gapminder_melt = gapminder_melt.dropna()

# Print the shape of gapminder
print(gapminder_melt.shape)

# In[29]:

# Add first subplot
plt.subplot(2, 1, 1)

# Create a histogram of life_expectancy
gapminder_melt.life_expectancy.plot(kind = 'hist')

# Group gapminder: gapminder_agg
gapminder_agg = gapminder_melt.groupby('year')['life_expectancy'].mean()

# Print the head of gapminder_agg
print(gapminder_agg.head())

# Print the tail of gapminder_agg
print(gapminder_agg.tail())

# Add second subplot
plt.subplot(2, 1, 2)

# Create a line plot of life expectancy per year
gapminder_agg.plot()

# Add title and specify axis labels
plt.title('Life expectancy over the years')
plt.ylabel('Life expectancy')
plt.xlabel('Year')

# Display the plots
plt.tight_layout()
plt.show()

# Save both DataFrames to csv files
gapminder_melt.to_csv('gapminder_melt.csv')
gapminder_agg.to_csv('gapminder_agg.csv')

# In[ ]: