import plotly
import plotly.io as pio
import plotly.graph_objects as go
import pandas as pd
from plotly.subplots import make_subplots

# list of default Plotly colors to color a trace with
DEFAULT_PLOTLY_COLORS=['rgb(31, 119, 180)', 'rgb(255, 127, 14)',
                       'rgb(44, 160, 44)', 'rgb(214, 39, 40)',
                       'rgb(148, 103, 189)', 'rgb(140, 86, 75)',
                       'rgb(227, 119, 194)', 'rgb(127, 127, 127)',
                       'rgb(188, 189, 34)', 'rgb(23, 190, 207)']

class graph_test() :
  # Constructor for routes.py to send/receive data
  #   from this program
  def __init__( self, state_list, feature1, feature2) :
    # Stores the states they want to display
    self.states = state_list

    # Stores the first feature user wants to display
    self.f1 = feature1

    # Stores the first feature user wants to display
    self.f2 = feature2
#-------------------------------------------------------------

def main() :
  # Write a list of states to see the Covid data of
  state_list = ['TX', 'FL', 'NJ']

  # Write the name first feature you want to find the data of
  feature1 = 'New Cases'

  # Write the name second feature you want to find the data of
  feature2 = 'New Deaths'

  #-------------------------------------------------------------------------------------
  # Initialize the nGraph object by calling the graph_test constructor
  nGraph = ''

  # Call the feature_graph() function below to create a Graph figure to display
  fig = ''

  # Uncomment line below once the fig object is returned from feature_graph()
  #pio.write_html(fig, file='workshop_graph.html', auto_open=False)
  #-------------------------------------------------------------------------------------

#-------------------------------------------------------------
def feature_graph( states, feature1, feature2 ) :

  #------------------------------------------------------------------------------------
  # Read CSV file generated from Task 1 using the Pandas library
  df = ''
  #------------------------------------------------------------------------------------

  #------------------------------------------------------------------------------------
  # Count the number of days that are available for each state
  # Rather than count it manually, use some of Pandas' built-in functions
  numDays = 0
  #------------------------------------------------------------------------------------

  # frames should correlate to the number of days that pass
  num_frames = numDays

  # lines that appear on the graph, should be one per state chosen
  num_traces = []

  #------------------------------------------------------------------------------------
  # Uncomment line below and convert the Date column in the CSV from a Datetime object
  #   to a String object in the form of mm-dd-YYYY format using Pandas
  #df['Date'] = ''
  #------------------------------------------------------------------------------------

  # stores the rows where each state is first used in the CSV file
  axis = []

  # finds the index for which row the state appears for the first time in the CSV
  strt_idx = 0

  for i in range(len(states)) :
    # this value will be used to determine how many lines to draw when the
    #   graph beginning to be generated
    num_traces.append( int(i) )

    # Store the indexes at which the state first appears in the CSV file
    axis.append(int(strt_idx))

    strt_idx = strt_idx + numDays + 1

  # creates a figure that will store 2 subgraphs, right next to each other
  fig = make_subplots(rows=1, cols=2, horizontal_spacing = 0.075,
                      specs=[[{"secondary_y": True}, {"secondary_y": True}]])

  # only show the state name at the far right of the drawn line (i.e. trace)
  all_text = []

  # all the data points will have nothing written on the other points other than
  #   the most recent data point
  for k in range(numDays) :
    all_text.append('')

  for j in range(len(axis)) :
    # represents the index at each iteration, used for convenience
    cur_idx = axis[j]

    #------------------------------------------------------------------------------------
    # Update x and y values read from a CSV and store those values
    #
    # x should be the dates chosen
    # y1 and y2 should be the values for each feature on that day
    x  = 0
    y1 = 0
    y2 = 0
    #------------------------------------------------------------------------------------

    # Create a way to only write the state's initials at the final
    #   data point and not on each data point, as is done here
    all_text.append(states[j])

    # x & y axis values for the first graph (on the left)
    fig.add_trace(go.Scatter(x = x, y = y1, name=states[j],
                             legendgroup=str(j),
                             marker=dict(color=DEFAULT_PLOTLY_COLORS[j]),
                             mode='lines+text',
                             text=all_text,
                             textposition='middle right'),
                  row=1, col=1, secondary_y=False)

    # x & y axis values for the second graph (on the right)
    fig.add_trace(go.Scatter(x = x, y = y2, name=st[j],
                             legendgroup=str(j),
                             marker=dict(color=DEFAULT_PLOTLY_COLORS[j]),
                             mode='lines+text',
                             text=all_text,
                             textposition='middle right',
                             showlegend=False),
                  row=1, col=2, secondary_y=False)

    # remove the name at the end of the list to make way for the name state chosen
    all_text.pop()

  # data is a parameter for Plotly's Frame object
  # it will determine what do draw from the given range in the dataset
  frames = []

  # Update the number of traces by two since there are two graphs
  for i in range(len(num_traces), len(num_traces)*2) :
    num_traces.append( int(i) )

  # want to create enough frames for all the users
  for i in range(num_frames+1) :
    # data is a parameter for Plotly's Frame object
    # it will storedata points what to draw from the dataset
    day_data = []

    for j in range(len(states)) :
      cur_idx = axis[j]

      #------------------------------------------------------------------------------------
      # Update x and y values read from a CSV and store those values
      #
      # x should be the dates chosen
      # y1 and y2 should be the values for each feature on that day
      x  = 0
      y1 = 0
      y2 = 0
      #------------------------------------------------------------------------------------

      day_data.append(go.Scatter(x=x, y=y1,visible=True))
      day_data.append(go.Scatter(x=x, y=y2,visible=True))

    # want to make an list of lists to store the different data ranges
    #   to draw from frame to frame
    if i == 0 :
      frames.append(go.Frame(data=None,
                             traces=num_traces))

    # Plotly will take drawing the dot as the first line of input
    # This will not be considered, we want to start with a line already drawn
    else :
      frames.append(go.Frame(data=day_data,
                             traces=num_traces))

  #------------------------------------------------------------------------------------
  # Create an array of date values from the start date up
  #   and an end date that is numDays after the fact
  dates = []
  #------------------------------------------------------------------------------------

  steps = []
  for i in range(num_frames+1):
    step = dict(
      method="animate",

      #--------------------------------------------------------------------------------
      # Update ticks on slider to print out values from dates array
      label=i,
      #--------------------------------------------------------------------------------

      args = [frames[i], dict(frame=dict(duration=500, redraw=False),
                         transition=dict(duration=0),
                         easing='linear',
                         fromcurrent=True,
                         mode='immediate')]
    )
    steps.append(step)

  # Creates button objects to be interactive with the slider
  play_button = dict(label='Play',
                     method='animate',
                     args=[None, dict(frame=dict(duration=0, redraw=False),
                                          transition=dict(duration=0),
                                          easing='linear',
                                          fromcurrent=True,
                                          mode='immediate')])

  pause_button = dict(label='Pause',
                      method='animate',
                      args=[None, dict(frame=dict(duration=0, redraw=False),
                                       transition=dict(duration=0),
                                       mode='immediate')])

  sliders = [dict(yanchor = 'top',
                  xanchor = 'left',
                  currentvalue = {'font': {'size': 15},'prefix': 'Date: ','visible': True,'xanchor': 'right'},
                  transition = {'duration': 0, 'easing': 'linear'},
                  pad = {'b': 10, 't': 50},
                  len = 0.8,
                  x = 0.1,
                  y = -0.15,
                  #---------------------------------------------------------------------
                  # Add the steps variable once the variable is complete
                  steps = []
                  #---------------------------------------------------------------------
            )]

  fig.frames=frames
  fig.update_layout(updatemenus=[dict(type='buttons',
                                      showactive=False,
                                      y=-0.35,
                                      x=0.05,
                                      xanchor='right',
                                      yanchor='top',
                                      buttons=[play_button, pause_button])],
                    sliders=sliders,
                    legend=dict(orientation='h',
                                yanchor='bottom',
                                y=1.02,
                                xanchor='right',
                                x=0.58,
                                title=dict(text='State(s):'),
                                valign='middle'
                                ),
                    height=550,
                    width=1500
                    )

  #------------------------------------------------------------------------------------
  # Update the x-axes to start from the first available date and end with the most
  #   recent date of data collected (i.e. update the 0s and 1s with the date list
  #   we made above)
  fig.update_xaxes(range=[ 0,
                           1 ],
                   row=1, col=1)

  fig.update_xaxes(range=[ 0,
                           1 ],
                   row=1, col=2)
  #------------------------------------------------------------------------------------

  #------------------------------------------------------------------------------------
  # Update the y-axes to include the names of the features
  fig.update_yaxes(title_text='Sample 1', row=1, col=1)
  fig.update_yaxes(title_text='Sample 2', row=1, col=2)
  #------------------------------------------------------------------------------------

  return fig
main()


from itertools import combinations

def part1():
    with open('input.txt', 'r') as file:
        input_numbers = file.readlines()
        input_numbers = list(map(int, input_numbers))

    combs_2 = combinations(input_numbers, 2)
    combs_3 = combinations(input_numbers, 3)

    for comb in combs_2:
        if sum(comb) == 2020:
            print('Part 1 answer:', comb[0] * comb[1])
            break

    for comb in combs_3:
        if sum(comb) == 2020:
            print('Part 2 answer:', comb[0] * comb[1] * comb[2])
            break

if __name__ == '__main__':
    part1()


import re

from utility import cn2dig, get_year, request_site_page, get_content_text
from bs4 import BeautifulSoup as bs
from oss_utils import oss_add_file, init_ali_oss

ali_bucket = init_ali_oss()

def fujian_circ(db, logger):
    for each_circ_data in db.circ_data.find({'origin': '福建保监局', 'status': {'$nin': ['ignored']}}):
        announcement_url = each_circ_data['url']
        announcement_title = each_circ_data['title']

        if db.circ_data.find(
                {'url': announcement_url, 'status': 'parsed'}).count() == 1 and db.parsed_data.find(
            {'origin_url': announcement_url, 'oss_file_origin_url': announcement_url,
             'parsed': True}).count() == 1:
            continue

        logger.info('福建保监局 ' + 'Url to parse: %s' % announcement_url)

        r = request_site_page(announcement_url)
        if r is None:
            logger.error('网页请求错误 %s' % announcement_url)
            continue
        content_soup = bs(r.content, 'lxml') if r else bs('', 'lxml')

        if db.parsed_data.find(
                {'origin_url': announcement_url, 'oss_file_origin_url': announcement_url}).count() == 0:
            oss_file_map = {
                'origin_url': announcement_url,
                'oss_file_origin_url': announcement_url,
                'origin_url_id': each_circ_data['_id'],
                'oss_file_type': 'html',
                'oss_file_name': announcement_title,
                'oss_file_content': r.text.encode(r.encoding).decode('utf-8'),
                'parsed': False
            }
            insert_response = db.parsed_data.insert_one(oss_file_map)
            file_id = insert_response.inserted_id
            oss_add_file(ali_bucket, str(file_id) + '/' + announcement_title + '.html',
                         r.text.encode(r.encoding).decode('utf-8'))
            db.circ_data.update_one({'_id': each_circ_data['_id']}, {'$set': {'status': 'parsed'}})
        else:
            db.circ_data.update_one({'_id': each_circ_data['_id']}, {'$set': {'status': 'parsed'}})
            file_id = db.parsed_data.find_one({'origin_url': announcement_url,
                                               'oss_file_origin_url': announcement_url})['_id']

        table_content = content_soup.find(id='tab_content')
        if not table_content:
            logger.error('网页请求错误 %s' % announcement_url)
            continue
        content_text = get_content_text(table_content.find_all('tr')[3])
        if content_text == '':
            continue
        title = table_content.find_all('tr')[0].text.strip()

        document_code_compiler = re.compile(r'((闽保监罚|闽银保监罚决字).\d{4}.\d+号)')
        if document_code_compiler.search(content_text):
            document_code = document_code_compiler.search(content_text).group(1).strip()
            litigant_compiler = re.compile(
                document_code.replace(r'[', r'\[').replace(r']', r'\]') +
                r'\n([\s\S]*?)' + r'(\n经查|\n经检查|'
                                  r'\n?依据.*?的有关规定|\n抽查|\n经抽查|'
                                  r'\n.*?现场检查|\n.*?进行调查|\n.*?调查|'
                                  r'\n.*?信访核查|\n.*?行政许可审核中发现|'
                                  r'\n?.*?我局根据信访件提供的线索|'
                                  r'\n?2008年3月21日)')
            litigant = litigant_compiler.search(content_text).group(1).strip()
        else:
            if document_code_compiler.search(title):
                document_code = document_code_compiler.search(title).group(1).strip()
            else:
                document_code = ''
            litigant_compiler = re.compile(r'^([\s\S]*?)\n' + r'(经查|经检查|依据.*?的有关规定|抽查|经抽查|.*?现场检查|'
                                                              r'.*进行调查)')
            litigant = litigant_compiler.search(content_text).group(1).strip()

        truth_text_str = r'((经查|.*?存在.*?行为)' \
                         r'([\s\S]*?))' \
                         r'((我局认为，)?(上述|以上).*?(事实|行为|事实)(,|，)?有.*?等证据(在案)?证明(,|，|。)(足以认定。)?|' \
                         r'(我局认为，|综上，)?[^，。,；\n]*?(上述|以上).*?(行为|问题|事实).*?违反.*?第.*条.*?(的规定)?|' \
                         r'你公司虚列会议费套取费用用于其他方面开支的行为，违反了《保险法》|' \
                         r'你公司未按规定使用经核准或备案的条款费率行为，违反了《保险法》|' \
                         r'根据《保险法》第一百五十条的规定，对你处以2万元罚款)'
        truth_compiler = re.compile(truth_text_str)
        if truth_compiler.search(content_text):
            truth = truth_compiler.search(content_text).group(1).strip()
        else:
            truth_text_str = litigant + r'([\s\S]*?)' \
                                        r'((我局认为，)?(上述|以上).*?(事实|行为|事实)(,|，)?有.*?等证据(在案)?证明(,|，|。)(足以认定。)?|' \
                                        r'(我局认为，|综上，)?(上述|以上).*?(行为|问题|事实).*?违反.*?第.*条.*?(的规定)?)'
            truth_compiler = re.compile(truth_text_str)
            truth = truth_compiler.search(content_text).group(1).strip()

        if '申辩' in content_text:
            defense_text_str = r'((针对.*?行为.*?申辩意见|(当事人)?[^，。,；\n]*?(未)?提出(了)?陈述申辩(意见)?|' \
                               r'[^，。,；\n]*?向我局(报送|递交|提出)[^，。,；\n]*?|本案在审理过程中.*?提出陈述申辩|' \
                               r'[^，。,；\n]*?在(申辩材料|陈述申辩|陈述申辩意见|申辩意见)中称|[^，。,；\n]*?在听证阶段提出|' \
                               r'[^，。,；\n]*?在法定期限内(未)?提出(了)?(听证要求|陈述申辩|陈述申辩及听证要求)|' \
                               r'你在.*?材料中(称|提出)|你[^，。,；\n]*?在.*?中未对我局认定的(违法|违规)事实|' \
                               r'[^，。,；\n]*?申辩称)' \
                               r'([\s\S]*?))' \
                               r'(因此，我局决定|' \
                               r'我局经复核(认为|决定)|' \
                               r'本案现已审理终结|' \
                               r'我局经复查[^，。,；\n]*?情况|' \
                               r'我局[^，。,；\n]*?认真复核|' \
                               r'经研究，对[^，。,；\n]*?予以采纳。|' \
                               r'我局认为.*?申辩理由|' \
                               r'依据.*?我局认为.*?的申辩理由|' \
                               r'经研究|' \
                               r'经我局审核，决定|' \
                               r'我局认为，上述违法行为事实清楚、证据确凿、法律法规适当|' \
                               r'我局对陈述申辩意见进行了复核|' \
                               r'经我局审核|' \
                               r'针对[^，。,；\n]*?的(陈述)?申辩意见，我局进行了核实|' \
                               r'经查，我局认为|' \
                               r'依据现场检查及听证情况|' \
                               r'我局对陈述和申辩材料进行了复核，我局认为|' \
                               r'经查|我局认为)'
            defense_compiler = re.compile(defense_text_str, re.MULTILINE)
            defense_list = defense_compiler.findall(content_text)
            if len(defense_list) != 0:
                defense = defense_list[-1][0].strip()
                defense_response_str = defense.replace(r'[', r'\[').replace(r']', r'\]') \
                                       + r'(([\s\S]*?)' + r'(本案现已审理终结。|不符合.*?情形。|根据.*?依法可从轻或者减轻行政处罚。|' \
                                                          r'对[^，。,；\n]*?申辩意见(不予|予以)采纳|因此.*?申辩理由.*?成立。|' \
                                                          r'我局认为.*?申辩(理由|意见).*?符合.*?第.*?条.*?的条件.(予以采纳。)?|' \
                                                          r'不予采纳。|因此，不予采纳当事人的陈述申辩意见。|' \
                                                          r'因此，对你提出的陈述和申辩意见不予采纳。))'
                defense_response_compiler = re.compile(defense_response_str, re.MULTILINE)
                if defense_response_compiler.search(content_text):
                    defense_response = defense_response_compiler.search(content_text).group(1).strip()
                else:
                    if '未' in defense:
                        defense_response = ''
            else:
                defense_text_str = '([^。；\n]*?向.*?公告送达了《行政处罚事先告知书》.*?提出陈述申辩。|' \
                                   '我局依法于2012年5月25日对你公司送达了《行政处罚事先告知书》，你公司在规定的时间内未提出陈述和申辩意见，也未要求举行听证。)'
                defense_compiler = re.compile(defense_text_str, re.MULTILINE)
                defense = defense_compiler.search(content_text).group(1).strip()
                defense_response = ''
        else:
            defense = defense_response = ''

        punishment_decision_text_str = r'(((依据|根据).*?第?.*?条.*?(规定)?.?(我局)?(决定|责令|给予|于.*?向.*?发出|对.*?作出|拟对你|对你)|' \
                                       r'我局经复核认为|我局决定.*?作出(如下|以下)(行政)?处罚：|' \
                                       r'我局作出如下处罚)' \
                                       r'([\s\S]*?))' \
                                       r'(请在本处罚决定书送达之日|当事人应当在接到本处罚决定书之日|如不服本处罚决定|' \
                                       r'请(在)?接到本处罚决定书之日|如不服从本处罚决定|[^。；\n]*?如对本处罚决定不服|' \
                                       r'[^。；\n]*?对以上行政处罚决定(如)?不服)'

        punishment_decision_compiler = re.compile(punishment_decision_text_str)
        punishment_decision = punishment_decision_compiler.search(content_text).group(1).strip()

        punishment_basis_str_list = [
            r'([^\n。；：:]*?)(问题|行为|事项|情况|事实|财务不真实|财务数据不真实)([^\n。；]*?)违(反|法)([^\n。；]*?)第.*?条.*?((的|之|等)(相关)?规定)?',
            r'吴淑汝代替投保人抄录风险提示语句，违反了《人身保险新型产品信息披露管理办法》第六条的规定',
            r'平安人寿上海电销中心上述行为《保险法》（2014年修正）第一百一十六条第一项的规定',
            r'富邦财产保险有限公司福州营销服务部未严格执行条款费率，违反了《保险法》第一百三十六条的规定',
            r'账外暗中支付给中介机构委托合同以外的利益，违反了《保险公司中介业务违法行为处罚办法》第十条',
            r'对代理人培训不到位、执行代理业务核查制度不到位，违反了《保险公司中介业务违法行为处罚办法》第六条和第七条',
            r'中国平安财产保险股份有限公司宁德中心支公司违反了《保险法》第一百一十六条第十项的规定',
            r'一、富邦财产保险有限公司福州营销服务部未严格执行条款费率，违反了《保险法》第一百三十六条的规定',
            r'二、富邦财产保险有限公司福州营销服务部虚列费用，违反了《保险法》第八十六条第二款的规定',
            r'我局认为，你公司未缴存保证金，也未投保职业责任保险，违反了《保险专业代理机构监管规定》第三十九条',
            r'你公司未按照要求及时预缴当年及清缴上年的监管费，违反了《保险专业代理机构监管规定》第六十三条',
            r'经查，在你任中国人民财产保险股份有限公司福清支公司经理期间，该公司在参加2009年福清市行政事业单位公务车辆统一投保项目（招标编号：0624-09180123）的投标活动中，违反了《保险法》第一百零七条的规定'
        ]
        punishment_basis_str = '|'.join(punishment_basis_str_list)
        punishment_basis_compiler = re.compile(r'[。\n；：:]' + '(' + punishment_basis_str + ')' +
                                               '.(\n?依据|\n?根据|\n?鉴于)', re.MULTILINE)
        punishment_basis_list = punishment_basis_compiler.findall(content_text)
        punishment_basis = '；'.join([kk[0].strip() for kk in punishment_basis_list])

        publish_date_text = re.search(
            punishment_decision.replace(r'(', r'\(').replace(r')', r'\)').replace(r'[', r'\[').replace(r']', r'\]').
            replace(r'*', r'\*') + r'([\s\S]*?)$', content_text).group(1).replace('\n', '')
        if re.search(r'.{4}年.{1,2}月.{1,3}日', publish_date_text):
            publish_date = re.findall('.{4}年.{1,2}月.{1,3}日', publish_date_text)[-1].replace(' ', '')
            m = re.match("([0-9零一二两三四五六七八九十〇○ＯOΟО]+年)?([0-9一二两三四五六七八九十]+)月?([0-9一二两三四五六七八九十]+)[号日]?", publish_date)
            real_publish_date = get_year(m.group(1)) + str(cn2dig(m.group(2))) + '月' + str(cn2dig(m.group(3))) + '日'
        else:
            publish_date_text = table_content.find_all('tr')[1].text
            publish_date = re.findall(r'\d{4}-\d{1,2}-\d{1,2}', publish_date_text)[-1]
            real_publish_date = publish_date.split('-')[0] + '年' + str(int(publish_date.split('-')[1])) + '月' + str(
                int(publish_date.split('-')[2])) + '日'

        result_map = {
            'announcementTitle': title,
            'announcementOrg': '福建银保监局',
            'announcementDate': real_publish_date,
            'announcementCode': document_code,
            'facts': truth,
            'defenseOpinion': defense,
            'defenseResponse': defense_response,
            'litigant': litigant[:-1] if litigant[-1] == '：' else litigant,
            'punishmentBasement': punishment_basis,
            'punishmentDecision': punishment_decision,
            'type': '行政处罚决定',
            'oss_file_id': file_id,
            'status': 'not checked'
        }
        logger.info(result_map)
        if db.announcement.find({'announcementTitle': title, 'oss_file_id': file_id}).count() == 0:
            db.announcement.insert_one(result_map)
            logger.info('福建保监局 数据解析 ' + ' -- 数据导入完成')
        else:
            logger.info('福建保监局 数据解析 ' + ' -- 数据已经存在')
        db.parsed_data.update_one({'_id': file_id}, {'$set': {'parsed': True}})
        logger.info('福建保监局 数据解析 ' + ' -- 修改parsed完成')


from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django_project.exhibition.models import Exhibition, Picture
import os
import logging

logger = logging.getLogger('exhibition')

@receiver(post_delete, sender=Exhibition)
def auto_delete_link_on_delete(sender, instance, **kwargs):

    if instance.link:
        if os.path.isfile(instance.link.path):
            os.remove(instance.link.path)

            logger.info(f'Exhibition link deleted {instance.id}')

@receiver(post_delete, sender=Picture)
def auto_delete_link_on_delete(sender, instance, **kwargs):

    if instance.link:
        if os.path.isfile(instance.link.path):
            os.remove(instance.link.path)

            logger.info(f'Picture link deleted {instance.id}')


"""Task 5: Map Plotting"""

import rasterio
import numpy
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
import numpy as np
import cartopy.crs as ccrs

def map_plot(user_location, user_node, high_point, high_node, dataset, shortest_path_gpd, flood_poly=False):
    """This function plots a map of the user_location, highest point and shortest path gpd df
    over an OS Explorer and elevation raster basemap.
    """
    print("\nPlotting your shortest route to higher ground...")
    # background and the map extent
    background = rasterio.open('data/background/raster-50k_2724246.tif')
    back_array = background.read(1)
    palette = np.array([value for key, value in background.colormap(1).items()])
    background_image = palette[back_array]

    bg_extent = [background.bounds.left, background.bounds.right, background.bounds.bottom, background.bounds.top]
    el_extent = [dataset.bounds.left, dataset.bounds.right, dataset.bounds.bottom, dataset.bounds.top]

    display_extent = [((user_location.x + high_point.x) / 2) - 5000,
                      ((user_location.x + high_point.x) / 2) + 5000,
                      ((user_location.y + high_point.y) / 2) - 5000,
                      ((user_location.y + high_point.y) / 2) + 5000]
    # Task 5: Map Plotting
    # defining the walking path between the user and the nearest NIT node
    # and defining the walking path between highest point and its nearest NIT node
    waking_route_user_node_lons = [user_location.x, user_node.x]
    waking_route_user_node_lats = [user_location.y, user_node.y]
    waking_route_highest_point_lons = [high_point.x, high_node.x]
    waking_route_highest_point_lats = [high_point.y, high_node.y]

    # Set up figure and extent
    fig = plt.figure(figsize=(3, 3), dpi=300)
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.OSGB())
    ax.set_extent(display_extent, crs=ccrs.OSGB())

    # imshow for the background
    ax.imshow(background_image, origin="upper", extent=bg_extent, zorder=0)

    # imshow for the elevation raster
    plt.imshow(dataset.read(1), extent=el_extent, cmap='gist_earth', origin='upper', zorder=0, alpha=0.65,
               resample='True', vmax=numpy.amax(dataset.read(1)), vmin=numpy.amin(dataset.read(1)))

    if flood_poly:
        for i in flood_poly.geoms:
            plt.plot(*i.exterior.xy, color="darkgray", linewidth=0.5, alpha=0.7)

    # plotting the shortest path
    shortest_path_gpd.plot(ax=ax, edgecolor="orangered", linewidth=0.5, zorder=2, label='ITN shortest path')

    # scattering the user point the highest elevation point
    ax.scatter(user_location.x, user_location.y, s=1.5, c='r', label='Your location', marker="*")
    ax.scatter(high_point.x, high_point.y, s=1.5, c='k', label='Highest_point', marker='*')
    # user_location.xy()[1] arrow
    x, y, arrow_length = 0.9, 0.95, 0.18
    ax.annotate('N', xy=(x, y), xytext=(x, y - arrow_length), arrowprops=dict(facecolor='black', width=2, headwidth=8),
                ha='center', va='center', fontsize=9, xycoords=ax.transAxes)

    # adding scale bar
    scalebar = AnchoredSizeBar(ax.transData, 1000, '1 km', 'lower center',
                               pad=0.1, color='black', frameon=False, size_vertical=1)

    ax.add_artist(scalebar)

    # 6) plotting the color bar
    cbar = plt.colorbar(fraction=0.03, pad=0.03)
    cbar.set_label(r"Elevation", size=8)
    cbar.ax.tick_params(labelsize=5)
    # 7) plotting the walking paths
    ax.plot(waking_route_user_node_lons, waking_route_user_node_lats, c='k', label='walking route', linewidth=0.5,
            linestyle='dashed')

    link_patch = mlines.Line2D([], [], color='orangered', label='ITN Shortest Path', linewidth=0.5)
    walk_patch = mlines.Line2D([], [], color='k', label='Walking route', linewidth=0.5, linestyle='dashed')
    user_star = mlines.Line2D([], [], color='r', marker="*", linestyle='None', markersize=2, label='Your location')
    high_point = mlines.Line2D([], [], color='k', marker='*', linestyle='None', markersize=2, label='Highest point')

    # Dynamic legend - can handle absence of flood_poly
    if flood_poly:
        flood_patch = mlines.Line2D([], [], color='darkgray', label='Flood line', linewidth=0.5)
        plt.legend(fontsize=3, loc=2, handles=[user_star, high_point, walk_patch, link_patch, flood_patch],
                   title="Legend")
    else:
        plt.legend(fontsize=3, loc=2, handles=[user_star, high_point, walk_patch, link_patch], title="Legend")
    ax.plot(waking_route_highest_point_lons, waking_route_highest_point_lats, c='k', label='walking route',
            linewidth=0.5, linestyle='dashed')

    plt.show()


import os
import time

from datetime import datetime, timedelta
from typing import Optional

from rich.console import Console
from rich.table import Table
from rich.text import Text
import yaml

from tsdapiclient.tools import (check_if_exp_is_within_range,
                                check_if_key_has_expired, debug_step,
                                get_config_path, get_claims,
                                check_if_key_has_expired,)

SESSION_STORE = get_config_path() + '/session'

def session_file_exists() -> bool:
    return False if not os.path.lexists(SESSION_STORE) else True

def session_is_expired(env: str, pnum: str, token_type: str) -> bool:
    if not session_file_exists():
        return True
    token = session_token(env, pnum, token_type)
    debug_step(f'found token: {token}')
    if not token:
        return True
    if check_if_key_has_expired(token):
        debug_step('session expired')
        return True
    else:
        debug_step('session has not expired')
        return False

def session_expires_soon(env: str, pnum: str, token_type: str, minutes: int = 10) -> bool:
    if not session_file_exists():
        return None
    token = session_token(env, pnum, token_type)
    if not token:
        return False
    target_time = datetime.utcnow() + timedelta(minutes=minutes)
    upper = int(time.mktime(target_time.timetuple()))
    lower = int(time.time())
    if check_if_exp_is_within_range(token, lower=lower, upper=upper):
        debug_step(f'session will expire in the next {minutes} minutes')
        return True
    else:
        debug_step('session will not expire soon')
        return False

def session_read(session_store: str = SESSION_STORE) -> dict:
    with open(session_store, 'r') as f:
        data = yaml.load(f, Loader=yaml.Loader)
    return data

def session_update(
    env: str,
    pnum: str,
    token_type: str,
    token: str,
    refresh_token: Optional[str] = None,
) -> None:
    default = {
        'prod': {},
        'alt': {},
        'test': {},
        'ec-prod': {},
        'ec-test': {},
        'dev': {},
    }
    if not session_file_exists():
        debug_step('creating new tacl session store')
        data = default
    try:
        data = session_read()
    except FileNotFoundError:
        data = default
    target = data.get(env, {}).get(pnum, {})
    target[token_type] = token
    target[f'{token_type}_refresh'] = refresh_token
    if not data.get(env):
        data[env] = {}
    data[env][pnum] = target
    debug_step('updating session')
    with open(SESSION_STORE, 'w') as f:
        f.write(yaml.dump(data, Dumper=yaml.Dumper))

def session_token(env: str, pnum: str, token_type: str) -> str:
    data = session_read()
    return data.get(env, {}).get(pnum, {}).get(token_type)

def session_refresh_token(env: str, pnum: str, token_type: str) -> str:
    data = session_read()
    return data.get(env, {}).get(pnum, {}).get(f'{token_type}_refresh')

def session_clear() -> None:
    data = {
        'prod': {},
        'alt': {},
        'test': {},
        'ec-prod': {},
        'ec-test': {},
        'dev': {},
    }
    with open(SESSION_STORE, 'w') as f:
        f.write(yaml.dump(data, Dumper=yaml.Dumper))

def session_print(session_file: str = SESSION_STORE) -> None:
    console = Console()
    try:
        data = session_read()
    except FileNotFoundError:
        print("No session file found")
        exit(1)

    table = Table(title=f"{__package__} session details", show_lines=True)
    table.add_column("Environment")
    table.add_column("Project")
    table.add_column("User")
    table.add_column("Groups")
    table.add_column("Type")
    table.add_column("Expiry")

    for env in data:
        for project in data[env].keys():
            for token_type in data[env][project].keys():
                token = data[env][project][token_type]
                if token:
                    claims = get_claims(token)
                    exp = claims['exp']
                    expiry = Text(datetime.fromtimestamp(exp).strftime('%Y-%m-%d %H:%M:%S'))
                    if check_if_key_has_expired(token):
                        expiry.stylize('bold red')
                    user = claims.get('user')
                    table.add_row(
                        env,
                        project,
                        user,
                        f"{', '.join(claims.get('groups', []))}",
                        claims.get("name", token_type),
                        expiry,
                    )
    if table.rows:
        console.print(table)
    else:
        console.print("No sessions found")


#!/usr/bin/env python3

import requests
import json
from pyproj import Transformer
import string

DEFAULT_STRASSEN_SAVE = 'strassen.json'
DEFAULT_ADRESSEN_SAVE = 'adressen.json'

def getCoordinateFromBbox(bbox):
    return [(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2]

def OSM_LongLat(osm):
    transformer = Transformer.from_crs("epsg:5972", "epsg:4326")
    lonLat = transformer.transform(osm[0], osm[1])
    return {
        'lon': lonLat[1],
        'lat': lonLat[0]}

def isAddress(a):
    return 'search_category' in a and a['search_category'] == '05_Adressen'

def isInStreet(a, street):
    return 'displaytext' in a and a['displaytext'].startswith(f'{street} ')

def formatAddress(a):
    return {
        'name': a['displaytext'][:-len(' (Adresse)')],
        'coord': OSM_LongLat(getCoordinateFromBbox(a['bbox']))
    }

def isStrasse(a):
    return 'search_category' in a and a['search_category'] == '01_Strassen'

def getAdressenInStrasse(strasse):
    response = requests.get(
    url="https://mapfinder.jena.de/search",
    params={
        'query':                strasse,
        'searchTables':         [],
        'searchFilters':        [],
        'searchArea':           'Jena',
        'searchCenter':         '',
        'searchRadius':         '',
        'topic':                'stadtplan',
        'resultLimit':          500,
        'resultLimitCategory':  5})

    return [formatAddress(a) for a in response.json()["results"][1:] if isAddress(a) and isInStreet(a, strasse)]

def getStrassen(letter):
    response = requests.get(
    url="https://mapfinder.jena.de/search",
    params={
        'query':                letter,
        'searchTables':         [],
        'searchFilters':        [],
        'searchArea':           'Jena',
        'searchCenter':         '',
        'searchRadius':         '',
        'topic':                'stadtplan',
        'resultLimit':          500,
        'resultLimitCategory':  5})

    return [a['displaytext'][:-len(" (Strasse)")] for a in response.json()["results"][1:] if isStrasse(a)]

def getAddresse(address):
    response = requests.get(
        url="https://mapfinder.jena.de/search",
        params={
            "query":                address,
            "searchTables":         [],
            "searchFilters":        [],
            "searchArea":           "Jena",
            "searchCenter":         "",
            "searchRadius":         "",
            "topic":                "stadtplan",
            "resultLimit":          5,
            "resultLimitCategory":  5})
    adressen = list(filter(
        lambda a: ("search_category" in a) and (a["search_category"] == "05_Adressen"),
        response.json()["results"]))

    if len(adressen) == 0:
        raise Exception(f"Can't find the adress '{address}'")
    if len(adressen) > 3:
        raise Exception(f"Can't find one distinct adress for '{address}'")

    return OSM_LongLat(getCoordinateFromBbox(adressen[0]["bbox"]))

def getAllStrassen():
    strassen = []
    for letter in string.ascii_uppercase:
        for strasse in getStrassen(letter):
            if strasse not in strassen:
                yield strasse
                strassen.append(strasse)

def saveAllStrassen(filename=DEFAULT_STRASSEN_SAVE):
    with open(filename, "w") as f:
        json.dump(list(getAllStrassen()), f)

def loadAllStrassen(filename=DEFAULT_STRASSEN_SAVE):
    with open(filename, "r") as f:
        strassen = json.load(f)
    return strassen

def getAllAdressenFromStrassen(strassen):
    for strasse in strassen:
        adressenInStrasse = getAdressenInStrasse(strasse)
        print(f'{strasse}: {len(adressenInStrasse):3d} Adressen')
        for adresse in adressenInStrasse:
            yield adresse

def saveAllAdressenFromStrassen(strassen, database):
    for adresse in getAllAdressenFromStrassen(strassen):
        database.insert(adresse)

if __name__ == '__main__':
    import addressDb

    saveAllStrassen()
    database = addressDb.addressDb()
    saveAllAdressenFromStrassen(loadAllStrassen(), database)


import csv
from utils.files_handling.files_locations import FilesLocations
from utils.db.db_init import get_mongo_client

class DatasetLoader:

    def load_dataset(self):
        if FilesLocations.DATASET_NAME.value == "antique":
            return self.load_antique()

        elif FilesLocations.DATASET_NAME.value == "antique_queries":
            return self.load_queries("antique_queries")

        elif FilesLocations.DATASET_NAME.value == "wikir_queries":
            return self.load_queries("wikir_queries")

        return self.load_wikir()

    @staticmethod
    def load_antique():
        documents = {}

        with open(FilesLocations.ANTIQUE_DOCUMENTS.value, encoding="utf-8") as documents_file:
            documents_reader = csv.reader(documents_file, delimiter="\t")
            for doc_id, doc_content in documents_reader:
                documents.update({doc_id: doc_content})

        return documents

    @staticmethod
    def load_wikir():
        documents = {}
        i = 0
        with open(FilesLocations.WIKIR_DOCUMENTS.value, encoding="utf-8") as documents_file:
            documents_reader = csv.reader(documents_file)
            for doc_id, doc_content in documents_reader:
                if i == 0:
                    i += 1
                    continue
                documents.update({doc_id: doc_content})

        return documents

    @staticmethod
    def load_queries(collection):
        col = get_mongo_client()["IR"][collection]
        documents = {}

        for query in list(col.find({})):
            documents.update({query['_id']: query['content']})

        return documents


#!/usr/bin/env python
import os
import sys
import csv
import time
import coards
import datetime
import numpy as np
from pupynere import netcdf_file

class ParserWriter(object):
    '''
    Handle all information needed to parse LR Waveglider CSV files and produce
    NetCDF for each of them
    '''

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

    def write_nc(self):
        esec_list = []
        ex = ['col1', 'col2','col3','latitude','longitude'] #Colum labels
#        ex = csv.reader(open('info.txt'))
        reader = csv.DictReader(open('test.txt')) #open de the file and save the data in reader
        #Do a list of each label with the data on it
        for r in reader:
            gmtDTString = r['Time'] #read the variable time and save it in CF-1.0 nercdf convention
            tt = time.strptime(gmtDTString, '%Y-%m-%d %H:%M:%S')
            diff = datetime.datetime(*tt[:6]) - datetime.datetime(1970,1,1,0,0,0)
            esec_list.append(diff.days * 86400 + diff.seconds)
            #Create the list of all the variable
            for v in ex:
                exec('%s_list = []' % (v))
                exec("%s_list.append(r['%s'])" % (v,v,))
#======================================================================
#       Create the NetCDF file
        outFile='foo1.nc'
        self.ncFile = netcdf_file(outFile, 'w')
        self.outFile = outFile

        # Trajectory dataset, time is the only netCDF dimension
        self.ncFile.createDimension('TIME', len(esec_list))

        self.time = self.ncFile.createVariable('TIME', 'float64', ('TIME',))
        self.time.units = 'seconds since 1970-01-01'
        self.time[:] = esec_list

        # Write  variables
        for v in ex:
            ncVar = v.replace(' ', '_', 42)
            # Only Latitude, Longitude, Depth, and Time variables are upper case to match other Glider data
            print(v)
            if v == 'Latitude' or v == 'Longitude':
                exec("self.%s = self.ncFile.createVariable('%s', 'float64', ('TIME',))" % (ncVar.lower(), ncVar.upper(), ))
            else:
                exec("self.%s = self.ncFile.createVariable('%s', 'float64', ('TIME',))" % (ncVar.lower(), ncVar, ))
            exec("self.%s.long_name = '%s'" % (ncVar.lower(), v, ))
            exec("self.%s[:] = %s_list" % (ncVar.lower(), ncVar, ))

        # Fudge up a depth variable with a value of zero
        self.depth = self.ncFile.createVariable('DEPTH', 'float64', ('TIME',))
        self.depth.long_name = 'Depth'
        self.depth.standard_name = 'depth'
        self.depth.units = 'm'
        self.depth[:] = np.zeros(len(self.time[:]))

        self.add_global_metadata()

        self.ncFile.close()

        # End write_pco2()

    def add_global_metadata(self):
        '''
        This is the main advantage of using a class for these methods.  This method uses the
        instance variables to write metadata specific for the data that are written.
        '''

        iso_now = datetime.datetime.now().isoformat()

        self.ncFile.title = ''
        self.ncFile.netcdf_version = '3.6'
        self.ncFile.Convention = 'CF-1.4'
        self.ncFile.date_created = iso_now
        self.ncFile.date_update = iso_now
        self.ncFile.date_modified = iso_now
        self.ncFile.cdm_data_type = 'trajectory'
        self.ncFile.CF_featureType = 'trajectory'
        self.ncFile.data_mode = 'R'
        self.ncFile.geospatial_lat_min = np.min(self.latitude[:])
        self.ncFile.geospatial_lat_max = np.max(self.latitude[:])
        self.ncFile.geospatial_lon_min = np.min(self.longitude[:])
        self.ncFile.geospatial_lon_max = np.max(self.longitude[:])
        self.ncFile.geospatial_lat_units = 'degree_north'
        self.ncFile.geospatial_lon_units = 'degree_east'

        self.ncFile.geospatial_vertical_min= np.min(self.depth[:])
        self.ncFile.geospatial_vertical_max= np.max(self.depth[:])
        self.ncFile.geospatial_vertical_units = 'm'
        self.ncFile.geospatial_vertical_positive = 'down'

        self.ncFile.time_coverage_start = coards.from_udunits(self.time[0], self.time.units).isoformat()
        self.ncFile.time_coverage_end = coards.from_udunits(self.time[-1], self.time.units).isoformat()

        self.ncFile.distribution_statement = 'Any use requires prior approval from the MBARI CANON PI: Dr. Francisco Chavez'
        self.ncFile.license = self.ncFile.distribution_statement
        self.ncFile.useconst = 'Not intended for legal use. Data may contain inaccuracies.'
        self.ncFile.history = 'Created by "%s" on %s' % (' '.join(sys.argv), iso_now,)

if __name__ == '__main__':

    # Accept arguement of data directory name, e.g. /mbari/Tracking/gliders
    dataDir = '/home/flopez/dev/stoqshg/loaders/MarMenor/toNetCDF'
    netc = ParserWriter(parentDir=dataDir)
    netc.write_nc()


import cv2, numpy as np

img1 = cv2.imread('../img/taekwonv1.jpg')
img2 = cv2.imread('../img/figures.jpg')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

# ORB 추출기 생성
detector = cv2.ORB_create()
# 키 포인트와 서술자 추출
kp1, desc1 = detector.detectAndCompute(gray1, None)
kp2, desc2 = detector.detectAndCompute(gray2, None)

# 인덱스 파라미터 설정 ---①
FLANN_INDEX_LSH = 6
index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 6,
                   key_size = 12,
                   multi_probe_level = 1)
# 검색 파라미터 설정 ---②
search_params=dict(checks=32)
# Flann 매처 생성 ---③
matcher = cv2.FlannBasedMatcher(index_params, search_params)
# 매칭 계산 ---④
matches = matcher.match(desc1, desc2)
# 매칭 그리기
res = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, \
            flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
# 결과 출력
cv2.imshow('Flann + ORB', res)
cv2.waitKey()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 23 14:03:36 2022

@author: Emma Peleg

This program runs DPV histogram data and procecess it

Maybe it will look at the melting Index? Maybe not
"""
import csv
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
from math import pi
import hdbscan
from scipy import stats

    ## this program can be used to analyze multiple files in multiple folders by inputting the path for the main containing folder
    ##i.e. put all of your DPV folders with data in one larger folder and run the program in the path of the larger folder
path_of_the_directory = input("Paste the path for the folder containing the desired DPV files :\n" )
ext = ".prt"

os.chdir(path_of_the_directory)

for root, dirs, files in os.walk(path_of_the_directory):
    for title in files:
        if title.endswith(".prt"):
            os.chdir(root) ##changes directory to working one each loop
            setname=title[0:-4]
            w = pd.read_table(title, header=0, delimiter='\s+')
            data_df = w[["Speed","Temperature"]].copy()

            ##diameter fixing using modal diameter from PSA
            EnergyB = np.array(w["EnergyB"])
            Old_Diameters = np.array(w["Diameter"])
            PSA_Diameter_mode = int(input("Enter modal diameter from PSA analysis: \n")) ## um
            Full_Temp = np.array(w["Temperature"])
            DC = (Old_Diameters)/(np.sqrt(EnergyB/Full_Temp**4))
            DC_mode = stats.mode(DC)[0]
            Old_D_mode=stats.mode(Old_Diameters)[0]
            New_DC=DC_mode*(PSA_Diameter_mode/Old_D_mode)
            Real_Diameters = New_DC*(np.sqrt(EnergyB/Full_Temp**4))

            data_df["Diameter"]=Real_Diameters ## adding fixed diameters
            data=np.array(data_df) ## data for speed temp diameter as array for hdbscan

            ## INPUT VARIABLES NEEDED

            """the official dataframe of the data from the DPV *.prt file is saved as df

            Proceed to do more stuff
            temp = temperature [C]
            vel = velocity [m/s]
            diam = diameter [um]
            tMelt = melting temperature [C]
            den = density [kg/m^3]  (1 g/cc = 1000 kg/m^3)
            k = thermal conductivity [W/mK]
            hf = heat of fusion [J/kg]
            h = heat transfer coefficient [W/m^2K]
            D = particle size [m]
            rp = radius of particle [m] ??

                        k = obj.thermalConductivity;
                        hf = obj.heatOfFusion;

                        biotNumber = 30000*(0.5*diam*(10^-6))/k;

                        MI = 3.3*(12*k*(temp-tMelt)*(sprayDist*(10^-3)))./(den*hf*(1+2./biotNumber).*((.5*diam*(10^-6)).^2).*vel);

                        %CALCULATE KE
                        volume = (4/3)*pi*((0.5*diam).^3)*(10^(-18));
                        KE = 0.5*den*volume.*(vel.^2) * 10^6;

            """

            """
            Do an outlier removal using hdbscan:
            """

            clusterer = hdbscan.HDBSCAN()

            fit=clusterer.fit(data_df)

            cluster_labels = clusterer.labels_
            min_cluster = clusterer.labels_.min()
            max_cluster = clusterer.labels_.max()
            cluster_prob = clusterer.probabilities_
            cluster_out = clusterer.outlier_scores_

            #sns.displot(clusterer.outlier_scores_[np.isfinite(clusterer.outlier_scores_)], rug=True)

            threshold = pd.Series(clusterer.outlier_scores_).quantile(0.9)
            outliers = np.where(clusterer.outlier_scores_ > threshold)[0]
            #cluster labels are outliers

            cluster_list = []
            for i in cluster_labels:
                cluster_list.append(i)

            outlier_list= []
            for index,i in enumerate(cluster_labels):
                if i == -1:
                    outlier_list.append(index)
                    #print(index,i)
         #plot xyz with color as label

            outlier_array = []
            for i in outlier_list:
                out = data[i]
                outlier_array.append(out)

            data_index_list=list(range(0,len(data)))

            for element in outlier_list:
                if element in data_index_list:
                    data_index_list.remove(element)

            clean_data=[]
                    ## removing all of the outliers and saving in new array
            for i in data_index_list:
                add_data=data[i]
                clean_data.append(add_data)
            clean_data_df = pd.DataFrame(clean_data,columns=["Velocity","Temperature","Diameter"])

            """
            Make the arrays for the different values without outliers
            """

            All_Temps = []      ##  list of all particle temperatures
            for i in clean_data_df["Temperature"]:
                All_Temps.append(float(i))

            Temp_Array=np.array(All_Temps)+273.15 ## converting to Kelvin

            All_Vel = []        ##  list of all velocities of particles
            for i in clean_data_df["Velocity"]:
                All_Vel.append(float(i))
            Velocity_Array=np.array(All_Vel)

            All_Diameters = []  ## list of all particle diameters
            for i in clean_data_df["Diameter"]:
                All_Diameters.append(float(i))
            Diameter_Array=np.array(All_Diameters)*(10**-6) #in meters!
            #fix diameter scale coefficient bc particles r wrong!

            ##use df and pandas using hdbscan (doesnt work with dataframes) so use a np array
            #labels clusters based on their values (-1 = statistical noise etc)
            #sort by cluster that contained most points, sort high to low
            #use velocity or T,V,D for outliers
            """

            Doing the melting index, kinetic energy, and oxidation index calculations

            """

            k = 138 ## thermal conductivity W/mK for Mo at 20C (>?????) change for different powders
            hf = 36/95.95*1000*1000 ## heat fusion for Mo in J/kg
            density_cc = 9 ## g/cc
            density = 9*(1000) ## density for Mo kg/m3
            Tm = 2623 ## melting temperature for Mo
            sprayDist = .15

            h = [] ## convective coefficient
            for i in Velocity_Array:
                h_app=0.0167*i**3 - 7.3956*i**2 + 1109.3*i - 43732
                h.append(h_app)
            h = np.array(h)

            biotNumber = h*(Diameter_Array)/k

            t_fly = []
            for i in Velocity_Array:
                t = (2*sprayDist)/i
                t_fly.append(t)

            """ Melting Index formulas"""
            simpMI = Temp_Array*t_fly/Diameter_Array

            MI = (24*k/(density*hf))*(1/(1+(4/biotNumber)))*(((Temp_Array-Tm)*(t_fly))/((Diameter_Array)**2))
            #low_Biot_MI = ((6*h)/(density*hf))*(1/(1+(4/biotNumber)))*(((Temp_Array-Tm)*(t_fly))/((Diameter_Array)**2))

            """ Kinetic Energy """

            Volume = (4/3)*pi*((.5*Diameter_Array)**3) ## cubic centimeters
            KE = 0.5*density*Volume*(Velocity_Array**2) ## Joules
            Average_KE = np.mean(KE)*10**6 ## micro Joules
            Average_KE_str = f"{Average_KE:.2f}"

            """ Oxidation Index"""

            # need oxygen concentration in gas phase

            """
            Plotting the velocities, temperatures, and particle diameters, and MI
            """
            ## Sturges Rule Calculations
            N = len(clean_data)
            K = int(1 + 3.322*np.log(N))

            fig, axs = plt.subplots(3,1,figsize=(6,6))

            axs[0].hist(Temp_Array,K,facecolor='blue',edgecolor='black')
            #axs[0].set_title('Temperature (C)')
            #axs[0].set_xlim(2500,5000)
            mean_temperature = np.mean(All_Temps)
            axs[0].set_xlabel('Temperature (C)' + " (Mean = " + str(round(mean_temperature,2)) + ")", fontsize=13)

            axs[1].hist(Velocity_Array,K,facecolor='red',edgecolor='black')
            mean_velocity = np.mean(All_Vel)
            axs[1].set_xlabel("Velocity (m/s)" + " (Mean = " + str(round(mean_velocity,2)) + ")", fontsize=13)
            #axs[1].set_title('Velocity (m/s)')
            #axs[1].set_xlim(30,300)

        #from DPV_Run_All import setname

                ##change diameter unit
            D_array_um = np.array(All_Diameters)

            axs[2].hist(D_array_um,K,facecolor='green',edgecolor='black')
            mean_diameters = np.mean(All_Diameters)
            axs[2].set_xlabel('Diameters (um)' + " (Mean = " + str(round(mean_diameters,2)) + ")", fontsize=13)
            #axs[2].set_title('Diameters (um)')
            #axs[2].set_xlim(0,60)
            plt.suptitle(setname)
            plt.tight_layout()

            plt.savefig("TVD_"+ setname[0] + ".png")
            for ax in axs.flat:
                ax.set(ylabel='Count')

            """ Melting Index Figure """

            MI_counts,MI_bins = np.histogram(MI,20)
            max_count_MI = max(MI_counts)-75
            middle_bin = np.mean(MI)-5

            ## find molten % of particles
            melt_threshold = 0
            melted_particles = [element for element in MI if element > melt_threshold]
            melted_number = len(melted_particles)
            molten_percent = len(melted_particles)/len(MI)*100
            molten_percent_str = f"{molten_percent:.2f}"

            fig2, ax1 = plt.subplots(1,1,figsize=(6,5))

            ax1=plt.hist(MI,K,facecolor='grey',edgecolor='black')
            #plt.title("Melting Index")
            plt.ylabel("Count")
            plt.xlabel("Melting Index" + ' (Molten Content '+ molten_percent_str +"%)")
            plt.axvline(x=0,ymin=0,color='black',linestyle='dotted',linewidth=3)
            plt.tight_layout()
            #plt.xlim(-5,5)
            plt.suptitle(setname)
            plt.tight_layout()
            plt.savefig("MI_"+ setname[0] + ".png")
            #plt.close()

            """ Kinetic Energy Figure """
            KE_counts,KE_bins = np.histogram(KE*10**6,20)
            max_count_KE = max(KE_counts)-10
            middle_bin_KE = np.mean(KE*10**6)+.5
            fig3, ax1 = plt.subplots(1,1,figsize=(6,5))

            ax1=plt.hist(KE*10**6,K,facecolor='grey',edgecolor='black')
            #plt.title("Melting Index")
            plt.ylabel("Count")
            plt.xlabel("Kinetic Energy [uJ]" + ' (Average KE '+ Average_KE_str +"uJ)")
            plt.tight_layout()
            x_max = 4.5*Average_KE
            #plt.text(middle_bin_KE,max_count_KE,'Average KE '+ Average_KE_str +"uJ",rotation=0)
            #plt.xlim(0,x_max)
            plt.suptitle(setname)
            plt.tight_layout()
            plt.savefig("KE_"+ setname[0] + ".png")


import numpy as np
import math
import tensorflow as tf
import matplotlib.pyplot as plt

#
# A ideia para mudança deste algoritmo é:
#   -> Usar uma base de dados ou usar dados gravados em arquivos do tipo Text.
#   -> Usar para fazer para treinar o modelo de preedição usando uma forma de regressão que pode ser Linear ou polinomial.
#   -> Pesquisa uma forma de receber dados constatemente sem ter que passar no modelo de treinamento
#   -> Otimizar código, usar boas práticas de programação para diminuir a complexidade do algoritmo.
#

def normalize(array):
    return (array - array.mean()) / array.std()

arq1 = open('dados/temperatura.txt', 'r')
arq2 = open('dados/velocidadeDoAr.txt', 'r')

dado1 = arq1.readlines()
dado2 = arq2.readlines()

# convert data
house_size = np.asarray([float(i) for i in dado1])
house_price = np.asarray([float(i) for i in dado2])

num_house = len(house_size)

print(len(house_size))
print(len(house_price))

# ake pegaremós 70% dos dados randômicos gerados para treinar o modelo de predição inteligênte
num_train_samples = math.floor(num_house * 0.7)

# Dados de treinamento 70%
train_house_size = np.asarray(house_size[:num_train_samples])
train_price = np.asarray(house_price[:num_train_samples])
train_house_size_norm = normalize(train_house_size)
train_price_norm = normalize(train_price)

# 30% dos valores gerados para o teste
test_house_size = np.asarray(house_size[num_train_samples:])
test_house_price = np.asarray(house_price[num_train_samples:])

#######################################################################################################################
#######################################################################################################################

tf_house_size = tf.placeholder(tf.float32, name='house_size')
tf_price = tf.placeholder(tf.float32, name='price')

tf_size_factor = tf.Variable(np.random.randn(), name='size_factor')
tf_price_offset = tf.Variable(np.random.randn(), name='price_offset')

tf_price_pred = tf_size_factor * tf_house_size + tf_price_offset

tf_cost = tf.reduce_sum(tf.pow(tf_price_pred - tf_price, 2)) / (2 * num_train_samples)

learning_rate = 0.01 #taxa de aprendizagem, valor ajustável. Busca o melhor possível.
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(tf_cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    num_training_iter = 2000
    for iteration in range(num_training_iter):
        for (x, y) in zip(train_house_size_norm, train_price_norm):
            sess.run(optimizer, feed_dict={tf_house_size: x, tf_price: y})

    training_cost = sess.run(tf_cost, feed_dict={tf_house_size: train_house_size_norm, tf_price: train_price_norm})

    def normalize_single_value(value, array):
        return (value - array.mean()) / array.std()

    def denormalize(value, array):
        return value * array.std() + array.mean()

    dados = []

    for (size, price) in zip(test_house_size, test_house_price):
        value = normalize_single_value(size, house_size)
        price_prediction = sess.run(tf_price_pred, feed_dict={tf_house_size:value})
        price_prediction = denormalize(price_prediction, house_price )
        dados.append(price_prediction)
        print("House size:",size, " Original price:", price, " Price Prediction:", price_prediction, "Diff:", (price_prediction - price))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(test_house_price, color='lightblue', linewidth=3)
ax.plot(dados, color='darkgreen')
plt.show()


from utils.http.XBAPIView import *
from .serializers import *
from utils.pay.wx.xcu import Openid

class OpenidViewSet(XBListModelMixin):
    serializer_class = OpenIdSerializers
    # 获取用户openid(用户注册或者微信登陆)
    many = False
    def get_queryset(self):

        code = self.request.query_params.get("code", 0)
        if code:
            data = Openid(code=code).openid()
            if data:
                return data
            else:
                None
        else:
            return None


import matplotlib.pyplot as plt
import numpy as np
import math
import csv
EWx = list()
EWy = list()
NSx = list()
NSy = list()
UDx = list()
UDy = list()
with open('elcentro_EW.dat', 'r') as csvfile:
	plots = csv.reader(csvfile, delimiter=" ")
	for row in plots:
		EWx.append(float(row[0]))
		EWy.append(float(row[1]))
with open('elcentro_NS.dat', 'r') as csvfile:
	plots = csv.reader(csvfile, delimiter=" ")
	for row in plots:
		NSx.append(float(row[0]))
		NSy.append(float(row[1]))
with open('elcentro_UP.dat', 'r') as csvfile:
	plots = csv.reader(csvfile, delimiter=" ")
	for row in plots:
		UDx.append(float(row[0]))
		UDy.append(float(row[1]))
plt.subplot(3,1,1)
plt.plot(EWx, EWy)
plt.title("EW")
plt.ylabel("Acceleration[g]")
plt.subplot(3,1,2)
plt.plot(NSx, NSy)
plt.title("NS")
plt.ylabel("Acceleration[g]")
plt.subplot(3,1,3)
plt.plot(UDx, UDy)
plt.ylabel("Acceleration[g]")
plt.title("UD")
plt.xlabel("Time[sec]")
plt.show()
PGA = max(NSy)
intesity = 3*(math.log10(PGA)+0.5)
print(PGA)
print(intesity)
FNSy = np.fft.fft(NSy)
freq = np.linspace(0.0,1.0/(2.0*(NSx[1]-NSx[0])),len(FNSy)/2)
plt.figure()
plt.plot(freq, 2.0/len(FNSy)*np.abs(FNSy[:len(FNSy)//2]))
plt.ylabel("Magnitude")
plt.xlabel("Frequency[Hz]")
plt.show()
PerdominantFrequency = max(FNSy)
print(np.abs(PerdominantFrequency))
plt.specgram(NSy,Fs=int(1.0/(NSx[1]-NSx[0])),NFFT=1024,noverlap=900)
plt.show()


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login/", views.login_view, name="login"),
    path("logout/", views.logout_view, name="logout"),
    path("register/", views.register, name="register"),
    path("create/", views.create, name="create"),
    path("listing/<int:id>/", views.listing, name="listing"),
    path("category/", views.category, name="category"),
    path("watchlist/", views.watchlist, name="watchlist"),
    path("delete/<int:listing_id>", views.delete, name="delete"),
    path("comments/", views.comments, name="comments"),
    path("add_bid/", views.add_bid, name="add_bid"),
    path("close_bid/", views.close_bid, name="close_bid")

]


import string
import collections
import random
from wordcloud import WordCloud
import matplotlib.pyplot as plt

class TrieNode():
    def __init__(self):
        self.children = {}
        self.isEnd = False

class Trie():
    def __init__(self):
        # Initialising the trie structure.
        self.root = TrieNode()
        self.word_list = []

    def add_Child(self, word):
        node = self.root
        if len(word) == 0:
            self.isEnd = True
            return
        for ch in word:
            if not node.children.get(ch):
                node.children[ch] = TrieNode()
            node = node.children[ch]
        node.isEnd = True

    def search_word(self,startOfSentence):
        words = startOfSentence.split(" ")
        prefix = words[-1]
        prev = startOfSentence[:-len(prefix)]
        node = self.root
        not_found = False
        temp_word = ''
        for ch in prefix:
            if not node.children.get(ch):
                not_found = True
                break

            temp_word += ch
            node = node.children[ch]
        if not_found:
            return 0
        elif node.isEnd and not node.children:
            return -1
        self.DFS(node, temp_word, prev)
        return self.word_list

    def DFS(self, node, word,prev):
        if node.isEnd:
            self.word_list.append(prev + word)

        for ch,node in node.children.items():
            self.DFS(node, word + ch,prev)

class project:
    def __init__(self):
        with open('1000_common_words.txt', 'r') as file2:
            self.common = file2.read().replace('\\n', '')
        with open('sherlock.txt', 'r') as file:
            self.text = file.read().replace('\\n', '')

    def text_to_string(self):
     # split into words by white space
        self.words = self.text.split()

        # remove punctuation from each word

        table = str.maketrans('', '', string.punctuation)
        self.filtered_words = [w.translate(table) for w in self.words]

        #replace common words
        self.filtered_words_common = [word for word in self.filtered_words if word.lower() not in self.common]

        # Counter
        self.count_All = collections.Counter(self.filtered_words)

        # Counter chapter wise
        self.Chapters = self.text.split('ADVENTURE')
        self.word_Chapters =[]
        for i in range(1,len(self.Chapters)):
            word = self.Chapters[i].split()
            word = [w.translate(table) for w in word]
            self.word_Chapters.append(word)
        self.count_ChapterWise = [collections.Counter(text) for text in self.word_Chapters]

    def getTotalNumberOfWords(self):
        return len(self.filtered_words)

    def getTotalUniqueWords(self):
        return len(self.count_All)

    def get20MostFrequentWords(self):
        return list(self.count_All.most_common(20))

    def get20MostInterestingFrequentWords(self):
        self.unique_remove_common = collections.Counter(self.filtered_words_common)
        return list(self.unique_remove_common.most_common(20))

    def get20LeastFrequentWords(self):
        return list(self.count_All.most_common()[:-20-1:-1])

    def getFrequencyOfWord(self,word):
        self.count =[]
        for i in range(len(self.word_Chapters)):
            self.count.append(self.count_ChapterWise[i][word])
        return self.count

    def getChapterQuoteAppears(self, quote):
        for i in range(len(self.Chapters)):
            if quote in self.Chapters[i].replace('\\n', ''):
                return "Chapter:"+ str(i)
        return "Quote Not Found"

    def generateCache(self):
        self.cache ={}
        def pair():
            for i in range(len(self.filtered_words)-2):
                yield (self.filtered_words[i], self.filtered_words[i+1])
        for w1,w2 in pair():
            if w1 in self.cache:
                self.cache[w1].append(w2)
            else:
                self.cache[w1] =[w2]

    def generateSentence(self, curWord, size =22):
        gen_words =[]
        random_index =0
        self.generateCache()
        for i in range(size):
            gen_words.append(curWord)
            random_index= random.randint(0,len(self.cache[curWord])-1)
            curWord = self.cache[curWord][random_index]
        return ' '.join(gen_words)

    def getAutocompleteSentence(self, startOfSentence):
        trie = Trie()
        for word in self.filtered_words:
            trie.add_Child(word)
        return trie.search_word(startOfSentence)
p = project()
p.text_to_string()
print("----------getTotalNumberOfWords----------")
print(p.getTotalNumberOfWords())
print()
print("------------getTotalUniqueWords------------")
print(p.getTotalUniqueWords())
print()
print("------------get20MostFrequentWords------------")
print(p.get20MostFrequentWords())
print()
print("------------get20MostInterestingFrequentWords------------")
print(p.get20MostInterestingFrequentWords())
print()
print("------------get20LeastFrequentWords---------------")
print(p.get20LeastFrequentWords())
print()
print("------------getFrequencyOfWord------------")
print(p.getFrequencyOfWord('Watson'))
print()
print("------------getChapterQuoteAppears------------")
print(p.getChapterQuoteAppears("There is nothing more deceptive than an obvious fact"))
print()
print("------------generateSentence----------------")
print(p.generateSentence('Sherlock'))
print()
print("------------getAutocompleteSentence------------")
print(p.getAutocompleteSentence('I have no do'))

# Visualization
d1 = dict(p.get20MostFrequentWords())
d2 = dict(p.get20MostInterestingFrequentWords())
d3 = dict(p.get20LeastFrequentWords())
wordcloud = WordCloud()
wordcloud.generate_from_frequencies(frequencies=d2)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

f, ax = plt.subplots()

freq = p.getFrequencyOfWord('Sherlock')
chapter = [1,2,3,4,5,6,7,8,9,10,11,12]
p = plt.bar(chapter, freq,color='#2e8b57')
plt.title('Usage of "Sherlock" per chapter')
plt.xlabel("Chapters")
plt.ylabel("Frequency")
ax.set_xticks(range(1, 13))
ax.set_xticklabels(chapter)
p[8].set_color('#8B0000')
p[9].set_color('#8B0000')
p[10].set_color('#8B0000')
p[11].set_color('#8B0000')
plt.show()


import struct

import numpy as np

from ..utils.enum_utils import IntEnum
from .defs import *

class CovarianceType(IntEnum):
    COVARIANCE_TYPE_UNKNOWN = 0
    COVARIANCE_TYPE_APPROXIMATED = 1
    COVARIANCE_TYPE_DIAGONAL_KNOWN = 2
    COVARIANCE_TYPE_KNOWN = 3

class ROSPoseMessage(MessagePayload):
    """!
    @brief ROS `Pose` message (@ref MessageType::ROS_POSE)

    The relative change in ENU position since the time of the first @ref PoseMessage, resolved in the local ENU frame at
    the time of the first @ref PoseMessage.
    """
    MESSAGE_TYPE = MessageType.ROS_POSE
    MESSAGE_VERSION = 0

    _FORMAT = '< 3d 4d'
    _SIZE: int = struct.calcsize(_FORMAT)

    def __init__(self):
        # The time of the message, in P1 time (beginning at power-on)
        self.p1_time = Timestamp()
        # relative ENU position (see ros.h messages)
        self.position_rel_m = np.full((3,), np.nan)
        # The platform orientation, represented as a quaternion with the scalar
        # component last (x, y, z, w)
        self.orientation = np.full((4,), np.nan)

    def pack(self, buffer: bytes = None, offset: int = 0, return_buffer: bool = True) -> (bytes, int):
        if buffer is None:
            buffer = bytearray(self.calcsize())

        initial_offset = offset

        offset += self.p1_time.pack(buffer, offset, return_buffer=False)

        struct.pack_into(ROSPoseMessage._FORMAT, buffer, offset,
                         self.position_rel_m[0], self.position_rel_m[1], self.position_rel_m[2],
                         self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3])

        if return_buffer:
            return buffer
        else:
            return offset - initial_offset

    def unpack(self, buffer: bytes, offset: int = 0, message_version: int = MessagePayload._UNSPECIFIED_VERSION) -> int:
        initial_offset = offset

        offset += self.p1_time.unpack(buffer, offset)
        (self.position_rel_m[0], self.position_rel_m[1], self.position_rel_m[2],
         self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3]) = \
            struct.unpack_from(ROSPoseMessage._FORMAT,
                               buffer=buffer, offset=offset)
        offset += ROSPoseMessage._SIZE
        return offset - initial_offset

    def __repr__(self):
        return '%s @ %s' % (self.MESSAGE_TYPE.name, self.p1_time)

    def __str__(self):
        string = 'ROS Pose Message @ %s\n' % str(self.p1_time)
        string += '  Position : %.6f, %.6f, %.6f \n' % tuple(self.position_rel_m)
        string += '  Orientation: %.12f, %.12f, %.12f, %.12f \n' % tuple(self.orientation)
        return string

    @classmethod
    def calcsize(cls) -> int:
        return Timestamp.calcsize() + ROSPoseMessage._SIZE

    @classmethod
    def to_numpy(cls, messages):
        result = {
            'p1_time': np.array([float(m.p1_time) for m in messages]),
            'position_rel_m': np.array([m.position_rel_m for m in messages]).T,
            'orientation': np.array([m.orientation for m in messages]).T,
        }
        return result

class ROSGPSFixMessage(MessagePayload):
    """!
    @brief ROS `GPSFix` message (MessageType::ROS_GPS_FIX).
    """
    MESSAGE_TYPE = MessageType.ROS_GPS_FIX
    MESSAGE_VERSION = 0

    _FORMAT = '< ddd ddd ddd d ddddd dddddddddd 9d B 3B'
    _SIZE: int = struct.calcsize(_FORMAT)

    def __init__(self):
        self.p1_time = Timestamp()
        # Geodetic Position (expressed using the WGS-84 ellipsoid)
        # Geodetic latitude (in degrees)
        self.latitude_deg = np.nan
        # Geodetic longitude (in degrees)
        self.longitude_deg = np.nan
        # Altitude above the ellipsoid (in meters)
        self.altitude_m = np.nan

        # Velocity
        # The vehicle direction from north (in degrees)
        self.track_deg = np.nan
        # The vehicle ground speed (in meters/second)
        self.speed_mps = np.nan
        # The vehicle vertical speed (in meters/second)
        self.climb_mps = np.nan

        # Vehicle Orientation (not supported)
        self.pitch_deg = np.nan
        self.roll_deg = np.nan
        self.dip_deg = np.nan

        # The GPS time of the message (in seconds), referenced to 1980/1/6
        self.gps_time = np.nan

        # Dilution Of Precision
        self.gdop = np.nan  # Geometric (position + time) DOP
        self.pdop = np.nan  # Positional (3D) DOP
        self.hdop = np.nan  # Horizontal DOP
        self.vdop = np.nan  # Vertical DOP
        self.tdop = np.nan  # Time DOP

        # Measurement Uncertainty (95% Confidence)
        # Spherical position uncertainty (in meters) [epe]
        self.err_3d_m = np.nan
        # Horizontal position uncertainty (in meters) [eph]
        self.err_horiz_m = np.nan
        # Vertical position uncertainty (in meters) [epv]
        self.err_vert_m = np.nan
        self.err_track_deg = np.nan  # Track uncertainty (in degrees) [epd]
        # Ground speed uncertainty (in meters/second) [eps]
        self.err_speed_mps = np.nan
        # Vertical speed uncertainty (in meters/second) [epc]
        self.err_climb_mps = np.nan
        self.err_time_sec = np.nan  # Time uncertainty (in seconds) [ept]
        self.err_pitch_deg = np.nan  # Pitch uncertainty (in degrees)
        self.err_roll_deg = np.nan  # Roll uncertainty (in degrees)
        self.err_dip_deg = np.nan   # Dip uncertainty (in degrees)

        # Position Covariance
        """
            The 3x3 position covariance matrix (in m^2), resolved in the local ENU
            frame. Values are stored in row-major order.
        """
        self.position_covariance_m2 = np.full((9,), np.nan)
        # The method in which @ref position_covariance_m2 was populated
        self.position_covariance_type = CovarianceType.COVARIANCE_TYPE_UNKNOWN

        self.reserved = np.full((3,), 0, np.uint8)

    def pack(self, buffer: bytes = None, offset: int = 0, return_buffer: bool = True) -> (bytes, int):
        if buffer is None:
            buffer = bytearray(self.calcsize())

        initial_offset = offset

        offset += self.p1_time.pack(buffer, offset, return_buffer=False)

        struct.pack_into(ROSGPSFixMessage._FORMAT, buffer, offset,
                         self.latitude_deg,
                         self.longitude_deg,
                         self.altitude_m,
                         self.track_deg,
                         self.speed_mps,
                         self.climb_mps,
                         self.pitch_deg,
                         self.roll_deg,
                         self.dip_deg,
                         self.gps_time,
                         self.gdop,
                         self.pdop,
                         self.hdop,
                         self.vdop,
                         self.tdop,
                         self.err_3d_m,
                         self.err_horiz_m,
                         self.err_vert_m,
                         self.err_track_deg,
                         self.err_speed_mps,
                         self.err_climb_mps,
                         self.err_time_sec,
                         self.err_pitch_deg,
                         self.err_roll_deg,
                         self.err_dip_deg,
                         self.position_covariance_m2[0], self.position_covariance_m2[1], self.position_covariance_m2[2],
                         self.position_covariance_m2[3], self.position_covariance_m2[4], self.position_covariance_m2[5],
                         self.position_covariance_m2[6], self.position_covariance_m2[7], self.position_covariance_m2[8],
                         self.position_covariance_type,
                         self.reserved[0], self.reserved[1], self.reserved[2])

        if return_buffer:
            return buffer
        else:
            return offset - initial_offset

    def unpack(self, buffer: bytes, offset: int = 0, message_version: int = MessagePayload._UNSPECIFIED_VERSION) -> int:
        initial_offset = offset

        offset += self.p1_time.unpack(buffer, offset)
        (self.latitude_deg,
         self.longitude_deg,
         self.altitude_m,
         self.track_deg,
         self.speed_mps,
         self.climb_mps,
         self.pitch_deg,
         self.roll_deg,
         self.dip_deg,
         self.gps_time,
         self.gdop,
         self.pdop,
         self.hdop,
         self.vdop,
         self.tdop,
         self.err_3d_m,
         self.err_horiz_m,
         self.err_vert_m,
         self.err_track_deg,
         self.err_speed_mps,
         self.err_climb_mps,
         self.err_time_sec,
         self.err_pitch_deg,
         self.err_roll_deg,
         self.err_dip_deg,
         self.position_covariance_m2[0], self.position_covariance_m2[1], self.position_covariance_m2[2],
         self.position_covariance_m2[3], self.position_covariance_m2[4], self.position_covariance_m2[5],
         self.position_covariance_m2[6], self.position_covariance_m2[7], self.position_covariance_m2[8],
         self.position_covariance_type,
         self.reserved[0], self.reserved[1], self.reserved[2]) = \
            struct.unpack_from(ROSGPSFixMessage._FORMAT,
                               buffer=buffer, offset=offset)
        offset += ROSGPSFixMessage._SIZE
        return offset - initial_offset

    def __repr__(self):
        return '%s @ %s' % (self.MESSAGE_TYPE.name, self.p1_time)

    def __str__(self):
        string = 'ROS GPSFix Message @ %s\n' % str(self.p1_time)
        string += '  Position : %.8f, %.8f, %.2f \n' % (
            self.latitude_deg, self.longitude_deg, self.altitude_m)
        string += '  Velocity: %.6f, %.6f, %.6f\n' % (
            self.track_deg, self.speed_mps, self.climb_mps)
        return string

    @classmethod
    def calcsize(cls) -> int:
        return Timestamp.calcsize() + ROSGPSFixMessage._SIZE

class ROSIMUMessage(MessagePayload):
    """!
    @brief ROS `Imu` message (@ref MessageType::ROS_IMU)
    """
    MESSAGE_TYPE = MessageType.ROS_IMU
    MESSAGE_VERSION = 0

    _FORMAT = '< 4d 9d 3d 9d 3d 9d'
    _SIZE: int = struct.calcsize(_FORMAT)

    def __init__(self):
        self.p1_time = Timestamp()
        # The platform orientation, represented as a quaternion with the scalar
        # component last (x, y, z, w)
        self.orientation = np.full((4,), 0.0)
        # Orientation covariance matrix. Set to -1 if not available
        self.orientation_covariance = np.full((9,), -1.0)
        # Vehicle x/y/z rate of rotation (in radians/second), resolved in the body
        # frame
        self.angular_velocity_rps = np.full((3,), 0.0)
        # Vehicle rate of rotation covariance matrix. Set to -1 if not available
        self.angular_velocity_covariance = np.full((9,), -1.0)
        # Vehicle x/y/z linear acceleration (in meters/second^2), resolved in the
        # body frame
        self.acceleration_mps2 = np.full((3,), 0.0)
        # Vehicle x/y/z acceleration covariance matrix. Set to -1 if not available
        self.acceleration_covariance = np.full((9,), -1.0)

    def pack(self, buffer: bytes = None, offset: int = 0, return_buffer: bool = True) -> (bytes, int):
        if buffer is None:
            buffer = bytearray(self.calcsize())

        initial_offset = offset

        offset += self.p1_time.pack(buffer, offset, return_buffer=False)

        struct.pack_into(
            ROSIMUMessage._FORMAT, buffer, offset,
            self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3],
            self.orientation_covariance[0], self.orientation_covariance[1], self.orientation_covariance[2],
            self.orientation_covariance[3], self.orientation_covariance[4], self.orientation_covariance[5],
            self.orientation_covariance[6], self.orientation_covariance[7], self.orientation_covariance[8],
            self.angular_velocity_rps[0], self.angular_velocity_rps[1], self.angular_velocity_rps[2],
            self.angular_velocity_covariance[0], self.angular_velocity_covariance[1], self.angular_velocity_covariance[2],  # noqa
            self.angular_velocity_covariance[3], self.angular_velocity_covariance[4], self.angular_velocity_covariance[5],  # noqa
            self.angular_velocity_covariance[6], self.angular_velocity_covariance[7], self.angular_velocity_covariance[8],  # noqa
            self.acceleration_mps2[0], self.acceleration_mps2[1], self.acceleration_mps2[2],
            self.acceleration_covariance[0], self.acceleration_covariance[1], self.acceleration_covariance[2],
            self.acceleration_covariance[3], self.acceleration_covariance[4], self.acceleration_covariance[5],
            self.acceleration_covariance[6], self.acceleration_covariance[7], self.acceleration_covariance[8],
            )

        if return_buffer:
            return buffer
        else:
            return offset - initial_offset

    def unpack(self, buffer: bytes, offset: int = 0, message_version: int = MessagePayload._UNSPECIFIED_VERSION) -> int:
        initial_offset = offset

        offset += self.p1_time.unpack(buffer, offset)
        (self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3],
         self.orientation_covariance[0], self.orientation_covariance[1], self.orientation_covariance[2],
         self.orientation_covariance[3], self.orientation_covariance[4], self.orientation_covariance[5],
         self.orientation_covariance[6], self.orientation_covariance[7], self.orientation_covariance[8],
         self.angular_velocity_rps[0], self.angular_velocity_rps[1], self.angular_velocity_rps[2],
         self.angular_velocity_covariance[0], self.angular_velocity_covariance[1], self.angular_velocity_covariance[2],
         self.angular_velocity_covariance[3], self.angular_velocity_covariance[4], self.angular_velocity_covariance[5],
         self.angular_velocity_covariance[6], self.angular_velocity_covariance[7], self.angular_velocity_covariance[8],
         self.acceleration_mps2[0], self.acceleration_mps2[1], self.acceleration_mps2[2],
         self.acceleration_covariance[0], self.acceleration_covariance[1], self.acceleration_covariance[2],
         self.acceleration_covariance[3], self.acceleration_covariance[4], self.acceleration_covariance[5],
         self.acceleration_covariance[6], self.acceleration_covariance[7], self.acceleration_covariance[8],
         ) = \
            struct.unpack_from(ROSIMUMessage._FORMAT,
                               buffer=buffer, offset=offset)
        offset += ROSIMUMessage._SIZE
        return offset - initial_offset

    def __repr__(self):
        return '%s @ %s' % (self.MESSAGE_TYPE.name, self.p1_time)

    def __str__(self):
        string = 'ROS IMU Message @ %s\n' % str(self.p1_time)
        string += '  Orientation: %.6f, %.6f, %.6f, %.6f \n' % tuple(
            self.orientation)
        string += '  Angular Velocity: %.6f, %.6f, %.6f \n' % tuple(
            self.angular_velocity_rps)
        string += '  Acceleration: %.6f, %.6f, %.6f \n' % tuple(
            self.acceleration_mps2)
        return string

    @classmethod
    def calcsize(cls) -> int:
        return Timestamp.calcsize() + ROSIMUMessage._SIZE


from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django.contrib.auth.models import Group
from zope.interface import named

class CustomUser(AbstractUser):
    ROLE = [
        (1, "Regular"),
        (2, "Administrator"),
    ]
    phone = models.CharField(max_length=10)
    role = models.PositiveSmallIntegerField(choices=ROLE, default=1)

@receiver(signal=post_save, sender=CustomUser)
def user_created(sender, instance, created, **kwargs):
    reg, created = Group.objects.get_or_create(name="Regular")
    admin, created = Group.objects.get_or_create(name="Administrator")
    if created:
        if instance.role == 1:
            print("Regular")
            reg.user_set.add(instance)
        else:
            print("Admin")
            admin.user_set.add(instance)
    else:
        if instance.role == 1:
            admin.user_set.remove(instance)
            reg.user_set.add(instance)
        else:
            reg.user_set.remove(instance)
            admin.user_set.add(instance)


import motor.motor_asyncio
from decouple import config

def mongo_client():
    database_url = config("DATABASE_URL")
    return motor.motor_asyncio.AsyncIOMotorClient(database_url).lightning

db = mongo_client()

def mongo_query(f):
    async def decorator(*args, **kwargs):
        result = await f(*args, **kwargs)
        return result or {}

    return decorator

@mongo_query
async def config_guild(guild_id, channel_id):
    return await db.guild_config.update_one(
        {"guild_id": guild_id},
        {
            "$set": {
                "guild_id": guild_id,
                "channel_id": channel_id,
            }
        },
        upsert=True,
    )

async def get_guild_config(guild_id):
    return await db.guild_config.find_one({"guild_id": guild_id})

@mongo_query
async def check_in_progress_lightning_talk(guild_id):
    return await db.lightning_talks.find_one(
        {
            "guild_id": guild_id,
            "in_progress": True,
        }
    )

@mongo_query
async def create_lightning_talk(guild_id, message_id):
    return await db.lightning_talks.insert_one(
        {
            "guild_id": guild_id,
            "message_id": message_id,
            "speakers_queue": [],
            "speakers": {},
            "in_progress": True,
            "open_registration": True,
            # TODO: created_at, updated_at
        }
    )

@mongo_query
async def close_lightning_talk(guild_id, message_id):
    return await db.lightning_talks.update_one(
        {
            "guild_id": guild_id,
            "message_id": message_id,
        },
        {
            "$set": {"open_registration": False},
        },
    )

@mongo_query
async def close_lightning_talk(guild_id, message_id):
    return await db.lightning_talks.update_one(
        {
            "guild_id": guild_id,
            "message_id": message_id,
        },
        {
            "$set": {"open_registration": False},
        },
    )

@mongo_query
async def set_speakers_order(lt, speakers):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$set": {"speakers": speakers},
        },
    )

@mongo_query
async def add_speaker_to_queue(lt, user):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$push": {"speakers_queue": user},
        },
    )

@mongo_query
async def remove_speaker_from_queue(lt, user):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$pull": {"speakers_queue": user},
        },
    )

@mongo_query
async def invite_speaker(lt, user_id, invite_message_id):
    lt["speakers"][str(user_id)]["invited"] = True
    lt["speakers"][str(user_id)]["invite_message_id"] = invite_message_id
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$set": {"speakers": lt["speakers"]},
        },
    )

@mongo_query
async def finish_lightning_talk(lt):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {"$set": {"in_progress": False}},
    )


# -*- coding: utf-8 -*-
"""
Created on Sun Dec 10 13:15:12 2017

@author: abaena
"""

#******************************************************************************
#Add logmapper-agent directory to python path for module execution
#******************************************************************************
if __name__ == '__main__':
    import os, sys
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..','..')))
#******************************************************************************

import logging
import threading
from enum import Enum
import datetime
import time

import logmappercommon.utils.logmapper_util as lmutil

logger = logging.getLogger(__name__)

#%%
#==============================================================================
#
#==============================================================================

class ThreadState(Enum):
    CREATED = 0
    STARTING = 1
    PROCESSING = 2
    IDLE = 3
    ENDING=4
    ENDED=5
    ERROR = 99

class OperationMode(Enum):
    NORMAL = 0
    TEST = 1

class ActionState(Enum):
    IDLE = 0
    PENDING = 1
    EXECUTING = 2

class LogMapperThread(threading.Thread):
    """
    ===========================================================================
    Thread Class for general purpose
    ===========================================================================

    **Methods**:
        start: (from Thread): Start Thread. Call run method
        stopRun: safe stop of the thread
    **Attributes**:
        None
    """
    def __init__(self, category, key, operationMode = OperationMode.NORMAL):
        threading.Thread.__init__(self)
        self.setName(category+key)
        self.key = key
        self.operationMode = operationMode
        self._stopRun = False
        self._state = ThreadState.CREATED
        self.errorDetail = None
        self._action = None
        self._actionState = ActionState.IDLE
        self.startDate = None
        self.endDate = None
        self.loopCount = 0
        self.loopCountBefore = 0
        self.bytesProcessed = 0
        self.bytesProcessedBefore = 0
        self.recordsProcessed = 0
        self.recordsProcessedBefore = 0
        self.fails = 0
        self.failsBefore = 0
        self.currentDate = None
        self.nextSaveDataDate = None
        self.enableDataSend = True

    def getCounters(self):
        d = {
                'count' : self.loopCount - self.loopCountBefore,
                'bytes' : self.bytesProcessed - self.bytesProcessedBefore,
                'records' : self.recordsProcessed - self.recordsProcessedBefore,
                'fails' : self.fails - self.failsBefore,
                }
        self.loopCountBefore = self.loopCount
        self.bytesProcessedBefore = self.bytesProcessed
        self.recordsProcessedBefore = self.recordsProcessed
        self.failsBefore = self.fails
        return d

    def stopRun(self):
        """
        Ask thread end safely
        """
        logger.debug("stopRun:"+self.name)
        self._stopRun = True

    def setState(self, state):
        logger.debug("setState:"+self.name+':'+str(state))
        self._state = state

    def getState(self):
        logger.debug("getState:"+self.name+':'+str(self._state))
        return self._state

    def setStateError(self, errorDetail):
        logger.error("setStateError:"+errorDetail)
        self.setState(ThreadState.ERROR)
        self.errorDetail = errorDetail

    def startAction(self, action):
        logger.debug("setAction:"+action)
        if self._actionState != ActionState.IDLE:
            return "BUSY"
        self._action = action
        self._actionState = ActionState.PENDING
        return "OK"

    def getActionState(self):
        logger.debug("getActionState:"+str(self._actionState))
        return (self._action, self._actionState)

    def run(self):
        logger.debug ("Start Thread:" + self.name)
        try:
            self.setState(ThreadState.STARTING)
            self.startDate=datetime.datetime.now()
            self.currentDate = datetime.date.today()
            self.nextSaveDataDate = lmutil.getNextLogMapperIntervalDate(self.startDate)
            self.process()
            self.setState(ThreadState.ENDED)
            self.endDate = datetime.datetime.now()
        except Exception as exc:
            self.setStateError(str(exc))
            logger.exception("Exception in "+self.name)

        logger.debug ("End Thread:" + self.name)

    def process(self):
        logger.debug("process:")

        self.setState(ThreadState.PROCESSING)
        while not self._stopRun:
            self.loopCount += 1
            time.sleep(10)
        self.setState(ThreadState.ENDING)

#%%
#==============================================================================
#
#==============================================================================

def getThread(name):
    """
    ===========================================================================
    Search in All Active Threads. Return the Thread Class with the same name
    ===========================================================================

    **Args**:
        name (str): name of the thread that is looking
    **Returns**:
        Thread object. None if it is not found
    """
    logger.debug('getThread:'+name)
    for t in threading.enumerate():
        if t.getName() == name:
           return t
    return None

#%%
"""
*******************************************************************************
Module Execution
This code helps to developer to know the usage of the module
*******************************************************************************
"""
if __name__ == '__main__':
    print('Start module execution:')
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%H:%M:%S')


import numpy as np, matplotlib.pyplot as plt
import pumpprobe as pp

# DATASETS WITH STIMULATION OF RID
ds_list = [
    "/projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_101248/",
    "/projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_152524/"
    ]

funa = pp.Funatlas.from_datasets(
                ds_list,
                merge_bilateral=True,merge_dorsoventral=False,merge_AWC=True)

occ1, occ2 = funa.get_occurrence_matrix()
occ3 = funa.get_observation_matrix()
ai_RID = funa.ids_to_i("RID")

fnames = ["external_data/GenesExpressing-npr-4-thrs2.csv",
          "external_data/GenesExpressing-npr-11-thrs2.csv",
          "external_data/GenesExpressing-pdfr-1-thrs2.csv",
          "external_data/GenesExpressing-daf-2-thrs2.csv"]

trans_rec_pairs = ["FLP-14,NPR-4",
                   "FLP-14,NPR-11",
                   "PDF-1,PDFR-1",
                   "INS-17,DAF-2"]

trans_exp_level = np.array([110634.0,110634.0,157972.0,1505.0])

'''excl_neu = [
    # Exclude neurons that are never observed
    "AIA_","RIB_","RIR_","RIF_","AUA_","RIR_","RMG_","RMF_","AIY_","ADF_","ALA","AVB_","RIS_","AVF_","RMH_","RIP_"
    # and that have not been observed in the recordings that I am using
    "ASK_","BAG_","RIC_","RIS_","ADE_","URB_","MC_",
    ]'''

x = np.arange(len(funa.head_ai))
exp_levels = np.zeros((len(fnames),len(funa.head_ai)))
for i_f in np.arange(len(fnames)):
    f = open(fnames[i_f],"r")
    lines = f.readlines()
    f.close()

    exp_levels_ = np.zeros(funa.n_neurons)

    for line in lines[1:]:
        s = line.split(",")
        cell_id = s[1][1:-1]
        exp_level = float(s[2])

        ai = funa.ids_to_i(cell_id)
        exp_levels_[ai] = exp_level*trans_exp_level[i_f]

    b = funa.reduce_to_head(exp_levels_)
    exp_levels[i_f] = b

# Sort them by the sum of the expression levels
tot_exp_levels = np.sum(exp_levels,axis=0)
bargsort = np.argsort(tot_exp_levels)[::-1]
lim = np.where(tot_exp_levels[bargsort]==0)[0][0]

fig = plt.figure(1,figsize=(16,9))
ax = fig.add_subplot(111)
x = np.arange(len(funa.head_ai))

prev_b = np.zeros(len(funa.head_ai))
for i_f in np.arange(len(fnames)):
    b = exp_levels[i_f][bargsort]

    # Set to zero the neurons that are never observed
    for i_hn in np.arange(len(funa.head_ids)):
        id_hn = str(funa.head_ids[bargsort][i_hn])
        ai_hn = funa.ids_to_i(id_hn)
        if occ3[ai_hn,ai_RID]==0:
            b[i_hn] = 0

    ax.bar(x,b,label=trans_rec_pairs[i_f],bottom=prev_b)
    prev_b += b

#ax.set_xticks(x[:lim])
ax.set_xticks(x)
#ax.set_xticklabels(funa.head_ids[bargsort][:lim],rotation=90,fontsize=9)
ax.set_xticklabels(funa.head_ids[bargsort],rotation=90,fontsize=9)
#ax.set_xlim(-1,lim)
ax.set_ylabel("CeNGEN transmitter count in RID * receptor counts in downstream neurons")
ax.set_title("\"Expected\" amplitude of reponse based on receptor and transmitter \n"+\
             "expression count from CeNGEN (transmitter,receptor)")
ax.legend()

# Manually compiled downstream neurons
'''downst_in_data = [
    # From the first 2D measurement (look it up, it's in the targeting tests)
    "AVD_",
    # From /projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_101248/
    # stim1
    "AVAL","AVEL","RMDVL","AVDL","AWBL","AVER","OLQD",
    # stim2
    "AVAL","SMDDL","RMDL","AVEL","RMDVL","SMBDL","AVDL","RMDDL","AWBL","RIG","RMDD_","CEPDL","AIMR","RIMR","OLQD","AVJR",
    # From /projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_152524/
    # stim 1 (note that some are inhibitory here, but were excitatory in the other recording. and some pairs are one exc and one inh, like AVE)
    "RMDVL","AVDL","AVJL","M2L","AVEL","I2L","RIG","RIG","AIBL","AVER","RMDVR","AVDR",
    # stim 2 (here both AVER and AVEL are inh)
    "AVDL","M2L","RMEL","AVEL","RIG","IL2VL","AIBL","RIVL","M1","URYDR","AVER","AIBR","RMDR","RMDVR","AVDR",
    ]

downst_in_data = funa.approximate_ids(downst_in_data,merge_bilateral=True,merge_dorsoventral=False,merge_AWC=True)
downst_in_data, downst_in_data_counts = np.unique(downst_in_data,return_counts=True)'''

downst_in_data_ai = np.where(occ1[:,ai_RID]>0)[0]

downst_in_data_ai_head = funa.ai_to_head(downst_in_data_ai)
exp_bars = np.zeros_like(tot_exp_levels)
exp_bars[downst_in_data_ai_head] = occ1[downst_in_data_ai,ai_RID]/occ3[downst_in_data_ai,ai_RID]
exp_bars = exp_bars[bargsort]
new_lim = np.max(np.where(exp_bars!=0)[0])

axb = ax.twinx()
#axb.bar(x,exp_bars,color="#9467bd",alpha=0.6,label="response count")
h_ais_barg = funa.head_ai[bargsort]
for i in np.arange(exp_bars.shape[0]):
    if occ3[h_ais_barg[i],ai_RID]>0:
        axb.plot((i,i),(0,1),c="purple",marker="x",alpha=0.3)
    if exp_bars[i]>0:
        if i==0:
            axb.plot((i,i),(0,exp_bars[i]),c="k",marker="x",label="fractional response count")
        else:
            axb.plot((i,i),(0,exp_bars[i]),c="k",marker="x")

#if new_lim>lim:
    #ax.set_xlim(-1,new_lim+1)
    #ax.set_xticks(x[:new_lim+1])
    #new_ticklabels = funa.head_ids[bargsort][:new_lim+1].copy()
    #ax.set_xticklabels(new_ticklabels,rotation=90,fontsize=9)
axb.set_ylim(0,)
axb.set_ylabel("Fraction of times neurons responded in experiments")
axb.legend(loc=5)

fig.tight_layout()
plt.show()