from django.shortcuts import render, get_object_or_404
from django.views.generic import View,ListView, DetailView, CreateView, UpdateView, DeleteView, TemplateView, FormView
from .models import Topic, Reply
from Interactions.models import VoteContent, CommentContent
from Development.models import ContentCategory
from .forms import TopicCreateForm, TopicEditForm, TopicCommentForm, ReplyCreateForm, TopicCreateInCatForm, ReplyEditForm
from django.urls import reverse_lazy
from django.core.paginator import Paginator
from django.http import HttpResponseRedirect
from django.contrib.contenttypes.models import ContentType
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin

def get_category_path(cat, current_path=""):
    if cat.parent_category:
        new_path = " > "
        new_path += str(cat.title)
        new_path += current_path
        return get_category_path(cat.parent_category, current_path=new_path)

    else:
        new_path = str(cat.title)
        new_path += current_path
        return new_path

def TopicCategoryView(request, cat_id):
    category = ContentCategory.objects.get(id=cat_id)
    skill_area_topics = Topic.objects.filter(category=cat_id)
    paginator = Paginator(skill_area_topics, 5)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    return render(request, 'community_view_category.html', {"category": category,
                                                        "skill_area_topics": page_obj})

class CommunityHomeView(TemplateView):
    template_name = "community_home.html"
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        skill_topic_set = {}
        for category in ContentCategory.objects.filter(global_standard=True):
            filtered_results = (get_category_path(category), list(Topic.objects.filter(category=category.id).order_by('-publish_date', '-publish_time')[:5]))
            skill_topic_set[category] = filtered_results
        x = sorted(skill_topic_set.items(), key=lambda x: x[1][0])

        context["category_topics"] = {k:v for k,v in x}
        return context

###############################################################################################
### For viewing a topic, replying and making comments

def create_comment_lists_by_len(qs, l_len):

    pagi_comments = []
    pagi_builder = []
    for comment in qs:
        if len(pagi_builder) >= l_len:
            pagi_comments.append(pagi_builder)
            pagi_builder = []
        pagi_builder.append(comment)
    pagi_comments.append(pagi_builder)

    return pagi_comments

def TopicView(request, pk):

    topic = get_object_or_404(Topic,id=pk)#

    topic_comment_form = TopicCommentForm(request.POST or None)#
    reply_comment_form = TopicCommentForm(request.POST or None)#
    template_name = "topic_view.html"
    topic_comment_split_list = []
    reply_comment_split_list = []
    all_replies_and_comments = []
    topic_votes = []
    if request.method == "GET":
    ### FOR TOPIC COMMENTS
        topic_comments = CommentContent.objects.filter_by_instance(topic)
        topic_votes = VoteContent.objects.filter_by_instance_vote_counts(topic)
        if topic_comments:
            topic_comments_votes = []
            for comment in topic_comments:
                comment_votes = VoteContent.objects.filter_by_instance_vote_counts(comment)
                topic_comments_votes.append((comment, comment_votes))
            topic_comment_split_list = create_comment_lists_by_len(topic_comments_votes, 5)

    ### FOR TOPIC REPLIES
        #### Check is there are replies
        topic_replies = Reply.objects.filter(on_post=pk)
        if topic_replies:
        ### Loop through the replies
            for topic_reply in topic_replies:
####################################################################################################
#### GETS REPLIES, THERE COMMENTS, AND THE COMMENTS VOTE COUNTS
            #### Find any comments replated to an individual reply
                reply_votes = VoteContent.objects.filter_by_instance_vote_counts(topic_reply)

                reply_comments = CommentContentContent.objects.filter_by_instance(topic_reply)
                reply_comments_votes = []
                if reply_comments:
                    for comment in reply_comments:

                        comment_votes = VoteContent.objects.filter_by_instance_vote_counts(comment)
                        reply_comments_votes.append((comment, comment_votes))

                    reply_comment_split_list = create_comment_lists_by_len(reply_comments_votes, 5)
            ### Save a single reply and all its comments as a tuple to a list of every reply and comment
                    all_replies_and_comments.append(((topic_reply, reply_votes),reply_comment_split_list))

                else:
                    all_replies_and_comments.append((
                                                    (topic_reply,reply_votes),
                                                    ))

####################################################################################################
#### COMMENT POSTING

#### topic

    if request.method =="POST" and 'commentfortopic' in request.POST:
        if request.user:
            if topic_comment_form.is_valid():
                c_body = topic_comment_form.cleaned_data.get('content_body')
                new_comment = CommentContent(author=request.user, content_type=ContentType.objects.get_for_model(Topic),body=c_body, object_id=topic.id)
                new_comment.save()
                return HttpResponseRedirect(request.path)
            else:
                print("form isnt valid")
        else:

            messages.info(request, 'You must be logged in to commment')

            return HttpResponseRedirect(request.path)
#### reply
    if request.method =="POST" and 'commentforreply' in request.POST:
        if request.user:
            if reply_comment_form.is_valid():
                c_body = reply_comment_form.cleaned_data.get('content_body')
                new_comment = CommentContent(author=request.user, content_type=ContentType.objects.get_for_model(Reply),body=c_body, object_id=request.POST['object_id'])
                new_comment.save()
                return HttpResponseRedirect(request.path)
            else:
                print("form isnt valid")
        else:
            messages.info(request, 'You must be logged in to commment')
            return HttpResponseRedirect(request.path)

####################################################################################################
#### TOPIC VOTING

#### UP
    if request.method =="POST" and 'topic_vote_up' in request.POST:
        past_vote = VoteContent.objects.filter_by_instance(topic)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(f"{request.path}/1")
            new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(Topic), object_id=topic.id, is_up_vote=True)
            new_vote.save()
            messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')

            return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)

#### DOWN
    if request.method =="POST" and 'topic_vote_down' in request.POST:
        past_vote = VoteContent.objects.filter_by_instance(topic)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(Topic), object_id=topic.id, is_up_vote=False)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)

####################################################################################################
#### Comment voting

#### UP
    if request.method =="POST" and 'comment_vote_up' in request.POST:
        obj_id = request.POST["topic_comment_id_to_vote_up"]
        comment_instance = CommentContent.objects.get(id=obj_id)
        past_vote = VoteContent.objects.filter_by_instance(comment_instance)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(CommentContent), object_id=obj_id, is_up_vote=True)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)

#### DOWN
    if request.method =="POST" and 'comment_vote_down' in request.POST:
        obj_id = request.POST["topic_comment_id_to_vote_down"]
        comment_instance = CommentContent.objects.get(id=obj_id)
        past_vote = VoteContent.objects.filter_by_instance(comment_instance)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                x =request.POST["reply_id_to_vote"]
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(CommentContent), object_id=obj_id, is_up_vote=False)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)

####################################################################################################
#### Reply voting

#### UP
    if request.method =="POST" and "reply_vote_up" in request.POST:
        obj_id = request.POST["reply_id_to_vote_up"]
        reply_instance = Reply.objects.get(id=obj_id)
        past_vote = VoteContent.objects.filter_by_instance(reply_instance)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(Reply), object_id=obj_id, is_up_vote=True)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)

#### Down
    if request.method =="POST" and 'reply_vote_down' in request.POST:
        obj_id = request.POST["reply_id_to_vote_down"]
        reply_instance = Reply.objects.get(id=obj_id)
        past_vote = VoteContent.objects.filter_by_instance(reply_instance)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(Reply), object_id=obj_id, is_up_vote=False)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)
####################################################################################################

####################################################################################################
#### Reply comment voting

#### UP
    if request.method =="POST" and "reply_comment_vote_up" in request.POST:
        obj_id = request.POST["reply_comment_id_to_vote_up"]
        reply_comment_instance = CommentContent.objects.get(id=obj_id)
        past_vote = VoteContent.objects.filter_by_instance(reply_comment_instance)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(CommentContent), object_id=obj_id, is_up_vote=True)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)

#### Down
    if request.method =="POST" and 'reply_comment_vote_down' in request.POST:
        obj_id = request.POST["reply_comment_id_to_vote_down"]
        reply_comment_instance = CommentContent.objects.get(id=obj_id)
        past_vote = VoteContent.objects.filter_by_instance(reply_comment_instance)
        if request.user:
            user_votes = past_vote.filter(votee=request.user)
            if user_votes:
                messages.info(request, 'You have already voted')
                return HttpResponseRedirect(request.path)
            else:
                new_vote = VoteContent(votee=request.user, content_type=ContentType.objects.get_for_model(CommentContent), object_id=obj_id, is_up_vote=False)
                new_vote.save()
                messages.success(request, 'Thankyou for your opinion - it might help others find what they need!')
                return HttpResponseRedirect(request.path)
        else:
            messages.info(request, 'You must be logged in to vote')
            return HttpResponseRedirect(request.path)
####################################################################################################

    context = {
                "topic": topic,
                "pagi_comments": topic_comment_split_list[:min(len(topic_comment_split_list), 6)],
                "topic_comment_form": topic_comment_form,
                "topic_replies": all_replies_and_comments,
                "reply_comment_form": reply_comment_form,
                "topic_votes": topic_votes,
                }

    return render(request, template_name, context)

class ReplyEdit(LoginRequiredMixin,UpdateView):
    login_url = '/login-or-register/'
    redirect_field_name = 'redirect_to'
    model = Reply
    form_class = ReplyEditForm
    template_name = "reply_edit.html"
###############################################################################################
### For viewing a topic, replying and making comments
class TopicCreate(LoginRequiredMixin,CreateView):
    login_url = '/login-or-register/'
    redirect_field_name = 'redirect_to'
    model = Topic
    form_class = TopicCreateForm
    template_name = "topic_create.html"

class ReplyCreate(LoginRequiredMixin,CreateView):
    login_url = '/login-or-register/'
    redirect_field_name = 'redirect_to'
    model = Reply
    form_class = ReplyCreateForm
    template_name = "reply_create.html"
    def get_success_url(self, **kwargs):
        return reverse_lazy('topic-view', args=(self.kwargs['pk'],))

    def get_context_data(self, **kwargs):
        self.reply_to = get_object_or_404(Topic, id=self.kwargs['pk'])
        kwargs['topic'] = self.reply_to
        return super().get_context_data(**kwargs)

    def form_valid(self, form):
        reply_to = get_object_or_404(Topic, id=self.kwargs['pk'])
        obj = form.save(commit=False)
        form.instance.author = self.request.user
        form.instance.on_post = reply_to
        messages.success(self.request, 'Your reply has been posted successfully')
        return super().form_valid(form)

class TopicCreateInCat(LoginRequiredMixin,CreateView):
    login_url = '/login-or-register/'
    redirect_field_name = 'redirect_to'
    model = Topic
    form_class = TopicCreateInCatForm
    template_name = "topic_create_in_cat.html"

    def get_success_url(self, **kwargs):
        return reverse_lazy('community-home')

    def get_context_data(self, **kwargs):
        on_cat = ContentCategory.objects.get(id=self.kwargs['cat_id'])
        kwargs['category'] = on_cat
        return super().get_context_data(**kwargs)

    def form_valid(self, form):
        form.instance.category = ContentCategory.objects.get(id=self.kwargs['cat_id'])
        form.instance.author = self.request.user
        messages.success(self.request, 'Your Topic has been posted successfully')
        return super().form_valid(form)

class TopicEdit(LoginRequiredMixin,UpdateView):
    login_url = '/login-or-register/'
    redirect_field_name = 'redirect_to'
    model= Topic
    form_class = TopicEditForm
    template_name = 'topic_edit.html'

class TopicDelete(LoginRequiredMixin,DeleteView):
    login_url = '/login-or-register/'
    redirect_field_name = 'redirect_to'
    model= Topic
    template_name = 'topic_delete.html'
    success_url = reverse_lazy('home')


__author__ = 'Nyall Dawson'
__date__ = 'November 2018'
__copyright__ = '(C) 2018, Nyall Dawson'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '176c06ceefb5f555205e72b20c962740cc0ec183'

import qgis  # NOQA

from utilities import unitTestDataPath

from qgis.PyQt.QtCore import QDir, Qt, QSize
from qgis.PyQt.QtGui import QImage, QColor, QPainter
from qgis.PyQt.QtXml import QDomDocument

from qgis.core import (QgsGeometry,
                       QgsFillSymbol,
                       QgsRenderContext,
                       QgsFeature,
                       QgsMapSettings,
                       QgsRenderChecker,
                       QgsReadWriteContext,
                       QgsSymbolLayerUtils,
                       QgsSimpleMarkerSymbolLayer,
                       QgsLineSymbolLayer,
                       QgsTemplatedLineSymbolLayerBase,
                       QgsMarkerLineSymbolLayer,
                       QgsMarkerSymbol,
                       QgsGeometryGeneratorSymbolLayer,
                       QgsSymbol,
                       QgsFontMarkerSymbolLayer,
                       QgsFontUtils,
                       QgsLineSymbol,
                       QgsSymbolLayer,
                       QgsProperty,
                       QgsRectangle,
                       QgsUnitTypes
                       )

from qgis.testing import unittest, start_app

start_app()
TEST_DATA_DIR = unitTestDataPath()

class TestQgsMarkerLineSymbolLayer(unittest.TestCase):

    def setUp(self):
        self.report = "
Python QgsMarkerLineSymbolLayer Tests
\n"

    def tearDown(self):
        report_file_path = "%s/qgistest.html" % QDir.tempPath()
        with open(report_file_path, 'a') as report_file:
            report_file.write(self.report)

    def testWidth(self):
        ms = QgsMapSettings()
        extent = QgsRectangle(100, 200, 100, 200)
        ms.setExtent(extent)
        ms.setOutputSize(QSize(400, 400))
        context = QgsRenderContext.fromMapSettings(ms)
        context.setScaleFactor(96 / 25.4)  # 96 DPI
        ms.setExtent(QgsRectangle(100, 150, 100, 150))
        ms.setOutputDpi(ms.outputDpi() * 2)
        context2 = QgsRenderContext.fromMapSettings(ms)
        context2.setScaleFactor(300 / 25.4)

        s = QgsFillSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 10)
        marker.setColor(QColor(255, 0, 0))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)

        self.assertEqual(marker_line.width(), 10)
        self.assertAlmostEqual(marker_line.width(context), 37.795275590551185, 3)
        self.assertAlmostEqual(marker_line.width(context2), 118.11023622047244, 3)

        marker_line.subSymbol().setSizeUnit(QgsUnitTypes.RenderPixels)
        self.assertAlmostEqual(marker_line.width(context), 10.0, 3)
        self.assertAlmostEqual(marker_line.width(context2), 10.0, 3)

    def testRingFilter(self):
        # test filtering rings during rendering
        s = QgsFillSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 4)
        marker.setColor(QColor(255, 0, 0))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)

        s.appendSymbolLayer(marker_line.clone())
        self.assertEqual(s.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.AllRings)
        s.symbolLayer(0).setRingFilter(QgsLineSymbolLayer.ExteriorRingOnly)
        self.assertEqual(s.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.ExteriorRingOnly)

        s2 = s.clone()
        self.assertEqual(s2.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.ExteriorRingOnly)

        doc = QDomDocument()
        context = QgsReadWriteContext()
        element = QgsSymbolLayerUtils.saveSymbol('test', s, doc, context)

        s2 = QgsSymbolLayerUtils.loadSymbol(element, context)
        self.assertEqual(s2.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.ExteriorRingOnly)

        # rendering test
        s3 = QgsFillSymbol()
        s3.deleteSymbolLayer(0)
        s3.appendSymbolLayer(
            QgsMarkerLineSymbolLayer())
        s3.symbolLayer(0).setRingFilter(QgsLineSymbolLayer.ExteriorRingOnly)
        s3.symbolLayer(0).setAverageAngleLength(0)

        g = QgsGeometry.fromWkt('Polygon((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1),(8 8, 9 8, 9 9, 8 9, 8 8))')
        rendered_image = self.renderGeometry(s3, g)
        assert self.imageCheck('markerline_exterioronly', 'markerline_exterioronly', rendered_image)

        s3.symbolLayer(0).setRingFilter(QgsLineSymbolLayer.InteriorRingsOnly)
        g = QgsGeometry.fromWkt('Polygon((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1),(8 8, 9 8, 9 9, 8 9, 8 8))')
        rendered_image = self.renderGeometry(s3, g)
        assert self.imageCheck('markerline_interioronly', 'markerline_interioronly', rendered_image)

    def testPartNum(self):
        # test geometry_part_num variable
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        sym_layer = QgsGeometryGeneratorSymbolLayer.create({'geometryModifier': 'segments_to_lines($geometry)'})
        sym_layer.setSymbolType(QgsSymbol.Line)
        s.appendSymbolLayer(sym_layer)

        marker_line = QgsMarkerLineSymbolLayer(False)
        marker_line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex)
        f = QgsFontUtils.getStandardTestFont('Bold', 24)
        marker = QgsFontMarkerSymbolLayer(f.family(), 'x', 24, QColor(255, 255, 0))
        marker.setDataDefinedProperty(QgsSymbolLayer.PropertyCharacter, QgsProperty.fromExpression('@geometry_part_num'))
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        marker_line.setAverageAngleLength(0)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)
        sym_layer.setSubSymbol(line_symbol)

        # rendering test
        g = QgsGeometry.fromWkt('LineString(0 0, 10 0, 10 10, 0 10)')
        rendered_image = self.renderGeometry(s, g, buffer=4)
        assert self.imageCheck('part_num_variable', 'part_num_variable', rendered_image)

        marker.setDataDefinedProperty(QgsSymbolLayer.PropertyCharacter,
                                      QgsProperty.fromExpression('@geometry_part_count'))

        # rendering test
        g = QgsGeometry.fromWkt('LineString(0 0, 10 0, 10 10, 0 10)')
        rendered_image = self.renderGeometry(s, g, buffer=4)
        assert self.imageCheck('part_count_variable', 'part_count_variable', rendered_image)

    def testMarkerAverageAngle(self):
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval)
        marker_line.setInterval(6)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 4)
        marker.setColor(QColor(255, 0, 0))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        marker_line.setAverageAngleLength(60)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)

        s.appendSymbolLayer(marker_line.clone())

        g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)')
        rendered_image = self.renderGeometry(s, g)
        assert self.imageCheck('markerline_average_angle', 'markerline_average_angle', rendered_image)

    def testMarkerAverageAngleRing(self):
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval)
        marker_line.setInterval(6)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 4)
        marker.setColor(QColor(255, 0, 0))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        marker_line.setAverageAngleLength(60)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)

        s.appendSymbolLayer(marker_line.clone())

        g = QgsGeometry.fromWkt('LineString(0 0, 0 10, 10 10, 10 0, 0 0)')
        rendered_image = self.renderGeometry(s, g)
        assert self.imageCheck('markerline_ring_average_angle', 'markerline_ring_average_angle', rendered_image)

    def testMarkerAverageAngleCenter(self):
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.CentralPoint)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 4)
        marker.setColor(QColor(255, 0, 0))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        marker_line.setAverageAngleLength(60)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)

        s.appendSymbolLayer(marker_line.clone())

        g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)')
        rendered_image = self.renderGeometry(s, g)
        assert self.imageCheck('markerline_center_average_angle', 'markerline_center_average_angle', rendered_image)

    def testRingNoDupe(self):
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval)
        marker_line.setInterval(10)
        marker_line.setIntervalUnit(QgsUnitTypes.RenderMapUnits)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Circle, 4)
        marker.setColor(QColor(255, 0, 0, 100))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)

        s.appendSymbolLayer(marker_line.clone())

        g = QgsGeometry.fromWkt('LineString(0 0, 0 10, 10 10, 10 0, 0 0)')
        rendered_image = self.renderGeometry(s, g)
        assert self.imageCheck('markerline_ring_no_dupes', 'markerline_ring_no_dupes', rendered_image)

    def testSinglePoint(self):
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval)
        marker_line.setInterval(1000)
        marker_line.setIntervalUnit(QgsUnitTypes.RenderMapUnits)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Circle, 4)
        marker.setColor(QColor(255, 0, 0, 100))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)

        s.appendSymbolLayer(marker_line.clone())

        g = QgsGeometry.fromWkt('LineString(0 0, 0 10, 10 10)')
        rendered_image = self.renderGeometry(s, g)
        assert self.imageCheck('markerline_single', 'markerline_single', rendered_image)

    def testNoPoint(self):
        s = QgsLineSymbol()
        s.deleteSymbolLayer(0)

        marker_line = QgsMarkerLineSymbolLayer(True)
        marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval)
        marker_line.setOffsetAlongLine(1000)
        marker_line.setIntervalUnit(QgsUnitTypes.RenderMapUnits)
        marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Circle, 4)
        marker.setColor(QColor(255, 0, 0, 100))
        marker.setStrokeStyle(Qt.NoPen)
        marker_symbol = QgsMarkerSymbol()
        marker_symbol.changeSymbolLayer(0, marker)
        marker_line.setSubSymbol(marker_symbol)
        line_symbol = QgsLineSymbol()
        line_symbol.changeSymbolLayer(0, marker_line)

        s.appendSymbolLayer(marker_line.clone())

        g = QgsGeometry.fromWkt('LineString(0 0, 0 10, 10 10)')
        rendered_image = self.renderGeometry(s, g)
        assert self.imageCheck('markerline_none', 'markerline_none', rendered_image)

    def renderGeometry(self, symbol, geom, buffer=20):
        f = QgsFeature()
        f.setGeometry(geom)

        image = QImage(200, 200, QImage.Format_RGB32)

        painter = QPainter()
        ms = QgsMapSettings()
        extent = geom.get().boundingBox()
        # buffer extent by 10%
        if extent.width() > 0:
            extent = extent.buffered((extent.height() + extent.width()) / buffer)
        else:
            extent = extent.buffered(buffer / 2)

        ms.setExtent(extent)
        ms.setOutputSize(image.size())
        context = QgsRenderContext.fromMapSettings(ms)
        context.setPainter(painter)
        context.setScaleFactor(96 / 25.4)  # 96 DPI
        context.expressionContext().setFeature(f)

        painter.begin(image)
        try:
            image.fill(QColor(0, 0, 0))
            symbol.startRender(context)
            symbol.renderFeature(f, context)
            symbol.stopRender(context)
        finally:
            painter.end()

        return image

    def imageCheck(self, name, reference_image, image):
        self.report += "
Render {}
\n".format(name)
        temp_dir = QDir.tempPath() + '/'
        file_name = temp_dir + 'symbol_' + name + ".png"
        image.save(file_name, "PNG")
        checker = QgsRenderChecker()
        checker.setControlPathPrefix("symbol_markerline")
        checker.setControlName("expected_" + reference_image)
        checker.setRenderedImage(file_name)
        checker.setColorTolerance(2)
        result = checker.compareImages(name, 20)
        self.report += checker.report()
        print((self.report))
        return result

if __name__ == '__main__':
    unittest.main()


import os
from random import random

image_dir = "/Users/aleksandrsimonyan/Desktop/cross_age_dataset_cleaned_and_resized"

def age_to_group(age):
    if age <= 20:
        return 0
    elif age > 20 and age <= 30:
        return 1
    elif age > 30 and age <= 40:
        return 2
    elif age > 40 and age <= 50:
        return 3
    elif age > 50:
        return 4

train_txt = []
test_txt = []
train_age_group0 = []
train_age_group1 = []
train_age_group2 = []
train_age_group3 = []
train_age_group4 = []

for filename in os.listdir(image_dir):
    if '.DS_Store' in filename:
        continue
    age = int(filename.split('_')[0])
    group = age_to_group(age)
    strline = filename + ' %d\n' % group
    if group == 0:
        train_age_group0.append(strline)
    elif group == 1:
        train_age_group1.append(strline)
    elif group == 2:
        train_age_group2.append(strline)
    elif group == 3:
        train_age_group3.append(strline)
    elif group == 4:
        train_age_group4.append(strline)
    train_txt.append(strline)

with open('files/GAN_csv_files/train_age_group_0.txt', 'w') as f:
    f.writelines(train_age_group0)

with open('files/GAN_csv_files/train_age_group_1.txt', 'w') as f:
    f.writelines(train_age_group1)

with open('files/GAN_csv_files/train_age_group_2.txt', 'w') as f:
    f.writelines(train_age_group2)

with open('files/GAN_csv_files/train_age_group_3.txt', 'w') as f:
    f.writelines(train_age_group3)

with open('files/GAN_csv_files/train_age_group_4.txt', 'w') as f:
    f.writelines(train_age_group4)

with open('files/GAN_csv_files/train.txt', 'w') as f:
    f.writelines(train_txt)


from osgeo import ogr, osr
"""
    Se va a extraer del shp los concellos de las comarcas de Galicia por los que pase el "Camino Francés"
	del Camino de Santiago: Arzúa, Santiago, Terra De Melide, A Ulloa, Lugo, Sarria, Os Ancares
"""

#Fichero de origen
fichero_origen = "shp/Concellos_IGN.shp"
#Lo abrimos con el data source de origen
data_source_origen = ogr.Open(fichero_origen)
#Recuperamos la capa del origen
capa_origen = data_source_origen.GetLayer()

#Preparamos el fichero /de destino/nuevo/a crear/
fichero_destino = "shp/concellos_camino_frances.shp"
#Creamos el data source de destino
data_source_destino = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(fichero_destino)
#Creamos la capa de destino, con la referencia espacial del fichero de origen
capa_destino = data_source_destino.CreateLayer("Concellos camino Frances",capa_origen.GetSpatialRef())

#A continuación, damos forma al molde.

#Un atributo con código de la provincia
esquema_atributo = ogr.FieldDefn("CodPROV", ogr.OFTString)
esquema_atributo.SetWidth(10)
capa_destino.CreateField(esquema_atributo)

#Un atributo con el código de la comarca
esquema_atributo = ogr.FieldDefn("CodCOM", ogr.OFTString)
esquema_atributo.SetWidth(10)
capa_destino.CreateField(esquema_atributo)

#Un atributo con el nombre de la comarca
esquema_atributo = ogr.FieldDefn("Comarca", ogr.OFTString)
esquema_atributo.SetWidth(30)
capa_destino.CreateField(esquema_atributo)

#Un atributo con el nombre de la provincia
esquema_atributo = ogr.FieldDefn("Provincia", ogr.OFTString)
esquema_atributo.SetWidth(30)
capa_destino.CreateField(esquema_atributo)

#Un atributo con el código de la concello
esquema_atributo = ogr.FieldDefn("Concello", ogr.OFTString)
esquema_atributo.SetWidth(10)
capa_destino.CreateField(esquema_atributo)

"""
    A partir de aquí, iteramos por las features de origen, y volcamos aquellas
    que cumplen la condición
"""
capa_destino_esquema = capa_destino.GetLayerDefn()
feature_origen = capa_origen.GetNextFeature()
while feature_origen:
    if (feature_origen.GetField("Comarca") == "Santiago") or (feature_origen.GetField("Comarca") == "Arzúa") or (feature_origen.GetField("Comarca") == "Terra De Melide") or (feature_origen.GetField("Comarca") == "A Ulloa") or (feature_origen.GetField("Comarca") == "Lugo") or (feature_origen.GetField("Comarca") == "Sarria") or (feature_origen.GetField("Comarca") == "Os Ancares"):
        #Creamos la feature de destino
        feature_destino = ogr.Feature(capa_destino_esquema)
        #Establecemos la geometría
        feature_destino.SetGeometry(feature_origen.GetGeometryRef())
        #Rellenamos los atributos
        feature_destino.SetField("CodPROV",feature_origen.GetField("CodPROV"))
        feature_destino.SetField("CodCOM",feature_origen.GetField("CodCOM"))
        feature_destino.SetField("Provincia",feature_origen.GetField("Provincia"))
        feature_destino.SetField("Comarca",feature_origen.GetField("Comarca"))
        feature_destino.SetField("Concello",feature_origen.GetField("Concello"))

        #Volcamos a la capa
        capa_destino.CreateFeature(feature_destino)
        #Limpiamos referencia
        feature_destino = None

    #Actualizamos la feature de origen
    feature_origen = capa_origen.GetNextFeature()

#Una vez acabado el bucle, liberamos los data sources

data_source_destino = None
data_source_origen = None


import pygame
from mutagen.mp3 import MP3 as mp3
from pygame.locals import *
import sys
import numpy as np
import board as bd
import field as fie
import os
from os.path import join as pjoin

def main():
    screen = pygame.display.set_mode((720, 600)) # ウィンウサイズの指定
    next = ""
    while(True):
        if next == "":
            next = select_window(screen)
        elif next == "Battle":
            next = battle_window(screen)
        elif next == "Rule":
            next = Rule_window(screen)

def select_window(screen):
    myfont = pygame.font.SysFont(None,40)
    title = pygame.image.load(pjoin(os.getcwd(),pjoin("figure","title.png"))).convert_alpha()
    start = myfont.render("press S to start",True,(0,0,0))
    while(True):
        screen.fill((122,255,122))
        screen.blit(start,(200,400))
        screen.blit(title,(20,50))
        pygame.display.update() # 画面更新
        pygame.time.wait(20) # 更新間隔
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == K_s:
                    return "Rule"

def result_window(screen,winner,field):
    myfont = pygame.font.SysFont(None,40)

    if winner > 0:
        winner = "black"
        res = myfont.render("{} win!".format(winner),True,(220,30,30))
    elif winner < 0:
        winner = "white"
        res = myfont.render("{} win!".format(winner),True,(220,30,30))
    else:
        res = myfont.render("tie game!",True,(220,30,30))

    ret = myfont.render("press R to replay".format(winner),True,(220,30,30))
    while(True):
        screen.fill((122,255,122))
        field.draw(screen)
        screen.blit(res,(200,200))
        screen.blit(ret,(200,350))
        pygame.display.update() # 画面更新
        pygame.time.wait(20) # 更新間隔
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == K_r:
                    return

def battle_window(screen):
    myfont = pygame.font.SysFont(None,120)
    myfont2 = pygame.font.SysFont(None,80)
    winrect = screen.get_rect()
    f = fie.field((90,80))
    flag = 0
    mouseDown_flag = False
    isBattle = True

    while(isBattle):
        screen.fill((122,255,122))
        bnum,wnum = f.bw_num()
        leftturn = f.leftturn()
        screen.blit(myfont.render("{}".format(bnum),True,(0,0,0)),(0,0))
        screen.blit(myfont.render("{}".format(wnum),True,(255,255,255)),(600,0))
        screen.blit(myfont2.render("{} turns left".format(leftturn),True,(255,122,0)),(180,0))
        isBattle = f.draw(screen)
        if not isBattle:
            pygame.mixer.stop()
            winner = f.result()
            result_window(screen,winner,f)
            return ""

        if pygame.mouse.get_pressed()[0]:#押している間は牌を表示
            pos = pygame.mouse.get_pos()
            f.putdemo(pos,screen)
            mouseDown_flag = True

        pygame.display.update() # 画面更新
        pygame.time.wait(20) # 更新間隔

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == K_p:
                    f.pass_turn()
            if event.type == MOUSEBUTTONUP and mouseDown_flag:
                pos = pygame.mouse.get_pos()
                f.put(pos)
                mouseDown_flag = False

def Rule_window(screen):
    myfont = pygame.font.SysFont(None,40)
    start = myfont.render("press S to start",True,(0,0,0))
    rule = pygame.image.load(pjoin(os.getcwd(),pjoin("figure","rulesetumei.png"))).convert_alpha()
    isRule = True

    while(isRule):
        screen.fill((122,255,122))
        screen.blit(start,(200,560))
        screen.blit(rule,(100,0))
        pygame.display.update() # 画面更新
        pygame.time.wait(20) # 更新間隔
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == K_s:
                    return "Battle"

if __name__ == "__main__":
    pygame.init() # 初期
    pygame.display.set_caption("reversi") # ウィンドウの上の方に出てくアレの指定
    main()


#!/usr/bin/env python3

import sys
import pif
import socket
import time
import logging

def getip(u):
  ans="unknown"
  try:
    # print("About to check "+u+"...")
    ans=socket.gethostbyname(u)
  except socket.gaierror:
    pass
  # end try
  return ans
# end def getip

domains=sys.argv[1:]
print("There are "+str(len(domains))+" URLs in the list.")

currip=pif.get_public_ip()
print("Our current IP is "+currip)

errors=0
notready=None
for url in domains:
  urlsip=getip(url)
  msg=url+" resolves to "+urlsip
  if urlsip==currip:
    print(msg+":  success!")
  else:
    print(msg+":  fail!")
    if notready==None:
      notready=url
    else:
      notready+=" "+url
    errors+=1
  # end if ip matches our own
# end for each domain

if errors>0:
  print("URLs which are not ready:  "+notready)
  exit(1)
else:
  print("Looking good!")
  exit(0)
# end if too many errors


from collections import deque
from sys import stdin

read = lambda: stdin.readline().rstrip()

n, m = map(int, read().split())
g = [[] for i in range(n + 1)]
r = [[] for i in range(n + 1)]

for i in range(m):
    a, b, c = map(int, read().split())
    g[a].append([b, -c])
    r[b].append(a)

q = deque()
visited = [False] * (n + 1)
visited[n] = True
q.append(n)

while q:
    cur = q.popleft()

    for j in r[cur]:
        if not visited[j]:
            visited[j] = True
            q.append(j)

INF = 1e9
dist = [INF] * (n + 1)
prev = [0] * (n + 1)
dist[1] = 0

for i in range(1, n + 1):
    for j in range(1, n + 1):
        for cur, d in g[j]:
            if dist[cur] > dist[j] + d:
                if i == (n - 1) and visited[cur]:
                    print(-1)
                    exit()

                dist[cur] = dist[j] + d
                prev[cur] = j

if dist[n] >= INF:
    print(-1)
else:
    res = [n]
    c = n

    for i in range(1, n + 1):
        if prev[c] == 0: continue
        res.append(prev[c])
        c = prev[c]

    print(' '.join(map(str, reversed(res))))


from django.conf.urls import url, include
from . import views

urlpatterns = [
    url(r'^$', views.user, name='user'),
    url(r'^orders', views.ordertable, name='ordertable'),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^order/(?P<pk>[0-9]+)/delete/$', views.delete, name='delete'),
    url(r'^order/(?P<pk>[0-9]+)/edit/$', views.edit, name='edit'),
]


from multiprocessing import Process,Pipe
import time

def f(conn):
    seq = (i for i in range(10))
    for i in range(10):
        time.sleep(2)
        # print("send:", i)
        conn.send([i, 'test', None])
    # print(conn.recv())
    conn.close()

if __name__ == "__main__":
    left, right = Pipe() #产生两个返回对象，一个是管道这一头，一个是另一头
    p = Process(target=f, args=(right,))
    p.start()

    while True:
        print ("rev:", left.recv())
        time.sleep(1)

    # parent_conn.send('father test')

    p.join()


import requests
import csv
from bs4 import BeautifulSoup

reqLinks=[]

with open('test.csv') as csv_file:
    csv_reader = csv.reader(csv_file,delimiter=',')
    for row in csv_reader:
        reqLinks = row

# req = requests.get('https://www.1mg.com/drugs/cipladine-ointment-137489')
# req = requests.get('https://www.1mg.com/drugs/ciplar-la-20-tablet-342850')

csvFile = open('data.csv','w')
row=["Name","Benifits","Manufacturer","Contains"]

writer = csv.writer(csvFile)
writer.writerow(row)

for rowItem in row:
    row[row.index(rowItem)]="No Data"

for reqLink in reqLinks:
    req = requests.get(reqLink)
    soup = BeautifulSoup(req.content,"html.parser")
    row[0]=soup.title.get_text()
    print('Extracting Data From '+ soup.title.get_text())
    for fileObj in soup.findAll('script'):
        if ("\"benefits\":[{" in fileObj.get_text()):
            # print("IN Benifits")
            # fileText = open('data.json','wb')
            textFile = '{'+ fileObj.get_text().split("\"benefits\":[{")[1].split('}]}]}')[0] +'}]}'
            # fileText.write(textFile.encode('utf-8'))
            row[1]=textFile

        if ("\"header\":\"Manufacturer\"" in fileObj.get_text()):
            # print("IN Manufacturer")
            # fileText = open('data.txt','wb')
            textFile = fileObj.get_text().split("\"header\":\"Manufacturer\",")[1].split("\"}")[0].split('>')[1]
            # fileText.write(textFile.encode('utf-8'))
            row[2]=textFile

        if ("\"header\":\"Contains\"" in fileObj.get_text()):
            # print("IN Contains")
            # fileText = open('data3.txt','wb')
            textFile = fileObj.get_text().split("\"header\":\"Contains\"")[1].split("\"}")[0].split('>')[1]
            # fileText.write(textFile.encode('utf-8'))
            row[3]=textFile

    for rowItem in row:
        if rowItem == '':
            row[row.index(rowItem)]="No Data"

    writer.writerow(row)
    for rowItem in row:
        row[row.index(rowItem)]="No Data"

# print(soup.prettify())
# print(soup.findAll('script')[2])

# res=soup.find("çlass='DrugOverview__content___22ZBX'")
# print(soup.html['id'])


# Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

# Load the data
train_data = 'train.csv'
train = pd.read_csv(train_data)
test_data = '/test.csv'
test = pd.read_csv(test_data)

## EDA AND PREPROCESSING
########################
# Checking for missing values in train set
train.isnull().sum()
# Checking for missing values in test set
test.isnull().sum()
# Function to replace missing values with the median
def replace_with_median(df, column_name):
    median_value = df[column_name].median()
    df[column_name].fillna(median_value, inplace=True)
# Replacing empty strings with NaN in train
train.replace('', np.nan, inplace=True)
# Replacing empty strings with NaN in test
test.replace('', np.nan, inplace=True)
# Checking missing values in train set after replacing empty strings
train.isnull().sum()
# Replacing the missing 'Embarked' values with the most frequent 'S'
train["Embarked"].fillna("S", inplace=True)
test["Embarked"].fillna("S", inplace=True)
# Replacing missing values of 'Age' column with the median
replace_with_median(train, 'Age')
replace_with_median(test, 'Age')
# Dropping 'Cabin', 'Name', 'Ticket', and 'PassengerId' columns in train and test
train.drop(['Cabin', 'Name', 'Ticket', 'PassengerId'], axis=1, inplace=True)
test.drop(['Cabin', 'Name', 'Ticket', 'PassengerId'], axis=1, inplace=True)
# Replacing rows of 'Fare' column in test set where values are NaN with the median
replace_with_median(test, 'Fare')
# Replacing categorical values of 'Sex' column by dummy variables
train = pd.get_dummies(train, columns=['Sex'], drop_first=True)
test = pd.get_dummies(test, columns=['Sex'], drop_first=True)
# Replacing categorical values of 'Embarked' column by dummy variables
train = pd.get_dummies(train, columns=['Embarked'], prefix='Embarked', drop_first=True)
test = pd.get_dummies(test, columns=['Embarked'], prefix='Embarked', drop_first=True)
# Checking data types of all columns in train set
train_data_types = train.dtypes
print("\nData types in train set:")
print(train_data_types)
# Checking data types of all columns in test set
test_data_types = test.dtypes
print("\nData types in test set:")
print(test_data_types)

## SPLIT AND STANDARDIZE THE DATA
#################################
X_train = train.iloc[:,1:].values
y_train = train.iloc[:,0].values
X_test = test.iloc[:,:]
X_test.isnull().sum()
# Initialize the StandardScaler
sc = StandardScaler()
# Feature Scaling for the full X_train and X_test
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Further split X_train into X_train_train and X_train_test for gaining more insights
X_train_train, X_train_test, y_train_train, y_train_test = train_test_split(X_train, y_train, test_size=0.25, random_state=42)

## BUILD THE FNN MODEL
######################
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
# Define the FNN model
class FNN(nn.Module):
    def __init__(self, input_size):
        super(FNN, self).__init__()
        self.fc1 = nn.Linear(input_size, 14)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Linear(14, 14)
        self.relu2 = nn.ReLU()
        self.fc3 = nn.Linear(14, 1)
        self.sigmoid = nn.Sigmoid()
    def forward(self, x):
        x = self.fc1(x)
        x = self.relu1(x)
        x = self.fc2(x)
        x = self.relu2(x)
        x = self.fc3(x)
        x = self.sigmoid(x)
        return x
# Initialize the FNN model
input_size = X_train_train.shape[1]
model = FNN(input_size)
# Define the optimizer
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Define the loss function
criterion = nn.BCELoss()
# Training loop
n_epochs = 100
for epoch in range(n_epochs):
    optimizer.zero_grad()
    outputs = model(torch.tensor(X_train_train, dtype=torch.float32))
    loss = criterion(outputs, torch.tensor(y_train_train, dtype=torch.float32).view_as(outputs))
    loss.backward()
    optimizer.step()
# Predicting the training set results
with torch.no_grad():
    y_pred_train = model(torch.tensor(X_train_train, dtype=torch.float32))
    y_pred_train = (y_pred_train > 0.5)
# Convert predictions to a NumPy array
y_pred_train = y_pred_train.numpy()

## EVALUATION OF THE MODEL
# ########################

# Accuracy Score
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_train_train, y_pred_train)
print("Accuracy of the model:", accuracy)
# Filter training data for male survivors
male_survivors = train[(train['Sex_male'] == 1) & (train['Survived'] == 1)]
# Filter training data for female survivors
female_survivors = train[(train['Sex_male'] == 0) & (train['Survived'] == 1)]
# Calculate the percentage of male survivors
percentage_male_survived = (len(male_survivors) / len(train[train['Sex_male'] == 1])) * 100
# Calculate the percentage of female survivors
percentage_female_survived = (len(female_survivors) / len(train[train['Sex_male'] == 0])) * 100

# RESULTS
# Accuracy of the model 0.74
# Percentage of male survivors: 18.89%
#Percentage of female survivors: 74.20%

Deep Learning/2. Feedforward Neural Network (FNN)/Script_Titanic.py


import logging

# logging.basicConfig(filename="F:\\pythonPjo\\testExcelCase\\log\\test.log", filemode="w", format="%(asctime)s %(name)s:%(levelname)s:%(message)s", datefmt="%d-%M-%Y %H:%M:%S", level=logging.DEBUG)
# logging.warning('this is warning')
# logging.info('this is info')

def get_logger(filename):
    logger_obj = logging.getLogger("mylogger")  #创建logger对象
    logger_obj.setLevel(logging.DEBUG)  #设置日志级别

    fh = logging.FileHandler(filename, encoding="UTF-8")  #创建handler输出到日志
    fh.setLevel(logging.DEBUG)

    ch = logging.StreamHandler()  #创建handler输出到控制台
    ch.setLevel(logging.DEBUG)

    # 定义输出格式
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    # 给logger对象添加handler
    logger_obj.addHandler(fh)
    logger_obj.addHandler(ch)
    return logger_obj

# logger_obj=get_logger("F:\\pythonPjo\\testExcelCase\\log\\logtest.txt")
# logger_obj.info("info")
# logger_obj.error("error")
# logger_obj.warning("warning")
# logger_obj.debug("debug")
# logger_obj.critical("critical")


import logging
import json
from collections import defaultdict
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.views.decorators.csrf import csrf_exempt

from seqr.models import Individual, SavedVariant, VariantTagType, VariantTag, VariantNote, VariantFunctionalData,\
    LocusListInterval, LocusListGene, Family, CAN_VIEW, CAN_EDIT, GeneNote
from seqr.model_utils import create_seqr_model, delete_seqr_model
from seqr.views.apis.auth_api import API_LOGIN_REQUIRED_URL
from seqr.views.apis.locus_list_api import get_project_locus_list_models
from seqr.utils.gene_utils import get_genes
from seqr.views.utils.json_to_orm_utils import update_model_from_json
from seqr.views.utils.json_utils import create_json_response
from seqr.views.utils.orm_to_json_utils import get_json_for_saved_variants, get_json_for_variant_tag, \
    get_json_for_variant_functional_data, get_json_for_variant_note, get_json_for_saved_variant, \
    get_json_for_gene_notes_by_gene_id
from seqr.views.utils.permissions_utils import get_project_and_check_permissions, check_permissions
from seqr.views.utils.variant_utils import update_project_saved_variant_json

logger = logging.getLogger(__name__)

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def saved_variant_data(request, project_guid, variant_guid=None):
    project = get_project_and_check_permissions(project_guid, request.user)
    family_guids = request.GET['families'].split(',') if request.GET.get('families') else None

    variant_query = SavedVariant.objects.filter(project=project)

    if family_guids:
        variant_query = variant_query.filter(family__guid__in=family_guids)
    if variant_guid:
        variant_query = variant_query.filter(guid=variant_guid)
        if variant_query.count() < 1:
            return create_json_response({}, status=404, reason='Variant {} not found'.format(variant_guid))

    individual_guids_by_id = {i.individual_id: i.guid for i in Individual.objects.filter(family__project=project)}

    saved_variants = get_json_for_saved_variants(variant_query, add_tags=True, add_details=True, project=project,
                                                 user=request.user, individual_guids_by_id=individual_guids_by_id)
    variants = {variant['variantGuid']: variant for variant in saved_variants if variant['notes'] or variant['tags']}
    genes = _saved_variant_genes(variants.values())
    _add_locus_lists([project], variants.values(), genes)

    return create_json_response({
        'savedVariantsByGuid': variants,
        'genesById': genes,
    })

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def create_saved_variant_handler(request):
    variant_json = json.loads(request.body)
    family_guid = variant_json.pop('familyGuid')
    non_variant_json = {
        k: variant_json.pop(k, None) for k in ['searchHash', 'tags', 'functionalData', 'notes', 'note', 'submitToClinvar']
    }

    family = Family.objects.get(guid=family_guid)
    check_permissions(family.project, request.user, CAN_VIEW)

    xpos = variant_json['xpos']
    ref = variant_json['ref']
    alt = variant_json['alt']
    # TODO remove project field from saved variants
    saved_variant = SavedVariant.objects.create(
        xpos=xpos,
        xpos_start=xpos,
        xpos_end=xpos + len(ref) - 1,
        ref=ref,
        alt=alt,
        family=family,
        project=family.project,
        saved_variant_json=json.dumps(variant_json)
    )

    if non_variant_json.get('note'):
        _create_variant_note(saved_variant, non_variant_json, request.user)
    elif non_variant_json.get('tags'):
        _create_new_tags(saved_variant, non_variant_json, request.user)

    variant_json.update(get_json_for_saved_variant(saved_variant, add_tags=True, project_guid=family.project.guid))
    return create_json_response({
        'savedVariantsByGuid': {saved_variant.guid: variant_json},
    })

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def create_variant_note_handler(request, variant_guid):
    request_json = json.loads(request.body)

    save_as_gene_note = request_json.get('saveAsGeneNote')
    saved_variant = SavedVariant.objects.get(guid=variant_guid)
    check_permissions(saved_variant.project, request.user, CAN_VIEW)

    if save_as_gene_note:
        gene_id = json.loads(saved_variant.saved_variant_json)['mainTranscript']['geneId']
        create_seqr_model(
            GeneNote,
            note=request_json.get('note'),
            gene_id=gene_id,
            created_by=request.user,
        )

    gene_note = {gene_id: {
        'notes': get_json_for_gene_notes_by_gene_id([gene_id], request.user).get(gene_id, [])}} if save_as_gene_note else {}

    _create_variant_note(saved_variant, request_json, request.user)

    variant_note = {variant_guid: {
        'notes': [get_json_for_variant_note(tag) for tag in saved_variant.variantnote_set.all()]},
    }

    return create_json_response({
        'savedVariantsByGuid': variant_note,
        'genesById': gene_note
    })

def _create_variant_note(saved_variant, note_json, user):
    create_seqr_model(
        VariantNote,
        saved_variant=saved_variant,
        note=note_json.get('note'),
        submit_to_clinvar=note_json.get('submitToClinvar') or False,
        search_hash=note_json.get('searchHash'),
        created_by=user,
    )

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def update_variant_note_handler(request, variant_guid, note_guid):
    saved_variant = SavedVariant.objects.get(guid=variant_guid)
    check_permissions(saved_variant.project, request.user, CAN_VIEW)
    note = VariantNote.objects.get(guid=note_guid, saved_variant=saved_variant)

    request_json = json.loads(request.body)
    update_model_from_json(note, request_json, allow_unknown_keys=True)

    return create_json_response({'savedVariantsByGuid': {variant_guid: {
        'notes': [get_json_for_variant_note(tag) for tag in saved_variant.variantnote_set.all()]
    }}})

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def delete_variant_note_handler(request, variant_guid, note_guid):
    saved_variant = SavedVariant.objects.get(guid=variant_guid)
    check_permissions(saved_variant.project, request.user, CAN_VIEW)
    note = VariantNote.objects.get(guid=note_guid, saved_variant=saved_variant)
    delete_seqr_model(note)
    return create_json_response({'savedVariantsByGuid': {variant_guid: {
        'notes': [get_json_for_variant_note(tag) for tag in saved_variant.variantnote_set.all()]
    }}})

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def update_variant_tags_handler(request, variant_guid):
    saved_variant = SavedVariant.objects.get(guid=variant_guid)
    check_permissions(saved_variant.project, request.user, CAN_VIEW)

    request_json = json.loads(request.body)
    updated_tags = request_json.get('tags', [])
    updated_functional_data = request_json.get('functionalData', [])

    # Update tags

    existing_tag_guids = [tag['tagGuid'] for tag in updated_tags if tag.get('tagGuid')]

    for tag in saved_variant.varianttag_set.exclude(guid__in=existing_tag_guids):
        delete_seqr_model(tag)

    _create_new_tags(saved_variant, request_json, request.user)

    # Update functional data

    existing_functional_guids = [tag['tagGuid'] for tag in updated_functional_data if tag.get('tagGuid')]

    for tag in saved_variant.variantfunctionaldata_set.exclude(guid__in=existing_functional_guids):
        delete_seqr_model(tag)

    for tag in updated_functional_data:
        if tag.get('tagGuid'):
            tag_model = VariantFunctionalData.objects.get(
                guid=tag.get('tagGuid'),
                functional_data_tag=tag.get('name'),
                saved_variant=saved_variant
            )
            update_model_from_json(tag_model, tag, allow_unknown_keys=True)
        else:
            create_seqr_model(
                VariantFunctionalData,
                saved_variant=saved_variant,
                functional_data_tag=tag.get('name'),
                metadata=tag.get('metadata'),
                search_hash=request_json.get('searchHash'),
                created_by=request.user,
            )

    return create_json_response({'savedVariantsByGuid': {
        variant_guid: {
            'tags': [get_json_for_variant_tag(tag) for tag in saved_variant.varianttag_set.all()],
            'functionalData': [get_json_for_variant_functional_data(tag) for tag in saved_variant.variantfunctionaldata_set.all()]
        }
    }})

def _create_new_tags(saved_variant, tags_json, user):
    tags = tags_json.get('tags', [])
    new_tags = [tag for tag in tags if not tag.get('tagGuid')]

    for tag in new_tags:
        variant_tag_type = VariantTagType.objects.get(
            Q(name=tag['name']),
            Q(project=saved_variant.project) | Q(project__isnull=True)
        )
        create_seqr_model(
            VariantTag,
            saved_variant=saved_variant,
            variant_tag_type=variant_tag_type,
            search_hash=tags_json.get('searchHash'),
            created_by=user,
        )

@login_required(login_url=API_LOGIN_REQUIRED_URL)
@csrf_exempt
def update_saved_variant_json(request, project_guid):
    project = get_project_and_check_permissions(project_guid, request.user, permission_level=CAN_EDIT)
    updated_saved_variant_guids = update_project_saved_variant_json(project)

    return create_json_response({variant_guid: None for variant_guid in updated_saved_variant_guids})

def _saved_variant_genes(variants):
    gene_ids = set()
    for variant in variants:
        gene_ids.update(variant['transcripts'].keys())
    genes = get_genes(gene_ids, add_dbnsfp=True, add_omim=True, add_constraints=True, add_primate_ai=True)
    for gene in genes.values():
        if gene:
            gene['locusListGuids'] = []
    return genes

def _add_locus_lists(projects, variants, genes):
    locus_lists = set()
    for project in projects:
        locus_lists.update(get_project_locus_list_models(project))
    for variant in variants:
        variant['locusListGuids'] = []

    locus_list_intervals_by_chrom = defaultdict(list)
    for interval in LocusListInterval.objects.filter(locus_list__in=locus_lists):
        locus_list_intervals_by_chrom[interval.chrom].append(interval)
    if locus_list_intervals_by_chrom:
        for variant in variants:
            for interval in locus_list_intervals_by_chrom[variant['chrom']]:
                pos = variant['pos'] if variant['genomeVersion'] == interval.genome_version else variant['liftedOverPos']
                if pos and interval.start <= int(pos) <= interval.end:
                    variant['locusListGuids'].append(interval.locus_list.guid)

    for locus_list_gene in LocusListGene.objects.filter(locus_list__in=locus_lists, gene_id__in=genes.keys()).prefetch_related('locus_list'):
        genes[locus_list_gene.gene_id]['locusListGuids'].append(locus_list_gene.locus_list.guid)

    return [locus_list.guid for locus_list in locus_lists]


from sage.all import DiGraph
from collections import Counter

with open("input-10","r") as f:
    l = [int(s[:-1]) for s in f]
    m = max(l) + 3
    l.append(m)
    l.append(0)
    l = sorted(l)

    # Part 1
    d = [0,0,0,0]
    for i in range(1,len(l)):
        d[l[i]-l[i-1]] += 1
    print(d[1]*d[3])

    # Part 2
    d = DiGraph()
    for i in range(1,len(l)):
        for j in range(max(0,i-3),i):
            if l[i]-l[j] <= 3:
                d.add_edge(l[j], l[i])

    counter = Counter({0 : 1})
    todo = [0]
    visited = set()
    to_visit = {0}
    while todo:
        v = todo.pop()
        visited.add(v)
        neighbors = sorted(list(d.neighbors_out(v)))
        for u in neighbors:
            counter[u] += counter[v]
            if u not in to_visit:
                to_visit.add(u)
                todo = [u] + todo
    print(counter[m])


import os
import argparse

"""
Given keypoint annotation by open3d, this script transfer the annotation
to the required info used for training. The resultant yaml file is save to
the processed/ subdirectory for each scene.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--log_root_path',
                    type=str, default='/home/wei/data/pdc/logs_proto',
                    help='The root path that contains lots of scene recorded at different time')
parser.add_argument('--scene_mesh_keypoint_path', type=str, default='keypoint.yaml',
                    help='The keypoint annotation input for each scene. '
                         'Note that this file will be relative to ${log_root_path}/2018-05-14.../processed/ folder')
parser.add_argument('--scene_image_keypoint_output_path', type=str, default='shoe_6_keypoint_image.yaml',
                    help='The image keypoint for each scene. '
                         'Note that this file will be relative to ${log_root_path}/2018-05-14.../processed/ folder')
args = parser.parse_args()

def main():
    # Get the exe file
    dir_path = os.path.dirname(os.path.realpath(__file__))
    bbox_exe_file = os.path.join(dir_path, 'bbox_from_mask_all.py')
    dir_path = os.path.abspath(os.path.join(dir_path, os.pardir))
    keypoint_exe_file = os.path.join(dir_path, 'keypoint_mesh2img.py')

    # the tmp file for keypoint image projection
    image_keypoint_tempfile = 'scene_bbox_keypoint_tmp.yaml'

    # Get the root keypoint
    log_root: str = args.log_root_path
    processed_counter = 0
    skip_counter = 0
    for folder in os.listdir(log_root):
        scene_log_path = os.path.join(log_root, folder)
        scene_processed_path = os.path.join(scene_log_path, 'processed')
        if os.path.isdir(scene_log_path):
            keypoint_yaml_path = os.path.join(scene_processed_path, args.scene_mesh_keypoint_path)
            output_yaml_path = os.path.join(scene_processed_path, image_keypoint_tempfile)
            if not os.path.exists(keypoint_yaml_path):
                print('Keypoint in %s is not founded!' % scene_log_path)
                skip_counter += 1
                continue

            # Run the python command
            command = 'python %s --keypoint_yaml_path %s --scene_log_path %s --output_yaml_path %s' \
                      % (keypoint_exe_file, keypoint_yaml_path, scene_log_path, output_yaml_path)
            print(command)
            os.system(command)
            processed_counter += 1

    # Also annotate the bounding box
    bbox_command = 'python %s --log_root_path %s --pose_yaml_filename %s --output_yaml_name %s' \
                   % (bbox_exe_file, log_root, image_keypoint_tempfile, args.scene_image_keypoint_output_path)
    print(bbox_command)
    os.system(bbox_command)

    # The cleanup
    for folder in os.listdir(log_root):
        scene_log_path = os.path.join(log_root, folder)
        scene_processed_path = os.path.join(scene_log_path, 'processed')
        if os.path.isdir(scene_log_path):
            tmp_yaml_path = os.path.join(scene_processed_path, image_keypoint_tempfile)
            if os.path.exists(tmp_yaml_path):
                os.remove(tmp_yaml_path)

    # Some info
    print('Processed logs (scenes) %d, while %d scene does not have keypoint.' % (processed_counter, skip_counter))

if __name__ == '__main__':
    main()


# initial_search_query.py
# By Anton A Rakos

# Can be ran with python3.7 initial_search_query.py

# This script houses the functions for processing an initial search query.

class Tokenize_search_query:

	def __init__(self, given_input):
		self.input_query = given_input
		self.all_links = []
		self.basic_search = []
		self.decoy1_search = []
		self.decoy2_search = []
		self.start_search_query()
		self.write_binary()

	def get_decoys(self, target):
		decoy1_search = []
		import wikipedia
		while len(decoy1_search) < target:
			try:
				decoy1 = wikipedia.page(wikipedia.random())
				decoy1_query = wikipedia.search(decoy1)

				for i in range(len(decoy1_query)):
					if len(decoy1_search) >= target:

						break
					else:
						try:
							member0 = wikipedia.page(decoy1_query[i])
							member0_list = member0.references
							for j in range(len(member0_list)):
								decoy1_search += [['00', member0_list[j]]]
								if len(decoy1_search) >= target:
									break

						except:
							continue

			except:
				continue

		return decoy1_search

	def start_search_query(self):
		import wikipedia

		wikipedia.set_lang("en")
		print(self.input_query)
		basic_query =  wikipedia.search(self.input_query)
		print(len(basic_query))
		print(basic_query)
		for i in range(len(basic_query)):
			if len(self.basic_search) >= 256:
				break
			try:
				member0 = wikipedia.page(basic_query[i])
				member0_list = member0.references
				go_0 = 1
				go_1 = 0
				go_2 = 0
				go_3 = 0
				go_4 = 0
				go_5 = 0
				go_6 = 0
				go_7 = 0
				go_8 = 0
				go_9 = 0
				go_10 = 0
				go_11 = 0
				go_12 = 0
				go_13 = 0
				go_14 = 0
				go_15 = 0
				for j in range(len(member0_list)):
					if(go_0 == 1):
						self.basic_search += [['01', member0_list[j]]]
						go_0 = 0
						go_1 = 1
					elif(go_1 == 1):
						self.basic_search += [['02', member0_list[j]]]
						go_1 = 0
						go_2 = 1
					elif(go_2 == 1):
						self.basic_search += [['03', member0_list[j]]]
						go_2 = 0
						go_3 = 1
					elif(go_3 == 1):
						self.basic_search += [['04', member0_list[j]]]
						go_3 = 0
						go_4 = 1
					elif(go_4 == 1):
						self.basic_search += [['05', member0_list[j]]]
						go_4 = 0
						go_5 = 1
					elif(go_5 == 1):
						self.basic_search += [['06', member0_list[j]]]
						go_5 = 0
						go_6 = 1
					elif(go_6 == 1):
						self.basic_search += [['07', member0_list[j]]]
						go_6 = 0
						go_7 = 1
					elif(go_7 == 1):
						self.basic_search += [['08', member0_list[j]]]
						go_7 = 0
						go_8 = 1
					elif(go_8 == 1):
						self.basic_search += [['09', member0_list[j]]]
						go_8 = 0
						go_9 = 1
					elif(go_9 == 1):
						self.basic_search += [['10', member0_list[j]]]
						go_9 = 0
						go_10 = 1
					elif(go_10 == 1):
						self.basic_search += [['11', member0_list[j]]]
						go_10 = 0
						go_11 = 1
					elif(go_11 == 1):
						self.basic_search += [['12', member0_list[j]]]
						go_11 = 0
						go_12 = 1
					elif(go_12 == 1):
						self.basic_search += [['13', member0_list[j]]]
						go_12 = 0
						go_13 = 1
					elif(go_13 == 1):
						self.basic_search += [['14', member0_list[j]]]
						go_13 = 0
						go_14 = 1
					elif(go_14 == 1):
						self.basic_search += [['15', member0_list[j]]]
						go_14 = 0
						go_15 = 1
					elif(go_15 == 1):
						self.basic_search += [['16', member0_list[j]]]
						go_15 = 0
						go_0 = 1

					if len(self.basic_search) >= 256:
						break
			except:
				continue

		self.decoy1_search = self.get_decoys(len(self.basic_search))
		self.decoy2_search = self.get_decoys(len(self.basic_search))

		print(self.basic_search)
		print(len(self.decoy1_search))
		print(len(self.decoy2_search))

	def write_binary(self):
		output_file = open("search_domain.txt", "w")

		for i in range(len(self.basic_search)):
			output_file.write(self.basic_search[i][0]+self.basic_search[i][1]+'\n')
			output_file.write(self.decoy1_search[i][0]+self.decoy1_search[i][1]+'\n')
			output_file.write(self.decoy2_search[i][0]+self.decoy2_search[i][1]+'\n')

		output_file.close()

import sys
import os

search_list = sys.argv[1:]
search_this = ' '.join(map(str,search_list ))
app = Tokenize_search_query(search_this)
os.system("rm logged*")
os.system("./runthisthread")
os.system("./runeval '"+search_this+"'")


from datafs import DataAPI
from datafs.core.data_archive import DataArchive
from datafs.managers.manager_dynamo import DynamoDBManager
from fs.s3fs import S3FS
from fs.osfs import OSFS

import tempfile
import shutil

tmpdir = tempfile.mkdtemp()

class MyArchive(DataArchive):

    def delete(self):
        raise IOError('Data archives cannot be deleted')

class MyAPI(DataAPI):

    _ArchiveConstructor = MyArchive
    '''
    ``MyArchive`` objects will be created instead of ``DataArchive`` objects
    '''

    def __init__(self, AWS_ACCESS_KEY, AWS_SECRET_KEY, *args, **kwargs):
        super(MyAPI, self).__init__(*args, **kwargs)

        # pre-configure the API with your organization's setup

        manager = DynamoDBManager(
            table_name='project_data',
            session_args={
                'aws_access_key_id': AWS_ACCESS_KEY,
                'aws_secret_access_key': AWS_SECRET_KEY},
            resource_args={
                'endpoint_url': 'http://localhost:8000/',
                'region_name': 'us-east-1'})

        if 'project_data' in manager.table_names:
            manager.delete_table('project_data')

        manager.create_archive_table('project_data', raise_on_err=False)

        self.attach_manager(manager)

        # Prevent changes to the manager configuration
        self.lock_manager()

        s3_bucket1 = S3FS(
            'org-bucket-1',
            aws_access_key=AWS_ACCESS_KEY,
            aws_secret_key=AWS_SECRET_KEY)

        s3_bucket2 = S3FS(
            'org-bucket-2',
            aws_access_key=AWS_ACCESS_KEY,
            aws_secret_key=AWS_SECRET_KEY)

        network_storage = OSFS(tmpdir)

        self.attach_authority('s3-1', s3_bucket1)
        self.attach_authority('s3-2', s3_bucket2)
        self.attach_authority('NAT-1', network_storage)

        # Prevent changes to the set of authorities
        self.lock_authorities()

def teardown():
    shutil.rmtree(tmpdir)


import pytest

from .. import tasks
from ..models import ReleaseNote

pytestmark = pytest.mark.django_db(transaction=True)

def test_update_task(rn_testdata):
    initial_count = ReleaseNote.objects.count()
    assert initial_count == 52
    tasks.check_if_release_note_update_needed(
        override_file="looking_for_group/releasenotes/tests/test_changelog_updated.rst"
    )
    assert ReleaseNote.objects.count() - initial_count == 1


from tkinter import*

top=Tk()
top.title("E-commerce")
count=0
count1=0

def addrec():

    f=open('dbs.txt','a')

    ItemName=s1.get()

    ItemNo=s2.get()

    Price=s3.get()

    MfgDate=s4.get()

    ExpDate=s5.get()

    f.writelines(ItemNo.ljust(20)+ItemNo.ljust(10)+Price.ljust(30)+ MfgDate.ljust(15)+ExpDate.ljust(15)+"\n")
    global count
    global count1

    f.close()
    s1.set("")
    s2.set("")
    s3.set("")
    s4.set("")
    s5.set("")

def nextrec():

    i=0

    f=open('dbs.txt','r')

    global count

    while(i<=count):

        l=f.readline()

        i=i+1

    l1=l.split()

    # print(l1[0],l1[1])	# If we want to print on console screen

    s1.set(l1[0])

    s2.set(l1[1])

    s3.set(l1[2])

    s4.set(l1[3])

    s5.set(l1[4])

    count=count+1

    f.close()

def prev():

    f=open('dbs.txt','r')

    i=0

    global count

    count=count-1

    while(i<=count-1):

        i=i+1

    l=f.readline(i-1)

    l5=l.split()

    # print(l1[0],l1[1])	# If we want to print on console screen

    s1.set(l5[0])

    s2.set(l5[1])

    s3.set(l5[2])

    s4.set(l5[3])

    s5.set(l5[4])

    f.close()

def delete():

    k=[s1.get(), s2.get(), s3.get(), s4.get(), s5.get()]

    f=open('dbs.txt',"r")

    h=f.readlines()

    f.close()

    f=open('dbs.txt',"w")

    for i in h:

        j=i.split()

        if(j!=k):

            f.writelines(j[0].ljust(20)+j[1].ljust(10)+j[2].ljust(30)+j[3].ljust(15)+j[4].ljust(15)+"\n")

    f.close()

def search():
  try:

    k=s1.get()

    f=open('dbs.txt',"r")

    h=f.readlines()

    for i in h:

        j=i.split()

        if(j[0]==k):

            s1.set(j[0])

            s2.set(j[1])

            s3.set(j[2])

            s4.set(j[3])

            s5.set(j[4])
  except:
    print( 'no such entry')
    f.close()

def update():
  try:

    ItemName=s1.get()

    ItemNo=s2.get()

    Price=s3.get()

    MgfDate=s4.get()

    ExpDate=s5.get()

    f=open("dbs.txt","r")

    h=f.readlines()
  except:

    print('no entry for updation')

    f.close()

    f=open("dbs.txt","w")

    flag=0

    for i in h:

        j=i.split()

        if(j[0]!=brand):

            f.writelines(j[0].ljust(20)+j[1].ljust(10)+j[2].ljust(30)+j[3].ljust(15)+j[4].ljust(15)+"\n")

        else :

            f.writelines(ItemName.ljust(20)+ItemNo.ljust(10)+Price.ljust(30)+MfgDate.ljust(15)+ExpDate.ljust(15)+"\n")

            flag=1

    f.close()

def lr():
  try:

    f=open('dbs.txt','r')

    a=0

    b=0

    for i in f:

        a=a+1

    f.seek(0)

    h=f.readlines()

    l=list(h)

    l1=l[a-1].split()

    s1.set(l1[0])

    s2.set(l1[1])

    s3.set(l1[2])

    s4.set(l1[3])

    s5.set(l1[4])

  except:

    print('no such record')
    f.close()

def fr():

    f=open('dbs.txt','r')

    a=0

    b=0

    for i in f:

        a=a+1

    f.seek(0)

    h=f.readlines()

    l=list(h)

    l1=l[0].split()

    s1.set(l1[0])

    s2.set(l1[1])

    s3.set(l1[2])

    s4.set(l1[3])

    s5.set(l1[4])

    f.close()

s1=StringVar()

s2=StringVar()

s3=StringVar()

s4=StringVar()

s5=StringVar()

l1=Label(top,text="ItemName")
l2=Label(top,text="ItemNo")
l3=Label(top,text ="Price")
l4=Label(top,text="MfgDate")
l5=Label(top,text="ExpDate")
t1=Entry(top,textvariable=s1)

t2=Entry(top,textvariable=s2)

t3=Entry(top,textvariable=s3)

t4=Entry(top,textvariable=s4)

t5=Entry(top,textvariable=s5)
b1=Button(top,text="Next", command=nextrec)

b2=Button(top,text="Add", command=addrec)

b3=Button(top,text="Delete", command=delete)

b4=Button(top,text="Search", command=search)

b5=Button(top,text="Update", command=update)

b7=Button(top,text="Last Record", command=lr)

b6=Button(top,text="First Record", command=fr)

b8=Button(top,text="Previous", command=prev)

l1.grid(row=2,column=1)

l2.grid(row=3,column=1)

l3.grid(row=4,column=1)

l4.grid(row=5,column=1)

l5.grid(row=6,column=1)

t1.grid(row=2,column=2)

t2.grid(row=3,column=2)

t3.grid(row=4,column=2)

t4.grid(row=5,column=2)

t5.grid(row=6,column=2)

b1.grid(row=12,column=2)

b2.grid(row=11,column=1)

b3.grid(row=11,column=2)

b4.grid(row=8,column=4)

b5.grid(row=11,column=4)

b6.grid(row=8,column=1)

b7.grid(row=8,column=2)

b8.grid(row=12,column=1)

top.mainloop()


from EBT3 import *
d = Calibrate('202105_UoB_Microbeam/Films/','Calibration/ROI')
d.load('Calibration/Calibration_UoB')
f = Fitting(d.Data)
f.fit()
OD = d.OD(6, roi=0)
Dose = f.coeff[0]*(OD**2)+f.coeff[1]*(OD)+f.coeff[2]
fig = plt.subplots()
plt.imshow(Dose)
plt.colorbar()
plt.show()


# A Treap (portmanteau of Tree and Heap) is
# a self-balancing binary search tree. The idea
#is to use randomization and the Binary Heap
# property to maintain balance with high
# probability. The expected time complexity of
# search, insert and delete is O(log n).
#
# Every node of a Treap maintains two values.
# 1) Key: follows standard BST ordering (left
# is smaller and right is greater)
# 2) Priority: randomly assigned value that
# follows the Max-Heap property (each node
# has a key that is less than or equal to
# the key of its parent. Rotations are used
# after insertion and deletion to ensure that
# the Treap maintains the Max heap property.

# randomized second attribute of Treap
import random

# Node class for representing a node in the Treap
class Node:
    def __init__(self, key=None):
        self.key = key
        self.priority = random.randint(1, 100)
        self.left = None
        self.right = None

# Function to rotate right at a given node
def rotate_right(root):
    new_root = root.left
    root.left = new_root.right
    new_root.right = root
    return new_root

# Function to rotate left at a given node
def rotate_left(root):
    new_root = root.right
    root.right = new_root.left
    new_root.left = root
    return new_root

# Function to search for a key in the Treap
def search(root, key):
    if root is None or root.key == key:
        return root

    if key < root.key:
        return search(root.left, key)

    return search(root.right, key)

# Function to insert a key into the Treap
def insert(root, key):
    if root is None:
        return Node(key)

    if key < root.key:
        root.left = insert(root.left, key)

        if root.left.priority > root.priority:
            root = rotate_right(root)
    else:
        root.right = insert(root.right, key)

        if root.right.priority > root.priority:
            root = rotate_left(root)

    return root

# Function to delete a key from the Treap
def delete(root, key):
    if root is None:
        return None

    if key < root.key:
        root.left = delete(root.left, key)
    elif key > root.key:
        root.right = delete(root.right, key)
    else:
        if root.left is None:
            temp = root.right
            root = None
            return temp
        elif root.right is None:
            temp = root.left
            root = None
            return temp

        if root.left.priority < root.right.priority:
            root = rotate_left(root)
            root.left = delete(root.left, key)
        else:
            root = rotate_right(root)
            root.right = delete(root.right, key)

    return root

# Utility function to print the Treap in inorder traversal
def inorder(root):
    if root:
        inorder(root.left)
        print(root.key, end=' ')
        inorder(root.right)

# Test the Treap implementation
if __name__ == '__main__':
    root = None

    root = insert(root, 20)
    root = insert(root, 10)
    root = insert(root, 30)
    root = insert(root, 5)
    root = insert(root, 15)
    root = insert(root, 25)
    root = insert(root, 35)

    print("Inorder traversal after insertions:")
    inorder(root)
    print("\n")

    root = delete(root, 10)
    root = delete(root, 30)

    print("Inorder traversal after deletions:")
    inorder(root)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import scipy
import colorsys
import random
import torchvision.models as models
from model.utils.config import cfg
#from model.roi_crop.functions.roi_crop import RoICropFunction
import cv2
import pdb

def save_net(fname, net):
    import h5py
    h5f = h5py.File(fname, mode='w')
    for k, v in net.state_dict().items():
        h5f.create_dataset(k, data=v.cpu().numpy())

def load_net(fname, net):
    import h5py
    h5f = h5py.File(fname, mode='r')
    for k, v in net.state_dict().items():
        param = torch.from_numpy(np.asarray(h5f[k]))
        v.copy_(param)

def weights_normal_init(model, dev=0.01):
    if isinstance(model, list):
        for m in model:
            weights_normal_init(m, dev)
    else:
        for m in model.modules():
            if isinstance(m, nn.Conv2d):
                m.weight.data.normal_(0.0, dev)
            elif isinstance(m, nn.Linear):
                m.weight.data.normal_(0.0, dev)

def clip_gradient(model, clip_norm):
    """Computes a gradient clipping coefficient based on gradient norm."""
    totalnorm = 0
    for p in model.parameters():
        if p.requires_grad:
            modulenorm = p.grad.data.norm()
            totalnorm += modulenorm ** 2
    totalnorm = np.sqrt(totalnorm)

    norm = clip_norm / max(totalnorm, clip_norm)
    for p in model.parameters():
        if p.requires_grad:
            p.grad.mul_(norm)

def vis_detections(im, class_name, dets, thresh=0.8):
    """Visual debugging of detections.
    第一个参数：img是原图
    第二个参数：（x，y）是矩阵的左上点坐标
    第三个参数：（x+w，y+h）是矩阵的右下点坐标
    第四个参数：（0,255,0）是画线对应的rgb颜色
    第五个参数：2是所画的线的宽度
    """
    # for i in range(np.minimum(10, dets.shape[0])):
    for i in range(dets.shape[0]):
        bbox = tuple(int(np.round(x)) for x in dets[i, :4])
        score = dets[i, -1]
        if score > thresh:
            if class_name == "star":
                cv2.rectangle(im, bbox[0:2], bbox[2:4], (0, 204, 0), 1)
                # cv2.putText(im, '%s: %.3f' % (class_name, score), (bbox[0], bbox[1] + 15), cv2.FONT_HERSHEY_PLAIN,
                #                 #             1.0, (0, 204, 0), thickness=1)
            else:
                cv2.rectangle(im, bbox[0:2], bbox[2:4], (0, 0, 255), 1)
                # cv2.putText(im, '%s: %.3f' % (class_name, score), (bbox[0], bbox[1] + 15), cv2.FONT_HERSHEY_PLAIN,
                #             1.0, (0, 0, 255), thickness=1)
    return im,all

# Borrow from matterport mask R-CNN implementation
def unmold_mask(mask, bbox, image_shape):
    """Converts a mask generated by the neural network into a format similar
    to it's original shape.
    mask: [height, width] of type float. A small, typically 28x28 mask.
    bbox: [y1, x1, y2, x2]. The box to fit the mask in.
    Returns a binary mask with the same size as the original image.
    """
    threshold = 0.1
    x1, y1, x2, y2 = bbox
    if x2 == x1 or y2==y1:
        return np.zeros(image_shape[:2], dtype=np.uint8)

    mask = scipy.misc.imresize(
        mask, (y2 - y1, x2 - x1), interp='bilinear').astype(np.float32) / 255.0
    mask = np.where(mask >= threshold, 1, 0).astype(np.uint8)

    # Put the mask in the right location.
    full_mask = np.zeros(image_shape[:2], dtype=np.uint8)
    full_mask[y1:y2, x1:x2] = mask
    return full_mask

# Borrowed from matterport mask R-CNN implementation

def apply_mask(image, mask, nomask, color, alpha=0.5):
    """Apply the given mask to the image.
    """
    mask[nomask != 0] = 0
    for c in range(3):
        image[:, :, c] = np.where(mask == 1,
                                  image[:, :, c] *
                                  (1 - alpha) + alpha * color[c] * 255,
                                  image[:, :, c])
    return image

def random_colors(N, bright=True):

    """
    Generate random colors.
    To get visually distinct colors, generate them in HSV space then
    convert to RGB.
    """
    brightness = 1.0 if bright else 0.7
    hsv = np.random.rand(N, 3)
    hsv[:, 1] = 1
    hsv[:, 2] = brightness
    # hsv = [(i / N, 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    random.shuffle(colors)
    return colors

def vis_det_and_mask(im, class_name, dets, masks, thresh=0.8):
    """Visual debugging of detections."""
    num_dets = np.minimum(10, dets.shape[0])
    colors_mask = random_colors(num_dets)
    colors_bbox = np.round(np.random.rand(num_dets, 3) * 255)
    # sort rois according to the coordinates, draw upper bbox first
    draw_mask = np.zeros(im.shape[:2], dtype=np.uint8)

    for i in range(1):
        bbox = tuple(int(np.round(x)) for x in dets[i, :4])
        mask = masks[i, :, :]
        full_mask = unmold_mask(mask, bbox, im.shape)

        score = dets[i, -1]
        if score > thresh:
            word_width = len(class_name)
            cv2.rectangle(im, bbox[0:2], bbox[2:4], colors_bbox[i], 2)
            cv2.rectangle(im, bbox[0:2], (bbox[0] + 18 + word_width*8, bbox[1]+15), colors_bbox[i], thickness=cv2.FILLED)
            apply_mask(im, full_mask, draw_mask, colors_mask[i], 0.5)
            draw_mask += full_mask
            cv2.putText(im, '%s' % (class_name), (bbox[0]+5, bbox[1] + 12), cv2.FONT_HERSHEY_PLAIN,
								1.0, (255,255,255), thickness=1)
    return im

def adjust_learning_rate(optimizer, decay=0.1):
    """Sets the learning rate to the initial LR decayed by 0.5 every 20 epochs"""
    for param_group in optimizer.param_groups:
        param_group['lr'] = decay * param_group['lr']

def save_checkpoint(state, filename):
    torch.save(state, filename)

def _smooth_l1_loss(bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights, sigma=1.0, dim=[1]):

    sigma_2 = sigma ** 2
    box_diff = bbox_pred - bbox_targets
    in_box_diff = bbox_inside_weights * box_diff
    abs_in_box_diff = torch.abs(in_box_diff)
    smoothL1_sign = (abs_in_box_diff < 1. / sigma_2).detach().float()
    in_loss_box = torch.pow(in_box_diff, 2) * (sigma_2 / 2.) * smoothL1_sign \
                  + (abs_in_box_diff - (0.5 / sigma_2)) * (1. - smoothL1_sign)
    out_loss_box = bbox_outside_weights * in_loss_box
    loss_box = out_loss_box

    s = loss_box.size(0)
    loss_box = loss_box.view(s, -1).sum(1).mean()
    # for i in sorted(dim, reverse=True):
    #   loss_box = loss_box.sum(i)
    # loss_box = loss_box.mean()
    return loss_box

def _crop_pool_layer(bottom, rois, max_pool=True):
    # code modified from
    # https://github.com/ruotianluo/pytorch-faster-rcnn
    # implement it using stn
    # box to affine
    # input (x1,y1,x2,y2)
    """
    [  x2-x1             x1 + x2 - W + 1  ]
    [  -----      0      ---------------  ]
    [  W - 1                  W - 1       ]
    [                                     ]
    [           y2-y1    y1 + y2 - H + 1  ]
    [    0      -----    ---------------  ]
    [           H - 1         H - 1      ]
    """
    rois = rois.detach()
    batch_size = bottom.size(0)
    D = bottom.size(1)
    H = bottom.size(2)
    W = bottom.size(3)
    roi_per_batch = rois.size(0) / batch_size
    x1 = rois[:, 1::4] / 16.0
    y1 = rois[:, 2::4] / 16.0
    x2 = rois[:, 3::4] / 16.0
    y2 = rois[:, 4::4] / 16.0

    height = bottom.size(2)
    width = bottom.size(3)

    # affine theta
    zero = Variable(rois.data.new(rois.size(0), 1).zero_())
    theta = torch.cat([\
      (x2 - x1) / (width - 1),
      zero,
      (x1 + x2 - width + 1) / (width - 1),
      zero,
      (y2 - y1) / (height - 1),
      (y1 + y2 - height + 1) / (height - 1)], 1).view(-1, 2, 3)

    if max_pool:
      pre_pool_size = cfg.POOLING_SIZE * 2
      grid = F.affine_grid(theta, torch.Size((rois.size(0), 1, pre_pool_size, pre_pool_size)))
      bottom = bottom.view(1, batch_size, D, H, W).contiguous().expand(roi_per_batch, batch_size, D, H, W)\
                                                                .contiguous().view(-1, D, H, W)
      crops = F.grid_sample(bottom, grid)
      crops = F.max_pool2d(crops, 2, 2)
    else:
      grid = F.affine_grid(theta, torch.Size((rois.size(0), 1, cfg.POOLING_SIZE, cfg.POOLING_SIZE)))
      bottom = bottom.view(1, batch_size, D, H, W).contiguous().expand(roi_per_batch, batch_size, D, H, W)\
                                                                .contiguous().view(-1, D, H, W)
      crops = F.grid_sample(bottom, grid)

    return crops, grid

def _affine_grid_gen(rois, input_size, grid_size):

    rois = rois.detach()
    x1 = rois[:, 1::4] / 16.0
    y1 = rois[:, 2::4] / 16.0
    x2 = rois[:, 3::4] / 16.0
    y2 = rois[:, 4::4] / 16.0

    height = input_size[0]
    width = input_size[1]

    zero = Variable(rois.data.new(rois.size(0), 1).zero_())
    theta = torch.cat([\
      (x2 - x1) / (width - 1),
      zero,
      (x1 + x2 - width + 1) / (width - 1),
      zero,
      (y2 - y1) / (height - 1),
      (y1 + y2 - height + 1) / (height - 1)], 1).view(-1, 2, 3)

    grid = F.affine_grid(theta, torch.Size((rois.size(0), 1, grid_size, grid_size)))

    return grid

def _affine_theta(rois, input_size):

    rois = rois.detach()
    x1 = rois[:, 1::4] / 16.0
    y1 = rois[:, 2::4] / 16.0
    x2 = rois[:, 3::4] / 16.0
    y2 = rois[:, 4::4] / 16.0

    height = input_size[0]
    width = input_size[1]

    zero = Variable(rois.data.new(rois.size(0), 1).zero_())

    theta = torch.cat([\
      (y2 - y1) / (height - 1),
      zero,
      (y1 + y2 - height + 1) / (height - 1),
      zero,
      (x2 - x1) / (width - 1),
      (x1 + x2 - width + 1) / (width - 1)], 1).view(-1, 2, 3)

    return theta

def compare_grid_sample():
    # do gradcheck
    N = random.randint(1, 8)
    C = 2 # random.randint(1, 8)
    H = 5 # random.randint(1, 8)
    W = 4 # random.randint(1, 8)
    input = Variable(torch.randn(N, C, H, W).cuda(), requires_grad=True)
    input_p = input.clone().data.contiguous()

    grid = Variable(torch.randn(N, H, W, 2).cuda(), requires_grad=True)
    grid_clone = grid.clone().contiguous()

    out_offcial = F.grid_sample(input, grid)
    grad_outputs = Variable(torch.rand(out_offcial.size()).cuda())
    grad_outputs_clone = grad_outputs.clone().contiguous()
    grad_inputs = torch.autograd.grad(out_offcial, (input, grid), grad_outputs.contiguous())
    grad_input_off = grad_inputs[0]

    crf = RoICropFunction()
    grid_yx = torch.stack([grid_clone.data[:,:,:,1], grid_clone.data[:,:,:,0]], 3).contiguous().cuda()
    out_stn = crf.forward(input_p, grid_yx)
    grad_inputs = crf.backward(grad_outputs_clone.data)
    grad_input_stn = grad_inputs[0]
    pdb.set_trace()

    delta = (grad_input_off.data - grad_input_stn).sum()

##  add by lq
class FocalLoss(nn.Module):
    r"""
        This criterion is a implemenation of Focal Loss, which is proposed in
        Focal Loss for Dense Object Detection.

            Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])

        The losses are averaged across observations for each minibatch.
        Args:
            alpha(1D Tensor, Variable) : the scalar factor for this criterion
            gamma(float, double) : gamma > 0; reduces the relative loss for well-classiﬁed examples (p > .5),
                                   putting more focus on hard, misclassiﬁed examples
            size_average(bool): size_average(bool): By default, the losses are averaged over observations for each minibatch.
                                However, if the field size_average is set to False, the losses are
                                instead summed for each minibatch.
    """

    def __init__(self, class_num, alpha=0.25, gamma=2, size_average=True):
        super(FocalLoss, self).__init__()
        if alpha is None:
            self.alpha = Variable(torch.ones(class_num, 1))
        else:
            if isinstance(alpha, Variable):
                self.alpha = alpha
            else:
                alpha = np.array([alpha,alpha,alpha],dtype= float).reshape(class_num,1)
                alpha = torch.DoubleTensor(alpha)
                self.alpha = Variable(alpha)
        self.gamma = gamma
        self.class_num = class_num
        self.size_average = size_average

    def forward(self, inputs, targets):
        N = inputs.size(0)
        C = inputs.size(1)
        P = F.softmax(inputs, dim=1)

        class_mask = inputs.data.new(N, C).fill_(0)
        class_mask = Variable(class_mask)
        ids = targets.view(-1, 1)
        class_mask.scatter_(1, ids.data, 1.)
        # print(class_mask)

        if inputs.is_cuda and not self.alpha.is_cuda:
            self.alpha = self.alpha.cuda()
        alpha = self.alpha[ids.data.view(-1)]
        probs = (P * class_mask).sum(1).view(-1, 1).type(torch.DoubleTensor).cuda()
        log_p = probs.log().type(torch.DoubleTensor).cuda()

        batch_loss = -alpha * (torch.pow((1 - probs), self.gamma)) * log_p

        if self.size_average:
            loss = batch_loss.mean()
        else:
            loss = batch_loss.sum()
        return loss