#%% ################################## CONFIG ##################################
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--vs', type=str, default='127.0.0.1', help='visdom server')
parser.add_argument('--vp', type=str, default='8097', help='visdom port')
parser.add_argument('--rs', type=str, default='127.0.0.1', help='Rstudio server')
parser.add_argument('--rp', type=str, default='8787', help='Rstudio port')
parser.add_argument('--ds', type=str, default='127.0.0.1', help='dash server')
parser.add_argument('--dp', type=str, default='8050', help='dash port')
args = parser.parse_args()

import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
# rstudio-server start/stop/restart # /etc/rstudio/rserver.conf
# python -m visdom.server -p 8097 --hostname 127.0.0.1
config = {}
config['visdom-server'] = 'http://' + args.vs
config['visdom-port'] = args.vp
config['R-server'] = 'http://' + args.rs
config['R-port'] = args.rp
config['dash-server'] = args.ds
config['dash-port'] = args.dp
#import torch
#import torch.nn as nn
#from visdom import Visdom
#vis = Visdom(server=config['visdom-server'], port=config['visdom-port'], env='main') # python -m visdom.sever [-post, --hostname]
#vis.close(env='main')
app = dash.Dash(suppress_callback_exceptions=True, external_stylesheets=[dbc.themes.BOOTSTRAP])

#%% ################################## CODEBLOCK ##################################
from plotly.subplots import make_subplots
import plotly.graph_objs as go

Component = type('Component', (dict,), {})

# O[T1,T1] : Korea/Press
KR = Component()
KR['T1,T1,0,0'] = html.Div([html.Div([html.P('MAIN'),
                                      dbc.Button('Naver', color='dark', href="https://news.naver.com/"),
                                      dbc.Button('서울신문', color='dark', href="https://www.seoul.co.kr/"),
                                      dbc.Button('조선일보', color='dark', href="https://www.chosun.com/"),
                                      dbc.Button('동아일보', color='dark', href="https://www.donga.com/"),
                                      dbc.Button('경향신문', color='dark', href="http://www.khan.co.kr/"),
                                      dbc.Button('한국일보', color='dark', href="https://www.hankookilbo.com/"),
                                      dbc.Button('중앙일보', color='dark', href="https://joongang.joins.com/"),
                                      dbc.Button('한겨례', color='dark', href="http://www.hani.co.kr/"),
                                      dbc.Button('국민일보', color='dark', href="http://www.kmib.co.kr/news/index.asp"),
                                      dbc.Button('세계일보', color='dark', href="http://www.segye.com/"),
                                      dbc.Button('문화일보', color='dark', href="http://www.munhwa.com/")]),
                            html.Div([html.P('Opinion'),
                                      dbc.Button('서울신문', color='secondary', href="https://www.seoul.co.kr/news/newsList.php?section=editorial"),
                                      dbc.Button('조선일보', color='secondary', href="https://www.chosun.com/opinion/"),
                                      dbc.Button('동아일보', color='secondary', href="https://www.donga.com/news/Opinion"),
                                      dbc.Button('경향신문', color='secondary', href="http://news.khan.co.kr/kh_opinion/"),
                                      dbc.Button('한국일보', color='secondary', href="https://www.hankookilbo.com/Opinion"),
                                      dbc.Button('중앙일보', color='secondary', href="https://news.joins.com/opinion?cloc=joongang-home-megatop"),
                                      dbc.Button('한겨례', color='secondary', href="http://www.hani.co.kr/arti/opinion/editorial/home01.html"),
                                      dbc.Button('국민일보', color='secondary', href="http://www.kmib.co.kr/opinion/index.asp"),
                                      dbc.Button('세계일보', color='secondary', href="http://www.segye.com/news/opinion"),
                                      dbc.Button('문화일보', color='secondary', href="http://www.munhwa.com/news/section.html?sec=opinion"),
                                      ])])
KR['T1,T1,1,0'] = html.Embed(src='https://news.naver.com/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,2,0'] = html.Embed(src='https://www.seoul.co.kr/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,3,0'] = html.Embed(src='https://www.chosun.com/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,4,0'] = html.Embed(src='https://www.donga.com/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,5,0'] = html.Embed(src='https://www.hankookilbo.com/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,6,0'] = html.Embed(src='https://joongang.joins.com/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,7,0'] = html.Embed(src='http://www.hani.co.kr/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,8,0'] = html.Embed(src='http://www.kmib.co.kr/news/index.asp', style={'width':'100%', 'height':'700px'})
KR['T1,T1,9,0'] = html.Embed(src='http://www.segye.com/', style={'width':'100%', 'height':'700px'})
KR['T1,T1,10,0'] = html.Embed(src='http://www.munhwa.com/', style={'width':'100%', 'height':'700px'})

# O[T1,T2] : Korea/Adminstration
KR['T1,T2,0,0'] = html.Div([dbc.Button('Order of Precedence', color='dark', href="https://en.wikipedia.org/wiki/South_Korean_order_of_precedence"),
                            dbc.Button('Korea', color='dark', href="https://www1.president.go.kr/about/government-organization"),
                            dbc.Button('청와대', color='dark', href="https://www1.president.go.kr/about/organization"),
                            dbc.Button('국무회의', color='dark', href="https://www1.president.go.kr/c/blue-house-stories"),
                            dbc.Button('정부인사', color='dark', href="https://namu.wiki/w/%EB%AC%B8%EC%9E%AC%EC%9D%B8%20%EC%A0%95%EB%B6%80/%EC%9D%B8%EC%82%AC"),
                            dbc.Button('법제처', color='dark', href="https://www.moleg.go.kr/"),
                            dbc.Button('법무부', color='dark', href="https://www.moj.go.kr/moj/index.do"),
                            dbc.Button('검찰청', color='dark', href="https://www.spo.go.kr/site/spo/main.do"),
                            dbc.Button('감사원', color='dark', href="https://www.bai.go.kr/bai/html/intro/organ/organizationchart.do;jsessionid=4rTWGWBrnjNCl3O7iF7-Xv0b.node01?mdex=bai83"),
                            dbc.Button('공정거래위원회', color='dark', href="https://www.ftc.go.kr/"),
                            dbc.Button('금융위원회', color='dark', href="http://www.fsc.go.kr/fsc040101"),
                            dbc.Button('금융감독원', color='dark', href="http://www.fss.or.kr/fss/kr/main.html"),
                            dbc.Button('기획재정부', color='dark', href="https://www.moef.go.kr/mi/orgnzt/org.do;jsessionid=9pLke3Kf4H9EZ0mVnLFi0Llt.node30?bbsId=MOSFBBS_000000000097&menuNo=9040100"),
                            dbc.Button('국세청', color='dark', href="https://nts.go.kr/nts/cm/cntnts/cntntsView.do?mi=6740&cntntsId=8140"),
                            ])
KR['T1,T2,1,0'] = html.Embed(src='https://www1.president.go.kr/about/government-organization', style={'width':'100%', 'height':'700px'})
KR['T1,T2,2,0'] = html.Div([html.Embed(src='https://www1.president.go.kr/about/organization', style={'width':'100%', 'height':'700px'}),
                            dcc.Markdown("""
                            ## Office of the President
                            - [Organization](https://www.law.go.kr/법령/대통령비서실직제)

                            """),])
KR['T1,T2,3,0'] = html.Embed(src='https://www.bai.go.kr/bai/html/intro/organ/organizationchart.do;jsessionid=4rTWGWBrnjNCl3O7iF7-Xv0b.node01?mdex=bai83', style={'width':'100%', 'height':'700px'})
KR['T1,T2,4,0'] = html.Embed(src='http://www.fsc.go.kr/fsc040101', style={'width':'100%', 'height':'700px'})
KR['T1,T2,5,0'] = html.Embed(src='https://www.moef.go.kr/mi/orgnzt/org.do;jsessionid=9pLke3Kf4H9EZ0mVnLFi0Llt.node30?bbsId=MOSFBBS_000000000097&menuNo=9040100', style={'width':'100%', 'height':'700px'})
KR['T1,T2,6,0'] = html.Embed(src='https://nts.go.kr/nts/cm/cntnts/cntntsView.do?mi=6740&cntntsId=8140', style={'width':'100%', 'height':'700px'})

# O[T1,T3] : Korea/Parties
KR['T1,T3,0,0'] = html.Div([dbc.Button('정당 목록', color="dark", href="https://namu.wiki/w/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD/%EC%A0%95%EB%8B%B9/%EB%AA%A9%EB%A1%9D"),
                            dbc.Button('정당 계보', color="dark", href="http://web.archive.org/web/20191128112759/http://museum.nec.go.kr/vote/voteKorea2.do"),
                            dbc.Button('더불어민주당', color='dark', href="https://theminjoo.kr/introduce/location"),
                            dbc.Button('더불어민주당/인사', color='dark', href="https://namu.wiki/w/%EB%8D%94%EB%B6%88%EC%96%B4%EB%AF%BC%EC%A3%BC%EB%8B%B9/%EC%A7%80%EB%8F%84%EB%B6%80"),
                            dbc.Button('국민의힘', color='dark', href="http://www.peoplepowerparty.kr/renewal/about/organization.do"),
                            dbc.Button('국민의당', color='dark', href="http://peopleparty.kr/"),
                            ])
KR['T1,T3,1,0'] = html.Embed(src='https://theminjoo.kr/introduce/location', style={'width':'100%', 'height':'700px'})
KR['T1,T3,2,0'] = html.Embed(src='http://www.peoplepowerparty.kr/renewal/about/organization.do', style={'width':'100%', 'height':'700px'})

# O[T1,T4] : Korea/Assembly
KR['T1,T4,0,0'] = html.Div([dbc.Button('Assembly', color='dark', href="https://www.assembly.go.kr/assm/userMain/main.do"),
                            dbc.Button('입법예고', color='dark', href="http://pal.assembly.go.kr/main/mainView.do"),
                            dbc.Button('위원회', color='dark', href="https://www.assembly.go.kr/assm/assemact/committee/committee01/assmCommittee/committeeUserList.do"),
                            dbc.Button('국정감사', color='dark', href="https://www.assembly.go.kr/assm/assemact/inspection/inspection01/assmInspection/inspectionUserList.do"),
                            dbc.Button('계류의안', color='dark', href="http://likms.assembly.go.kr/bill/MooringBill.do"),
                            dbc.Button('처리의안', color='dark', href="http://likms.assembly.go.kr/bill/FinishBill.do"),
                            ])
KR['T1,T4,1,0'] = html.Embed(src='https://finance.na.go.kr:444/finance/statute/statute01.do', style={'width':'100%', 'height':'700px'})

# O[T1,T5] : Korea/Judiciary
KR['T1,T5,0,0'] = html.Div([dbc.Button('법령센터', color='dark', href="https://www.law.go.kr/LSW/main.html"),
                            dbc.Button('Supreme Court of Korea', color='dark', href="https://www.scourt.go.kr/supreme/supreme.jsp"),
                            ])
KR['T1,T5,1,0'] = html.Div([dbc.Button("Kim & Chang", color='dark', href="https://www.kimchang.com/ko/main.kc"),
                            dbc.Button("Lee & Ko", color='dark', href="http://www.leeko.com/newmain/index.do?lang=KR"),
                            dbc.Button("YULCHON LLC", color='dark', href="https://www.yulchon.com/ko/main/main.do"),
                            dbc.Button("YOON & YANG", color='dark', href="http://www.hwawoo.com/kor/main.do"),
                            dbc.Button("Bae, Kim & Lee LLC", color='dark', href="http://www.bkl.co.kr/main/ko/main.jsp#law/main.jsp"),
                            dbc.Button("SHIN & KIM", color='dark', href="https://www.shinkim.com/kor"),
                            dbc.Button("BARUNLAW", color='dark', href="https://www.barunlaw.com/"),
                            dbc.Button("DONGIN", color='dark', href="http://www.donginlaw.co.kr/"),
                            dbc.Button("JIPYONG", color='dark', href="http://www.jipyong.com/kr/main/main.php"),
                            dbc.Button("HMP", color='dark', href="http://hmplaw.com/html/"),
                            dbc.Button("KCL", color='dark', href="http://www.kcllaw.com/"),
                            dcc.Markdown("""
                            ## Kim & Chang
                            ## Lee & Ko
                            ## YULCHON LLC
                            ## YOON & YANG
                            ## Bae, Kim & Lee LLC
                            ## SHIN & KIM
                            ## BARUNLAW
                            ## DONGIN
                            ## JIPYONG
                            ## HMP
                            ## KCL
                            """),
                            ])

# O[T1,T6] : Korea/Elections
KR['T1,T6,0,0'] = html.Div([dbc.Button('중앙선거관리위원회', color='dark', href="https://nec.go.kr/site/nec/main.do"),
                            dbc.Button('22/대선', color='dark', href="https://namu.wiki/w/%EC%A0%9C20%EB%8C%80%20%EB%8C%80%ED%86%B5%EB%A0%B9%20%EC%84%A0%EA%B1%B0"),
                            dbc.Button('24/총선', color='dark', href="https://namu.wiki/w/%EC%A0%9C22%EB%8C%80%20%EA%B5%AD%ED%9A%8C%EC%9D%98%EC%9B%90%20%EC%84%A0%EA%B1%B0"),
                            dbc.Button('22/지선', color='dark', href="https://namu.wiki/w/%EC%A0%9C8%ED%9A%8C%20%EC%A0%84%EA%B5%AD%EB%8F%99%EC%8B%9C%EC%A7%80%EB%B0%A9%EC%84%A0%EA%B1%B0"),
                            dbc.Button('21/재보선', color='dark', href="https://namu.wiki/w/2021%EB%85%84%20%EC%9E%AC%EB%B3%B4%EA%B6%90%EC%84%A0%EA%B1%B0"),
                            ])

# O[T1,T7] : Korea/Local Goverment
KR['T1,T7,0,0'] = html.Div([dbc.Button('Seoul', color='dark', href="https://www.seoul.go.kr/main/index.jsp"),
                            dbc.Button('Busan', color='dark', href="https://www.busan.go.kr/open/index.jsp"),
                            dbc.Button('Daegu', color='dark', href="http://www.daegu.go.kr/intro.jsp"),
                            dbc.Button('Incheon', color='dark', href="https://www.incheon.go.kr/index"),
                            dbc.Button('Gwangju', color='dark', href="https://www.gwangju.go.kr/intro/index.html"),
                            dbc.Button('Daejeon', color='dark', href="https://www.daejeon.go.kr/index.do"),
                            dbc.Button('Ulsan', color='dark', href="https://www.ulsan.go.kr/u/rep/main.ulsan"),
                            dbc.Button('Sejong', color='dark', href="https://www.sejong.go.kr/index.jsp"),
                            dbc.Button('Gyeonggi', color='dark', href="https://www.gg.go.kr/"),
                            dbc.Button('Gangwon', color='dark', href="http://www.provin.gangwon.kr/gw/portal"),
                            dbc.Button('Chungcheonbuk', color='dark', href="https://www.chungbuk.go.kr/intro/index.html"),
                            dbc.Button('Chungcheonnam', color='dark', href="http://www.chungnam.go.kr/main.do"),
                            dbc.Button('Jeollabuk', color='dark', href="https://www.jeonbuk.go.kr/index.jeonbuk"),
                            dbc.Button('Jeollanam', color='dark', href="https://www.jeonnam.go.kr/main.do"),
                            dbc.Button('Gyeongsangbuk', color='dark', href="https://www.gb.go.kr/Main/index.html"),
                            dbc.Button('Gyeongsangnam', color='dark', href="https://www.gyeongnam.go.kr/index.gyeong"),
                            dbc.Button('Jeju', color='dark', href="https://www.jeju.go.kr/index.htm"),
                            ])
KR['T1,T7,1,0'] = html.Div([])
KR['T1,T7,2,0'] = html.Div([])
KR['T1,T7,3,0'] = html.Div([])
KR['T1,T7,4,0'] = html.Div([])
KR['T1,T7,5,0'] = html.Div([])
KR['T1,T7,6,0'] = html.Div([])
KR['T1,T7,7,0'] = html.Div([])
KR['T1,T7,8,0'] = html.Div([])
KR['T1,T7,9,0'] = html.Div([])
KR['T1,T7,10,0'] = html.Div([])
KR['T1,T7,11,0'] = html.Div([])
KR['T1,T7,12,0'] = html.Div([])
KR['T1,T7,13,0'] = html.Div([])
KR['T1,T7,14,0'] = html.Div([])
KR['T1,T7,15,0'] = html.Div([])
KR['T1,T7,16,0'] = html.Div([])
KR['T1,T7,17,0'] = html.Div([])

# O[T1,T8] : Korea/Education
KR['T1,T8,0,0'] = html.Div([dbc.Button('SNU', color='dark', href="https://www.snu.ac.kr/index.html"),
                            dbc.Button('YONSEI', color='dark', href="https://www.yonsei.ac.kr/sc/"),
                            dbc.Button('KOREA', color='dark', href="http://www.korea.ac.kr/mbshome/mbs/university/index.do"),
                            dbc.Button('SKKU', color='dark', href="https://www.skku.edu/skku/index.do"),
                            ])
KR['T1,T8,1,0'] = html.Embed(src='https://news.naver.com/', style={'width':'100%', 'height':'700px'})

# O[T2,0,0] : United States
US = Component()
US['T2,T1,0,0'] = html.Div([dbc.Button('Cable News Network(CNN)', color='dark', href="https://edition.cnn.com/"),
                            ])
US['T2,T1,1,0'] = html.Embed(src='https://edition.cnn.com/', style={'width':'100%', 'height':'700px'})
US['T2,T2,0,0'] = html.Div([dbc.Button('Order of Precedence', color='dark', href="https://en.wikipedia.org/wiki/United_States_order_of_precedence"),
                            dbc.Button('The United States Government', color='dark', href="https://www.usa.gov/"),
                            dbc.Button('The white house', color='dark', href="https://www.whitehouse.gov/"),
                            dbc.Button('Organization', color='dark', href="https://www.whitehouse.gov/about-the-white-house/our-government/"),
                            dbc.Button('United States Department of Treasury', color='dark', href="https://home.treasury.gov/"),
                            ])
US['T2,T3,0,0'] = html.Div([])
US['T2,T4,0,0'] = html.Div([])
US['T2,T5,0,0'] = html.Div([])
US['T2,T5,1,0'] = dcc.Markdown("""
- [List of largest law firms by revenue](https://en.wikipedia.org/wiki/List_of_largest_law_firms_by_revenue)
- [List of largest law firms by profits per partner](https://en.wikipedia.org/wiki/List_of_largest_law_firms_by_profits_per_partner)
""")
US['T2,T6,0,0'] = html.Div([])
US['T2,T7,0,0'] = html.Div([])
US['T2,T8,0,0'] = dcc.Markdown("""

""")

# O[T3,T1,0,0] : International
IO = Component()
IO['T3,T1,0,0'] = html.Div([])

#%% ################################## DASHBOARD ##################################
T = {}
# KOREA Titles
T['T1,T1,0,0'] = 'Press'
T['T1,T1,1,0'] = 'Naver'
T['T1,T1,2,0'] = '서울신문'
T['T1,T1,3,0'] = '조선일보'
T['T1,T1,4,0'] = '동아일보'
T['T1,T1,5,0'] = '한국일보'
T['T1,T1,6,0'] = '중앙일보'
T['T1,T1,7,0'] = '한겨례'
T['T1,T1,8,0'] = '국민일보'
T['T1,T1,9,0'] = '세계일보'
T['T1,T1,10,0'] = '문화일보'
T['T1,T2,0,0'] = 'Administration'
T['T1,T2,1,0'] = 'Korea'
T['T1,T2,2,0'] = '청와대'
T['T1,T2,3,0'] = '감사원'
T['T1,T2,4,0'] = '금융위원회'
T['T1,T2,5,0'] = '기획재정부'
T['T1,T2,6,0'] = '국세청'
T['T1,T3,0,0'] = 'Parties'
T['T1,T3,1,0'] = '더불어민주당'
T['T1,T3,2,0'] = '국민의힘'
T['T1,T4,0,0'] = 'Assembly'
T['T1,T4,1,0'] = '기획재정위원회'
T['T1,T5,0,0'] = 'Judiciary'
T['T1,T5,1,0'] = 'Law Firms'
T['T1,T6,0,0'] = 'Elections'
T['T1,T7,0,0'] = 'Local Governments'
T['T1,T7,1,0'] = 'Seoul Metropolitan Government'
T['T1,T7,2,0'] = 'Busan Metropolitan City'
T['T1,T7,3,0'] = 'Daegu Metropolitan City'
T['T1,T7,4,0'] = 'Incheon Metropolitan City'
T['T1,T7,5,0'] = 'Gwangju Metropolitan City'
T['T1,T7,6,0'] = 'Daejeon Metropolitan City'
T['T1,T7,7,0'] = 'Ulsan Metropolitan City'
T['T1,T7,8,0'] = 'Sejong Special Self-Governing City'
T['T1,T7,9,0'] = 'Gyeonggi-do'
T['T1,T7,10,0'] = 'Gangwon-do'
T['T1,T7,11,0'] = 'Chungcheongbuk-do'
T['T1,T7,12,0'] = 'Chungcheongnam-do'
T['T1,T7,13,0'] = 'Jeollabuk-do'
T['T1,T7,14,0'] = 'Jeollanam-do'
T['T1,T7,15,0'] = 'Gyeongsangbuk-do'
T['T1,T7,16,0'] = 'Gyeongsangnam-do'
T['T1,T7,17,0'] = 'Jeju Special Self-Governing Province'
T['T1,T8,0,0'] = 'University'
T['T1,T8,1,0'] = 'News'
# USA Titles
T['T2,T1,0,0'] = 'Press'
T['T2,T1,1,0'] = 'Cable News Network(CNN)'
T['T2,T2,0,0'] = 'Administration'
T['T2,T3,0,0'] = 'Parties'
T['T2,T4,0,0'] = 'Assembly'
T['T2,T5,0,0'] = 'Judiciary'
T['T2,T5,1,0'] = 'Law Firms'
T['T2,T6,0,0'] = 'Elections'
T['T2,T7,0,0'] = 'Local Governments'
T['T2,T8,0,0'] = 'University'
T['T2,T8,1,0'] = 'News'
# International Titles
T['T3,T1,0,0'] = 'Organization'

O = {}
O['T,_,_'] = None
# KOREA Objects
O['T1,T1,0,0'] = KR['T1,T1,0,0']
O['T1,T1,1,0'] = KR['T1,T1,1,0']
O['T1,T1,2,0'] = KR['T1,T1,2,0']
O['T1,T1,3,0'] = KR['T1,T1,3,0']
O['T1,T1,4,0'] = KR['T1,T1,4,0']
O['T1,T1,5,0'] = KR['T1,T1,5,0']
O['T1,T1,6,0'] = KR['T1,T1,6,0']
O['T1,T1,7,0'] = KR['T1,T1,7,0']
O['T1,T1,8,0'] = KR['T1,T1,8,0']
O['T1,T1,9,0'] = KR['T1,T1,9,0']
O['T1,T1,10,0'] = KR['T1,T1,10,0']
O['T1,T2,0,0'] = KR['T1,T2,0,0']
O['T1,T2,1,0'] = KR['T1,T2,1,0']
O['T1,T2,2,0'] = KR['T1,T2,2,0']
O['T1,T2,3,0'] = KR['T1,T2,3,0']
O['T1,T2,4,0'] = KR['T1,T2,4,0']
O['T1,T2,5,0'] = KR['T1,T2,5,0']
O['T1,T2,6,0'] = KR['T1,T2,6,0']
O['T1,T3,0,0'] = KR['T1,T3,0,0']
O['T1,T3,1,0'] = KR['T1,T3,1,0']
O['T1,T3,2,0'] = KR['T1,T3,2,0']
O['T1,T4,0,0'] = KR['T1,T4,0,0']
O['T1,T4,1,0'] = KR['T1,T4,1,0']
O['T1,T5,0,0'] = KR['T1,T5,0,0']
O['T1,T5,1,0'] = KR['T1,T5,1,0']
O['T1,T6,0,0'] = KR['T1,T6,0,0']
O['T1,T7,0,0'] = KR['T1,T7,0,0']
O['T1,T7,1,0'] = KR['T1,T7,1,0']
O['T1,T7,2,0'] = KR['T1,T7,2,0']
O['T1,T7,3,0'] = KR['T1,T7,3,0']
O['T1,T7,4,0'] = KR['T1,T7,4,0']
O['T1,T7,5,0'] = KR['T1,T7,5,0']
O['T1,T7,6,0'] = KR['T1,T7,6,0']
O['T1,T7,7,0'] = KR['T1,T7,7,0']
O['T1,T7,8,0'] = KR['T1,T7,8,0']
O['T1,T7,9,0'] = KR['T1,T7,9,0']
O['T1,T7,10,0'] = KR['T1,T7,10,0']
O['T1,T7,11,0'] = KR['T1,T7,11,0']
O['T1,T7,12,0'] = KR['T1,T7,12,0']
O['T1,T7,13,0'] = KR['T1,T7,13,0']
O['T1,T7,14,0'] = KR['T1,T7,14,0']
O['T1,T7,15,0'] = KR['T1,T7,15,0']
O['T1,T7,16,0'] = KR['T1,T7,16,0']
O['T1,T7,17,0'] = KR['T1,T7,17,0']
O['T1,T8,0,0'] = KR['T1,T8,0,0']
O['T1,T8,1,0'] = KR['T1,T8,1,0']
# USA Objects
O['T2,T1,0,0'] = US['T2,T1,0,0']
O['T2,T1,1,0'] = US['T2,T1,1,0']
O['T2,T2,0,0'] = US['T2,T2,0,0']
O['T2,T3,0,0'] = US['T2,T3,0,0']
O['T2,T4,0,0'] = US['T2,T4,0,0']
O['T2,T5,0,0'] = US['T2,T5,0,0']
O['T2,T5,1,0'] = US['T2,T5,1,0']
O['T2,T6,0,0'] = US['T2,T6,0,0']
O['T2,T7,0,0'] = US['T2,T7,0,0']
O['T2,T8,0,0'] = US['T2,T8,0,0']
# International Objects
O['T3,T1,0,0'] = IO['T3,T1,0,0']

C = {} # color code : primary, secondary, info, success, warning, danger, light, dark
# KOREA
C['T1,T1,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,0,0']), dbc.CardBody(O['T1,T1,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,1,0']), dbc.CardBody(O['T1,T1,1,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,2,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,2,0']), dbc.CardBody(O['T1,T1,2,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,3,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,3,0']), dbc.CardBody(O['T1,T1,3,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,4,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,4,0']), dbc.CardBody(O['T1,T1,4,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,5,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,5,0']), dbc.CardBody(O['T1,T1,5,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,6,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,6,0']), dbc.CardBody(O['T1,T1,6,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,7,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,7,0']), dbc.CardBody(O['T1,T1,7,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,8,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,8,0']), dbc.CardBody(O['T1,T1,8,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,9,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,9,0']), dbc.CardBody(O['T1,T1,9,0'])], color='light', inverse=False, outline=True)]
C['T1,T1,10,0'] = [dbc.Card([dbc.CardHeader(T['T1,T1,10,0']), dbc.CardBody(O['T1,T1,10,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,0,0']), dbc.CardBody(O['T1,T2,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,1,0']), dbc.CardBody(O['T1,T2,1,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,2,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,2,0']), dbc.CardBody(O['T1,T2,2,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,3,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,3,0']), dbc.CardBody(O['T1,T2,3,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,4,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,4,0']), dbc.CardBody(O['T1,T2,4,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,5,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,5,0']), dbc.CardBody(O['T1,T2,5,0'])], color='light', inverse=False, outline=True)]
C['T1,T2,6,0'] = [dbc.Card([dbc.CardHeader(T['T1,T2,6,0']), dbc.CardBody(O['T1,T2,6,0'])], color='light', inverse=False, outline=True)]
C['T1,T3,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T3,0,0']), dbc.CardBody(O['T1,T3,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T3,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T3,1,0']), dbc.CardBody(O['T1,T3,1,0'])], color='light', inverse=False, outline=True)]
C['T1,T3,2,0'] = [dbc.Card([dbc.CardHeader(T['T1,T3,2,0']), dbc.CardBody(O['T1,T3,2,0'])], color='light', inverse=False, outline=True)]
C['T1,T4,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T4,0,0']), dbc.CardBody(O['T1,T4,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T4,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T4,1,0']), dbc.CardBody(O['T1,T4,1,0'])], color='light', inverse=False, outline=True)]
C['T1,T5,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T5,0,0']), dbc.CardBody(O['T1,T5,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T5,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T5,1,0']), dbc.CardBody(O['T1,T5,1,0'])], color='light', inverse=False, outline=True)]
C['T1,T6,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T6,0,0']), dbc.CardBody(O['T1,T6,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,0,0']), dbc.CardBody(O['T1,T7,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,1,0']), dbc.CardBody(O['T1,T7,1,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,2,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,2,0']), dbc.CardBody(O['T1,T7,2,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,3,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,3,0']), dbc.CardBody(O['T1,T7,3,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,4,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,4,0']), dbc.CardBody(O['T1,T7,4,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,5,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,5,0']), dbc.CardBody(O['T1,T7,5,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,6,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,6,0']), dbc.CardBody(O['T1,T7,6,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,7,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,7,0']), dbc.CardBody(O['T1,T7,7,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,8,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,8,0']), dbc.CardBody(O['T1,T7,8,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,9,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,9,0']), dbc.CardBody(O['T1,T7,9,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,10,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,10,0']), dbc.CardBody(O['T1,T7,10,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,11,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,11,0']), dbc.CardBody(O['T1,T7,11,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,12,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,12,0']), dbc.CardBody(O['T1,T7,12,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,13,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,13,0']), dbc.CardBody(O['T1,T7,13,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,14,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,14,0']), dbc.CardBody(O['T1,T7,14,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,15,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,15,0']), dbc.CardBody(O['T1,T7,15,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,16,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,16,0']), dbc.CardBody(O['T1,T7,16,0'])], color='light', inverse=False, outline=True)]
C['T1,T7,17,0'] = [dbc.Card([dbc.CardHeader(T['T1,T7,17,0']), dbc.CardBody(O['T1,T7,17,0'])], color='light', inverse=False, outline=True)]
C['T1,T8,0,0'] = [dbc.Card([dbc.CardHeader(T['T1,T8,0,0']), dbc.CardBody(O['T1,T8,0,0'])], color='light', inverse=False, outline=True)]
C['T1,T8,1,0'] = [dbc.Card([dbc.CardHeader(T['T1,T8,1,0']), dbc.CardBody(O['T1,T8,1,0'])], color='light', inverse=False, outline=True)]
# USA
C['T2,T1,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T1,0,0']), dbc.CardBody(O['T2,T1,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T1,1,0'] = [dbc.Card([dbc.CardHeader(T['T2,T1,1,0']), dbc.CardBody(O['T2,T1,1,0'])], color='light', inverse=False, outline=True)]
C['T2,T2,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T2,0,0']), dbc.CardBody(O['T2,T2,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T3,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T3,0,0']), dbc.CardBody(O['T2,T3,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T4,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T4,0,0']), dbc.CardBody(O['T2,T4,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T5,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T5,0,0']), dbc.CardBody(O['T2,T5,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T5,1,0'] = [dbc.Card([dbc.CardHeader(T['T2,T5,1,0']), dbc.CardBody(O['T2,T5,1,0'])], color='light', inverse=False, outline=True)]
C['T2,T6,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T6,0,0']), dbc.CardBody(O['T2,T6,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T7,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T7,0,0']), dbc.CardBody(O['T2,T7,0,0'])], color='light', inverse=False, outline=True)]
C['T2,T8,0,0'] = [dbc.Card([dbc.CardHeader(T['T2,T8,0,0']), dbc.CardBody(O['T2,T8,0,0'])], color='light', inverse=False, outline=True)]
# International
C['T3,T1,0,0'] = [dbc.Card([dbc.CardHeader(T['T3,T1,0,0']), dbc.CardBody(O['T3,T1,0,0'])], color='light', inverse=False, outline=True)]
################################## DASHBOARD ##################################
contents = {}; contents['page'] = {}; page_layouts = {}
# KOREA Tabs
contents['page']['tab1'] = {}
contents['page']['tab1']['tab1'] = [dbc.Row([dbc.Col(C['T1,T1,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,1,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,2,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,3,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,4,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,5,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,6,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,7,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,8,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,9,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T1,10,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab2'] = [dbc.Row([dbc.Col(C['T1,T2,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T2,1,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T2,2,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T2,3,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T2,4,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T2,5,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T2,6,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab3'] = [dbc.Row([dbc.Col(C['T1,T3,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T3,1,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T3,2,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab4'] = [dbc.Row([dbc.Col(C['T1,T4,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T4,1,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab5'] = [dbc.Row([dbc.Col(C['T1,T5,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T5,1,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab6'] = [dbc.Row([dbc.Col(C['T1,T6,0,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab7'] = [dbc.Row([dbc.Col(C['T1,T7,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,1,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,2,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,3,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,4,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,5,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,6,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,7,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,8,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,9,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,10,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,11,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,12,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,13,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,14,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,15,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,16,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T7,17,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab1']['tab8'] = [dbc.Row([dbc.Col(C['T1,T8,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T1,T8,1,0'], width=12)]), html.Br(),
                                    html.Br()]
# USA Tabs
contents['page']['tab2'] = {}
contents['page']['tab2']['tab1'] = [dbc.Row([dbc.Col(C['T2,T1,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T2,T1,1,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab2'] = [dbc.Row([dbc.Col(C['T2,T2,0,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab3'] = [dbc.Row([dbc.Col(C['T2,T3,0,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab4'] = [dbc.Row([dbc.Col(C['T2,T4,0,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab5'] = [dbc.Row([dbc.Col(C['T2,T5,0,0'], width=12)]), html.Br(),
                                    dbc.Row([dbc.Col(C['T2,T5,1,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab6'] = [dbc.Row([dbc.Col(C['T2,T6,0,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab7'] = [dbc.Row([dbc.Col(C['T2,T7,0,0'], width=12)]), html.Br(),
                                    html.Br()]
contents['page']['tab2']['tab8'] = [dbc.Row([dbc.Col(C['T2,T8,0,0'], width=12)]), html.Br(),
                                    html.Br()]
# International Tabs
contents['page']['tab3'] = {}
contents['page']['tab3']['tab1'] = [dbc.Row([dbc.Col(C['T3,T1,0,0'], width=12)]), html.Br(),
                                    html.Br()]

# TAB1 : KOREA
contents['page']['tab1']['tabs'] = dbc.Tabs([dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab1'])), label="Press", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab2'])), label="Administration", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab3'])), label="Parties", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab4'])), label="Assembly", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab5'])), label="Judiciary", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab6'])), label="Elections", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab7'])), label="Local Governments", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tab8'])), label="Education", disabled=False),
                                             ])
# TAB2 : USA
contents['page']['tab2']['tabs'] = dbc.Tabs([dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab1'])), label="Press", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab2'])), label="Administration", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab3'])), label="Parties", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab4'])), label="Assembly", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab5'])), label="Judiciary", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab6'])), label="Elections", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab7'])), label="Local Governments", disabled=False),
                                             dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tab8'])), label="Education", disabled=False),
                                             ])
# TAB3 : International
contents['page']['tab3']['tabs'] = dbc.Tabs([dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab3']['tab1'])), label="Organization", disabled=False),
                                             ])
page_layouts['page'] = dbc.Tabs([dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab1']['tabs'])), label="Korea", disabled=False),
                                 dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab2']['tabs'])), label="United States", disabled=False),
                                 dbc.Tab(dbc.Card(dbc.CardBody(contents['page']['tab3']['tabs'])), label="International", disabled=False),
                                 ])
main = dbc.Jumbotron([html.H2('analysis/Politics'),
                      html.H6('Ailever : Promulgate values for a better tomorrow'), html.Hr(),
                      html.Div([dbc.Button("Home", color="secondary", href='https://ailever.github.io/'),
                                dbc.Button("Source", color="secondary", href='https://github.com/ailever/openapi/blob/master/analysis/Politics.py'),
                                dbc.Button("Naver", color="secondary", href="https://news.naver.com/"),
                                dbc.Button("Google Trend", color="secondary", href="https://trends.google.com/trends/explore"),
                                dbc.Button("DataLab", color="secondary", href="https://datalab.naver.com/"),
                                dbc.Button('Kakao Map', color='secondary', href="https://map.kakao.com/"),
                                dbc.Button('Google Map', color='secondary', href="https://www.google.co.kr/maps/"),
                                dbc.Button("Plotly", color="secondary", href="https://plotly.com/python/"),
                                dbc.Button("Rstudio", color="secondary", href=config['R-server']+':'+config['R-port']),
                                dbc.Button("Real-Time Analysis", id='real-time', color="secondary", href=config['visdom-server']+':'+config['visdom-port'])]),
                      html.Div([dbc.Button("National Indices", color="dark", href="https://www.index.go.kr/main.do"),
                                dbc.Button("BOK Indices", color="dark", href="http://ecos.bok.or.kr/"),
                                dbc.Button("Dart", color="dark", href="http://dart.fss.or.kr/"),
                                dbc.Button("Alio", color="dark", href="http://www.alio.go.kr/home.do"),
                                dbc.Button("eGroup", color="dark", href="https://www.egroup.go.kr/egps/wi/mainPage.do"),
                                dbc.Button('Time Line 1', color='dark', href="https://en.wikipedia.org/wiki/2021"),
                                dbc.Button('Time Line 2', color='dark', href="https://ko.wikipedia.org/wiki/2021%EB%85%84"),
                                dbc.Button('Time Line 3', color='dark', href="https://namu.wiki/w/2021%EB%85%84"),
                                ]),
                      html.P(id='visdom-server')])
app.layout = html.Div([main, page_layouts['page']])
if __name__ == '__main__':
    app.run_server(host=config['dash-server'], port=config['dash-port'], debug=True)


import argparse
import os

import torch
import torch.nn.utils.rnn
import torch.utils.data

from model.model import DockRegressor
from model.vocab import get_vocab_from_file

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

from train import getconfig, get_input_data,mycollate, ToyDataset, test_model

def main(args):
    config = getconfig(args)
    config['testdir'] = args.testdir
    print("loading data.")
    vocab, c2i, i2c = get_vocab_from_file(args.testdir + "/vocab.txt")
    print("Vocab size is", len(vocab))
    s_t, e_t, n_t = get_input_data(args.testdir + "/out.txt", args.testdir + "/out_y.txt", args.testdir + "/out_names.txt", c2i)
    test_data = ToyDataset(s_t, e_t, n_t)
    print("Done.")

    ## make data generator
    test_dataloader = torch.utils.data.DataLoader(test_data, pin_memory=True, batch_size=config['batch_size'],
                                                  collate_fn=mycollate, num_workers=0, shuffle=False)

    model = DockRegressor(config['vocab_size'], config['emb_size'], max_len=config['max_len']).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)

    epoch_start = 0
    if args.ct:
        print("Continuing from save.")
        pt = torch.load(args.logdir + "/autosave.model.pt")
        model.load_state_dict(pt['state_dict'])
        optimizer.load_state_dict(pt['optim_state_dict'])

    test_model(model, optimizer, test_dataloader, config)

if __name__ == '__main__':
    print("Note: This script is very picky. This will only run on a GPU. ")
    parser = argparse.ArgumentParser()
    parser.add_argument('--logdir', help='place to store things.', type=str, required=True)
    parser.add_argument('--testdir', type=str, required=True)
    parser.add_argument('--ct', help='continue training for longer', action='store_true')

    args = parser.parse_args()
    path = args.logdir
    try:
        os.mkdir(path)
    except OSError:
        print("Creation of the directory %s failed. Maybe it already exists? I will overwrite :)" % path)
    else:
        print("Successfully created the directory %s " % path)

    main(args)


"""
    compile active_cmssw:
        Compiles the CMSSW version which is linked under workspace/current

    Usage:
        compile [ncpu=1]

    Parameters:
        npcu: number of CPUs to be used for compilation. Default: 1
"""
import logging
import hepshell
import os
from hepshell.interpreter import time_function

LOG = logging.getLogger(__name__)
from l1t_cli.setup import ACTIVE_CMSSW
CMSSW_SRC = os.path.join(ACTIVE_CMSSW, 'src')

class Command(hepshell.Command):

    DEFAULTS = {
        'ncpu': 1,
    }

    def __init__(self, path=__file__, doc=__doc__):
        super(Command, self).__init__(path, doc)

    @time_function('compile', LOG)
    def run(self, args, variables):
        if not self.__can_run():
            return False
        self.__prepare(args, variables)
        n_jobs = int(self.__variables['ncpu'])
        commands = [
            'cd {CMSSW_SRC}',
            'source /cvmfs/cms.cern.ch/cmsset_default.sh',
            'eval `/cvmfs/cms.cern.ch/common/scram runtime -sh`',
            'scram b -j{n_jobs}'
        ]

        all_in_one = ' && '.join(commands)
        all_in_one = all_in_one.format(CMSSW_SRC=CMSSW_SRC, n_jobs=n_jobs)

        from hepshell.interpreter import call
        call(all_in_one, logger=LOG, shell=True)

        return True

    def __can_run(self):
        if not os.path.exists(CMSSW_SRC):
            LOG.error('CMSSW is not set up: {0}'.format(CMSSW_SRC))
            return False

        return True


import functools
import pathlib
import re
import typing as typ

import attr
import bs4
import requests

from . import Candidate, Election

FRAGMENT_RE = re.compile(
    r'[0-9`~!@#$%^&*()_|+\=?;:\'"’,.<>\{\}\[\]\\\/]', re.IGNORECASE
)

class InvalidPage(Exception):
    pass

@functools.lru_cache(maxsize=1)
def bs4_loader():
    for loader in ["lxml", "html.parser"]:
        try:
            bs4.BeautifulSoup("<html></html>", loader)
            return loader
        except:
            pass
    raise ValueError("Cannot find a suitable loader")

def none_if_default(value, default):
    return None if value == default else value

def text_if_present(soup):
    if soup:
        return "".join(soup.stripped_strings)
    else:
        return None

def parse(base_url, content):
    _, _, id = base_url.strip("/").rpartition("/")
    soup = bs4.BeautifulSoup(content, bs4_loader())
    print("  ", soup.title)
    if "field_electiontitle" in soup.title.string:
        raise InvalidPage()
    type, election, a_electorate = parse_election(
        "".join(soup.find("h1").stripped_strings)
    )
    b_electorate = parse_electorate(
        "".join(soup.find(class_="views-field-field-subtitle").stripped_strings)
    )
    if a_electorate is None:
        electorate = b_electorate
    else:
        assert b_electorate is not None, f"{a_electorate!r}  {b_electorate!r}"
        assert a_electorate == b_electorate, f"{a_electorate!r}  {b_electorate!r}"
        electorate = a_electorate
    print(f"  {election!r} | {electorate!r}")
    return Election(
        id,
        type,
        election,
        electorate,
        [
            parse_candidate(base_url, election, electorate, candidate)
            for candidate in soup.find_all(class_="candidate")
        ],
    )

def parse_election(heading):
    type, name, ward = None, heading, None
    if heading.endswith(" Ward)"):
        useful, _, _ = heading.partition(" Ward)")
        name, _, ward = useful.partition(" (")
    elif "(" in heading or ")" in heading:
        raise ValueError("Possible invalid election")

    if name.endswith(" Regional Council"):
        name, _, _ = name.partition(" Regional Council")
        type = "Regional Council"
    elif name.endswith(" District Council"):
        name, _, _ = name.partition(" District Council")
        type = "Council"
    elif name.endswith(" City Council"):
        name, _, _ = name.partition(" City Council")
        type = "Council"
    elif name.endswith(" Council"):
        name, _, _ = name.partition(" Council")
        type = "Council"
    elif name.endswith(" Local Board"):
        name, _, _ = name.partition(" Local Board")
        type = "Local Board"
    elif name.endswith(" DHB"):
        name, _, _ = name.partition(" DHB")
        type = "DHB"
    elif name.startswith("Mayor of ") and name.endswith(" District"):
        name, _, _ = name.partition(" District")
        _, _, name = name.partition("Mayor of")
        type = "Mayor"
    elif name.startswith("Mayor of "):
        _, _, name = name.partition("Mayor of")
        type = "Mayor"
    else:
        raise ValueError(f"Unknown election {name} / {heading}")
    if type == "Council" and name.startswith("Mayor of "):
        _, _, name = name.partition("Mayor of")
        type = "Mayor"

    return type, name, ward

def parse_electorate(subtitle):
    if subtitle.startswith("Your vote for the members of"):
        # DHB & local board(for Auckland)
        return None
    elif subtitle.startswith("Your vote for the mayor of"):
        return None
    elif subtitle.startswith("Uncontested: there will be no vote "):
        _, _, part = subtitle.partition("Uncontested: there will be no ")
        return parse_electorate(f"Your {part}")
    elif " Ward/" in subtitle:
        a, _, b = subtitle.rpartition(" Ward/")
        return parse_electorate(f"{a}/{b} Ward")
    elif subtitle.startswith("Your vote for the councillors for the"):
        _, _, area = subtitle.partition("Your vote for the councillors for the")
        return parse_electorate_area(area)
    elif subtitle.startswith("Your vote for the councillor for the"):
        _, _, area = subtitle.partition("Your vote for the councillor for the")
        return parse_electorate_area(area)
    else:
        raise ValueError(f"Unknown electorate type: {subtitle}")

def parse_electorate_area(area):
    if area.endswith("Ward"):
        electorate, _, _ = area.rpartition("Ward")
        assert electorate, f"{area} => Ward"
    elif area.endswith("Constituency"):
        electorate, _, _ = area.rpartition("Constituency")
        assert electorate, f"{area} => Constituency"
    else:
        raise ValueError(f"Unknown councillor type: {area}")
    return electorate.strip()

def parse_candidate(base_url, election, electorate, soup):
    fname = text_if_present(soup.find(class_="field-name-field-first-name")) or ""
    lname = text_if_present(soup.find(class_="field-name-field-last-name")) or ""
    name = f"{fname} {lname}"
    fragment = FRAGMENT_RE.sub("", f"{fname} {lname}")

    extras = {}

    return Candidate(
        election=election,
        electorate=electorate,
        url=f"{base_url}#{fragment}",
        name=name,
        image=none_if_default(
            soup.find(class_="field-name-field-image").find("img")["src"],
            "https://www.policylocal.nz/sites/default/files/styles/round_portrait/public/default_images/default_profile_image.png?itok=bTqalX0V",
        ),
        affiliation=text_if_present(soup.find(class_="field-name-field-affiliation")),
        why_stand=parse_headered_section(soup.find(class_="field-name-field-cq-5")),
        priorities=parse_priorities(soup.find(class_="field-name-field-cq-5")),
        why_elect=parse_headered_section(soup.find(class_="field-name-field-cq-9")),
        before_politics=parse_headered_section(
            soup.find(class_="field-name-field-cq-2")
        ),
        age=parse_headered_section(soup.find(class_="field-name-field-cq-3")),
        lives_in=parse_headered_section(soup.find(class_="field-name-field-cq-4")),
        social_media=parse_social_media(soup.find(class_="group-social")),
    )

def parse_headered_section(soup):
    content = "".join(c for c in soup.children if isinstance(c, bs4.NavigableString))
    return none_if_default(content, "No response given")

def parse_priorities(soup):
    # This will return an empty list if the content is `No response given`
    return ["".join(p.stripped_strings) for p in soup.find_all(class_="priority")]

def parse_social_media(soup):
    if not soup:
        return {}
    social = {}
    for e in soup.find_all(class_="item-list"):
        media_type = e["id"]
        _, _, media_type = media_type.partition("linkicon-node-candidate-field-")
        media_type, _, _ = media_type.rpartition("-")
        assert media_type, e["id"]
        social[media_type] = e.find("a")["href"]


import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import ttest_rel, chisquare

type_met = ["CpG", "CHG", "CHH"]
type_plant = ["Col-0 FH", "Col-0 AR", "Col-0 GS", "Cvi FH", "Cvi AR", "Cvi GS"]
type_dot = ["ro", "go", "bo"]
type_stage = ["G", "H", "T", "BT", "MG"]
type_gen = ["WT", "dme-2/+"]

data_wt = [0, 0, 0, 3+2+2, 27+23+17+23]
data_dme = [3+1, 17+23+23+25, 1+20+2, 18+1+1+22, 24+1]

print(data_wt, data_dme)

data_nor_wt = [i / sum(data_wt) for i in data_wt]
data_nor_dme = [i / sum(data_dme) for i in data_dme]

print(data_nor_wt, data_nor_dme)

fig, ax = plt.subplots()
ax.set_title("Seeds on the development stage")
x = np.arange(5)

plt.bar(x - 0.125, data_wt, 0.25)
plt.bar(x + 0.125, data_dme, 0.25)

label = data_wt+data_dme

for n, patch in enumerate(ax.patches):
    plt.text(patch.get_x()+0.125, patch.get_height() + plt.ylim()[1]*0.02,
             label[n], fontsize=10, ha="center")
plt.xticks(x, type_stage)
plt.legend(type_gen, loc="upper left")

plt.ylim([0, plt.ylim()[1]*1.05])

plt.xlabel("Stage of the seed")
plt.ylabel("# of seed")
plt.show()

fig, ax = plt.subplots()
ax.set_title("Proportion of the seeds on the development stage")
x = np.arange(5)

plt.bar(x - 0.125, data_nor_wt, 0.25)
plt.bar(x + 0.125, data_nor_dme, 0.25)

label = [round(i, 2) for i in (data_nor_wt+data_nor_dme)]

for n, patch in enumerate(ax.patches):
    plt.text(patch.get_x()+0.125, patch.get_height() + plt.ylim()[1]*0.02,
             label[n], fontsize=10, ha="center")
plt.xticks(x, type_stage)
plt.legend(type_gen, loc="upper left")

plt.ylim([0, plt.ylim()[1]*1.05])

plt.xlabel("Stage of the seed")
plt.ylabel("proportion of seed")
plt.show()


import math
from tqdm import tqdm

def is_divisible(a, b):
	# a, b : string
	# returns True if a is divisible by b else false
    if len(a) <= len(b) + 1:
    	# exit condition
    	if int(a) % int(b) == 0 : return True
    	return False
    res = int(a[:len(b)+1]) % int(b)
    new_a = str(res)+a[len(b)+1:]
    return is_divisible(new_a,b)

def remove_10(inp):
	ret = inp
	while(ret % 2 == 0):
		ret //= 2
	while(ret % 5 == 0):
		ret //= 5
	return ret

def num_recurring_cycle(inp):
	inp = remove_10(inp)
	if inp == 1 : return 0
	cnt = 1
	while(True):
		if is_divisible("9"*cnt,str(inp)) : break
		cnt += 1
	return cnt

rec_list = [num_recurring_cycle(num) for num in tqdm(range(1,1001))]
print(rec_list)
print(max(rec_list))
print(rec_list.index(max(rec_list))+1)


import json
import matplotlib.pyplot as plt
import seaborn as sns
import sys
import pylab
import numpy as np
import pprint
import pandas as pd
from pandas import json_normalize

#[edges] -> uplink_Mac, downlinkMac, uplinkPortNumber, rateMbps, uplinkPortNum
#[vertices] -> mac, model, name, type (type can be "DEVICE" or "CLIENT"
def parseTopology():
    with open("topology.json", "r", encoding="utf-8") as f:
        top = json.loads(f.read())
        edges = top["default"]["edges"]
        vertices = top["default"]["vertices"]
        #df_edges = json_normalize(edges)
        #df_vertices = json_normalize(vertices)
        return edges,vertices

#[network] -> mac, ip, name
#[radios] na/ng -> maxPower, maxSpeedMegabitsPerSecond
#[appInfo] -> apCount,switchCount,clientCount,wiredClients, wirelessClients, throughput
#[lastSpeedTest] -> downloadSpeed, ping, runDate, uploadSpeed
#[portTable] -> fullDuplex, ifname, name, throughputRx, throughputTx, type, usageRx, usageTx
def parseBasicInfo():
    with open("basicInfo.json","r", encoding="utf-8") as f:
        basicInfo = json.loads(f.read())
        network = basicInfo["detail"]["deviceList"]["network"]
        radios = basicInfo["detail"]["deviceList"]["network"]["radios"]
        appInfo = basicInfo["detail"]["apps"]["info"]
        lastSpeedTest = basicInfo["detail"]["apps"]["lastSpeedTest"]
        portTable = basicInfo["detail"]["apps"]["info"]["port_table"]

#[default] -> ip, mac, model, type, bytes-d, tx_bytes-d, rx_bytes-d, bytes-r, connect_request_ip
#[uplink] -> bytes, tx_bytes, rx_bytes, tx_packets, rx_packets
#[downlink_table] -> mac, port_idx, speed, full_duplex
#[radio_table] -> radio, name, channel, radio_caps, radio_capts2, max_txpower, min_txpower, channel
#[port_table] -> media, mac, ifname, port_poe, poe_caps, speed_caps, speed, port_idx
#[ethernet_table] ->mac, num_port, name
#[last_uplink] -> uplink_mac, uplink_device, uplink_remote_port, port_idx, type

def parseDevices():
    with open("devices.json", "r", encoding="utf-8") as f:
        devices = json.loads(f.read())
        default = devices[0]["default"]
        uplink = devices[0]["default"]["uplink"]
        downlink_table = devices[0]["default"]["downlink_table"]
        radio_table = devices[0]["default"]["radio_table"]
        port_table = devices[0]["default"]["port_table"]
        ethernet_table = devices[0]["default"]["ethernet_table"]
        last_uplink = devices[0]["default"]["last_uplink"]
        return default

#[port_table] -> name, rx_broadcast, rx_bytes, rx_dropped, rx_errors, rx_packets, rx_multicast,
#tx_broadcast, tx_bytes, tx_multicast, tx_dropped, tx_packets, port_idx, media, speed_caps,
#is_uplink, full_duplex
def parseNetworkSysconf():
    with open("network_sysconf_11-07-2022 (1).json", "r", encoding="utf-8") as f:
        data = json.loads(f.read())
        #print(len(data["device"]))
        print(len(data["device"]["0"]["port_table"]))
        port_table = []
        for x in range(0,len(data["device"])-1):
            for y in range(0,len(data["device"][str(x)]["port_table"])-1):
                if len(data["device"][str(x)]["port_table"]) != 0:
                    port_table.append(data["device"][str(x)]["port_table"][str(y)])

    printCharts(port_table)

#bar chart in future using basic info
#y (number of bytes) rx & tx usage, color coded, diff lines
#x mac/ip of device
def barChart():
    #print the device types and ip
    devices_default = parseDevices()
    for i in range(len(devices_default)):
        print(devices_default[i]["type"] + " : "+ devices_default[i]["mac"])
        #if("downlink_table" in devices_default[i]):
        #    print("downlink_table in "+ str(i))

    edges, verticies = parseTopology()

    connections = {}
    clients = []
    devices = []
    totalclients = 0
    totaldevice = 0
    for i in range(len(verticies)):
        print(verticies[i]["type"] + " : " + verticies[i]["mac"])
        #connections.update({verticies[i]["mac"]:[]})
        if(verticies[i]["type"]=="DEVICE"):
            devices.append(verticies[i]["mac"])
            connections.update({verticies[i]["mac"]:[]})
            totaldevice+=1
        else:
            clients.append(verticies[i]["mac"])
            totalclients+=1
    print("total clients: "+str(totalclients)+" total devices: "+str(totaldevice) + "\n")

    for i in range(len(edges)):
        print(edges[i]["type"] + " : " + edges[i]["downlinkMac"] + " -> " + edges[i]["uplinkMac"])
        if(edges[i]["uplinkMac"] in connections):
            connections[edges[i]["uplinkMac"]].append(edges[i]["downlinkMac"])

    for k,v in connections.items():
        print(k)
        if(len(v)!=0):
            for y in range(len(v)):
                print("\t"+str(connections[k][y]))
        else:
            print("\tN/A")

    print("Clients: "+str(clients))
    print("Devices: "+str(devices))

    for i in range(len(devices_default)):
        #print(i)
        if devices_default[i]["mac"] in devices:
            print(i)

def printCharts(port_table = []):
    df = json_normalize(port_table)
    sns.kdeplot(df['rx_bytes'])
    #df.plot(kind = 'line',
    #         x = 'rx_bytes',
    #         y = 'tx_bytes',
    #         color = 'blue')
    plt.title('Rx Density')
    plt.savefig('Rx_density.png')
    plt.show()
    print(df)
    #df = json_normalize(vertices)
    #print(df)

def main():
    barChart()

main()
#parseNetworkSysconf()
#printCharts()


import os
import os.path
import torch
import json
import random
import math
import numpy as np
from PIL import Image
from torchvision import datasets, transforms
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_INCEPTION_MEAN, IMAGENET_INCEPTION_STD
from timm.data import create_transform
from transforms import RandomResizedCropAndInterpolationWithTwoPic, _pil_interp
from torchvision.datasets.vision import VisionDataset
from typing import Any, Callable, cast, Dict, List, Optional, Tuple

class MaskingGenerator:
    def __init__(
            self, input_size, num_masking_patches, min_num_patches=4, max_num_patches=None,
            min_aspect=0.3, max_aspect=None):
        if not isinstance(input_size, tuple):
            input_size = (input_size, ) * 2
        self.height, self.width = input_size

        self.num_patches = self.height * self.width
        self.num_masking_patches = num_masking_patches

        self.min_num_patches = min_num_patches
        self.max_num_patches = num_masking_patches if max_num_patches is None else max_num_patches

        max_aspect = max_aspect or 1 / min_aspect
        self.log_aspect_ratio = (math.log(min_aspect), math.log(max_aspect))

    def __repr__(self):
        repr_str = "Generator(%d, %d -> [%d ~ %d], max = %d, %.3f ~ %.3f)" % (
            self.height, self.width, self.min_num_patches, self.max_num_patches,
            self.num_masking_patches, self.log_aspect_ratio[0], self.log_aspect_ratio[1])
        return repr_str

    def get_shape(self):
        return self.height, self.width

    def _mask(self, mask, max_mask_patches):
        delta = 0
        for attempt in range(10):
            target_area = random.uniform(self.min_num_patches, max_mask_patches)
            aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
            h = int(round(math.sqrt(target_area * aspect_ratio)))
            w = int(round(math.sqrt(target_area / aspect_ratio)))
            if w < self.width and h < self.height:
                top = random.randint(0, self.height - h)
                left = random.randint(0, self.width - w)

                num_masked = mask[top: top + h, left: left + w].sum()
                # Overlap
                if 0 < h * w - num_masked <= max_mask_patches:
                    for i in range(top, top + h):
                        for j in range(left, left + w):
                            if mask[i, j] == 0:
                                mask[i, j] = 1
                                delta += 1

                if delta > 0:
                    break
        return delta

    def __call__(self):
        mask = np.zeros(shape=self.get_shape(), dtype=np.int32)
        mask_count = 0
        while mask_count < self.num_masking_patches:
            max_mask_patches = self.num_masking_patches - mask_count
            max_mask_patches = min(max_mask_patches, self.max_num_patches)

            delta = self._mask(mask, max_mask_patches)
            if delta == 0:
                break
            else:
                mask_count += delta

        # maintain a fix number {self.num_masking_patches}
        if mask_count > self.num_masking_patches:
            delta = mask_count - self.num_masking_patches
            mask_x, mask_y = mask.nonzero()
            to_vis = np.random.choice(mask_x.shape[0], delta, replace=False)
            mask[mask_x[to_vis], mask_y[to_vis]] = 0

        elif mask_count < self.num_masking_patches:
            delta = self.num_masking_patches - mask_count
            mask_x, mask_y = (mask == 0).nonzero()
            to_mask = np.random.choice(mask_x.shape[0], delta, replace=False)
            mask[mask_x[to_mask], mask_y[to_mask]] = 1

        assert mask.sum() == self.num_masking_patches, f"mask: {mask}, mask count {mask.sum()}"

        return mask

def has_file_allowed_extension(filename: str, extensions: Tuple[str, ...]) -> bool:
    """Checks if a file is an allowed extension.

    Args:
        filename (string): path to a file
        extensions (tuple of strings): extensions to consider (lowercase)

    Returns:
        bool: True if the filename ends with one of given extensions
    """
    return filename.lower().endswith(extensions)

def is_image_file(filename: str) -> bool:
    """Checks if a file is an allowed image extension.

    Args:
        filename (string): path to a file

    Returns:
        bool: True if the filename ends with a known image extension
    """
    return has_file_allowed_extension(filename, IMG_EXTENSIONS)

def make_dataset(
    directory: str,
    class_to_idx: Dict[str, int],
    extensions: Optional[Tuple[str, ...]] = None,
    is_valid_file: Optional[Callable[[str], bool]] = None,
) -> List[Tuple[str, int]]:
    instances = []
    directory = os.path.expanduser(directory)
    both_none = extensions is None and is_valid_file is None
    both_something = extensions is not None and is_valid_file is not None
    if both_none or both_something:
        raise ValueError("Both extensions and is_valid_file cannot be None or not None at the same time")
    if extensions is not None:
        def is_valid_file(x: str) -> bool:
            return has_file_allowed_extension(x, cast(Tuple[str, ...], extensions))
    is_valid_file = cast(Callable[[str], bool], is_valid_file)
    for target_class in sorted(class_to_idx.keys()):
        class_index = class_to_idx[target_class]
        target_dir = os.path.join(directory, target_class)
        if not os.path.isdir(target_dir):
            continue
        for root, _, fnames in sorted(os.walk(target_dir, followlinks=True)):
            for fname in sorted(fnames):
                path = os.path.join(root, fname)
                if is_valid_file(path):
                    item = path, class_index
                    instances.append(item)
    return instances

class DatasetFolder(VisionDataset):
    """A generic data loader where the samples are arranged in this way: ::

        root/class_x/xxx.ext
        root/class_x/xxy.ext
        root/class_x/xxz.ext

        root/class_y/123.ext
        root/class_y/nsdf3.ext
        root/class_y/asd932_.ext

    Args:
        root (string): Root directory path.
        loader (callable): A function to load a sample given its path.
        extensions (tuple[string]): A list of allowed extensions.
            both extensions and is_valid_file should not be passed.
        transform (callable, optional): A function/transform that takes in
            a sample and returns a transformed version.
            E.g, ``transforms.RandomCrop`` for images.
        target_transform (callable, optional): A function/transform that takes
            in the target and transforms it.
        is_valid_file (callable, optional): A function that takes path of a file
            and check if the file is a valid file (used to check of corrupt files)
            both extensions and is_valid_file should not be passed.

     Attributes:
        classes (list): List of the class names sorted alphabetically.
        class_to_idx (dict): Dict with items (class_name, class_index).
        samples (list): List of (sample path, class_index) tuples
        targets (list): The class_index value for each image in the dataset
    """

    def __init__(
            self,
            root: str,
            loader: Callable[[str], Any],
            extensions: Optional[Tuple[str, ...]] = None,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
            is_valid_file: Optional[Callable[[str], bool]] = None,
            index_file: Optional[str] = None,
    ) -> None:
        super(DatasetFolder, self).__init__(root, transform=transform,
                                            target_transform=target_transform)
        if index_file is None:
            classes, class_to_idx = self._find_classes(self.root)
            samples = make_dataset(self.root, class_to_idx, extensions, is_valid_file)
            if len(samples) == 0:
                msg = "Found 0 files in subfolders of: {}\n".format(self.root)
                if extensions is not None:
                    msg += "Supported extensions are: {}".format(",".join(extensions))
                raise RuntimeError(msg)
        else:
            with open(index_file, mode="r", encoding="utf-8") as reader:
                classes = []
                index_data = {}
                for line in reader:
                    data = json.loads(line)
                    class_name = data["class"]
                    classes.append(class_name)
                    index_data[class_name] = data["files"]

                classes.sort()
                class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
                samples = []
                for class_name in index_data:
                    class_index = class_to_idx[class_name]
                    for each_file in index_data[class_name]:
                        samples.append(
                            (os.path.join(root, class_name, each_file),
                            class_index)
                        )

        self.loader = loader
        self.extensions = extensions

        self.classes = classes
        self.class_to_idx = class_to_idx
        self.samples = samples
        self.targets = [s[1] for s in samples]

        print("Find %d classes and %d samples in root!" % (len(classes), len(samples)))

    def _find_classes(self, dir: str) -> Tuple[List[str], Dict[str, int]]:
        """
        Finds the class folders in a dataset.

        Args:
            dir (string): Root directory path.

        Returns:
            tuple: (classes, class_to_idx) where classes are relative to (dir), and class_to_idx is a dictionary.

        Ensures:
            No class is a subdirectory of another.
        """
        classes = [d.name for d in os.scandir(dir) if d.is_dir()]
        classes.sort()
        class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
        return classes, class_to_idx

    def __getitem__(self, index: int) -> Tuple[Any, Any]:
        """
        Args:
            index (int): Index

        Returns:
            tuple: (sample, target) where target is class_index of the target class.
        """
        while True:
            try:
                path, target = self.samples[index]
                sample = self.loader(path)
                break
            except Exception as e:
                print(e)
                index = random.randint(0, len(self.samples) - 1)

        if self.transform is not None:
            sample = self.transform(sample)
        if self.target_transform is not None:
            target = self.target_transform(target)

        return sample, target

    def __len__(self) -> int:
        return len(self.samples)

    def filenames(self, indices=[], basename=False):
        if indices:
            if basename:
                return [os.path.basename(self.samples[i][0]) for i in indices]
            else:
                return [self.samples[i][0] for i in indices]
        else:
            if basename:
                return [os.path.basename(x[0]) for x in self.samples]
            else:
                return [x[0] for x in self.samples]

IMG_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')

def pil_loader(path: str) -> Image.Image:
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        img = Image.open(f)
        return img.convert('RGB')

# TODO: specify the return type
def accimage_loader(path: str) -> Any:
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)

def default_loader(path: str) -> Any:
    from torchvision import get_image_backend
    if get_image_backend() == 'accimage':
        return accimage_loader(path)
    else:
        return pil_loader(path)

class ImageFolder(DatasetFolder):
    """A generic data loader where the images are arranged in this way: ::

        root/dog/xxx.png
        root/dog/xxy.png
        root/dog/xxz.png

        root/cat/123.png
        root/cat/nsdf3.png
        root/cat/asd932_.png

    Args:
        root (string): Root directory path.
        transform (callable, optional): A function/transform that takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        loader (callable, optional): A function to load an image given its path.
        is_valid_file (callable, optional): A function that takes path of an Image file
            and check if the file is a valid file (used to check of corrupt files)

     Attributes:
        classes (list): List of the class names sorted alphabetically.
        class_to_idx (dict): Dict with items (class_name, class_index).
        imgs (list): List of (image path, class_index) tuples
    """

    def __init__(
            self,
            root: str,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
            loader: Callable[[str], Any] = default_loader,
            is_valid_file: Optional[Callable[[str], bool]] = None,
            index_file: Optional[str] = None,
    ):
        super(ImageFolder, self).__init__(root, loader, IMG_EXTENSIONS if is_valid_file is None else None,
                                          transform=transform,
                                          target_transform=target_transform,
                                          is_valid_file=is_valid_file, index_file=index_file)
        self.imgs = self.samples

class DataAugmentationForBEiT(object):
    def __init__(self, args):
        imagenet_default_mean_and_std = args.imagenet_default_mean_and_std
        mean = IMAGENET_INCEPTION_MEAN if not imagenet_default_mean_and_std else IMAGENET_DEFAULT_MEAN
        std = IMAGENET_INCEPTION_STD if not imagenet_default_mean_and_std else IMAGENET_DEFAULT_STD

        # oringinal beit data augmentation
        self.common_transform = transforms.Compose([
            transforms.ColorJitter(0.4, 0.4, 0.4),
            transforms.RandomHorizontalFlip(p=0.5),
            RandomResizedCropAndInterpolationWithTwoPic(
                size=args.input_size, second_size=args.second_input_size, scale=(args.min_crop_scale, 1.0),
                interpolation=args.train_interpolation, second_interpolation=args.second_interpolation,
            ),
        ])

        self.patch_transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(
                mean=torch.tensor(mean),
                std=torch.tensor(std))
        ])

        self.visual_token_transform = transforms.Compose([
            transforms.ToTensor(),])

        self.masked_position_generator = MaskingGenerator(
            args.window_size, num_masking_patches=args.num_mask_patches,
            max_num_patches=args.max_mask_patches_per_block,
            min_num_patches=args.min_mask_patches_per_block,
        )

    def __call__(self, image):
        for_patches, for_visual_tokens = self.common_transform(image)
        return \
            self.patch_transform(for_patches), self.visual_token_transform(for_visual_tokens), \
            self.masked_position_generator()

    def __repr__(self):
        repr = "(DataAugmentationForBEiT,\n"
        repr += "  common_transform = %s,\n" % str(self.common_transform)
        repr += "  patch_transform = %s,\n" % str(self.patch_transform)
        repr += "  visual_tokens_transform = %s,\n" % str(self.visual_token_transform)
        repr += "  Masked position generator = %s,\n" % str(self.masked_position_generator)
        repr += ")"
        return repr

def build_beit_pretraining_dataset(args):
    transform = DataAugmentationForBEiT(args)
    print("Data Aug = %s" % str(transform))

    return ImageFolder(args.data_path, transform=transform)

#################################################
# Dataset and Transforms for Tokenizer Training
# ###############################################
def build_vqkd_dataset(is_train, args):
    if is_train:
        t = []
        if args.color_jitter > 0.:
            t.append(transforms.ColorJitter(args.color_jitter, args.color_jitter, args.color_jitter))
        t.append(transforms.RandomResizedCrop(args.input_size, scale=(args.min_crop_scale, 1.0), interpolation=_pil_interp(args.train_interpolation)))
        t.append(transforms.RandomHorizontalFlip(0.5))
        t.append(transforms.ToTensor())
        transform = transforms.Compose(t)
    else:
        t = []
        if args.input_size < 384:
            args.crop_pct = 224 / 256
        else:
            args.crop_pct = 1.0
        size = int(args.input_size / args.crop_pct)
        t.append(
            transforms.Resize(size, interpolation=_pil_interp(args.train_interpolation)),  # to maintain same ratio w.r.t. 224 images
        )
        t.append(transforms.CenterCrop(args.input_size))
        t.append(transforms.ToTensor())
        transform = transforms.Compose(t)

    print(f"{'Train' if is_train else 'Test'} Data Aug: {str(transform)}")
    import pdb;pdb.set_trace()
    if args.data_set == 'image_folder':
        if is_train:
            return ImageFolder(args.data_path, transform=transform)
        else:
            if args.eval_data_path == '':
                return ImageFolder(args.data_path, transform=transform)
            else:
                return ImageFolder(args.eval_data_path, transform=transform)

    else:
        raise NotImplementedError()

###############################################
# Dataset and Transforms for Ft
# #############################################
def build_dataset(is_train, args):
    transform = build_transform(is_train, args)

    print("Transform = ")
    if isinstance(transform, tuple):
        for trans in transform:
            print(" - - - - - - - - - - ")
            for t in trans.transforms:
                print(t)
    else:
        for t in transform.transforms:
            print(t)
    print("---------------------------")

    if args.data_set == 'CIFAR':
        dataset = datasets.CIFAR100(args.data_path, train=is_train, transform=transform)
        nb_classes = 100
    elif args.data_set == 'IMNET':
        root = os.path.join(args.data_path, 'train' if is_train else 'val')
        dataset = datasets.ImageFolder(root, transform=transform)
        nb_classes = 1000
    elif args.data_set == "image_folder":
        root = args.data_path if is_train else args.eval_data_path
        index_file = args.image_folder_class_index_file
        dataset = ImageFolder(root, transform=transform, index_file=index_file)
        nb_classes = args.nb_classes
        assert len(dataset.class_to_idx) == nb_classes
    else:
        raise NotImplementedError()
    assert nb_classes == args.nb_classes
    print("Number of the class = %d" % args.nb_classes)

    return dataset, nb_classes

def build_transform(is_train, args):
    resize_im = args.input_size > 32
    imagenet_default_mean_and_std = args.imagenet_default_mean_and_std
    mean = IMAGENET_INCEPTION_MEAN if not imagenet_default_mean_and_std else IMAGENET_DEFAULT_MEAN
    std = IMAGENET_INCEPTION_STD if not imagenet_default_mean_and_std else IMAGENET_DEFAULT_STD

    if is_train:
        # this should always dispatch to transforms_imagenet_train
        transform = create_transform(
            input_size=args.input_size,
            is_training=True,
            color_jitter=args.color_jitter,
            auto_augment=args.aa,
            interpolation=args.train_interpolation,
            re_prob=args.reprob,
            re_mode=args.remode,
            re_count=args.recount,
            mean=mean,
            std=std,
        )
        if not resize_im:
            # replace RandomResizedCropAndInterpolation with
            # RandomCrop
            transform.transforms[0] = transforms.RandomCrop(
                args.input_size, padding=4)
        return transform

    t = []
    if resize_im:
        if args.crop_pct is None:
            if args.input_size < 384:
                args.crop_pct = 224 / 256
            else:
                args.crop_pct = 1.0
        size = int(args.input_size / args.crop_pct)
        t.append(
            transforms.Resize(size, interpolation=3),  # to maintain same ratio w.r.t. 224 images
        )
        t.append(transforms.CenterCrop(args.input_size))

    t.append(transforms.ToTensor())
    t.append(transforms.Normalize(mean, std))
    return transforms.Compose(t)


from multiprocessing import Process, Pipe

def revr(conn, x):
  conn.send(x[::-1])

if __name__ == "__main__":
  print("Enter a word : ")
  a= input()
  parent, child = Pipe()
  p= Process(target=revr, args=(child,a))
  p.start()
  p.join()

  print("Reverse of Word : ")
  print(parent.recv())


#!/usr/bin/env python3

import os
import sys
import re
import shlex
import argparse
import subprocess as sp
import multiprocessing as mp

regex_ok = re.compile(r"^! (One|Two)-way A[AN]I (1|2| ):\s*(\d+\.\d+)% \(SD:\s*(\d+\.\d+)%\), from (\d+) (fragments|proteins)\.$")
regex_error = re.compile(r"^Insuffi.*ent hits to estimate (one|two)-way A[AN]I:\s*(\d+)\.?$")

def perc_calculation(params):
    accno1 = params[0]
    accno2 = params[1]
    hits = params[2]
    cache = params[3]
    ext = params[4]
    dec = params[5]
    window = params[6]
    step = params[7]

    seq1 = os.path.join(cache, accno1+'.'+ext)
    seq2 = os.path.join(cache, accno2+'.'+ext)

    cmd = ['./ani.rb',
               '-1', shlex.quote(seq1),
               '-2', shlex.quote(seq2),
               '-n', shlex.quote(str(hits)),
               '-d', shlex.quote(str(dec)),
               '-w', shlex.quote(str(window)),
               '-s', shlex.quote(str(step)),
               '-t', '1'
          ]
    values = []
    try:
        pc = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE,
                    universal_newlines=True, check=True)
    except sp.CalledProcessError as err:
        if err.returncode < 0:
            raise
    else:
        lines = pc.stdout.strip().splitlines()
        if len(lines) != 3:
            return values
        for idx, line in enumerate(lines):
            m = regex_ok.match(line)
            if m is not None:
                if (idx == 0) and (m.group(1) == 'One') and (m.group(2) == '1'):
                    values.extend(m.group(3, 4, 5))
                elif (idx == 1) and (m.group(1) == 'One') and (m.group(2) == '2'):
                    values.extend(m.group(3, 4, 5))
                elif (idx == 2) and (m.group(1) == 'Two') and (m.group(2) == ' '):
                    values.extend(m.group(3, 4, 5))
                else:
                    values = []
                    break
            else:
                m = regex_error.match(line)
                if m is not None:
                    if ((idx == 0) or (idx == 1)) and (m.group(1) == 'one'):
                        values.extend(('0', '0', m.group(2)))
                    elif (idx == 2) and (m.group(1) == 'two'):
                        values.extend(('0', '0', m.group(2)))
                    else:
                        values = []
                        break
                else:
                    values = []
                    break
        else:
            if len(values) != 9:
                values = []

    return values

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Calculate ANI distances')
    parser.add_argument('accessions', type=str,
                        help='input file with accession numbers')
    parser.add_argument('output', type=str,
                        help='base output name')
    parser.add_argument('seq_cache', type=str,
                        help='subdirectory with sequence files')
    parser.add_argument('-e', '--extension', type=str,
                        default='fna',
                        help='extension of sequence files')
    parser.add_argument('-t', '--threads', type=int,
                        default=1,
                        help='number of parallel threads to be used')
    parser.add_argument('-n', '--hits', type=int,
                        default=50,
                        help='minimum number of hits')
    parser.add_argument('-p', '--perc', type=float,
                        help='minimum (aprox.) shared length of shorter genome')
    parser.add_argument('-d', '--dec', type=int,
                        default=3,
                        help='decimal positions to report')
    parser.add_argument('-w', '--window', type=int,
                        default=1000,
                        help='window length')
    parser.add_argument('-s', '--step', type=int,
                        default=200,
                        help='sliding window step')
    parser.add_argument('--version', action='version', version='%(prog)s 1.0')
    args = parser.parse_args()

    plasmids = []
    pl_size = {}
    with open(args.accessions) as fh:
        for line in fh:
            if line.strip() == '':
                continue
            plasmids.append(line.strip())
            seq_file = os.path.join(args.seq_cache, line.strip()+'.'+args.extension)
            with open(seq_file) as fh_fna:
                next(fh_fna)
                pl_size[line.strip()] = len(next(fh_fna)) - 1

    tasks = []
    threshold = {}
    data = {}
    plasmids_tmp = plasmids[:]
    for p1 in plasmids:
        threshold[p1] = {}
        data[p1] = {}
        for p2 in plasmids_tmp:
            if args.perc:
                core_length = args.perc * min(pl_size[p1], pl_size[p2])
                if core_length <= args.window:
                    threshold[p1][p2] = 1
                else:
                    threshold[p1][p2] = 1 + int((core_length - args.window) // args.step)
            else:
                threshold[p1][p2] = args.hits
            tasks.append([p1, p2, threshold[p1][p2], args.seq_cache, \
                          args.extension, args.dec, args.window, args.step])
            data[p1][p2] = None
        plasmids_tmp.remove(p1)

    headers = ('Query genome', 'Reference genome', \
               'Query size', 'Reference size', 'min_hits', \
               'ANI_o1', 'ANI_o1_SD', 'ANI_o1_frag', \
               'ANI_o2', 'ANI_o2_SD', 'ANI_o2_frag', \
               'ANI_tw', 'ANI_tw_SD', 'ANI_tw_frag')
    if os.path.isfile(args.output):
        with open(args.output) as fh:
            header_line = next(fh).strip()
            if header_line != '\t'.join(headers):
                print('Wrong header:', header_line, file=sys.stderr)
                sys.exit(1)
            for line in fh:
                if line.strip() == '':
                    continue
                if (line[-1] != '\n') or (len(line.strip().split('\t')) != len(headers)):
                    print('Bad formatted line:', line, file=sys.stderr)
                    sys.exit(1)
                p1, p2, leftover = line.strip().split('\t', 2)
                if (p1 not in plasmids) or (p2 not in plasmids):
                    print('Wrong list of plasmids:', p1, p2, file=sys.stderr)
                    continue
                if data[p1][p2] != None:
                    print('Duplicatted line:', line, file=sys.stderr)
                else:
                    for idx, t in enumerate(tasks):
                        if (t[0] == p1) and (t[1] == p2):
                            tasks.pop(idx)
                            break
                    else:
                        print('Orphan task:', p1, p2, file=sys.stderr)
                        sys.exit(1)
                data[p1][p2] = leftover.split('\t', 3)[3]

    total = len(plasmids)**2
    processed = total - len(tasks)
    print('Processing {0:d} plasmids => {1:d} comparisons'.format(len(plasmids), total), file=sys.stderr)
    print('\t{0:d} ({1:.2%}) already calculated'.format(processed, processed/total), file=sys.stderr)

    pool_size = args.threads
    with mp.Pool(processes = pool_size) as pool:
        outputs = pool.map(perc_calculation, tasks)
        pool.close()
        pool.join()

    for t, o in zip(tasks, outputs):
        data[t[0]][t[1]] = '\t'.join(o)

    with open(args.output, 'w') as fh:
        fh.write('\t'.join(headers)+'\n')
        for p1 in data:
            for p2 in data[p1]:
                if len(data[p1][p2]) == 0:
                    print('Warning: Error calculating ANI for', p1, '=>', p2, file=sys.stderr)
                    continue
                items = (p1, p2, str(pl_size[p1]), str(pl_size[p2]),
                         str(threshold[p1][p2]), data[p1][p2])
                fh.write('\t'.join(items)+'\n')


import seaborn as sns
import matplotlib.pyplot as plt
import geopandas as gpd
from matplotlib.colors import ListedColormap
import os

def corr_matrix (df):
    print("---plotting---")
    os.system("say plotting")
    correlation_matrix=df.corr()
    return correlation_matrix

def heat_map (df):
    print("---plotting---")
    sns.heatmap(corr_matrix(df));
    plt.savefig("images/heat_map")
    plt.show()

def pollution_plot (df):
    print("---plotting---")
    map_1=df[["region", "year", "pollution"]].groupby("region").agg({"pollution":"mean"})

    # Load shapefile of world countries
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

    # Merge dataframe with shapefile using country names as the key
    merged = world.merge(map_1, left_on='name', right_on='region')

    # Create choropleth map using population data
    ax = merged.plot(column='pollution', cmap='Blues', figsize=(25, 10), edgecolor='black', linewidth=0.5, missing_kwds={'color': 'white'}, legend=True)

    # Add title and axis labels
    ax.set_title('Pollution worldwide', fontsize=20)
    ax.set_xlabel('Longitude', fontsize=15)
    ax.set_ylabel('Latitude', fontsize=15)

    plt.savefig("images/plot_pollution")
    plt.show()

def deaths_plot (df):
    print("---plotting---")
    latest_data = df[['region', 'year', 'number of deaths by air pollution']].groupby('region').agg({"number of deaths by air pollution":"mean"})

    # Load shapefile of world countries
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

    # Merge dataframe with shapefile using country names as the key
    merged = world.merge(latest_data, left_on='name', right_on='region')

    # Create choropleth map using population data
    ax = merged.plot(column='number of deaths by air pollution', cmap='Reds', figsize=(25, 10), edgecolor='black', linewidth=0.5, missing_kwds={'color': 'white'}, legend=True)

    # Add title and axis labels
    ax.set_title('number of deaths by air pollution', fontsize=20)
    ax.set_xlabel('Longitude', fontsize=15)
    ax.set_ylabel('Latitude', fontsize=15)

    plt.savefig("images/plot_deaths")
    plt.show()

def sales_chargers (df):
    print("---plotting---")
    condition_1 = df['number of chargers'] < 20000
    sns.set(style="ticks")
    chargers_plot=df[condition_1]
    sns.scatterplot(x="number of chargers", y="sales", data=chargers_plot)
    plt.title("Sales vs number of chargers", y=1.05)
    plt.savefig("images/sales_chargers")
    plt.show()

def sales_pollution (df):
    print("---plotting---")
    sns.set(style="ticks")
    condition_2 = df['pollution'] < 30
    pollution_plot=df[condition_2]
    sns.scatterplot(x="pollution", y="sales", data=pollution_plot)
    plt.title("Sales vs pollution", y=1.05)
    plt.title("Sales vs number of chargers", y=1.05)
    plt.savefig("images/sales_pollution")
    plt.show()


# random test/experimental code that william unaccountably wanted to
# check into git

import math
import numpy as np
import scipy.sparse as sp

import tensorflow as tf

from tensorlog import matrixdb,mutil

def go():
  db = matrixdb.MatrixDB.loadFile("test-data/textcattoy_corpus.cfacts")
  m = db.matEncoding[('hasWord',2)]
  print('m',m.shape,m.nnz)
  def tfidf_transform(tf_matrix):
    # implements Salton's TFIDF transformation, ie l2-normalized
    # vector after scaling by: log(tf+1.0) * log(#docs/idf)
    df = tf_matrix.sum(axis=0)
    # count docs by summing word counts w/in each row of m,
    # clipping the sum down to 1, and and then adding up
    ndoc = np.clip(tf_matrix.sum(axis=1),0.0,1.0).sum()
    # this ensures idfs will be zero for any terms with df==0, after I
    # take the log
    df[df==0] = ndoc
    idf = np.log( np.reciprocal(df) * ndoc )
    # first compute log(tf+1.0)
    scaled_tf_matrix = mutil.mapData(lambda v:np.log(v+1.0),tf_matrix)
    # now multiply by idf factor
    unnormalized_tfidf_matrix = mutil.broadcastAndComponentwiseMultiply(scaled_tf_matrix, sp.csr_matrix(idf))
    # compute normalizer needed for l2 normalization
    normalizer = mutil.mapData(lambda v:v*v, unnormalized_tfidf_matrix).sum(axis=1)
    normalizer = np.sqrt(normalizer)
    # finally, multiply unnormalized_tfidf_matrix and normalizer,
    # which is complicated since they are sparse, scipy.sparse doesn't
    # support broadcasting, and
    # mutil.broadcastAndComponentwiseMultiply can only broadcast along
    # rows. so we need to transpose everything before and after, and
    # convert into csr matrices
    tmp1 = sp.csr_matrix(unnormalized_tfidf_matrix.transpose())
    tmp2 = sp.csr_matrix(normalizer.transpose())
    tmp2.eliminate_zeros()
    tmp3 = mutil.mapData(np.reciprocal,tmp2)
    tmp4 = mutil.broadcastAndComponentwiseMultiply(tmp1,tmp3)
    result = sp.csr_matrix(tmp4.transpose())
    return result
  m = tfidf_transform(m)

def go1():
  dense = tf.Variable([[0,0,10,1,0,0],[0,0,-2,3,0,0]], dtype=tf.float32)
  sm1 = tf.nn.softmax(dense)

  denseReplacing0WithNeg10 = tf.where(
      dense>0.0,
      dense,
      tf.ones(tf.shape(dense), tf.float32)*(-10.0))
  sm2 = tf.nn.softmax(denseReplacing0WithNeg10)

  nz_indices = tf.where(tf.not_equal(dense, tf.constant(0, dtype=tf.float32)))
  nz_values = tf.gather_nd(dense,nz_indices)
  sparse = tf.SparseTensor(nz_indices, nz_values, dense.get_shape())
  sm3 = tf.sparse_softmax(sparse)
  dm3a = tf.sparse_to_dense(sm3.indices,sm3.get_shape(),sm3.values)
  dm3b = tf.scatter_nd(sm3.indices,sm3.values,dense.get_shape())

  session = tf.Session()
  session.run(tf.global_variables_initializer())
  from tensorflow.python.framework import ops
  for v in nz_indices,nz_values,sparse,sm3,dm3a,dm3b:
    print('gradient of op',v,ops.get_gradient_function(v.op))

  print('dense sm - direct',session.run(sm1))
  print('dense sm - with -10 trick',session.run(sm2))
  print('sparse sm',session.run(sm3))
  print('densified sparse sm - old',session.run(dm3a))
  print('densified sparse sm - new',session.run(dm3a))

if __name__ == "__main__":
  print('trying')
  go()


#!/usr/bin/env python
"""
    Script to replay fuzzer .log files created by caringcaribou
    (c) Jannik Schmied, 2023
"""
import os
import subprocess
import sys
from argparse import ArgumentParser, Namespace

def parse_args() -> Namespace:
    """Parse command line arguments"""
    parser: ArgumentParser = ArgumentParser()
    parser.add_argument("--file", "-f", type=str, required=True)
    parser.add_argument("--interface", "-i", type=str, required=True)

    return parser.parse_args()

def main():
    args = parse_args()

    if os.path.exists(args.file):
        with open(args.file, 'r') as messages:
            for msg_data in messages:
                msg = ["cansend", args.interface, msg_data]

                try:
                    proc = subprocess.Popen(msg, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    print(f"[i] Current: {msg.strip()}", end='\r')
                except KeyboardInterrupt:
                    print("\n[*] Stopped. (Interrupted by user)")
                    exit(0)
                except Exception as e:
                    print("[!] Error:", e)
                finally:
                    proc.terminate()
    else:
        print("[!] Error: file does not exist!")
        sys.exit(1)

    print("\n[+] Finished.\n")
    sys.exit(0)

if __name__ == '__main__':
    main()


import yaml
import os
import sys
import cv2
import glob
from terminaltables import AsciiTable
from progress.bar import Bar

def openAndResize(path,size):
    try:
        im = cv2.imread(path)
        old_size = im.shape[:2] # old_size is in (height, width) format

        ratio = float(size)/max(old_size)
        new_size = tuple([int(x*ratio) for x in old_size])

        # new_size should be in (width, height) format
        im = cv2.resize(im, (new_size[1], new_size[0]))

        delta_w = size - new_size[1]
        delta_h = size - new_size[0]
        top, bottom = delta_h//2, delta_h-(delta_h//2)
        left, right = delta_w//2, delta_w-(delta_w//2)

        color = [0, 0, 0]
        new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
            value=color)
        return 1,new_im
    except:
        print(sys.exc_info()[0])
        print("Error Image : ",path)
        return -1,[] #error

dir_path = os.path.dirname(os.path.realpath(__file__))
# no slash dir path
with open("config.yml", "r") as ymlfile:
    cfg = yaml.safe_load(ymlfile)

# {'input': {'folder': 'input'}, 'output': {'size': 32, 'folder': 'output'}}

inputPath = cfg['input']['folder']
outputPath = cfg['output']['folder']
imgSize = cfg['output']['size']

folders = [f for f in glob.glob(inputPath + "/*/", recursive=True)]

# Print input table
input_data = [
    ['Letter', 'count'],
]
total_count = 0
letters = []
for fol in folders:
    letter = fol.rsplit("/")[1]
    letters.append(letter)

    files = [f for f in glob.glob(fol + "**/*.png", recursive=True)]
    # Append to table
    #input/ആ/
    input_data.append([letter,len(files)])
    total_count+= len(files)

table = AsciiTable(input_data)
print("Input")
print(table.table)
print("Total :"+str(total_count))

count = 0
error_count = 0
bar = Bar('Processing', max=total_count)
for fol in folders:
    files = [f for f in glob.glob(fol + "**/*.png", recursive=True)]
    letter = fol.rsplit("/")[1]
    outputFolder = outputPath+"/"+letter+"/"
    #make output folder of not exist
    os.makedirs(outputFolder, exist_ok=True)
    current_count = 0
    for file in files:
        status,image = openAndResize(path=file,size=imgSize)
        filename = outputFolder+str(current_count)+".png"
        if(status != -1):
            cv2.imwrite(filename, image)
            count+=1
            current_count+=1
            bar.next()
        else:
            error_count+=1

print("\nDone!")
print("Success Images: "+str(count))
print("Error Images: "+str(error_count))

# files = [f for f in glob.glob(path + "**/*.txt", recursive=True)]

# r=root, d=directories, f = files