import asyncio
from datetime import timedelta

import weasyprint
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.exceptions import ValidationError
from django.core.files.storage import default_storage
from django.db import transaction
from django.db.models import Prefetch, Q, Count, Sum, F, fields, ExpressionWrapper, IntegerField, Avg, Max
from django.db.models.functions import Coalesce
from django.forms import modelformset_factory
from django.http import Http404, HttpResponseForbidden, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404

# Create your views here.
from django.template.loader import render_to_string
from django.utils import timezone

from conf.constants import EXERCISE_CHOICES
from notifications.views import create_raw_notifications
from staff_schedule.models import TrainingSessionInfoSub
from tasks.forms import EditSubTaskQuestionForm, CreateTaskForm, CreateSubTaskForm, CreateTaskDocumentForm
from tasks.helpers.task_helpers import save_all_attached_tasks, info_generator_helper
from tasks.models import Task, SubTask, SubTaskDependency, SubTaskFormQuestion, SubTaskHelperFile, TaskMember, \
    TaskHelperFile
from tasks.tasks import create_text_file
from trainer.models import SessionExerciseItem, CardiovascularActiveTest, StaminaActiveTest, FlexibilityActiveTest

def belongs_to_working_on(user, task_id):
    task = Task.objects.prefetch_related(
        Prefetch('task_members', queryset=TaskMember.objects.filter(member=user))
    ).get(id=task_id)
    return task.task_members.filter(member=user).exists()

def user_belongs_to_working_on(view_func):
    def wrapper(request, *args, **kwargs):
        task_id = kwargs.get('task_id')
        if belongs_to_working_on(request.user, task_id):
            return view_func(request, *args, **kwargs)
        else:
            return HttpResponseForbidden("You are not authorized to access this page.")

    return wrapper

@login_required()
def my_tasks(request):
    # filter through tasks to display all the tasks

    # this will be the conditional if the subtasks are not being shown —
    # by default, they will not be shown on the main page:
    # user_subtasks = SubTask.objects.filter(assigned_to=request.user).select_related('task')
    user_tasks = Task.objects.filter(
        Q(subtask_items__assigned_to=request.user) | Q(task_members__member=request.user)
    ).exclude(
        Q(status="COMP") | Q(status="ARCH")
    ).distinct("status", "id").order_by("status", "id", "start_date"
                                        ).prefetch_related("task_members", "subtask_items",
                                                           "subtask_items__from_subtask", "subtask_items__to_subtask")
    context = {
        "user_tasks": user_tasks,
        "create_task_form": CreateTaskForm()
    }
    return render(request, 'my-tasks.html', context)

@login_required()
def create_new_task(request):
    if request.method == "POST" and request.POST.get("submit-task"):
        form = CreateTaskForm(request.POST)
        if form.is_valid():
            created_task = form.save()
            created_task.members.add(*get_user_model().objects.filter(
                id__in=request.POST.getlist("task_members")
            ))
            return redirect("tasks:my-tasks")
        else:
            user_tasks = Task.objects.filter(
                Q(subtask_items__assigned_to=request.user) | Q(task_members__member=request.user)
            ).exclude(
                Q(status="COMP") | Q(status="ARCH")
            ).distinct("status", "id").order_by("status", "id", "start_date"
                                                ).prefetch_related("subtask_items", "subtask_items__from_subtask",
                                                                   "subtask_items__to_subtask")
            context = {
                "user_tasks": user_tasks,
                "create_task_form": CreateTaskForm(),
            }
            return render(request, 'my-tasks.html', context)

@login_required()
@user_belongs_to_working_on
def create_subtask(request, task_id):
    if request.method == "POST" and request.POST.get("subtask-creation"):
        form = CreateSubTaskForm(request.POST)
        if form.is_valid():
            form.save()
            TaskMember.objects.get_or_create(
                task_id=task_id,
                member=form.cleaned_data["assign_to"]
            )
            return redirect("tasks:task-detail", task_id, request.POST.get("slug"))

@login_required()
@user_belongs_to_working_on
def task_detail(request, task_id, slug):
    # todo: caching and database indexes here
    # todo: consider lazy loading the dependencies for later maybe using HTMX preload
    task = Task.objects.select_related("task_leader") \
        .prefetch_related(
        "subtask_items__from_subtask__to_subtask",
        "subtask_items__to_subtask__from_subtask", "task_files", "task_links").get(id=task_id)
    task_member = TaskMember.objects.get(task=task, member=request.user)
    create_subtask_form = CreateSubTaskForm(initial={"task": task, "created_by": request.user})
    context = {"task": task, "task_member": task_member, "create_subtask_form": create_subtask_form}
    return render(request, "task-detail.html", context)

@login_required()
@user_belongs_to_working_on
def subtask_detail(request, task_id, subtask_id, slug):
    if request.method == "POST" and request.POST.get("form-subtask-submission"):
        subtask_form_question_formset = modelformset_factory(SubTaskFormQuestion,
                                                             form=EditSubTaskQuestionForm, extra=0, edit_only=True)
        subtask = SubTask.objects.select_related("task").prefetch_related("subtask_questions").get(id=subtask_id)
        formset = subtask_form_question_formset(request.POST,
                                                queryset=SubTaskFormQuestion.objects.filter(subtask=subtask))
        if formset.is_valid():
            formset.save()
            # refresh subtask
            subtask = SubTask.objects.select_related("task").prefetch_related("subtask_questions").get(id=subtask_id)
            # todo: consider removing dependencies once the task is marked as complete
            save_all_attached_tasks(subtask)
            if subtask.convert_to_text:
                # todo — create a text file with answers
                create_text_file(subtask)
            return redirect("tasks:task-detail", subtask.task.id, subtask.task.slug)
        else:
            non_form_errors = formset.non_form_errors()
            form_errors = [form.errors for form in formset]
            # todo: log this
            raise ValidationError("An Error Occurred")
    if request.method == "POST" and request.POST.get("uploading-files"):
        subtask = SubTask.objects.get(id=subtask_id)
        files = request.FILES.getlist("file_field")
        for file in files:
            SubTaskHelperFile.objects.create(
                document=file, subtask_id=subtask_id
            )
        save_all_attached_tasks(subtask)
        return redirect("tasks:task-detail", task_id, subtask.task.slug)
    if request.method == "POST" and request.POST.get("complete-generic"):
        subtask = SubTask.objects.get(id=subtask_id)
        save_all_attached_tasks(subtask)
        return redirect("tasks:task-detail", subtask.task.id, subtask.task.slug)
    else:
        subtask = SubTask.objects.select_related("task").prefetch_related("subtask_files").get(id=subtask_id)
        if subtask.subtask_type == "FTSK":
            subtask_form_question_formset = modelformset_factory(SubTaskFormQuestion,
                                                                 form=EditSubTaskQuestionForm, extra=0, edit_only=True)
            formset = subtask_form_question_formset(queryset=SubTaskFormQuestion.objects.filter(subtask=subtask))
            context = {"subtask": subtask, "formset": formset}
            return render(request, "subtask-templates/subtask-question-detail.html", context)
        elif subtask.subtask_type == "UTSK":
            context = {"subtask": subtask}
            return render(request, "subtask-templates/subtask-upload-detail.html", context)
        else:
            context = {"subtask": subtask}
            return render(request, "subtask-templates/subtask-generic-detail.html", context)

@login_required()
@user_belongs_to_working_on
def create_task_files(request, task_id):
    if request.method == "POST":
        task = get_object_or_404(Task, id=task_id)
        files = request.FILES.getlist("file_field")
        for file in files:
            TaskHelperFile.objects.create(
                document=file, task_id=task_id
            )
        return redirect("tasks:task-detail", task_id, task.slug)

@login_required
def create_training_report(request, task_id):
    task = Task.objects.select_related("linked_relationship", "linked_relationship__client",
                                       "linked_relationship__trainer").get(id=task_id)
    current_time = timezone.now()
    report_starting_from = current_time - timedelta(days=30)
    training_sessions = TrainingSessionInfoSub.objects.filter(
        session_start_time__gte=report_starting_from, personal_training_event=task.linked_relationship,
        session_end_time__isnull=False,
    ).annotate(
        session_duration=ExpressionWrapper(F("session_end_time") - F("session_start_time"),
                                           output_field=fields.DurationField())
    ).prefetch_related("exercises_performed")
    training_aggregate = training_sessions.aggregate(num_exercises=Count("exercises_performed"),
                                                     num_reps=Sum("exercises_performed__reps"),
                                                     num_sets=Sum("exercises_performed__sets"))
    session_detail_aggregate = training_sessions.aggregate(num_of_sessions=Count("id"))
    exercises_per_session = round(
        float(training_aggregate["num_exercises"] / session_detail_aggregate["num_of_sessions"]), 2)

    # exercise details querysets:
    exercise_display_names = dict(EXERCISE_CHOICES)
    top_exercises = training_sessions.values("exercises_performed__exercise_name").annotate(
        top_sets_count=Coalesce(Sum("exercises_performed__sets"), 0),
        top_reps_count=Coalesce(Sum("exercises_performed__reps"), 0),
    ).order_by("-top_sets_count")[:5]

    for exercise in top_exercises:
        print(exercise['exercises_performed__exercise_name'], exercise['top_sets_count'])

    total_time = 0
    for session in training_sessions:
        total_time = int(session.session_duration.total_seconds() / 60)

    # trainer comments:
    trainer_comment_file = TaskHelperFile.objects.filter(
        document__contains=f"Trainer-Report-{task_id}"
    ).first()
    trainer_comment_content = []
    if trainer_comment_file:
        file = trainer_comment_file.document
        # open file
        with default_storage.open(file.name, "r") as trainer_report_file:
            for line in trainer_report_file:
                line = line.strip()
                if line[:3] == "[Q]":
                    question = line.split("[Q]")[1]
                    trainer_comment_content.append(question)
                elif line[:3] == "[A]":
                    answer = line.split("[A]")[1]
                    trainer_comment_content.append(answer)
            trainer_report_file.close()

    # active-tests:
    try:
        cardio_test_results = CardiovascularActiveTest.objects.get(
            client_id=task.linked_relationship.client.id, measuring=True
        )
    except CardiovascularActiveTest.DoesNotExist:
        cardio_test_results = None

    try:
        stamina_test_results = StaminaActiveTest.objects.get(client_id=task.linked_relationship.client.id,
                                                             measuring=True)
    except StaminaActiveTest.DoesNotExist:
        stamina_test_results = None

    try:
        flexibility_test_results = FlexibilityActiveTest.objects.get(client_id=task.linked_relationship.client.id,
                                                                     measuring=True)
    except FlexibilityActiveTest.DoesNotExist:
        flexibility_test_results = None

    cardio_info = info_generator_helper(cardio_test_results) \
        if cardio_test_results and cardio_test_results.measurements != {} else None
    stamina_info = info_generator_helper(stamina_test_results) \
        if stamina_test_results and stamina_test_results.measurements != {} else None
    flexibility_info = info_generator_helper(flexibility_test_results) \
        if flexibility_test_results and flexibility_test_results.measurements != {} else None

    context = {
        "task": task,
        "training_sessions": training_sessions,
        "training_aggregate": training_aggregate,
        "session_detail_aggregate": session_detail_aggregate,
        "top_exercises": top_exercises,
        "exercise_display_names": exercise_display_names,
        "exercises_performance_details": (total_time, exercises_per_session),
        "trainer_comments": trainer_comment_content,
        "cardio_info": cardio_info,
        "stamina_info": stamina_info,
        "flexibility_info": flexibility_info,
    }
    html = render_to_string('reports/30-day-report.html', context)
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = f'filename=test-subtask.pdf'
    # todo: before production — take a look at static here vs django3
    weasyprint.HTML(string=html, base_url=request.build_absolute_uri()).write_pdf(response,
                                                                                stylesheets=[weasyprint.CSS(
                                                                                'static/' + 'ISMS_NODE/scss'
                                                                                '/training_reports'
                                                                                '.css')])
    return response

def view_training_report(request, task_id):
    task = Task.objects.select_related("linked_relationship", "linked_relationship__client",
                                       "linked_relationship__trainer").get(id=task_id)
    current_time = timezone.now()
    report_starting_from = current_time - timedelta(days=30)
    training_sessions = TrainingSessionInfoSub.objects.filter(
        session_start_time__gte=report_starting_from, personal_training_event=task.linked_relationship,
        session_end_time__isnull=False,
    ).annotate(
        session_duration=ExpressionWrapper(F("session_end_time") - F("session_start_time"),
                                           output_field=fields.DurationField())
    ).prefetch_related("exercises_performed")
    training_aggregate = training_sessions.aggregate(num_exercises=Count("exercises_performed"),
                                                     num_reps=Sum("exercises_performed__reps"),
                                                     num_sets=Sum("exercises_performed__sets"))
    session_detail_aggregate = training_sessions.aggregate(num_of_sessions=Count("id"))
    exercises_per_session = round(
        float(training_aggregate["num_exercises"] / session_detail_aggregate["num_of_sessions"]), 2)

    # exercise details querysets:
    exercise_display_names = dict(EXERCISE_CHOICES)
    top_exercises = training_sessions.values("exercises_performed__exercise_name").annotate(
        top_sets_count=Coalesce(Sum("exercises_performed__sets"), 0),
        top_reps_count=Coalesce(Sum("exercises_performed__reps"), 0),
    ).order_by("-top_sets_count")[:5]

    for exercise in top_exercises:
        print(exercise['exercises_performed__exercise_name'], exercise['top_sets_count'])

    total_time = 0
    for session in training_sessions:
        total_time = int(session.session_duration.total_seconds() / 60)

    # exercise performance duration has been converted to minutes
    context = {
        "task": task,
        "training_sessions": training_sessions,
        "training_aggregate": training_aggregate,
        "session_detail_aggregate": session_detail_aggregate,
        "top_exercises": top_exercises,
        "exercise_display_names": exercise_display_names,
        "exercises_performance_details": (total_time, exercises_per_session)
    }
    return render(request, "reports/view-30-day-report.html", context)
    pass


"""
=================================
Gaussian Mixture Model Ellipsoids
=================================

Plot the confidence ellipsoids of a mixture of two Gaussians
obtained with Expectation Maximisation (``GaussianMixture`` class) and
Variational Inference (``BayesianGaussianMixture`` class models with
a Dirichlet process prior).

Both models have access to five components with which to fit the data. Note
that the Expectation Maximisation model will necessarily use all five
components while the Variational Inference model will effectively only use as
many as are needed for a good fit. Here we can see that the Expectation
Maximisation model splits some components arbitrarily, because it is trying to
fit too many components, while the Dirichlet Process model adapts it number of
state automatically.

This example doesn't show it, as we're in a low-dimensional space, but
another advantage of the Dirichlet process model is that it can fit
full covariance matrices effectively even when there are less examples
per cluster than there are dimensions in the data, due to
regularization properties of the inference algorithm.

"""

import itertools

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from scipy import linalg

from sklearn import mixture

color_iter = itertools.cycle(["navy", "c", "cornflowerblue", "gold", "darkorange"])

def plot_results(X, Y_, means, covariances, index, title):
    splot = plt.subplot(2, 1, 1 + index)
    for i, (mean, covar, color) in enumerate(zip(means, covariances, color_iter)):
        v, w = linalg.eigh(covar)
        v = 2.0 * np.sqrt(2.0) * np.sqrt(v)
        u = w[0] / linalg.norm(w[0])
        # as the DP will not use every component it has access to
        # unless it needs it, we shouldn't plot the redundant
        # components.
        if not np.any(Y_ == i):
            continue
        plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], 0.8, color=color)

        # Plot an ellipse to show the Gaussian component
        angle = np.arctan(u[1] / u[0])
        angle = 180.0 * angle / np.pi  # convert to degrees
        ell = mpl.patches.Ellipse(mean, v[0], v[1], angle=180.0 + angle, color=color)
        ell.set_clip_box(splot.bbox)
        ell.set_alpha(0.5)
        splot.add_artist(ell)

    plt.xlim(-9.0, 5.0)
    plt.ylim(-3.0, 6.0)
    plt.xticks(())
    plt.yticks(())
    plt.title(title)

# Number of samples per component
n_samples = 500

# Generate random sample, two components
np.random.seed(0)
C = np.array([[0.0, -0.1], [1.7, 0.4]])
X = np.r_[
    np.dot(np.random.randn(n_samples, 2), C),
    0.7 * np.random.randn(n_samples, 2) + np.array([-6, 3]),
]

# Fit a Gaussian mixture with EM using five components
gmm = mixture.GaussianMixture(n_components=5, covariance_type="full").fit(X)
plot_results(X, gmm.predict(X), gmm.means_, gmm.covariances_, 0, "Gaussian Mixture")

# Fit a Dirichlet process Gaussian mixture using five components
dpgmm = mixture.BayesianGaussianMixture(n_components=5, covariance_type="full").fit(X)
plot_results(
    X,
    dpgmm.predict(X),
    dpgmm.means_,
    dpgmm.covariances_,
    1,
    "Bayesian Gaussian Mixture with a Dirichlet process prior",
)

plt.show()


import torch
import torch.nn as nn
from torchsummary import summary

class Conv3x3(nn.Module):
    def __init__(self, inp_dim, out_dim):
        super().__init__()
        self.conv = nn.Conv2d(inp_dim, out_dim, 3, 1, 1)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.conv(x)
        x = self.relu(x)
        return x

class HandSegNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Sequential(
            Conv3x3(3, 64),
            Conv3x3(64, 64),
            nn.MaxPool2d(4, 2, 1)
        )
        self.layer2 = nn.Sequential(
            Conv3x3(64, 128),
            Conv3x3(128, 128),
            nn.MaxPool2d(4, 2, 1)
        )
        self.layer3 = nn.Sequential(
            Conv3x3(128, 256),
            Conv3x3(256, 256),
            Conv3x3(256, 256),
            Conv3x3(256, 256),
            nn.MaxPool2d(4, 2, 1)
        )
        self.layer4 = nn.Sequential(
            Conv3x3(256, 512),
            Conv3x3(512, 512),
            Conv3x3(512, 512),
            Conv3x3(512, 512),
            Conv3x3(512, 512),
            nn.Conv2d(512, 2, 1),
            nn.Upsample(size=(256, 256), mode='bilinear', align_corners=True)
        )

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        return x

if __name__ == '__main__':
    device = torch.device('cuda')
    net = HandSegNet().to(device)
    summary(net, (3, 256, 256))
    x = torch.randn((1, 3, 256, 256)).to(device)
    out = net(x)
    print(out.shape)


from openpyxl.styles import Color, Font, PatternFill, colors
from openpyxl.styles import Font, Color, colors
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font, Border, Side
from openpyxl import load_workbook
from openpyxl.utils import column_index_from_string

# 엑셀 파일 불러오기
workbook = load_workbook(filename='파일이름.xlsx')
worksheet = workbook.active

# 칼럼1과 칼럼2를 저장할 리스트와 시트에서 데이터를 불러와서 저장할 리스트 생성
col1_values = []
col2_values = []
data = []

# 시트에서 데이터를 불러와서 저장
for row in worksheet.iter_rows(min_row=2, values_only=True):
    col1_value = str(row[4])  # 5번째 칸에서 칼럼1 데이터를 문자열로 변환하여 가져옴
    col2_value = str(row[6])  # 7번째 칸에서 칼럼2 데이터를 문자열로 변환하여 가져옴
    other_columns_data = row[:4] + row[5:6] + row[7:]  # 날짜와 관련 없는 칼럼들의 데이터
    col1_values.append(col1_value)
    col2_values.append(col2_value)
    data.append((col1_value, col2_value, *other_columns_data))

# 칼럼1, 칼럼2를 기준으로 데이터 정렬
sorted_data = [x for _, x in sorted(zip(
    zip(col1_values, col2_values), data), key=lambda pair: (pair[0][0], pair[0][1]))]

# 시트에서 데이터 삭제
start_col = column_index_from_string('A')
end_col = column_index_from_string('L')
worksheet.delete_rows(2, worksheet.max_row)

# 정렬된 데이터 시트에 삽입
for i, row_data in enumerate(sorted_data):
    col1, col2, *other_columns_data = row_data
    row = other_columns_data[:4] + [col1] + \
        other_columns_data[4:5] + [col2] + other_columns_data[5:]
    for j, val in enumerate(row):
        worksheet.cell(row=i+2, column=j+start_col, value=val)

# 엑셀 파일 저장
workbook.save(filename='파일이름1.xlsx')

### 이제 폰트와 선을 설정하자!!!!!!!!

filename = '파일이름1.xlsx'
sheetname = 'Sheet1'
wb = load_workbook(filename)
ws = wb[sheetname]

data = []
for row in ws.iter_rows(min_row=2, values_only=True):
    data.append(row)

col1_idx = 4  # 칼럼1 인덱스 (0부터 시작)
col2_idx = 6  # 칼럼2 인덱스 (0부터 시작)

# 글자체와 크기 설정
font2 = Font(name='NGULIM', size=12, bold=True)
font1 = Font(name='새굴림', size=11)

# 전체 범위에 테두리 설정
border = Border(left=Side(style='thin'), right=Side(style='thin'),
                top=Side(style='thin'), bottom=Side(style='thin'))
max_row = ws.max_row
max_col = ws.max_column
for row in ws.iter_rows(min_row=1, max_row=max_row, max_col=max_col):
    for cell in row:
        cell.border = border

# 각 칼럼별 글자체와 크기 설정
for col_num in range(1, max_col + 1):
    col_letter = get_column_letter(col_num)
    if col_num in [col1_idx+1, col2_idx+1]:  # 칼럼1과 칼럼2 다음 칼럼
        for cell in ws[col_letter]:
            cell.font = font2
    elif col_num in [col1_idx+1, col2_idx+1]:  # 칼럼1과 칼럼2
        for cell in ws[col_letter]:
            cell.font = font1
    else:
        for cell in ws[col_letter]:
            cell.font = font1

sorted_data = sorted(data, key=lambda x: (x[col1_idx], x[col2_idx]))

for row_num, row in enumerate(sorted_data, start=2):
    for col_num, value in enumerate(row, start=1):
        col_letter = get_column_letter(col_num)
        cell = ws.cell(row=row_num, column=col_num, value=value)
        cell.border = border
        if col_num in [col1_idx+1, col2_idx+1]:  # 칼럼1과 칼럼2 다음 칼럼
            cell.font = font2
        elif col_num in [col1_idx+1, col2_idx+1]:  # 칼럼1과 칼럼2
            cell.font = font1

wb.save(filename)

filename = '파일이름1.xlsx'
sheetname = 'Sheet1'
wb = load_workbook(filename)
ws = wb[sheetname]

data = []

# 노란색으로 칠하기 위한 PatternFill 객체 생성
yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')

for i, row in enumerate(ws.iter_rows(min_row=2)):
    if row[2].value == '222':  # 3번째 열(column)의 값이 '222'인 경우
        for cell in row:

            cell.font = Font(color=colors.YELLOW)
    else:
        for cell in row:

            cell.font = Font(color=colors.YELLOW)

wb.save('파일이름2.xlsx')

테스트_엑셀소트기능추가.py

테스트_엑셀소트기능추가.py


import sys
import logging
import datetime
import webbrowser
from PyQt5 import QtWidgets, QtCore, QtGui

import serial
from serial.tools import list_ports

import HvGUI
import HvController as hv
import workers

ICON_RED_LED = ":/icons/led-red-on.png"
ICON_GREEN_LED = ":/icons/green-led-on.png"

class MainWindow(QtWidgets.QMainWindow, HvGUI.Ui_MainWindow):
    '''
    Main instance for the GUI of the HV controller panel

    HvControllerGUI is designed to operate a HV power supply from
    Glassman remotely through a graphical user interface. It was
    developped for use with a FJ model +40kV 3.0 mA.

    It makes use of QRunnables and QThreadPool for running small
    repetitive opperations without freezing the GUI. It also makes
    extensive use of PyQt signal and slot design for communication
    between threads.

    '''

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        # Setup application logger
        self.logger = logging.getLogger('hvController')
        self.logger.setLevel(logging.INFO)
        # create file handler which logs info messages
        fh = logging.FileHandler('hvCtrl.log', mode='w')
        fh.setLevel(logging.INFO)
        # create formatter and add it to the handler
        formatter = logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        # add the handler to the logger
        self.logger.addHandler(fh)

        self.hvdevice = hv.HvController()
        self.querytimer = QtCore.QTimer()
        self.checktimer = QtCore.QTimer()
        self.setupTimers()
        self.threadpool = QtCore.QThreadPool()
        self._setupUiDesign()

        # Extract some useful values from the GUI
        self.targetHV = self.voltValueToSet.value()
        self.targetI = self.curValueToSet.value()
        # TODO : add settings for saving preferences

    def _setupUiDesign(self):
        '''Prepare the initial desgin of the GUI. '''
        self.voltValueToSet.setMaximum(self.hvdevice.MAX_VOLTAGE)
        self.curValueToSet.setMaximum(self.hvdevice.MAX_CURENT)
        self.disableAll()
        for name, desc, add in sorted(list_ports.comports()):
            self.prtList.addItem(name)
        # Put COM4 port as default: adapt to workstation or improve
        # and implement with QSettings
        if self.prtList.findText('COM4') >= 0:
            self.prtList.setCurrentIndex(self.prtList.findText('COM4'))

    def setupTimers(self):
        '''Configure the timers for query and stability check'''
        # The Glassman HV has a communication timeout of 1.5 s
        # so we perform a query every 500 ms
        self.querytimer.setInterval(500)
        self.querytimer.timeout.connect(self.hvdevice.queryHV)
        self.querytimer.timeout.connect(self.updateStatus)

        self.checktimer.setInterval(60000)  # can be changed to longer
        self.checktimer.timeout.connect(self.checkStability)

        self.now = datetime.datetime.today()
        self.lastEntryTime = datetime.datetime.today()

    # ---------------- Slots --------------
    # define here pyqtslot using connectSlotsByName() convention
    @QtCore.pyqtSlot()
    def on_actionAbout_triggered(self):
        self.showMessage("HvControllerGUI Copyright (C) 2018-2019 "
                         "Aurélie Vancraeyenest"
                         "\n\n This program is licensed under the"
                         " Apache License, Version 2.0 "
                         "\n\nThis program comes with ABSOLUTELY NO WARRANTY."
                         "\nThis is free software, and you are welcome to "
                         "redistribute it under certain conditions. "
                         "See LICENSE file for details.")

    @QtCore.pyqtSlot()
    def on_actionHV_firmware_version_triggered(self):
        if self.hvdevice.device.is_open:
            message = self.hvdevice.version()
            self.showMessage(message)
        else:
            self.showMessage('The device COM port should be open'
                             ' to get the firmware version.'
                             '\nOpen the port and try again.')

    @QtCore.pyqtSlot()
    def on_actionOnline_documentation_triggered(self):
        webbrowser.open('https://github.com/avancra/HvControllerGUI')

    @QtCore.pyqtSlot()
    def on_prtOpenBtn_clicked(self):
        '''
        Open the selected port and instantiate an HV controller

        Get the port from the GUI, open it and init the hv controler.
        If success, start a timer which will query the HV every 0.5s.
        '''
        # ??? : check for opened port and close it
        portName = self.prtList.currentText()

        try:
            self.hvdevice.openPortHV(portName)
        except serial.SerialException:
            QtWidgets.QMessageBox.warning(
                    self, 'HV ctrl',
                    'Serial Exception: could not open the {} port'
                    .format(portName))
        else:
            self.querytimer.start()
            self.checktimer.start()
            self.enableAll()
            self.prtList.setEnabled(False)
            self.prtOpenBtn.setEnabled(False)
            self.updateStatus()

    @QtCore.pyqtSlot()
    def on_prtCloseBtn_clicked(self):
        ''' Close the current port and disable the GUI widget once closed '''

        self.querytimer.stop()
        try:
            output = self.hvdevice.closePortHV()
        except serial.SerialException:
            QtWidgets.QMessageBox.warning(
                    self, 'HV ctrl',
                    '''Serial Exception: could not close the {} port'''
                    .format(self.hvdevice.device.port))
        else:
            self.prtList.setEnabled(True)
            self.prtOpenBtn.setEnabled(True)
            self.disableAll()
            self.cmdOutText.append(output)

    @QtCore.pyqtSlot()
    def on_queryBtn_clicked(self):
        '''
        Method to launch the query thread worker through the threadpool

        Keyword arguments
        -----------------
        verbosity : bool
        '''

        queryWk = workers.HvWorker(self.hvdevice.queryHV)
        queryWk.kwargs['verbosity'] = True
        queryWk.signals.output.connect(self.printOutput)
        queryWk.signals.done.connect(self.updateStatus)
        queryWk.signals.done.connect(self.querytimer.start)

        self.querytimer.stop()
        self.threadpool.start(queryWk)

    @QtCore.pyqtSlot()
    def on_setBtn_clicked(self):
        '''
        Method to launch the set HV thread worker through the threadpool

        Keyword arguments
        -----------------
        voltToSet : float
            voltage
        curToSet : float
            current
        verbosity : bool
        '''
        self.targetHV = round(self.voltValueToSet.value(), 2)
        self.targetI = round(self.curValueToSet.value(), 2)
        setWk = workers.HvWorker(self.hvdevice.setHV)
        setWk.kwargs['voltToSet'] = self.targetHV
        setWk.kwargs['curToSet'] = self.targetI
        setWk.kwargs['verbosity'] = True

        setWk.signals.done.connect(self.updateStatus)
        setWk.signals.done.connect(self.querytimer.start)
        setWk.signals.output.connect(self.printOutput)

        self.querytimer.stop()
        self.threadpool.start(setWk)

    @QtCore.pyqtSlot()
    def on_resetBtn_clicked(self):
        '''
        Method to launch the reset HV thread worker through the threadpool

        Keyword arguments
        -----------------
        verbosity : 'bool'
        '''
        self.voltValueToSet.setValue(0.0)
        self.curValueToSet.setValue(0.0)
        self.targetHV = 0.0
        self.targetI = 0.0

        resetWK = workers.HvWorker(self.hvdevice.resetHV)
        resetWK.kwargs['verbosity'] = True

        resetWK.signals.done.connect(self.updateStatus)
        resetWK.signals.done.connect(self.querytimer.start)
        resetWK.signals.output.connect(self.printOutput)

        self.querytimer.stop()
        self.threadpool.start(resetWK)

    @QtCore.pyqtSlot()
    def on_actionExit_triggered(self):
        self.querytimer.stop()
        self.checktimer.stop()
        if self.hvdevice.device.is_open:
            self.hvdevice.closePortHV()
        self.close()

    # ---------------- Other slots --------------
    # define here other pyqtslots
    @QtCore.pyqtSlot()
    def updateStatus(self):
        '''
        Update the values/icons of the GUI corresponding to the HV status

        Updated values
        --------------
        hvOn : bool
            True if HV is on
        fault : bool
            True if HV is faulty
        ctrlMode : str
            'voltage' or 'current'
        voltage : float
            Voltage value in kV
        current : float
            Current value in mA
        '''

        self.voltValueRead.display(self.hvdevice.voltage)
        self.curValueRead.display(self.hvdevice.current)
        if self.hvdevice.ctrlMode == "voltage":
            self.ctrlModeVoltBtn.setChecked(True)
        elif self.hvdevice.ctrlMode == "current":
            self.ctrlModeCurBtn.setChecked(True)

        if self.hvdevice.hvOn is True:
            self.hvOnLed.setPixmap(QtGui.QPixmap(ICON_GREEN_LED))
        elif self.hvdevice.hvOn is False:
            self.hvOnLed.setPixmap(QtGui.QPixmap(ICON_RED_LED))
        else:
            self.hvOnLed.setEnabled(False)

        if self.hvdevice.fault is True:
            self.faultLed.setPixmap(QtGui.QPixmap(ICON_RED_LED))
        else:
            self.faultLed.setEnabled(False)

    @QtCore.pyqtSlot(str)
    def printOutput(self, s):
        '''
        Append command outputs to the text box of the GUI

        Parameters
        ----------
        s : str
            emitted via threadpool threads
        '''
        self.cmdOutText.append(s)

    @QtCore.pyqtSlot()
    def checkStability(self):
        ''' Check if the voltage is within a 0.2 V from the targetted one '''
        delta = 0.2
        if (self.targetHV-delta < self.hvdevice.voltage < self.targetHV+delta):
            # if last log entry more than 1min:
            self.makeLogEntry('HV stability ok: %.2f',
                              value=self.hvdevice.voltage, level='info')
        else:
            self.makeLogEntry('HV stability fails: %.2f',
                              value=self.hvdevice.voltage, level='warning')
            self.makeLogEntry('Try to return to target value...  %.2f',
                              value=self.targetHV, level='warning')
            self.on_setBtn_clicked()
            if (self.targetHV-delta < self.hvdevice.voltage < self.targetHV+delta):
                self.makeLogEntry('HV back to target voltage: %.2f',
                                  value=self.hvdevice.voltage, level='info')
            else:
                self.makeLogEntry('Tentative failed, voltage value: %.2f',
                                  value=self.hvdevice.voltage, level='warning')

    @QtCore.pyqtSlot()
    def programEnded(self):
        QtWidgets.QMessageBox.warning(self, "Warning", "Thread is done")

    # --------------- Other class methods --------
    def disableAll(self):
        ''' Disable all the widgets for HV control of the GUI '''

        self.voltValueToSet.setEnabled(False)
        self.curValueToSet.setEnabled(False)
        self.queryBtn.setEnabled(False)
        self.setBtn.setEnabled(False)
        self.resetBtn.setEnabled(False)
        self.faultLed.setEnabled(False)
        self.hvOnLed.setEnabled(False)
        self.prtCloseBtn.setEnabled(False)
        self.prgSelectBtn.setEnabled(False)
        self.prgStartBtn.setEnabled(False)
        self.prgStopBtn.setEnabled(False)
        self.prgPlotVoltBtn.setEnabled(False)
        self.prgFilenameLineEdit.setEnabled(False)

    def enableAll(self):
        ''' Enable all the widgets of the GUI for HV control '''

        self.voltValueToSet.setEnabled(True)
        self.curValueToSet.setEnabled(True)
        self.queryBtn.setEnabled(True)
        self.setBtn.setEnabled(True)
        self.resetBtn.setEnabled(True)
        self.faultLed.setEnabled(True)
        self.hvOnLed.setEnabled(True)
        self.prtCloseBtn.setEnabled(True)
        # for future use when functionnality is implemented
#        self.prgSelectBtn.setEnabled(True)
#        self.prgStartBtn.setEnabled(True)
#        self.prgStopBtn.setEnabled(True)
#        self.prgPlotVoltBtn.setEnabled(True)
#        self.prgFilenameLineEdit.setEnabled(True)

    def showMessage(self, message):
        '''
        Display a message in an "Information" message box with 'OK' button.

        Parameters:
        -----------
        message : str
            Message to print in the message box
        '''

        messageBox = QtWidgets.QMessageBox()
        messageBox.setText(message)
        messageBox.setWindowTitle("Info")
        messageBox.setIcon(QtWidgets.QMessageBox.Information)
        messageBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        messageBox.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)

        # Show the window
        messageBox.raise_()
        messageBox.exec_()

    def makeLogEntry(self, message, **kwargs):
        '''
        Make a new entry if the last one was done less than 10 min ago

        Parameters
        ---------
        message : str
            Message to print to the log file
        kwargs : dict
            keyword arguments

        keyword arguments
        -----------------
        value : obj
            Variable to log (type must match the format given in the message)
        level : str
            Level of the log entry (lower case)
        '''

        self.now = datetime.datetime.today()
        dtime = self.now - self.lastEntryTime
        if dtime.total_seconds()/60 > 10:
            if kwargs['level'] == 'debug':
                self.logger.debug(message, kwargs['value'])
                self.lastEntryTime = self.now
            elif kwargs['level'] == 'info':
                self.logger.info(message, kwargs['value'])
                self.lastEntryTime = self.now
            elif kwargs['level'] == 'warning':
                self.logger.warning(message, kwargs['value'])
                self.lastEntryTime = self.now
            elif kwargs['level'] == 'error':
                self.logger.error(message, kwargs['value'])
                self.lastEntryTime = self.now
            elif kwargs['level'] == 'critical':
                self.logger.critical(message, kwargs['value'])
                self.lastEntryTime = self.now

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec()


"""
How Bootcamps - Stone - /código[s]
Data: 18/02/2022
Autor: Camila Rodrigues Costa
Descrição: Lista de exercícios
Tema: Funções

Exercício 3:

Faça uma função que retorne o reverso de um número inteiro informado. Por exemplo: 127 -> 721.
"""
valor = input("Digite um munero inteiro com pelo menos 3 digitus: ")

def num_reverso(num):
    reverse_num = num[::-1]
    return reverse_num

if __name__=='__main__':
    print(num_reverso(valor))


"""empty message

Revision ID: df9ded0dc2c5
Revises:
Create Date: 2022-07-08 13:28:24.481791

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'df9ded0dc2c5'
down_revision = None
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users',
    sa.Column('id', postgresql.UUID(), nullable=False),
    sa.Column('first_name', sa.String(length=50), nullable=True),
    sa.Column('last_name', sa.String(length=50), nullable=True),
    sa.Column('username', sa.String(length=100), nullable=False),
    sa.Column('email', sa.String(length=100), nullable=False),
    sa.Column('hashed_password', sa.String(length=200), nullable=False),
    sa.Column('is_superuser', sa.Boolean(), nullable=False),
    sa.Column('disabled', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email'),
    sa.UniqueConstraint('id'),
    sa.UniqueConstraint('username')
    )
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('users')
    # ### end Alembic commands ###


#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

# make message persistent
channel.queue_declare(queue='hello1',durable=True)#durable定义持久化q（这里为什么不用之前的hello，是因为之前已经定义了hello为不持久化，所以你必须重新生成一个q，设置持久化或者删除之前的hello）

channel.basic_publish(exchange='',
                      routing_key='hello1',
                      body='Hello World88!',
                      properties=pika.BasicProperties(
                        delivery_mode=2, #持久化消息
                      ))
print(" [x] Sent 'Hello World2!'")
connection.close()


from sourpea.primitives import *
from sourpea.util import *
import pandas as pd
import os
_dir=os.path.dirname(__file__)
### DERIVED FACTORS
vuwv = Factor("vuwv", ["heqezc","mopvax","qetxx","adc","oxfzjn"])
ojq = Factor("ojq", ["heqezc","mopvax","qetxx","adc","oxfzjn"])
pyl = Factor("pyl", ["heqezc","mopvax","qetxx","adc","oxfzjn"])
def gjyzgy(vuwv, ojq, pyl):
    return vuwv == ojq or vuwv == pyl
def ivjfzr(vuwv, ojq, pyl):
    return not (vuwv == ojq) and vuwv != pyl
crzh = Factor("kcy", [DerivedLevel("qal", WithinTrial(gjyzgy, [vuwv, ojq, pyl])), DerivedLevel("grhrf", WithinTrial(ivjfzr, [vuwv, ojq, pyl]))])
### EXPERIMENT
design=[vuwv,ojq,pyl,crzh]
crossing=[crzh]
### APPENDIX
block=Block(design,crossing,[])
sequence_code_1=trials_from_csv(os.path.join(_dir,"out_code_1/2_2_4_0.csv"))
nr_input_factors=2
nr_output_levels=2
test_code_1=block.test(sequence_code_1)
try:
	sequence_code_2=trials_from_csv(os.path.join(_dir,"out_code_2/2_2_4_0.csv"))
	test_code_2=block.test(sequence_code_2)
except:
	test_code_2={"pValue":0}
df=pd.read_csv(os.path.join(_dir,"result_sour.csv"))
df.loc[len(df.index)] = [test_code_1["pValue"], test_code_2["pValue"], test_code_1["levels"], test_code_2["levels"], nr_input_factors, nr_output_levels]
df.to_csv(os.path.join(_dir,"result_sour.csv"), index=False)


idade = int(input("Digite sua idade: "))
nota = input("Dê a sua nota entre : ruim, regular, bom ou excelente: ")
cont_idade = 0
media = 0
quant = 0
ruim = 0
bom = 0
cont_excelente = 0
excelente = 0
mais_velha = idade
stop = 0

while ( stop != 2):

    stop = int(input("Deseja continuar ? \n <1> sim \n <2> não \n Digite sua resposta: "))

    if ( idade > mais_velha):
        mais_velha = idade

    if ( nota == 'ruim' or nota == 'regular'):
        ruim = ruim + 1

    if( nota == 'bom'):
        bom = bom +1
        cont_idade = cont_idade + idade
        media = cont_idade / bom

    if ( idade >= 30 and nota == 'excelente'):
        cont_excelente = cont_excelente + 1

    if(stop == 1):
        idade = int(input("\n Digite sua idade: "))
        nota = input("\n Dê a sua nota entre : ruim, regular, bom ou excelente: ")

print("\n Média da idade das pessoas que responderam bom: %d anos "%media)
print("\n Quantidade de respostas ruim/regular: %d"%ruim)
print("\n Pessoas acima de 30 anos que deram excelente como resposta: %d"%cont_excelente)
print("\n Pessoa mais velha que respondeu ao questionário: %d anos"%mais_velha)


from netCDF4 import Dataset as ncfile
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

path = 'E:\\project\\oilspill_rescue\\rescue\\output\\'

nc_file = ncfile(path + 'qz_ty1_100p.nc', 'r')
# print(nc_file)
lon_list = nc_file.variables['lon']             # 读取NC文件中的变量
lat_list = nc_file.variables['lat']
lon = np.array(lon_list)                        # 转成数组
lat = np.array(lat_list)
# lon_0 = lon_list[0, :]                               # 读取数组第1行的所有列
# lon_0[lon_0 > 360] = np.nan                     # 数组中大于360的数据都赋值NAN
# lon_mean = np.nanmean(lon_0)                    # 求数组中除NAN外数据的平均值

lon_mean = []
lat_mean = []
for i in range(len(lon[0, :])):

    lon_temp = lon[:, i]
    lat_temp = lat[:, i]

    lon_temp[lon_temp > 360] = np.nan
    lat_temp[lat_temp > 360] = np.nan

    lon_temp_mean = np.nanmean(lon_temp)
    lat_temp_mean = np.nanmean(lat_temp)

    lon_mean.append(lon_temp_mean)
    lat_mean.append(lat_temp_mean)

m = Basemap(projection='cyl', resolution='h', llcrnrlon=118.8, llcrnrlat=24.7,urcrnrlon=119.2, urcrnrlat=24.9)

m.drawcoastlines()  # 画岸线

m.fillcontinents(color='#DFF0D8', lake_color='aqua')
m.drawmapboundary(fill_color='#D9EDF7')

m.drawparallels(np.arange(24.7, 24.9, 0.05), labels=[1,0,0,0])
m.drawmeridians(np.arange(118.8, 119.2, 0.1), labels=[0,0,0,1])
#
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False   #用来正常显示负号
#
m.plot(lon_mean, lat_mean, 'bo-', linewidth=2, markersize=3, label=u'轨迹')
m.plot(lon_mean[0], lat_mean[0], 'r^', markersize=6, label=u'初始位置')
plt.legend(loc='upper left')
plt.show()


import mtbmtbg.moire_tb as mtb
import mtbmtbg.moire_cont as mcont
import mtbmtbg.moire_setup as mset
import mtbmtbg.moire_analysis as manal
from mtbmtbg.config import DataType, EngineType, ValleyType

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import pybinding as pb

def chemical_potential(emesh: np.ndarray) -> float:
    """determine the feimi energy for an insulator

    Args:
        emesh (np.ndarray): eigenvalues return by tb solver

    Returns:
        float: fermi energy
    """

    n_band = emesh[0].shape[0]
    condunction_min = np.min(emesh[:, n_band//2])
    vallence_max = np.max(emesh[:, n_band//2-1])
    mu = (condunction_min+vallence_max)/2

    return mu

def band_plot_module(ax: plt.axes,
                     kline: np.ndarray,
                     emesh: np.ndarray,
                     n_k: int,
                     bands: int,
                     shape: str = "-",
                     color: str = "blue",
                     alpha: float = 1,
                     figname: str = "",
                     mu: bool = False):
    """plot module for TBG

    Args:
        ax (plt.axes): ax object
        kline (np.ndarray): kline
        emesh (np.ndarray): band eigen value
        n_k (int): number of kpnts on each path
        bands (int): number of bands plotted above or below fermi energy
        shape (str, optional): shape. Defaults to "-".
        color (str, optional): color. Defaults to "blue".
    """

    ax.set_xticks([kline[0], kline[n_k], kline[2*n_k], kline[3*n_k]])
    ax.set_xticklabels([r"$\bar{K}$", r"$\bar{\Gamma}$", r"$\bar{M}$", r"$\bar{K}^\prime$"])
    ax.set_xlim(0, kline[-1])
    ax.set_ylabel("Engergy (eV)")
    ax.axvline(x=kline[0], color="black")
    ax.axvline(x=kline[n_k], color="black")
    ax.axvline(x=kline[2*n_k], color="black")
    ax.axvline(x=kline[3*n_k], color="black")

    n_band = emesh[0].shape[0]
    for i in range(bands):
        ax.plot(kline, emesh[:, n_band//2+i], shape, c=color, lw=1, alpha=alpha)
        ax.plot(kline, emesh[:, n_band//2-1-i], shape, c=color, lw=1, alpha=alpha)

    if mu == True:
        mu_val = chemical_potential(emesh)
        ax.axhline(y=mu_val, linewidth=1.5, linestyle="--", color="grey")

    ax.set_title(figname)

def glist_plot_module(ax: plt.axes, glist: np.ndarray, val: np.ndarray, figname: str = ""):
    """plot values on glist

    Args:
        ax (plt.axes): pyplot axes object
        glist (np.ndarray): glist used in the calculation
        val (np.ndarray): value you care about like wavefunctions or moire potential
        figname (str, optional): name of the figure. Defaults to "".
    """
    assert glist.shape[0] == val.shape[0]
    ax.set_aspect('equal', 'box')
    #p = ax.scatter(glist[:, 0], glist[:, 1], c=np.log(val/np.max(val)), s=200, cmap='Purples')
    p = ax.scatter(glist[:, 0],
                   glist[:, 1],
                   c=val,
                   norm=colors.LogNorm(vmin=val.min()+1e-10, vmax=val.max()),
                   s=200,
                   cmap='RdBu_r')
    ax.scatter(glist[:, 0], glist[:, 1], marker='o', s=200, c='w', edgecolors='black', alpha=0.1)
    ax.set_title(figname)

    return p

def tb_plot_sparsetb(n_moire: int, n_g: int, n_k: int, bands: int, datatype: str, pathname="./", figname="", mu=False):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k, True, datatype, engine=EngineType.TBSPARSE)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, shape='.', figname=figname, mu=mu)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_sparsetb.png", dpi=500)
    plt.close()

def tb_plot_fulltb(n_moire: int, n_g: int, n_k: int, bands: int, datatype: str, pathname="./", figname="", mu=False):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k, True, datatype, engine=EngineType.TBFULL)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, figname=figname, mu=mu)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_fulltb.png", dpi=500)
    plt.close()

def tb_plot_tbplw_sepv(n_moire: int, n_g: int, n_k: int, bands: int, datatype: str, pathname="./", figname="",
                       mu=False):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k, True, datatype, valley=ValleyType.VALLEYK1)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, figname=figname, mu=mu)
    ret = mtb.tb_solver(n_moire, n_g, n_k, True, datatype, valley=ValleyType.VALLEYK2)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, figname=figname, mu=mu)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_tbplw_sepv.png", dpi=500)
    plt.close()

def tb_plot_tbplw_combv(n_moire: int,
                        n_g: int,
                        n_k: int,
                        bands: int,
                        datatype: str,
                        pathname="./",
                        figname="",
                        mu=False):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k, True, datatype, valley=ValleyType.VALLEYC)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, figname=figname, mu=mu)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_tbplw_combv.png", dpi=500)
    plt.close()

def fulltb_sepv_cmp(n_moire: int,
                    n_g: int,
                    n_k1: int,
                    n_k2: int,
                    band1: int,
                    band2: int,
                    datatype: str,
                    pathname="./",
                    figname=""):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k1, True, datatype, valley=ValleyType.VALLEYK1)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k1, band1, figname=figname)
    ret = mtb.tb_solver(n_moire, n_g, n_k1, True, datatype, valley=ValleyType.VALLEYK2)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k1, band1, figname=figname)
    ret = mtb.tb_solver(n_moire, n_g, n_k2, True, datatype, engine=EngineType.TBFULL)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k2, band2, shape='.', color='red', alpha=0.5, figname=figname)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_fulltb_sepv_cmp.png", dpi=500)
    plt.close()

def fulltb_combv_cmp(n_moire: int,
                     n_g: int,
                     n_k1: int,
                     n_k2: int,
                     band1: int,
                     band2: int,
                     datatype: str,
                     pathname="./",
                     figname=""):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k1, True, datatype, valley=ValleyType.VALLEYC)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k1, band1, figname=figname)
    ret = mtb.tb_solver(n_moire, n_g, n_k2, True, datatype, engine=EngineType.TBFULL)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k2, band2, shape='.', color='red', alpha=0.5, figname=figname)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_fulltb_combv_cmp.png", dpi=500)
    plt.close()

def sparsetb_sepv_cmp(n_moire: int,
                      n_g: int,
                      n_k1: int,
                      n_k2: int,
                      band1: int,
                      band2: int,
                      datatype: str,
                      pathname="./",
                      figname=""):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k1, True, datatype, valley=ValleyType.VALLEYK1)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k1, band1, figname=figname)
    ret = mtb.tb_solver(n_moire, n_g, n_k1, True, datatype, valley=ValleyType.VALLEYK2)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k1, band1, figname=figname)
    ret = mtb.tb_solver(n_moire, n_g, n_k2, True, datatype, engine=EngineType.TBSPARSE)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k2, band2, shape='.', color='red', alpha=0.5, figname=figname)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_sparsetb_sepv_cmp.png", dpi=500)
    plt.close()

def sparsetb_combv_cmp(n_moire: int,
                       n_g: int,
                       n_k1: int,
                       n_k2: int,
                       band1: int,
                       band2: int,
                       datatype: str,
                       pathname="./",
                       figname=""):
    fig, ax = plt.subplots()
    ret = mtb.tb_solver(n_moire, n_g, n_k1, True, datatype, valley=ValleyType.VALLEYC)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k1, band1, figname=figname)
    ret = mtb.tb_solver(n_moire, n_g, n_k2, True, datatype, engine=EngineType.TBSPARSE)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k2, band2, shape='.', color='red', alpha=0.5, figname=figname)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_sparsetb_combv_cmp.png", dpi=500)
    plt.close()

def moire_potential_plot(n_moire: int,
                         n_g: int,
                         kpnt: str = 'gamma',
                         u: str = 'u1',
                         pathname="./",
                         datatype=DataType.CORRU,
                         valley=ValleyType.VALLEYK1):
    ret = manal.analyze_moire_potential(n_moire, n_g, datatype, valley)
    # unit [nm^-1]
    glist = ret['glist']*10
    # unit [meV]
    u_val = ret['mpot'][kpnt][u]*1000
    fig, ax = plt.subplots()

    p = glist_plot_module(ax, glist, u_val)
    cbar = fig.colorbar(p, ax=ax)
    cbar.ax.set_ylabel('$u$ [meV]', rotation=270, labelpad=12)
    adjust_spines(ax, ['left', 'bottom'])
    ax.set_xlabel('[nm$^{-1}$]')
    ax.set_ylabel('[nm$^{-1}$]')

    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_"+kpnt+"_"+u+"_potential.png", dpi=500)
    plt.close()

def moire_band_convergence_plot(n_moire: int,
                                n_g: int,
                                kpnt: str = 'gamma',
                                u: str = 'u1',
                                pathname="./",
                                datatype=DataType.CORRU,
                                valley=ValleyType.VALLEYK1):

    ret = manal.analyze_band_convergence(n_moire, n_g, datatype, valley)
    glist = ret['glist']
    band_val = ret['band'][kpnt]
    fig, ax = plt.subplots()
    glist_plot_module(ax, glist, band_val)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+datatype+"_"+kpnt+"_band_convergence.png", dpi=500)
    plt.close()

def cont_plot_combv(
        n_moire: int,
        n_g: int,
        n_k: int,
        bands: int,
        pathname="./",
        figname="",
):
    fig, ax = plt.subplots()
    ret = mcont.cont_solver(n_moire, n_g, n_k, valley=1)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, figname=figname)
    ret = mcont.cont_solver(n_moire, n_g, n_k, valley=-1)
    emesh = ret['emesh']
    kline = ret['kline']
    band_plot_module(ax, kline, emesh, n_k, bands, figname=figname)
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+"_cont_combv.png", dpi=500)
    plt.close()

def two_graphene_monolayers():
    """Two individual AA stacked layers of monolayer graphene without any interlayer hopping"""
    from pybinding.repository.graphene.constants import a_cc, a, t
    c0 = 0.335  # [nm] graphene interlayer spacing
    lat = pb.Lattice(a1=[a*np.sqrt(3)/2, -a/2], a2=[a*np.sqrt(3)/2, a/2])
    lat.add_sublattices(('A1', [0, 0, c0/2]), ('B1', [2*a/np.sqrt(3), 0, c0/2]), ('A2', [0, 0, -c0/2]),
                        ('B2', [2*a/np.sqrt(3), 0, -c0/2]))
    lat.register_hopping_energies({'gamma0': t})
    lat.add_hoppings(
        # layer 1
        ([0, -1], 'A1', 'B1', 'gamma0'),
        ([-1, 0], 'A1', 'B1', 'gamma0'),
        ([-1, -1], 'A1', 'B1', 'gamma0'),
        # layer 2
        ([0, -1], 'A2', 'B2', 'gamma0'),
        ([-1, 0], 'A2', 'B2', 'gamma0'),
        ([-1, -1], 'A2', 'B2', 'gamma0'),
        # not interlayer hopping
    )
    lat.min_neighbors = 2
    return lat

def twist_layers(theta):
    # from degrees to radians and get a half
    theta = theta/180*np.pi/2

    @pb.site_position_modifier
    def rotate(x, y, z):
        # rotate layer 1 and layer 2 separately
        layer2 = (z<0)
        x0 = x[layer2]
        y0 = y[layer2]
        x[layer2] = x0*np.cos(-theta)-y0*np.sin(-theta)
        y[layer2] = y0*np.cos(-theta)+x0*np.sin(-theta)

        layer1 = (z>0)
        x0 = x[layer1]
        y0 = y[layer1]
        x[layer1] = x0*np.cos(theta)-y0*np.sin(theta)
        y[layer1] = y0*np.cos(theta)+x0*np.sin(theta)

        return x, y, z

    return rotate

def real_space_plot(n_moire: int, pathname="./"):
    from pybinding.repository.graphene.constants import a
    _, theta_d = mset._set_moire_angle(n_moire)
    l_theta = a/(2*np.sin(theta_d/180*np.pi/2))

    model = pb.Model(two_graphene_monolayers(), pb.regular_polygon(num_sides=6, radius=1.5*l_theta, angle=np.pi/6),
                     twist_layers(theta=theta_d))
    plt.figure(figsize=(6.5, 6.5))
    model.plot(site={"radius": 0})
    model.shape.plot()

    plt.quiver(0, 0, 1/2*l_theta, np.sqrt(3)/2*l_theta, scale_units='xy', scale=1, color=(1, 0, 0, 0.5))
    plt.text(1/4*l_theta, np.sqrt(3)/4*l_theta, r'${L}_1$', fontsize=18)
    plt.quiver(0, 0, -1/2*l_theta, np.sqrt(3)/2*l_theta, scale_units='xy', scale=1, color=(1, 0, 0, 0.5))
    plt.text(-1/3*l_theta, np.sqrt(3)/4*l_theta, r'${L}_2$', fontsize=18)
    plt.tight_layout()
    plt.savefig(pathname+"moire_"+str(n_moire)+"_"+"_realspace.png", dpi=500)
    plt.close()

def adjust_spines(ax, spines):
    for loc, spine in ax.spines.items():
        if loc in spines:
            spine.set_position(('outward', 10))  # outward by 10 points
        else:
            spine.set_color('none')  # don't draw spine

    # turn off ticks where there is no spine
    if 'left' in spines:
        ax.yaxis.set_ticks_position('left')
    else:
        # no yaxis ticks
        ax.yaxis.set_ticks([])

    if 'bottom' in spines:
        ax.xaxis.set_ticks_position('bottom')
    else:
        # no xaxis ticks
        ax.xaxis.set_ticks([])


import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
    README = readme.read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='django-reviewable',
    version='0.3.9.2',
    packages=find_packages(),
    include_package_data=True,
    url='https://github.com/jacobwindsor/django-reviewable',
    license='MIT License',
    author='Jacob Windsor',
    author_email='jacob.windsor@ymail.com',
    description='Simple, customizable reviews for any of your Django models',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Framework :: Django :: 1.9',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3.5',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)


# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Data.csv')
dataset = dataset.drop(['Unnamed: 0'], axis=1)
#PREPROCESSING DATA
#Counting the number of Nan values
dataset.isna().sum()
#The below is the output we get
'''
Name                    0
Location                0
Year                    0
Kilometers_Driven       0
Fuel_Type               0
Transmission            0
Owner_Type              0
Mileage                 2            -> 7th index
Engine                 36
Power                  36
Seats                  42
New_Price            5195
Price                   0
dtype: int64
'''
#Adjusting the unknown values in the dataset
dataset['Engine'].fillna(value='unknown',inplace=True)
dataset['Mileage'].fillna(value='unknown',inplace=True)
dataset['Power'].fillna(value='unknown',inplace=True)
dataset['Seats'].fillna(value=dataset['Seats'].mean(), inplace=True)

#now let's see the unkown values again
dataset.isna().sum()
'''
Name                    0
Location                0
Year                    0
Kilometers_Driven       0
Fuel_Type               0
Transmission            0
Owner_Type              0
Mileage                 0
Engine                  0
Power                   0
Seats                   0
New_Price            5195
Price                   0
dtype: int64
'''

#As New_Price has many unknown values let's drop the whole column.
dataset = dataset.drop(['New_Price'], axis = 1)

#Now our data is clean so we can proceed to further steps
#Dividing the data into two variables X-> Independent and Y-> Dependent
X = pd.DataFrame(dataset.iloc[:, :-1].values)
X.columns = dataset.columns[:-1]        #Setting the columns for the X
y = dataset.iloc[:, -1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

X_train["Name"].value_counts()
make_train = X_train["Name"].str.split(" ", expand=True)
make_test  = X_test["Name"].str.split(" ", expand=True)

#I will add a Manufacturer column to the dataset as the sale depends mostly on the Manufacturer.
X_train["Manufacturer"] = make_train[0]
X_test["Manufacturer"] = make_test[0]

#Extracting the numerical values of Milage, Engine, Power
milage_train = X_train["Mileage"].str.split(" ", expand=True)
milage_test = X_test["Mileage"].str.split(" ", expand=True)
X_train["Mileage"] = pd.to_numeric(milage_train[0], errors = 'coerce')
X_test["Mileage"] = pd.to_numeric(milage_test[0], errors = 'coerce')

engine_train = X_train["Engine"].str.split(" ", expand=True)
engine_test  = X_test["Engine"].str.split(" ", expand=True)
X_train["Engine"] = pd.to_numeric(engine_train[0], errors = 'coerce')
X_test["Engine"]  = pd.to_numeric(engine_test[0], errors = 'coerce')

power_train = X_train["Power"].str.split(" ", expand=True)
power_test  = X_test["Power"].str.split(" ", expand=True)
X_train["Power"] = pd.to_numeric(power_train[0], errors = 'coerce')
X_test["Power"]  = pd.to_numeric(power_test[0], errors = 'coerce')

#Replacing the Null values with Mean of all the values in that column
X_train["Engine"].fillna(X_train["Engine"].astype("float64").mean(), inplace = True)
X_test["Engine"].fillna(X_train["Engine"].astype("float64").mean(), inplace = True)

X_train["Mileage"].fillna(X_train["Mileage"].astype("float64").mean(), inplace = True)
X_test["Mileage"].fillna(X_train["Mileage"].astype("float64").mean(), inplace = True)

X_train["Power"].fillna(X_train["Power"].astype("float64").mean(), inplace = True)
X_test["Power"].fillna(X_train["Power"].astype("float64").mean(), inplace = True)

X_train["Seats"].fillna(X_train["Seats"].astype("float64").mean(), inplace = True)
X_test["Seats"].fillna(X_train["Seats"].astype("float64").mean(), inplace = True)

#Dropping all the unnecessary columns in the dataset
#Name is no longer required as we are considering the Manufacturer as the sole base.
X_train = X_train.drop("Name", axis = 1)
X_test = X_test.drop("Name", axis = 1)
#Location should not determine the price of the car.
X_train = X_train.drop("Location", axis = 1)
X_test = X_test.drop("Location", axis = 1)
#Year has no significance unless we consider how old the given car is
X_train = X_train.drop("Year", axis = 1)
X_test = X_test.drop("Year", axis = 1)

#EXPLORATORY DATA ANALYSIS
#Manufacturers VS Number of cars plotting
plt.figure(figsize = (14, 10))
plot = sns.countplot( x = "Manufacturer", data = X_train)
plt.xticks(rotation = 90)
#Annotating the plot
for p in plot.patches:
    plot.annotate(p.get_height(),
                        (p.get_x() + p.get_width() / 2.0,
                         p.get_height()),
                        ha = 'center',
                        va = 'center',
                        xytext = (0, 5),
                        textcoords = 'offset points')

plt.title("Count of cars based on manufacturers")
plt.xlabel("Manufacturer")
plt.ylabel("Count of cars")

plt.figure(figsize = (14, 10))
plt.hist(X_train["Kilometers_Driven"], bins =[0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000] )
plt.xlabel("Kilometers Driven")
plt.ylabel("Count of cars")
plt.show()

plt.figure(figsize = (14, 10))
plt.bar(X_train["Fuel_Type"], height = np.arange(len(X_train["Fuel_Type"])), width = 0.4, color = ['red', 'blue', 'yellow', 'green'])
plt.xlabel("Fuel Type")
plt.ylabel("Count")
plt.show()

plt.figure(figsize = (14, 10))
plt.bar(X_train["Transmission"], height = np.arange(len(X_train["Transmission"])), width = 0.4, color = ['orange','green'])
plt.xlabel("Transmission")
plt.ylabel("Count")
plt.show()

plt.figure(figsize = (14, 10))
plt.bar(X_train["Owner_Type"], height = np.arange(len(X_train["Owner_Type"])), width = 0.4, color = ['orange','green'])
plt.xlabel("Owner Type")
plt.ylabel("Count")
plt.show()

plt.figure(figsize = (14, 10))
plt.scatter(X_train["Mileage"], np.arange(len(X_train["Mileage"])), color = 'r')
plt.xlabel("Mileage")
plt.ylabel("Count")
plt.show()

plt.figure(figsize = (14, 10))
plot = sns.countplot( x = "Seats", data = X_train)
plt.xticks(rotation = 90)
#Annotating the plot
for p in plot.patches:
    plot.annotate(p.get_height(),
                        (p.get_x() + p.get_width() / 2,
                         p.get_height()),
                        ha = 'center',
                        va = 'center',
                        xytext = (0, 5),
                        textcoords = 'offset points')

#Categorical Data
#let's create dummy columns for categorical columns before we begin training.
X_train = pd.get_dummies(X_train,
                         columns = ["Manufacturer", "Fuel_Type", "Transmission", "Owner_Type"],
                         drop_first = True)
X_test = pd.get_dummies(X_test,
                         columns = ["Manufacturer", "Fuel_Type", "Transmission", "Owner_Type"],
                         drop_first = True)
#It might be possible that the dummy column creation would be different in test and train data, thus, I'd fill in all missing columns with zeros
missing_cols = set(X_train.columns) - set(X_test.columns)
for col in missing_cols:
    X_test[col] = 0
X_test = X_test[X_train.columns]

#Feature Scaling the data
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

#Fitting simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
#predicting the Test set results
y_pred = regressor.predict(X_test)

from sklearn.metrics import r2_score
r2_score(y_test, y_pred)

from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators = 100)
rf.fit(X_train, y_train.ravel())
y_pred = rf.predict(X_test)
r2_score(y_test, y_pred)

'''
    Output : 0.8104532069495102
'''


import requests
import utils.log as log

SOURCE_NAME = "xpd"

def get_source(source):
    tracking_id = source["tracking_id"]
    url = "http://gzdgj.kingtrans.net/WebTrack"

    s = requests.session()

    querystring = {"action":"list"}
    payload = "language=zh&istrack=false&Submit=%E6%9F%A5%E8%AF%A2&bills=" + tracking_id
    headers = {
        "content-type": "application/x-www-form-urlencoded",
        }

    r = s.request("POST", url, data=payload, headers=headers, params=querystring)

    querystring = {"action":"repeat"}
    payload = "index=0&isRepeat=no&language=zh&billid=" + tracking_id
    headers = {
        "content-type": "application/x-www-form-urlencoded",
        }

    r = s.request("POST", url, data=payload, headers=headers, params=querystring)

    return r.text.split("\n",1)[1]


from elasticsearch import Elasticsearch
import pandas as pd
import json
import time
import logging
from datetime import date
import datetime
import re
import sys
import os
import os.path
import ENUMERATE

#//GLOBAL VARIABLES//

#Maximum size of the Elastiseach page
max_size = 1000

#Path & postfix of file name format [path][date index][postfix].json
path2result = './JSON_Storage/'
postfix = '_ips_connected.json'

path2final = "./Archive/ips_connected_"
final_postfix = ".json"

def main():
    start_time = time.time()
    es_node = sys.argv[1]
    index = sys.argv[2]
    days = 0
    columns = ["destination.ip","source.ip"]
    ip_to_scan = sys.argv[3]
    dest_query = "destination.ip : " + ip_to_scan
    src_query = "source.ip : " + ip_to_scan
    query_list = [dest_query, src_query]
    ip_whitelist = ENUMERATE.txt2list("./Whitelist/ip_whitelist.txt")
    begin = sys.argv[4]
    end = sys.argv[5]
    if end == "TODAY":
        n = ENUMERATE.differenceINbegin(begin)
        days = ENUMERATE.arkime_last_n_days(n)
    else:
        n = ENUMERATE.difference2dates(begin, end)
        days = ENUMERATE.arkime_between_n_days(end, n)
    ips_dict = (ENUMERATE.pull_ips(es_node,index,days,query_list,columns,
                                        path2result,postfix))
    ips = []
    for address in ips_dict.keys():
        if address == ip_to_scan:
            for dst_address in ips_dict[address]['destination.ip'].keys():
                if not(dst_address in ips):
                    ips.append(dst_address)
        elif not(address in ips):
            ips.append(address)

    columns = ENUMERATE.txt2list("./Columns/columns.txt")
    dir_path = "./Results/" + "Connections_" + ip_to_scan
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    for ip_addr in ips:

        ip_connected_summary = ENUMERATE.enumerate_single(es_node, index, days,[ip_addr],
                                                               columns,path2result,postfix)
        standard_ports = (ENUMERATE.enumerate_display(ip_connected_summary))
        ENUMERATE.dict2json(standard_ports,dir_path+"/"+ip_addr+"<-->"+
                                 ip_to_scan+final_postfix)
    print("Time to Complete Enumeration of", ip_to_scan, "is:", time.time()-start_time, "SECS")

main()


import torch
from torch.utils.data import DataLoader, Dataset
import os
from pathlib import Path
import numpy as np
from PIL import Image

def cal_mean_std(path: str):
    channels_sum, channels_squared_sum, nums = 0, 0, 0
    path_list = os.listdir(path)
    for img_path in path_list:
        image_path = os.path.join(path, img_path)
        # image = torch.from_numpy(np.array(Image.open(image_path)) / 255).permute([2, 0, 1]).float()
        image = torch.from_numpy(np.array(Image.open(image_path))).permute([2, 0, 1]).float()
        channels_sum += torch.mean(image, dim=[1, 2])
        channels_squared_sum += torch.mean(image**2, dim=[1, 2])
        nums += 1
    mean = channels_sum / nums
    std = (channels_squared_sum / nums - mean**2)**0.5
    return mean, std

if __name__ == '__main__':
    path = os.path.abspath("../data1/coco/train2017/JPEGImages")
    mean, std = cal_mean_std(path=path)
    print(f'mean : {mean}, std : {std}')
# mean : tensor([131.5901, 129.8181, 126.6339]), std : tensor([56.4909, 55.3395, 56.3917])


from __future__ import absolute_import, unicode_literals
from celery import Celery
from django.conf import settings
import os

app = Celery('review1')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'review1.settings')
#settings.configure(default_settings=review1_defaults, DEBUG=True)
app.config_from_object('django.conf:settings', namespace='CELERY')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

app = Celery('proj',
             broker='redis://',
             backend='redis://',
             include=['mainApp.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    result_expires=3600,
)

if __name__ == '__main__':
    app.start()


import pygame
import time

pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096) #setting 16 bit size; 32 or 64 bit dosent work.
pygame.mixer.init()
win =  pygame.display.set_mode((500,480))

pygame.display.set_caption("First Game")

#loading images of our character. images of enemy in enemy class
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
bg = pygame.image.load('bg.jpg')
char = pygame.image.load('standing.png')

#loading audio files
t = pygame.mixer.music
t.load('bullet.wav')
t.load('hit.wav')

clock = pygame.time.Clock()

class player():
    def __init__(self, x, y, height, width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.vel = 10
        self.isJump = False
        self.jumpcount = 10
        self.left = False
        self.right = False
        self.walkcount = 0
        self.standing = True
        self.hitbox = (self.x + 17, self.y+ 11, 29, 52) #(x,y, width, height)

    def draw(self, win):
        if self.walkcount +1>= 27:
            self.walkcount = 0

        if not(self.standing):
            if self.left:
                win.blit(walkLeft[man.walkcount//3], (self.x,self.y))
                self.walkcount +=1
            elif self.right:
                win.blit(walkRight[man.walkcount//3], (man.x,man.y))
                self.walkcount +=1
        else:
            if self.right:
                win.blit(walkRight[0],(self.x,self.y))
            else:
                win.blit(walkLeft[0],(self.x,self.y))
        self.hitbox = (self.x + 17, self.y+ 11, 29, 52)  #to make hitbox move with the player
        #pygame.draw.rect(win, (255,0,0), self.hitbox, 2)

    def hit(self):
        self.isJump = False #because if jumping man hits goblin, it continues its downward jump.
        self.jumpcount = 10
        self.x = 60
        self.y = 410
        goblin.x = 300
        goblin.y = 410
        walkcount = 0
        font1 = pygame.font.SysFont('corbel', 100)
        text = font1.render('-5',1, (255, 0, 0))
        win.blit(text,(250 - (text.get_width()/2), 240- (text.get_height()/2) ))
        pygame.display.update()
        time.sleep(2)

class Projectile():
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 5

    def draw(self, win):
        pygame.draw.circle( win, self.color, (self.x, self.y), self.radius)

class Enemy():
    walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
    walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x,self.end]
        self.walkcount = 0
        self.vel = 3
        self.hitbox = (self.x + 17, self.y+2, 31, 57)
        self.health = 10
        self.visible = True

    def draw(self,win):
        if self.visible:
            self.move()  #very importanat to call on move function
            if self.walkcount + 1 >= 33:
                self.walkcount = 0
            if self.vel > 0:
                win.blit(self.walkRight[self.walkcount//3],(self.x, self.y))
                self.walkcount += 1
            else:
                win.blit(self.walkLeft[self.walkcount//3], (self.x, self.y))
                self.walkcount += 1
            pygame.draw.rect(win,(255,0,0), (self.hitbox[0],self.hitbox[1]- 20,50, 10))
            pygame.draw.rect(win,(0,128,0), (self.hitbox[0],self.hitbox[1]-20,50 -(5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 17, self.y+2, 31, 57)
            #pygame.draw.rect(win, (255,0,0), self.hitbox, 2)

    def move(self):
        if self.vel>0:
            if self.x +self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkcount = 0
        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkcount = 0

    def hit(self):
        if self.health > 1:
            self.health -= 1
        else:
            self.visible = False
            walkcount = 0

def redrawgamewindow():
    win.blit(bg, (0,0))
    man.draw(win)
    goblin.draw(win)
    for bullet in bullets:
        bullet.draw(win)
    if goblin.visible:
        global font
        font = pygame.font.SysFont('corbel', 30, True)
        text = font.render('Score: ' + str(score),1 , (0,0,0))
        win.blit(text, (380,10))
    else:
        text3 = font.render('Your score is '+ str(score), 1, (0,0,110))
        win.blit(text3,(250 - (text3.get_width()/2), 240- (text3.get_height()/2)))
    pygame.display.update()

#creating instances of characters
man = player(300, 410, 64, 64)
goblin = Enemy(50,410,64,64,450)

bulletsound = pygame.mixer.Sound('bullet.wav')
hitsound = pygame.mixer.Sound('hit.wav')
bgsound = pygame.mixer.Sound('bgmusic (2).wav')
bgsound.play(-1)

score = 0
shootcount = 0
bullets = []

run = True
while run:
    clock.tick(27)
    if shootcount in range(3): #basic timer for bullets
        shootcount += 1
    else:
        shootcount =  0

    for event in pygame.event.get(): #an entry in the list of events, get means get the entry.
        if event.type == pygame.QUIT:
            run = False

    for bullet in bullets:
        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.vel*bullet.facing
        else:
            bullets.pop(bullets.index(bullet))
        if goblin.visible:
            if pygame.Rect(goblin.hitbox).collidepoint(bullet.x, bullet.y):
                goblin.hit()
                score += 1
                hitsound.play()
                bullets.pop(bullets.index(bullet))

    if goblin.visible:
        if pygame.Rect(goblin.hitbox).colliderect(man.hitbox):
            man.hit()
            score -= 5

    keys = pygame.key.get_pressed()
    if man.left:
        facing = -1
    else:
        facing = 1
    if keys[pygame.K_SPACE] and shootcount == 0:
        bulletsound.play()
        if len(bullets)<5:
            bullets.append(Projectile(round(man.x + man.width//2),round(man.y +man.height//2),5, (0,0,0), facing))

    if keys[pygame.K_RIGHT] and man.x < 500-man.width-man.vel: #alwasys put man.width rather than arbitary value
        man.x += man.vel                                    #so that you dont have to remember .
        man.right = True
        man.left = False
        man.standing = False

    elif keys[pygame.K_LEFT] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False

    else:
        man.standing = True
        man.walkcount = 0

    if not(man.isJump):
        if keys[pygame.K_UP]:
            man.isJump = True
            man.right = False
            man.left = False
            man.walkcount = 0

    else:
        if man.jumpcount >= -10:
            neg =1
            if man.jumpcount<0:
                neg = -1
            man.y -= (man.jumpcount**2)/2 *neg
            man.jumpcount -= 1
        else:
            man.isJump = False
            man.jumpcount = 10

    redrawgamewindow()

pygame.quit()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This module contains the Char2Float class.
"""
from harpia.GUI.fieldtypes import *
from harpia.model.plugin import Plugin

class Char2Float(Plugin):

    # --------------------------------------------------------------------------
    def __init__(self):
        Plugin.__init__(self)

        self.language = "javascript"
        self.framework = "webaudio"
        self.help = "Char to Float"
        self.label = "Char 2 Float"
        self.color = "200:200:25:150"
        self.in_ports = [{"type":"harpia.extensions.javascript.ports.char",
                "label":"Char Input",
                "name":"char_input"}
                ]
        self.out_ports = [{"type":"harpia.extensions.javascript.ports.float",
                "label":"Float Output",
                "name":"float_output"}
            ]
        self.properties = [{"name": "float",
                            "label": "Output float",
                                     "type": HARPIA_FLOAT,
                                     "lower": 0,
                                     "upper": 20000,
                                     "step": 1,
                                     "value": 60
                            },
                           {"name": "char",
                            "label": "Input Char",
                            "type": HARPIA_STRING,
                            "value": "a"
                            }
                           ]
        self.group = "Conversion"

        self.codes[1] = """
// block_$id$ = Char 2 Float
var $out_ports[float_output]$ = [];
var $in_ports[char_input]$ = function(value){
    if (value != '$prop[char]$')
        return true;
    for (var i = 0; i < $out_ports[float_output]$.length ; i++){
        $out_ports[float_output]$[i]($prop[float]$);
    }
    return true;
    };
"""


class Twitter:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.f = collections.defaultdict(set)  # f[followerId] = {followeeId1,followeeId2...}
        self.time = 0
        self.tweet = collections.defaultdict(list)  # tweet[userId] = [(tweetId1,time1),(tweetId2,time2)...]

    def postTweet(self, userId: int, tweetId: int) -> None:
        """
        Compose a new tweet.
        """
        #print('postTweet',userId,tweetId)
        self.tweet[userId].append((tweetId,self.time))
        self.time += 1

    def getNewsFeed(self, userId: int) -> List[int]:
        """
        Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
        """
        #print('getNewsFeed',userId)
        userCur = dict()
        time = []   # 时间顺序
        orderedUser = [] # 按照时间顺序存储userId
        #print(self.f[userId] | {userId})
        for u in self.f[userId] | {userId}:
            if len(self.tweet[u]) > 0:
                userCur[u] = -1
                loc = bisect.bisect(time,self.tweet[u][-1][1])
                time.insert(loc,self.tweet[u][-1][1])
                orderedUser.insert(loc,u)
        cnt = 0
        ans = []
        while cnt < 10 and orderedUser:
            time.pop()
            lastUser = orderedUser.pop()
            ans.append(self.tweet[lastUser][userCur[lastUser]][0])
            userCur[lastUser] -= 1
            if -userCur[lastUser] <= len(self.tweet[lastUser]):
                loc = bisect.bisect(time,self.tweet[lastUser][userCur[lastUser]][1])
                time.insert(loc,self.tweet[lastUser][userCur[lastUser]][1])
                orderedUser.insert(loc,lastUser)
            cnt += 1
            #print(time)
            #print(orderedUser)
            #print(userCur)
            #print(lastUser)
            #print(ans)
        return ans

    def follow(self, followerId: int, followeeId: int) -> None:
        """
        Follower follows a followee. If the operation is invalid, it should be a no-op.
        """
        self.f[followerId].add(followeeId)

    def unfollow(self, followerId: int, followeeId: int) -> None:
        """
        Follower unfollows a followee. If the operation is invalid, it should be a no-op.
        """
        self.f[followerId].discard(followeeId)

# Your Twitter object will be instantiated and called as such:
# obj = Twitter()
# obj.postTweet(userId,tweetId)
# param_2 = obj.getNewsFeed(userId)
# obj.follow(followerId,followeeId)
# obj.unfollow(followerId,followeeId)


from django.urls import path
from . import views

urlpatterns = [
    path('', views.homePage, name='home'),  # URL pattern for the home page
    path('signup/', views.signUp, name='signup'),  # URL pattern for the sign-up page
    path('signin/', views.signIn, name='signin'),  # URL pattern for the sign-in page
    path('update_profile/', views.updateProfile, name='update_profile'),
    path('place_bid/', views.place_bid, name='place_bid'),
]


import sqlite3

BETA = 200
K = 200
#: The actual score for win.
WIN = 1.
#: The actual score for draw.
DRAW = 0.5
#: The actual score for loss.
LOSS = 0.

def expect(rating, other_rating, beta=BETA):
    """The "E" function in Elo. It calculates the expected score of the
    first rating by the second rating.
    """
    # http://www.chess-mind.com/en/elo-system
    diff = float(other_rating) - float(rating)
    f_factor = 2 * beta  # rating disparity
    return 1. / (1 + 10 ** (diff / f_factor))

def adjust(rating, series):
    """Calculates the adjustment value."""
    return sum(score - expect(rating, other_rating)
               for score, other_rating in series)

def rate(rating, series, k):
    """Calculates new ratings by the game result series."""
    new_rating = float(rating) + k * adjust(rating, series)
    return new_rating

def rate_1vs1(rating1, rating2, k=K, drawn=False):
        scores = (DRAW, DRAW) if drawn else (WIN, LOSS)
        return (rate(rating1, [(scores[0], rating2)], k),
                rate(rating2, [(scores[1], rating1)], k))

SQL_SELECT_IMAGES_ALL = '''
SELECT *
  FROM images
'''

SQL_SELECT_MATCHES_ALL = '''
SELECT *
  FROM matches
 ORDER BY match_id
'''

conn = sqlite3.connect('example/image-echelon.db')
conn.row_factory = sqlite3.Row

c = conn.cursor()

images = {}
for row in c.execute(SQL_SELECT_IMAGES_ALL):
    images[row['image_id']] = {'name': row['name'], 'rating': 1200.0, 'wins': 0, 'losses': 0}

c.close()

c = conn.cursor()

K_DEF = 10
K_STEPS = [
    {'K': 200.0, 'TOTAL': 1200},
    {'K': 175.0, 'TOTAL': 1600},
    {'K': 150.0, 'TOTAL': 2000},
    {'K': 125.0, 'TOTAL': 2400},
    {'K': 100.0, 'TOTAL': 2800},
    {'K': 75.0, 'TOTAL': 4000},
    {'K': 50.0, 'TOTAL': 5200},
    {'K': 25.0, 'TOTAL': 3600},
    {'K': 10.0, 'TOTAL': -1},

]

def get_k(num):
    if len(K_STEPS) == 0:
        return K_DEF

    k = K_STEPS[0]['K']
    for step in K_STEPS:
        k = step['K']
        if num <= step['TOTAL']:
            break

    return k

current_k = get_k(1)

for row in c.execute(SQL_SELECT_MATCHES_ALL):
    if current_k != get_k(row['match_id']):
        current_k = get_k(row['match_id'])

    new_ratings = rate_1vs1(images[row['winner_id']]['rating'], images[row['loser_id']]['rating'], 10)
    images[row['winner_id']]['rating'] = new_ratings[0]
    images[row['winner_id']]['wins'] = images[row['winner_id']]['wins'] + 1
    images[row['loser_id']]['rating'] = new_ratings[1]
    images[row['loser_id']]['losses'] = images[row['loser_id']]['losses'] + 1

conn.commit()
conn.close()

new_images = sorted(images.items(), key=lambda x: x[1]['rating'], reverse=True)

for i in new_images[:10]:
    print(i)


from copy import deepcopy
from glob import glob
from os.path import basename

import utaupy

PATH_SINSY_LABEL_DIR = 'lab_input_sinsy'
PATH_OTO2LAB_LABEL_DIR = 'lab_input_oto2lab'
PATH_OUTPUT_LABEL_DIR = 'lab_output'

def check_label_diff_for_debug(labobj_oto2lab, labobj_sinsy):
    """
    デバッグ用に音素数を数える
    1つ違うとき・・・前奏または後奏のsilが一致しない
    2つ違うとき・・・前奏と構想の両方、または間奏のsilが一致しない
    """
    oto2lab_sil = [phoneme for phoneme in labobj_oto2lab if phoneme.symbol == 'sil']
    sinsy_sil = [phoneme for phoneme in labobj_sinsy if phoneme.symbol == 'sil']
    oto2lab_pau = [phoneme for phoneme in labobj_oto2lab if phoneme.symbol == 'pau']
    sinsy_pau = [phoneme for phoneme in labobj_sinsy if phoneme.symbol == 'pau']
    oto2lab_br = [phoneme for phoneme in labobj_oto2lab if phoneme.symbol == 'br']
    sinsy_br = [phoneme for phoneme in labobj_sinsy if phoneme.symbol == 'br']
    oto2lab_cl = [phoneme for phoneme in labobj_oto2lab if phoneme.symbol == 'cl']
    sinsy_cl = [phoneme for phoneme in labobj_sinsy if phoneme.symbol == 'cl']
    oto2lab_w = [phoneme for phoneme in labobj_oto2lab if phoneme.symbol == 'w']
    sinsy_w = [phoneme for phoneme in labobj_sinsy if phoneme.symbol == 'w']
    print('  oto2labのラベルの音素数   :', len(labobj_oto2lab))
    print('  Sinsy  のラベルの音素数   :', len(labobj_sinsy))
    print('  oto2labのラベル中のpauの数:', len(oto2lab_pau))
    print('  Sinsy  のラベル中のpauの数:', len(sinsy_pau))
    print('  oto2labのラベル中のsilの数:', len(oto2lab_sil))
    print('  Sinsy  のラベル中のsilの数:', len(sinsy_sil))
    print('  oto2labのラベル中のbrの数 :', len(oto2lab_br))
    print('  Sinsy  のラベル中のbrの数 :', len(sinsy_br))
    print('  oto2labのラベル中のclの数 :', len(oto2lab_cl))
    print('  Sinsy  のラベル中のclの数 :', len(sinsy_cl))
    print('  oto2labのラベル中のwの数 :', len(oto2lab_w))
    print('  Sinsy  のラベル中のwの数 :', len(sinsy_w))
    assert len(oto2lab_br) == len(sinsy_br), 'br の個数が一致しません。'
    assert len(oto2lab_w) == len(sinsy_w), 'w の個数が一致しません。'
    assert len(labobj_oto2lab) - len(oto2lab_pau) - len(oto2lab_sil) \
        == len(labobj_sinsy) - len(sinsy_pau) - len(sinsy_sil), \
        'pau, sil 以外の音素で登録ミスが存在するようです。'

def compare_all_phonemes(labobj_oto2lab, labobj_sinsy):
    """
    すべての音素が一致するかチェックする。
    """
    len_labobj_oto2lab = len(labobj_oto2lab)
    len_labobj_sinsy = len(labobj_sinsy)
    assert len_labobj_oto2lab == len_labobj_sinsy, \
        f'音素数が一致しません。出力するLABの音素数: {len_labobj_oto2lab}, Sinsyの音素数 {len_labobj_sinsy}'
    oto2lab_all_phonemes = [phoneme.symbol for phoneme in labobj_oto2lab]
    sinsy_all_phonemes = [phoneme.symbol for phoneme in labobj_sinsy]
    for i, (ph_oto2lab, ph_sinsy) in enumerate(zip(oto2lab_all_phonemes, sinsy_all_phonemes)):
        assert ph_oto2lab == ph_sinsy, \
            f'{i+1} 行目付近の音素が一致しません。oto2labの音素: {ph_oto2lab}, Sinsyの音素: {ph_sinsy}'

def check_start_end_match(labobj):
    end = 0
    for phoneme in labobj:
        start = phoneme.start
        assert end == start
        end = phoneme.end

def delete_pau_and_sil(labobj):
    """
    pauとsilを全部消す
    """
    labobj.data = [phoneme for phoneme in labobj if phoneme.symbol not in ('pau', 'sil')]

def insert_pau_and_sil(labobj_oto2lab, labobj_sinsy):
    """
    pauとsilを入れる。それ以外の音素は完璧に一致している前提とする。
    """
    for i, phoneme_sinsy in enumerate(labobj_sinsy):
        if phoneme_sinsy.symbol in ('pau', 'sil'):
            labobj_oto2lab.insert(i, deepcopy(phoneme_sinsy))

def restore_pau_time(labobj_oto2lab):
    """
    一度削除されたことによってpauの開始時刻と終了時刻が崩れているため、
    前後の音符の音素の終了時刻と開始時刻から、本来の値を復元する。
    """
    rest_symbols = ('pau', 'sil')
    for i, current_phoneme in enumerate(labobj_oto2lab[1:], 1):
        if current_phoneme.symbol == 'pau':
            previous_phoneme = labobj_oto2lab[i - 1]
            if previous_phoneme.symbol not in rest_symbols:
                current_phoneme.start = previous_phoneme.end
    for i, current_phoneme in enumerate(labobj_oto2lab[:-1]):
        if current_phoneme.symbol == 'pau':
            next_phoneme = labobj_oto2lab[i + 1]
            if next_phoneme.symbol not in rest_symbols:
                current_phoneme.end = next_phoneme.start

def main_wrap(path_lab_oto2lab, path_lab_sinsy, path_lab_out):
    """
    間奏の処理とか前奏の処理とかクラスオブジェクト化とかをまとめて実行する。
    path_lab_oto2lab: oto2lab で作ったLABファイルのパス
    path_lab_sinsy  : Sinsy で作ったLABファイルのパス
    path_lab_out    : 処理結果の出力パス
    """
    # 前処理
    labobj_oto2lab = utaupy.label.load(path_lab_oto2lab)
    labobj_sinsy = utaupy.label.load(path_lab_sinsy)
    print('  処理前-------------------------')
    check_label_diff_for_debug(labobj_oto2lab, labobj_sinsy)
    # oto2labのラベルのsilを全部消す
    delete_pau_and_sil(labobj_oto2lab)
    # oto2labのラベルとSinsyのラベルを比較してsilを挿入する
    insert_pau_and_sil(labobj_oto2lab, labobj_sinsy)
    # pauが失った時間情報を復元する
    restore_pau_time(labobj_oto2lab)
    print('  処理後-------------------------')
    check_label_diff_for_debug(labobj_oto2lab, labobj_sinsy)
    labobj_oto2lab.check_invalid_time(threshold=5)
    # ファイル出力
    labobj_oto2lab.write(path_lab_out)
    compare_all_phonemes(labobj_oto2lab, labobj_sinsy)
    check_start_end_match(labobj_oto2lab)

def main():
    """
    PATHを指定して全体の処理をする。
    """
    path_sinsy_label_dir = PATH_SINSY_LABEL_DIR
    path_oto2lab_label_dir = PATH_OTO2LAB_LABEL_DIR
    path_output_label_dir = PATH_OUTPUT_LABEL_DIR

    # LABファイルを取得
    sinsy_labels = glob(f'{path_sinsy_label_dir}/*.lab')
    oto2lab_labels = glob(f'{path_sinsy_label_dir}/*.lab')
    len_sinsy_labels = len(sinsy_labels)
    len_oto2lab_labels = len(oto2lab_labels)
    print('len_sinsy_labels  :', len_sinsy_labels)
    print('len_oto2lab_labels:', len_sinsy_labels)
    assert len_sinsy_labels == len_oto2lab_labels, 'sinsyのラベルファイル数とoto2labのラベルファイル数が一致しません。'
    # 書き換えを始める。
    for path_lab_sinsy in sinsy_labels:
        print('--------------------------------------------------')
        songname = basename(path_lab_sinsy).replace('_sinsy.lab', '').replace('.lab', '')
        path_lab_oto2lab = f'{path_oto2lab_label_dir}/{songname}.lab'
        print('path_lab_oto2lab:', path_lab_oto2lab)
        print('path_lab_sinsy  :', path_lab_sinsy)
        path_lab_out = f'{path_output_label_dir}/{songname}.lab'
        main_wrap(path_lab_oto2lab, path_lab_sinsy, path_lab_out)

if __name__ == '__main__':
    main()