from django.contrib.contenttypes.models import ContentType
from django.core.paginator import Paginator
from django.db import transaction
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.utils.html import escape
from django.views import View
from django.views.generic import TemplateView

from homeland_party.const import MAP_WIDTH_PX, MAP_HEIGHT_PX, YANDEX_API_KEY
from homeland_party.mixins import CustomTemplateViewMixin
from personal_cabinet.models.models import Profile
from veche.mixins import InitiativeViewMixis
from veche.models import Community, CommunityRequest, RequestStats, RequestResolution, Initiative, MessageInitiative, \
    ResolutionInitiative

class MainVecheView(CustomTemplateViewMixin, TemplateView):
    template_name = 'veche_main.html'

class GeoTenView(CustomTemplateViewMixin, TemplateView):

    def get(self, request, *args, **kwargs):
        user = request.user
        profile = self._get_profile()
        if not profile:
            return HttpResponse('Отсутствует профиль пользователя', status=400)

        geo = profile.get_geo()
        context = self.get_context_data()
        extra_context = {
            'geo': geo,
            'address_text': str(geo) if geo else '',
        }
        context.update(extra_context)
        return render(request, 'veche_geo_ten.html', context=context)

    def post(self, request, *args, **kwargs):
        user = request.user
        profile = self._get_profile()
        if not profile:
            return HttpResponse('Отсутствует профиль пользователя', status=400)

        user_can_create_geo_community = profile.user_can_create_geo_community()
        if user_can_create_geo_community:
            community_data = {
                'author': user,
                'type': Community.COMMUNITY_TYPE_TEN_KEY,
                'max_participants': Community.MAX_PARTICIPANTS[Community.COMMUNITY_TYPE_TEN_KEY],
            }
            community = Community.objects.create(**community_data)
            community_geo = profile.get_geo()
            community_geo.pk = None
            community_geo.object_type = ContentType.objects.get_for_model(Community)
            community_geo.object_id = community.pk
            community_geo.save()
            profile.geo_community.add(community)
            profile.save()

            redirect_url = request.build_absolute_uri(reverse('veche:my_geo_ten'))
            data = {'redirect_url': redirect_url}
            response = JsonResponse(data, status=200)
        else:
            response = HttpResponse('Вы не можете создать географическую десятку', status=400)
        return response

class JoinGeoTenView(CustomTemplateViewMixin, TemplateView):

    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        if context['user_has_not_geo_community_request']:
            profile = self._get_profile()
            if not profile:
                return HttpResponse('Отсутствует профиль пользователя', status=400)

            geo = profile.get_geo()
            geo_tens_for_join_qs = Community.get_geo_tens_for_join()
            extra_context = {
                'geo': geo,
                'geo_tens_for_join_qs': geo_tens_for_join_qs,
                'map_width': MAP_WIDTH_PX,
                'map_height': MAP_HEIGHT_PX,
            }
            context.update(extra_context)
            response = render(request, 'veche_join_geo_ten.html', context=context)
        else:
            response = HttpResponse('Вы уже подали заявку на вступление в географическую десятку', status=400)
        return response

    def post(self, request, geo_ten_id):
        context = self.get_context_data()
        if context['user_has_not_geo_community_request']:
            geo_ten = Community.objects.filter(pk=geo_ten_id).first()
            if geo_ten:
                is_geo_ten_open = geo_ten.is_geo_ten_open()
                if is_geo_ten_open:
                    comment = escape(request.POST.get('comment'))[:1000]
                    CommunityRequest.objects.create(author=request.user, community=geo_ten, comment=comment)
                    response = JsonResponse({}, status=200)
                else:
                    response = JsonResponse({'message': 'Географическая десятка уже укомплектована'}, status=400)
            else:
                response = JsonResponse({'message': 'Заявка подана на несуществующую географическую десятку'},
                                        status=400)
        else:
            response = JsonResponse({'message': 'Вы уже подали заявку на вступление в географическую десятку'},
                                    status=400)
        return response

class GeoCommunityParticipiants(CustomTemplateViewMixin, View):
    def get(self, request, *args, **kwargs):
        try:
            geo_community = Community.objects.get(pk=request.GET.get('geo_community_id'))
        except Exception:
            result = []
            status = 400
        else:
            profiles_qs = geo_community.get_profiles_qs()
            if profiles_qs.exists():
                result = []
                for profile in profiles_qs:
                    card_data = profile.get_card_data()
                    data = {
                        'user_name': card_data['user_name'],
                        'first_name': card_data['first_name'],
                        'last_name': card_data['last_name'],
                        'profile_id': profile.pk,
                        'user_id': profile.user.pk,
                    }
                    result.append(data)
                status = 200
            else:
                result = []
                status = 400

        return JsonResponse(result, status=status, safe=False)

class MyGeoTenView(CustomTemplateViewMixin, TemplateView):

    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        profile = self._get_profile()
        community = profile.get_first_geo_ten_community()
        profiles = community.get_profiles_qs() if community else []
        extra_context = {
            'community': community,
            'profiles': profiles,
        }
        context.update(extra_context)
        return render(request, 'veche_my_geo_ten.html', context=context)

class UserCardView(CustomTemplateViewMixin, TemplateView):

    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        response = render(request, 'veche_user_card.html', context=context)
        return response

class MyRequestsView(CustomTemplateViewMixin, TemplateView):
    AGREE_URL_ALIAS = 'agree_request'
    REJECT_URL_ALIAS = 'reject_request'

    def get(self, request, *args, **kwargs):
        profile = self._get_profile()
        if not profile:
            return HttpResponse('Отсутствует профиль пользователя', status=400)

        requests_user_need_to_approve = profile.get_requests_user_need_to_approve()
        created_by_user_community_request = profile.get_created_by_user_community_request()
        created_by_user_community_request_solved = profile.get_created_by_user_community_request_solved()
        user_resolutions = profile.get_user_resolutions()
        if created_by_user_community_request:
            created_by_user_community_request_stats = created_by_user_community_request.get_request_stats()
        else:
            created_by_user_community_request_stats = RequestStats(0, 0, 0)
        context = self.get_context_data()
        extra_context = {
            'requests_user_need_to_approve': requests_user_need_to_approve,
            'created_by_user_community_request': created_by_user_community_request,
            'created_by_user_community_request_stats': created_by_user_community_request_stats,
            'created_by_user_community_request_solved': created_by_user_community_request_solved,
            'user_resolutions': user_resolutions,
        }
        context.update(extra_context)
        return render(request, 'veche_my_requests.html', context=context)

    def post(self, request):
        user = request.user
        profile = self._get_profile()
        if not profile:
            return JsonResponse({'message': 'Отсутствует профиль пользователя'}, status=400)

        request_id = request.POST.get('request_id')
        try:
            request_id = int(request_id)
        except Exception:
            response = JsonResponse({'message': 'Неверный идентификатор заявки'}, status=400)
        else:
            if request_id:
                try:
                    community_request = CommunityRequest.objects.get(pk=request_id)
                except Exception:
                    response = JsonResponse({'message': 'Нельзя согласовать заявку, т.к. переданы неверные данные'},
                                            status=400)
                else:
                    is_it_my_request_id = profile.is_it_my_request(some_request=community_request)
                    if is_it_my_request_id:
                        did_user_resolve_request = profile.did_user_resolve_request(community_request=community_request)

                        if did_user_resolve_request:
                            response = JsonResponse({'message': 'Данная заявка вами уже согласована'}, status=400)
                        else:
                            if community_request.community.is_geo_ten_open():
                                current_url_alias = request.resolver_match.url_name
                                if current_url_alias == self.AGREE_URL_ALIAS:
                                    self._agree_request(community_request=community_request, user=user)
                                if current_url_alias == self.REJECT_URL_ALIAS:
                                    self._reject_request(community_request=community_request, user=user)
                                response = JsonResponse({'message': 'Заявка успешно обработана'}, status=200)

                                if current_url_alias not in [self.AGREE_URL_ALIAS, self.REJECT_URL_ALIAS]:
                                    response = JsonResponse({'message': 'Неверный запрос'}, status=400)
                            else:
                                response = JsonResponse({'message': 'Десятка уже укомплектована'}, status=400)
                    else:
                        response = JsonResponse({'message': 'Нельзя согласовать чужую заявку'}, status=400)
            else:
                response = JsonResponse({'message': 'Нельзя согласовать заявку, т.к. переданы неверные данные'},
                                        status=400)
        return response

    def _agree_request(self, community_request, user):
        RequestResolution.objects.create(author=user, resolution=RequestResolution.RESOLUTION_AGREED_KEY,
                                         community_request=community_request)
        did_all_participiants_resolve = community_request.did_all_participants_resolve()
        does_request_have_reject = community_request.does_request_have_reject()
        if does_request_have_reject:
            community_request.status = CommunityRequest.REQUEST_STATUS_REJECTED_KEY
            community_request.save()
        else:
            if did_all_participiants_resolve:
                with transaction.atomic():
                    community_request.status = CommunityRequest.REQUEST_STATUS_AGREED_KEY
                    community_request.save()
                    profile = Profile.objects.get(user=community_request.author)
                    profile.geo_community.add(community_request.community)
                    profile.save()

    def _reject_request(self, community_request, user):
        RequestResolution.objects.create(author=user, resolution=RequestResolution.RESOLUTION_REJECTED_KEY,
                                         community_request=community_request)
        community_request.status = CommunityRequest.REQUEST_STATUS_REJECTED_KEY
        community_request.save()

class GeoTenInitiativesView(CustomTemplateViewMixin, TemplateView):
    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        community = context['profile'].get_first_geo_ten_community()
        initiatives = community.get_initiatives if community else []
        initiative_label_length_limit = Initiative.INITIATIVE_LABEL_LENGTH_LIMIT
        extra_context = {
            'community': community,
            'initiatives': initiatives,
            'initiative_label_length_limit': initiative_label_length_limit,
        }
        context.update(extra_context)
        response = render(request, 'veche_geo_ten_initiatives.html', context=context)
        return response

    def post(self, request):
        initiative_label = escape(request.POST.get('label'))
        initiative_text = escape(request.POST.get('text'))
        if len(initiative_label) * len(initiative_text) == 0:
            return JsonResponse({'message': 'Заголовок и текст инициативы должны быть обязателно указаны'}, status=400)

        profile = self._get_profile()
        if not profile:
            return JsonResponse({'message': 'Отсутствует профиль пользователя'}, status=400)

        community = profile.get_first_geo_ten_community()
        community_is_ok = bool(community) and community.is_possible_to_create_initiative()
        if profile.user_in_geo_community() and community_is_ok:
            data = {
                'author': request.user,
                'community': community,
                'initiative_label': initiative_label,
                'initiative_text': initiative_text,
            }
            initiative = Initiative.objects.create(**data)
            response = JsonResponse({'message': 'Инициатива успешно создана'}, status=200)
        else:
            msg = (
                'Создать инициативу нельзя. Либо пользователь не состоит в гео десятке либо гео дестка не '
                'укомплектована'
            )
            response = JsonResponse({'message': msg}, status=200)
        return response

class InitiativeView(InitiativeViewMixis, CustomTemplateViewMixin, TemplateView):
    MESSAGES_ON_PAGE = 10

    def get(self, request, *args, **kwargs):
        initiative_id = kwargs.get('initiative_id')
        response_400 = self._check_initiative(initiative_id)
        if response_400:
            return response_400

        initiative = Initiative.objects.get(pk=initiative_id)
        context = self.get_context_data()
        community = context['profile'].get_first_geo_ten_community()
        messages_qs = initiative.initiative_messages.all().order_by('created_at')
        paginator = Paginator(messages_qs, self.MESSAGES_ON_PAGE)
        page_number = request.GET.get('page')
        page_obj = paginator.get_page(page_number)

        profile = context['profile']
        user_checked_initiative = profile.did_user_check_initiative(initiative=initiative)
        user_resolution_for_initiative = profile.get_user_resolution_for_initiative(initiative=initiative)
        agree_users = initiative.get_agree_users()
        reject_users = initiative.get_reject_users()
        extra_context = {
            'community': community,
            'initiative': initiative,
            'page_obj': page_obj,
            'user_checked_initiative': user_checked_initiative,
            'user_resolution_for_initiative': user_resolution_for_initiative,
            'agree_users': agree_users,
            'reject_users': reject_users,
        }
        context.update(extra_context)
        response = render(request, 'veche_initiative.html', context=context)
        return response

    def post(self, request, *args, **kwargs):
        initiative_id = kwargs.get('initiative_id')
        response_400 = self._check_initiative(initiative_id, is_resp_http=False)
        if response_400:
            return response_400

        initiative = Initiative.objects.get(pk=initiative_id)

        initiative_closed_400 = self._check_if_initiative_closed(initiative=initiative)
        if initiative_closed_400:
            return initiative_closed_400

        message_empty_400 = self._check_if_message_empty(request=request)
        if message_empty_400:
            return message_empty_400

        user_checked_initiative_400 = self._check_if_user_checked_initiative(initiative=initiative)
        if user_checked_initiative_400:
            return user_checked_initiative_400

        message_text = escape(request.POST.get('message', ''))
        new_message = MessageInitiative.objects.create(initiative=initiative, author=request.user, message=message_text)

        messages_qs = initiative.initiative_messages.all().order_by('created_at')
        paginator = Paginator(messages_qs, self.MESSAGES_ON_PAGE)
        data = {
            'pageNum': paginator.num_pages,
            'message_id': new_message.pk
        }
        return JsonResponse(data, status=200)

class ResolutionInitiativeView(InitiativeViewMixis, CustomTemplateViewMixin, TemplateView):
    AGREE_URL_ALIAS = 'agree_initiative'
    REJECT_URL_ALIAS = 'reject_initiative'

    def post(self, request, *args, **kwargs):
        initiative_id = kwargs.get('initiative_id')
        response_400 = self._check_initiative(initiative_id, is_resp_http=False)
        if response_400:
            return response_400

        initiative = Initiative.objects.get(pk=initiative_id)

        initiative_closed_400 = self._check_if_initiative_closed(initiative=initiative)
        if initiative_closed_400:
            return initiative_closed_400

        user_checked_initiative_400 = self._check_if_user_checked_initiative(initiative=initiative)
        if user_checked_initiative_400:
            return user_checked_initiative_400

        current_url_alias = request.resolver_match.url_name
        if current_url_alias == self.AGREE_URL_ALIAS:
            ResolutionInitiative.objects.create(author=request.user,
                                                resolution=ResolutionInitiative.RESOLUTION_AGREED_KEY,
                                                initiative=initiative)
        if current_url_alias == self.REJECT_URL_ALIAS:
            message_empty_400 = self._check_if_message_empty(request=request)
            if message_empty_400:
                return message_empty_400

            message_text = escape(request.POST.get('message', ''))
            ResolutionInitiative.objects.create(author=request.user,
                                                resolution=ResolutionInitiative.RESOLUTION_REJECTED_KEY,
                                                initiative=initiative,
                                                message=message_text)

        return JsonResponse({}, status=200)


# %%
import matplotlib.pyplot as plt
from brainrender.colors import makePalette, colorMap
import numpy as np
from scipy.signal import medfilt
from pathlib import Path
from palettable.cmocean.sequential import Matter_8 as CMAP
from palettable.cmocean.sequential import Speed_8 as CMAP2
from palettable.cmocean.sequential import Deep_8 as CMAP3
from matplotlib.lines import Line2D

from fcutils.plotting.utils import calc_nrows_ncols, set_figure_subplots_aspect, clean_axes, save_figure
from fcutils.plotting.plot_elements import plot_mean_and_error

from Analysis  import (
        mice,
        sessions,
        recordings,
        recording_names,
        stimuli,
        clean_stimuli,
        get_mouse_session_data,
        sessions_fps,
        mouse_sessions,
        pxperframe_to_cmpersec,
)
from Analysis.tag_aligned import (
    manual_tags,
    get_tags_by,
    get_next_tag,
    get_last_tag,
)

# %%
fld = Path('D:\\Dropbox (UCL - SWC)\\Project_vgatPAG\\analysis\\doric\\Fede\\plots\\ManualTagsAligned')

# %%
"""
    For each tag plot all ROIs signals aligned

    UTILS
"""

# ----------------------------------- Utils ---------------------------------- #

def make_signal_ax(ax, ttl, start, end):
    t = end - start
    ax.axvline(n_frames_pre, lw=2, color='k', ls='--', alpha=.3, zorder=-1)

    ax.set(title=ttl, xticks=[0, n_frames_pre, t],
                    xlim = [0, t],
                    xticklabels=[-n_sec_pre, 0, n_sec_post],
                    xlabel='time (s)', ylabel=f'signal {"baselined" if NORMALIZE else ""}')

def process_sig(sig, start, end, n_sec_post, norm=False, filter=True):
    if filter: # median filter
        sig = medfilt(sig, kernel_size=5)
    if norm: # normalize with baseline
        baseline = np.mean(sig[start: start + n_frames_pre - 1])
        sig =  sig - baseline
    return sig

# ------------------------------ Plotting utils ------------------------------ #
def outline(ax, x, y, color, **kwargs):
    ls = kwargs.pop('ls', '-')
    ax.plot(x, y, color=[.4, .4, .4], lw=6, zorder=-1, ls=ls)
    ax.plot(x, y, color=color, lw=4, ls=ls, **kwargs)

def get_colors(tags):
    colors = []
    for n, (i, tag) in enumerate(tags.iterrows()):

        if 'Loom' in tag.event_type:
            cmap = CMAP
        elif 'US' in tag.event_type:
            cmap = CMAP2
        elif 'None' in tag.event_type:
            cmap = CMAP3
        else:
            raise NotImplementedError
        colors.append(colorMap(n, name=cmap.mpl_colormap, vmin=-1, vmax=len(tags)+1))

    return colors

def plot_tracking_trace(ax, tracking, color, label, start, end, mark_frame = None, stim_frame=None, escape_start_frame=None):
    # plot tracking
    ax.plot(tracking.x[start:start+n_frames_pre], tracking.y[start:start+n_frames_pre], color=color, ls=':', lw=4)
    outline(ax, tracking.x[start+n_frames_pre:end], tracking.y[start+n_frames_pre:end], color)

    # mark start and end
    ax.scatter(tracking.x[start+n_frames_pre], tracking.y[start+n_frames_pre], s=150,
            color=color, lw=1, edgecolors=[.2, .2, .2], zorder=99)

    if mark_frame is not None:
        ax.scatter(tracking.x[mark_frame], tracking.y[mark_frame], s=150, marker='^',
                color=color, lw=2, edgecolors='k', zorder=99)

    if stim_frame is not None:
        if stim_frame-start > 0: # don't show if it's too in the past
            ax.scatter(tracking.x[stim_frame], tracking.y[stim_frame], s=150,
                    color='k', lw=2, edgecolors=color, zorder=99)

    if escape_start_frame is not None:
            ax.scatter(tracking.x[stim_frame], tracking.y[stim_frame], s=150, marker='D',
                    color=color, lw=2, edgecolors='k', zorder=99)

def plot_speed_trace(ax, speed, xpos, color, label, start, end, mark_frame = None, stim_frame=None, escape_start_frame=None):
    # plot tracking
    ax.plot(speed[start:start+n_frames_pre], xpos[start:start+n_frames_pre], color=color, ls=':', lw=4)
    outline(ax, speed[start+n_frames_pre:end], xpos[start+n_frames_pre:end], color)

    # mark start and end
    ax.scatter(speed[start+n_frames_pre], xpos[start+n_frames_pre], s=150,
            color=color, lw=1, edgecolors=[.2, .2, .2], zorder=99)

    if mark_frame is not None:
        ax.scatter(speed[mark_frame], xpos[mark_frame], s=150, marker='^',
                color=color, lw=2, edgecolors='k', zorder=99)

    if stim_frame is not None:
        if stim_frame-start > 0: # don't show if it's too in the past
            ax.scatter(speed[stim_frame], xpos[stim_frame], s=150,
                    color='k', lw=2, edgecolors=color, zorder=99)

    if escape_start_frame is not None:
            ax.scatter(speed[stim_frame], xpos[stim_frame], s=150, marker='D',
                    color=color, lw=2, edgecolors='k', zorder=99)

def plot_signal_trace(ax, sig, color, start, end, mark_frame=None, stim_frame=None, escape_start_frame=None):
    if not HIGHLIGHT_MEAN:
        lw = 2.5
    else:
        lw = .5
    t = end-start
    # plot tracking
    ax.plot(sig[start:end], label=str(tag.stim_frame),
        color=[.4, .4, .4], lw=lw+1, zorder=-1, alpha=.7)
    ax.plot(sig[start:end], label=str(tag.stim_frame),
        color=color, lw=lw, alpha=.7)

    if not HIGHLIGHT_MEAN:
        if mark_frame is not None:
            ax.scatter(mark_frame-start, sig[mark_frame], s=150,
                    color='white', lw=2, edgecolors=color, zorder=99)

        if stim_frame is not None:
            if stim_frame-start>0: # don't show it it's too in the past
                ax.scatter(stim_frame-start, sig[stim_frame], s=150,
                        color='k', lw=2, edgecolors=color, zorder=99)

        if escape_start_frame is not None:
                ax.scatter(escape_start_frame-start, sig[escape_start_frame], s=150, marker='D',
                        color=color, lw=2, edgecolors='k', zorder=99)

def plot_means(axarr, means, event_means, color='r', mark=True):
    try:
        x = np.mean(means.pop('x'), 0)
        y = np.mean(means.pop('y'), 0)
        s = np.mean(means.pop('s'), 0)
    except:
        pass

    sigs_std = [np.std(v, 0) for v in means.values()]
    sigs = [np.median(v, 0) for v in means.values()]

    try:
        arrive = int(np.mean(event_means['arrive']))
    except:
        arrive = None

    stim = int(np.mean(event_means['stim']))

    try:
        turn = int(np.mean(event_means['turn']))
    except:
        turn = None

    for n, sig in enumerate(sigs):
        axarr[n+2].plot(sig, lw=5, color='k')
        plot_mean_and_error(sig, sigs_std[n], axarr[n+2], color=color, lw=3)

        if mark:
            if arrive is not None:
                axarr[n+2].scatter(arrive, sig[arrive], s=250,
                        alpha=.4,
                        color='white', lw=2, edgecolors='k', zorder=99)

            if stim >= 0:
                axarr[n+2].scatter(stim, sig[stim], s=250,
                        alpha=.4,
                        color='k', lw=2, edgecolors=color, zorder=99)

            if turn is not None:
                axarr[n+2].scatter(turn, sig[turn], s=250, marker='D',
                        alpha=.4,
                        color='white', lw=2, edgecolors='k', zorder=99)

# %%

include_sessions = [  # including only sessions with lots of data
    'BF166p3-19JUN19',
    'BF164p2-19JUN24',
    'BF164p2-19JUL15',
    'BF161p1-19JUN04',
    'BF164p1-19JUN05',
    'BF164p2-19JUN26'
]

# TODO look into how to normalize the data

# Params
fps = 30
n_sec_pre = 4
n_sec_post = 4
n_frames_pre = n_sec_pre * fps
n_frames_post = n_sec_post * fps

tag_type = 'VideoTag_B'
event_type =  ['Loom_Escape', 'US_Escape', 'LoomUS_Escape'] #
event_type2 = ['None_Escape'] #

NORMALIZE = True # if true divide sig by the mean of the sig in n_sec_pre
FILTER = True # if true data are median filtered to remove artefact
HIGHLIGHT_MEAN = True # if true plots are made to highlight the mean across trials instead of individual trials

event_types = list(manual_tags.event_type.unique())

for mouse, sess, sessname in mouse_sessions:
    # if sessname not in include_sessions: continue
    print(f'Processing {sessname}\n')

    # Get data
    tracking, ang_vel, speed, shelter_distance, signals, nrois, is_rec = get_mouse_session_data(mouse, sess, sessions)
    if is_rec is None: continue
    tags = get_tags_by(mouse=mouse, sess_name=sess, event_type=event_type, tag_type=tag_type)
    tags2 = get_tags_by(mouse=mouse, sess_name=sess, event_type=event_type2, tag_type=tag_type)

    # print(get_tags_by(mouse=mouse, sess_name=sess, tag_type=tag_type).event_type.unique())
    # continue

    if not len(tags):
        print(f'Didnt find any tags of type {tag_type} in session {sessname}')
        continue

    at_shelt_tags = get_tags_by(mouse=mouse, sess_name=sess, event_type=event_type, tag_type='VideoTag_E')
    escape_start_tags = get_tags_by(mouse=mouse, sess_name=sess, event_type=event_type, tag_type='VideoTag_L')
    failures_tags = get_tags_by(mouse=mouse, sess_name=sess, event_type=event_type, tag_type='VideoTag_D')

    # Prep figure
    rows, cols = calc_nrows_ncols(len(signals) + 2, (30, 15))
    f, axarr = plt.subplots(ncols=cols, nrows=rows, figsize = (30, 15))
    f.suptitle(f'{sessname} traces aligned to {tag_type} | {"".join(event_type)} | baselined: {NORMALIZE}')
    axarr = axarr.flatten()

    # make colors
    colors = get_colors(tags)

    # Plot data
    means = {s:[] for s, _ in enumerate(signals)}
    means['x'], means['y'], means['s'] = [], [], []
    event_means = dict(stim=[], turn=[], arrive=[])

    for count, (color, (i, tag)) in enumerate(zip(colors, tags.iterrows())):
        start = tag.session_frame - n_frames_pre
        end = tag.session_frame + n_frames_post

        if np.sum(is_rec[start:end]) == 0: # recording was off
            continue

        nxt_at_shelt = get_next_tag(tag.session_frame, at_shelt_tags)
        escape_start = get_last_tag(tag.session_frame, escape_start_tags)
        failed = get_next_tag(tag.session_frame, failures_tags)

        event_means['stim'].append(tag.session_stim_frame -  start)
        if escape_start is not None:
            event_means['turn'].append(escape_start -  start)
        if nxt_at_shelt is not None:
            event_means['arrive'].append(nxt_at_shelt -  start)

        if start > len(tracking):
            print(f'!! tag frame {start} is out of bounds: # frames: {len(tracking)}')
            raise ValueError

        # PLOT TRACKING
        plot_tracking_trace(axarr[0], tracking, color, f'frame {start}', start, end,
                                 mark_frame=nxt_at_shelt,
                                 stim_frame=tag.session_stim_frame,
                                 escape_start_frame = escape_start,
                                 )

        # PLOT SPEED and S distance
        plot_speed_trace(axarr[1], pxperframe_to_cmpersec(speed), tracking.x,
                                color, f'frame {start}', start, end,
                                 mark_frame=nxt_at_shelt,
                                 stim_frame=tag.session_stim_frame,
                                 escape_start_frame = escape_start,
                                 )

        # STORE SIGNS FOR MEANS
        means['x'].append(tracking.x[start:end])
        means['y'].append(tracking.y[start:end])
        means['s'].append(speed[start:end])

        # PLOT SIGNALS
        for roin, sig in enumerate(signals):
            if np.std(sig[start:end]) == 0:
                raise ValueError
            sig = process_sig(sig, start, end, n_sec_pre, norm=NORMALIZE, filter=FILTER)
            plot_signal_trace(axarr[roin+2],
                                sig,
                                color if not HIGHLIGHT_MEAN else [.1, .1, .1],
                                start, end,
                                mark_frame=nxt_at_shelt,
                                stim_frame = tag.session_stim_frame,
                                escape_start_frame = escape_start,
                                )
            means[roin].append(sig[start:end])

    # means2 = {s:[] for s, _ in enumerate(signals)}
    # for count, (color, (i, tag)) in enumerate(zip(colors, tags2.iterrows())):
    #     start = tag.session_frame - n_frames_pre
    #     end = tag.session_frame + n_frames_post

    #     if np.sum(is_rec[start:end]) == 0: # recording was off
    #         continue

    #     nxt_at_shelt = get_next_tag(tag.session_frame, at_shelt_tags)
    #     escape_start = get_last_tag(tag.session_frame, escape_start_tags)
    #     failed = get_next_tag(tag.session_frame, failures_tags)

    #     event_means['stim'].append(tag.session_stim_frame -  start)
    #     if escape_start is not None:
    #         event_means['turn'].append(escape_start -  start)
    #     if nxt_at_shelt is not None:
    #         event_means['arrive'].append(nxt_at_shelt -  start)

    #     if start > len(tracking):
    #         print(f'!! tag frame {start} is out of bounds: # frames: {len(tracking)}')
    #         raise ValueError

    #     # PLOT TRACKING
    #     plot_tracking_trace(axarr[0], tracking, color, f'frame {start}', start, end,
    #                              mark_frame=nxt_at_shelt,
    #                              stim_frame=tag.session_stim_frame,
    #                              escape_start_frame = escape_start,
    #                              )

    #     # PLOT SPEED and S distance
    #     plot_speed_trace(axarr[1], pxperframe_to_cmpersec(speed), tracking.x,
    #                             color, f'frame {start}', start, end,
    #                              mark_frame=nxt_at_shelt,
    #                              stim_frame=tag.session_stim_frame,
    #                              escape_start_frame = escape_start,
    #                              )

    #     # PLOT SIGNALS
    #     for roin, sig in enumerate(signals):
    #         if np.std(sig[start:end]) == 0:
    #             raise ValueError
    #         sig = process_sig(sig, start, end, n_sec_pre, norm=NORMALIZE, filter=FILTER)
    #         plot_signal_trace(axarr[roin+2],
    #                             sig,
    #                             color if not HIGHLIGHT_MEAN else [.1, .1, .1],
    #                             start, end,
    #                             mark_frame=nxt_at_shelt,
    #                             stim_frame = tag.session_stim_frame,
    #                             escape_start_frame = escape_start,
    #                             )
    #         means2[roin].append(sig[start:end])

    #     # MAKE CUSTOM LEGEND
    #     if count == 0:
    #         legend_elements = [
    #             Line2D([0], [0], color=color, lw=4, marker='D', label='Turn', markersize=10, markeredgecolor='k'),
    #             Line2D([0], [0], color=color, lw=4, marker='o', label='Run', markersize=10, markeredgecolor='k'),
    #             Line2D([0], [0], color=color, lw=4, marker='^', label='Shelter', markersize=10, markeredgecolor='k'),
    #             Line2D([0], [0], color=color, lw=4, label=str(tag.stim_frame)),
    #         ]

    # # PLOT MEAN TRACES
    # if HIGHLIGHT_MEAN:
    #     plot_means(axarr, means, event_means)
    #     try:
    #         plot_means(axarr, means2, event_means, color='b', mark=False)
    #     except : pass

    # fix figure
    for n, ax in enumerate(axarr[2:]):
        if n >= len(signals):
            ax.axis('off')
        else:
            make_signal_ax(ax, f'ROI {n}', start, end)

    axarr[0].set(title='tracking', xlabel='X (px)', ylabel='Y (px)')
    axarr[0].legend(handles=legend_elements)
    axarr[1].set(title='tracking', xlabel='speed (cm/s)', ylabel='X (px) ')

    clean_axes(f)
    set_figure_subplots_aspect(right=.85, top=.9, wspace=.4)
    save_figure(f, str(fld / f'{sessname}_tag_{tag_type[-1]}_{"".join(event_type)}_mean_{HIGHLIGHT_MEAN}'), svg=False)

    # break

with open(str(fld / '00_info.txt'), 'w') as out:
    out.write('INFO')
    out.write(f'''IMAGES GENERATED WITH PARAMS:
                        fps = {fps }
                        n_sec_pre = {n_sec_pre}
                        n_sec_post = {n_sec_post}
                        tag_type = {tag_type}
                        event_type = {event_type}
                        NORMALIZE = {NORMALIZE}
                        FILTER = {FILTER}

                    RED = LOOM/LOOM+ULTRASOUND
                    GREEN = ULTRASOUND
                    BLUE = SPONTANEOUS
''')

# %%
"""
    Look at the correlation across signals for each ROI before and after the event onset
"""

def get_corr_mtx(signals):
    mtx = np.zeros((len(signals), len(signals)))

    for i, a in enumerate(signals):
        for j,b in enumerate(signals):
            if i == j:
                mtx[i, j] = np.nan
                mtx[j, i] = np.nan
            else:
                coeff = pearsonr(a, b)[0]
                mtx[i, j] = coeff
                mtx[j, i] = coeff

    return mtx

for mouse, sess, sessname in mouse_sessions:
    # if sessname not in include_sessions: continue
    print(f'Processing {sessname}\n')

    # Get data
    tracking, ang_vel, speed, shelter_distance, signals, nrois, is_rec = get_mouse_session_data(mouse, sess, sessions)
    if is_rec is None: continue
    tags = get_tags_by(mouse=mouse, sess_name=sess, event_type=event_type, tag_type=tag_type)

    clean_sigs = {n:dict(pre=[], post=[]) for n, s in enumerate(signals)}
    for count, (color, (i, tag)) in enumerate(zip(colors, tags.iterrows())):
        start = tag.session_frame - n_frames_pre
        end = tag.session_frame + n_frames_post

        for roin, sig in enumerate(signals):
                if np.std(sig[start:end]) == 0:
                    raise ValueError
                sig = process_sig(sig, start, end, n_sec_pre, norm=NORMALIZE, filter=FILTER)
                clean_sigs[roin]['pre'].append(sig[start:start+n_frames_pre-1])
                clean_sigs[roin]['post'].append(sig[start+n_frames_pre+1:end])

    # make figure
    rows, cols = calc_nrows_ncols(len(signals)*2, (30, 15))
    f, axarr = plt.subplots(ncols=cols, nrows=rows, figsize = (30, 15))
    axarr = axarr.flatten()

    axn = 0
    for roin in range(nrois):
        corr_mtx_pre = get_corr_mtx(clean_sigs[roin]['pre'])
        axarr[axn].imshow(corr_mtx_pre,  cmap='bwr', vmin=-1, vmax=1)
        axn += 1

        corr_mtx_post = get_corr_mtx(clean_sigs[roin]['post'])
        axarr[axn].imshow(corr_mtx_post,  cmap='bwr', vmin=-1, vmax=1)
        axn += 1

    break

# %%
from scipy.stats.stats import pearsonr
mtx = np.zeros((len(tags), len(tags)))

for i, a in enumerate(clean_sigs[0]['pre']):
    for j,b in enumerate(clean_sigs[0]['pre']):
        if i == j:
            mtx[i, j] = np.nan
        coeff = pearsonr(a, b)[0]
        mtx[i, j] = coeff
        mtx[j, i] = coeff

plt.imshow(mtx, cmap='bwr', vmin=-1, vmax=1)

# %%


#!/usr/bin/python

import sys
import os
import json

from Xlib import X
from Xlib.display import Display

from wmctrl_window import WmctrlWindow

def main(argv):
  display = Display()
  root = display.screen().root
  root.change_attributes(event_mask=X.PropertyChangeMask | X.SubstructureNotifyMask)
  skip_windows = ['tk.TkN/Awindowswitcher', 'xfce4-appfinder.Xfce4-appfinderxavierApplicationFinder']

  NET_ACTIVE_WINDOW = display.intern_atom('_NET_ACTIVE_WINDOW')

  try:
    while True:
      event = display.next_event()
      if event.type != X.PropertyNotify:
        continue

      response = root.get_full_property(NET_ACTIVE_WINDOW, X.AnyPropertyType)
      win_id = hex(response.value[0]).rstrip('L').lstrip('0x')

      if len(win_id) == 0:
        continue

      wmctrl_out = os.popen('sleep 0.1; wmctrl -dliGux | grep -i {win_id}'.format(win_id=win_id)).read()
      wmctrl_out = wmctrl_out.split('\n')
      wmctrl_out.pop()

      if len(wmctrl_out) != 1:
        continue

      window = WmctrlWindow(wmctrl_out[0])
      # ignore window switcher
      if window.name in skip_windows:
        continue

      str_json = open("/home/danilo/scripts/flip360_wids.json", "r").read()
      jjson = json.loads(str_json)

      key_json = 'm{m}{w}'.format(m=window.monitor, w=window.workspace)
      if jjson.get(key_json, None) == None:
        continue

      window_previous = WmctrlWindow(jjson[key_json])
      if window.id == window_previous.id:
        continue

      jjson[key_json] = window.str_win
      flip360_wids = open("/home/danilo/scripts/flip360_wids.json", "w")
      flip360_wids.write(json.dumps(jjson))
      flip360_wids.close()

  finally:
    display.close()

if __name__ == '__main__':
  sys.exit(main(sys.argv))


from pymongo import MongoClient
import time
from datetime import datetime

# connect
def connect():
    client = MongoClient('172.16.2.70', 27017)
    return client

def load_dkline(client):
    table = client.kline.history_klines_day_0
    start = time.time()

    now = datetime.now()
    etime = now.strftime("%Y%m%d")
    print("dkline end time:", etime)

    # read stocks
    with open('/data/quant/kline/stocks', 'r') as fd:
        stock = fd.readline()
        while stock:
            stock = stock[0:9]
            # creat data file
            df = '/data/quant/kline/.cache/dkline/' + stock
            with open(df, 'w') as dfd:
                print(df, etime, stock)
                ret = table.find({'date': {'$lte': etime}, 'stock_code': stock})
                cnt = ret.count()
                print(stock, ":", cnt)
                for i in range(cnt):
                    r = ret[i]
                    try:
                        d = str(r['open_px']) + ' ' + str(r['high_px']) + ' '+ str(r['low_px']) + ' ' + str(r['close_px']) + ' ' \
                                + str(r['preclose_px']) + ' ' + str(r['date']) + ' ' + str(r['business_amount']) + ' ' + str(r['business_balance']) + '\n'

                    except KeyError as e:
                        d = str(r['data'][0][1]) + ' ' + str(r['data'][0][2]) + ' ' + str(r['data'][0][3]) + ' ' + str(r['data'][0][4]) + ' ' \
                                + str(r['preclose_px']) + ' ' + str(r['data'][0][0]) + ' ' + str(r['data'][0][5]) + ' ' + str(r['data'][0][6]) + '\n'
                    except Exception as e:
                        print(r)
                        print("error:", e)
                        continue
                    dfd.write(d)
                #dfd.flush()
                dfd.close()

            stock = fd.readline()

    end = time.time()
    print("cost:", end-start)

if __name__ == '__main__':
    client = connect()
    load_dkline(client)


# ABCDE
# ABCD
# ABC
# AB
# A

n=int(input("enter the number"))
i=0
while i<=n:
    k=ord("A")
    j=1
    while j<=5-i:
        print(chr(k),end="")
        k=k+1
        j=j+1
    print()
    i=i+1


from query import Query

query = Query()

# Get course number only, instead of course name
# e.g. CS 101 How to Humblebrag -> CS 196
def get_course_number(course_name):
    course_bd = course_name.split(" ")
    return course_bd[0] + " " + course_bd[1]

# Get a dictionary that maps course name to its reviews
def get_all_reviews_dict():
    # Dictionaty that maps course number -> list of reviews
    # e.g. CS 225 -> ["good", "blah blah", "dezznuts"]
    review_dict = {}

    # Get all course's cid and name
    # e.g. 1 AAS 100 Intro Asian American Studies
    courses = query.get_all_cids()

    for course in courses:
        reviews = query.get_text_reviews_cid(course[0])
        course_number = get_course_number(course[1])

        # Only insert courses with reviews into the dictionary
        if len(reviews):
            review_dict[course_number] = reviews[0][0]

    return review_dict

# Get a list of reviews without course information
def get_all_reviews_list():
    reviews_raw = query.get_text_reviews_no_cid()
    reviews = []

    for review in reviews_raw:
        reviews.append(review[0])

    return reviews

# Get a list of reviews course information
# Require a course id as the parameter, e.g. 2260 for CS 225
def get_all_reviews_list_cid(cid):
    reviews_raw = query.get_text_reviews_cid(cid)
    reviews = []
    for review in reviews_raw:
        reviews.append(review[0])
    return reviews

# Testing
# if __name__ == "__main__":
#     print get_all_reviews_list_cid(2260)


from tensorflow.keras import layers, Model
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
import matplotlib.pyplot as plt
import numpy as np

weight_path = 'inception_v3.h5'
np.random.seed(48)

# training, validating and testing data generators
training_datagen = ImageDataGenerator(rescale=1./255)
training_data = training_datagen.flow_from_directory(
        r'hand signs/training',
        target_size=(150, 150),
        batch_size=50,
        class_mode='binary'
    )

validation_datagen = ImageDataGenerator(rescale=1./255)
validation_data = validation_datagen.flow_from_directory(
        r'hand signs/validation',
        target_size=(150, 150),
        batch_size=50,
        class_mode='binary'
    )

test_datagen = ImageDataGenerator(rescale=1./255)
test_data = test_datagen.flow_from_directory(
        r'hand signs/test',
        target_size=(150,150),
        batch_size=1,
        class_mode='binary',
    )

# Using the inception model
pretrained_model = InceptionV3(input_shape=(150, 150, 3), include_top=False, weights=None)
pretrained_model.load_weights(weight_path)

# freeze the layers
for layer in pretrained_model.layers:
    layer.trainable = False

last_layer = pretrained_model.get_layer('mixed7')
last_output = last_layer.output

# Creating our shallow network
x = layers.Flatten()(last_output)
x = layers.Dense(1024, activation='tanh')(x)
x = layers.Dropout(0.3)(x)
x = layers.Dense(512, activation='tanh')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(256, activation='tanh')(x)
x = layers.Dense(64, activation='tanh')(x)
x = layers.Dense(10, activation='softmax')(x)

model = Model(pretrained_model.input, x)

model.compile(
        optimizer=Adam(lr=0.0001),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )

# to run the training model only.
history = model.fit(
    training_data,
    steps_per_epoch=8,
    epochs=50,
    verbose=2,
    callbacks=[EarlyStopping(monitor='loss', patience=4)]
)

# Uncomment the ModelCheckpoint for saving the best model
callbacks = [
    EarlyStopping(monitor='val_loss', patience=4),
    # ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True),
]

# To run the model with validation
# history = model.fit(
#         training_data,
#         steps_per_epoch=8,
#         epochs=50,
#         validation_data=validation_data,
#         validation_steps=4,
#         callbacks=callbacks,
#         verbose=2,
#     )

# Evaluation from test data
results = model.evaluate(test_data, batch_size=50, verbose=0)
print(f'\nEvaluation Results: accuracy: {round(results[1], 4)} and loss: {round(results[0], 4)}')

# visualization of accracy and loss
# acc = history.history['accuracy']
# loss = history.history['loss']
# val_acc = history.history['val_accuracy']
# val_loss = history.history['val_loss']
# epochs = range(len(acc))

# plt.plot(epochs, acc, 'r', marker='.', label='accuraacy')
# plt.plot(epochs, val_acc, 'b', marker='.', label='validation accuracy')
# plt.plot(epochs, loss, 'b', label='loss')
# plt.plot(epochs, val_loss, 'y', label='validation loss')
# plt.legend()
# plt.show()


from scipy.special import softmax
import numpy as np
import time
import neat

from toric_game_env import ToricGameEnv
from perspectives import Perspectives

from config import GameMode, RewardMode

class ToricCodeGame():
    def __init__(self, config):
        self.board_size = config["Physics"]["distance"]
        self.max_steps = config["Training"]["max_steps"]
        self.epsilon = config["Training"]["epsilon"]
        self.network_type = config["Training"]["network_type"]
        self.substrate_type = config["Training"]["substrate_type"]
        self.rotation_invariant_decoder = config["Training"]["rotation_invariant_decoder"]

        self.env = ToricGameEnv(self.board_size)

        # Very important to change the seed here
        # Otherwise for game evaluation in parallel
        # All game objects will share the same seed leading to biased results
        np.random.seed()

        # The perspective includes the masking of star operators
        self.perspectives = Perspectives(self.board_size,
                                        remove_star_op=True,
                                        remove_qubits=not config["Training"]["memory"])

    # Return the score of the game
    # In evaluation mode, the fitness is in {0, 1} corresponding to success or failure
    # In training mode, fitness can be defined differently
    def play(self, nn, error_rate, error_mode, reward_mode, mode, seed=None, verbose=False):
        #if not seed is None:
        #    np.random.seed(seed)

        current_state = self.env.generate_errors(error_rate, error_mode)

        # If there is no syndrome in the initial configuration
        # Either we generate a new one containing syndromes
        # Or if there happens to be a logical error, we return a failure
        if mode == GameMode["TRAINING"]:
            while self.env.done and error_rate>0:
                if self.env.reward == -1:
                    # In both reward modes BINARY or CURRICULUM, since it is not possible to correct these errors, there is no reward nor penalty
                    return {"fitness":0, "error_rate": error_rate, "outcome":"logical_error", "nsteps":0}

                current_state = self.env.generate_errors(error_rate, error_mode)

        # In evaluation mode, we keep even these empty initial configurations
        elif self.env.done:
            if self.env.reward == -1:
                return {"fitness":0, "error_rate": error_rate, "outcome":"logical_error", "nsteps":0}
            else:
                return {"fitness":1, "error_rate": error_rate, "outcome":"success", "nsteps":0}

        if verbose:
            print("Initial", current_state)
            print(self.env.done, self.env.state.syndrome_pos)
            if verbose > 1: self.env.render()

        for step in range(self.max_steps+1):
            current_state = current_state.flatten()

            if self.network_type == 'cppn' and self.substrate_type == 1:
                probs, actions = self._get_actions(nn, current_state)
            else:
                if not self.rotation_invariant_decoder:
                    probs, actions = self._get_actions_with_perspectives(nn, current_state)
                else:
                    probs, actions = self._get_actions_with_perspectives_rotation_invariant(nn, current_state)

            # To avoid calling rand() when evaluating (for testing purposes)
            if self.epsilon == 0 or mode == GameMode["EVALUATION"]:
                action=actions[np.argmax(probs)]
            else:
                # epsilon-greedy search
                if np.random.rand() < self.epsilon:
                    action = actions[np.random.randint(len(actions))]
                else:
                    action = actions[np.argmax(probs)]

            current_state, reward, done, info = self.env.step(action)

            if verbose:
                print(step, current_state, reward, action, info["message"])
                if verbose > 1: self.env.render()

            # if no syndromes are present anymore
            if done:
                # Fitness is 1 if there is no logical error
                # 0 if there is a logical error
                if mode == GameMode["TRAINING"]:
                    # The harder the puzzle the higher the reward and the lower the penalty
                    # The easier the puzzles the lower the reward and the higher the penalty
                    if reward_mode == RewardMode["BINARY"]:
                        fitness = (reward+1)/2
                    elif reward_mode == RewardMode["CURRICULUM"]:
                        fitness = (reward-1)/2 + len(self.env.initial_qubits_flips)/(2*self.board_size**2)

                    return {"fitness": fitness, "error_rate": error_rate, "outcome":info["message"], "nsteps":step}
                else:
                    return {"fitness":(reward+1)/2, "error_rate": error_rate, "outcome":info["message"], "nsteps":step}

        # When the number of moves went beyond max_steps
        if mode == GameMode["TRAINING"]:
            # The harder the puzzle the higher the reward and the lower the penalty
            # The easier the puzzles the lower the reward and the higher the penalty
            if reward_mode == RewardMode["BINARY"]:
                fitness = 0
            elif reward_mode == RewardMode["CURRICULUM"]:
                fitness = -1 + len(self.env.initial_qubits_flips)/(2*self.board_size**2)
            return {"fitness": fitness, "error_rate": error_rate, "outcome":info["message"], "nsteps":step}
        else:
            return {"fitness":0, "error_rate": error_rate, "outcome":"max_steps", "nsteps":max_step}

    # In this case, the output layer has 4 nodes
    def _get_actions_with_perspectives(self, nn, current_state):
        # Go over perspectives
        actions, probs=[], []
        for plaq in self.env.state.syndrome_pos:
            # Shift the board to center the syndrome
            # Also includes masking of star operators
            indices = self.perspectives.shift_from(plaq)

            input = current_state[indices]

            # NN outputs 4 values
            probs += list(nn.activate(input))

            # 4 possible actions
            # Bad convention for action order, but we must keep it for retrocompatibility
            actions += [[(plaq[0]+1)%(2*self.board_size), plaq[1]]]
            actions += [[(plaq[0]-1)%(2*self.board_size), plaq[1]]]
            actions += [[plaq[0], (plaq[1]+1)%(2*self.board_size)]]
            actions += [[plaq[0], (plaq[1]-1)%(2*self.board_size)]]

        return probs, actions

    # In this case, the output layer has 1 node
    def _get_actions_with_perspectives_rotation_invariant(self, nn, current_state):

        # Go over syndromes
        actions, probs=[], []
        for plaq in self.env.state.syndrome_pos:
            # Rotation  order corresponding the 90degree anti-clockwise rotation
            qubit_flips_rotationally_ordered=[]
            qubit_flips_rotationally_ordered += [[(plaq[0]+1)%(2*self.board_size), plaq[1]]]
            qubit_flips_rotationally_ordered += [[plaq[0], (plaq[1]-1)%(2*self.board_size)]]
            qubit_flips_rotationally_ordered += [[(plaq[0]-1)%(2*self.board_size), plaq[1]]]
            qubit_flips_rotationally_ordered += [[plaq[0], (plaq[1]+1)%(2*self.board_size)]]

            for rot_i in range(4):
                indices = self.perspectives.shift_from(plaq, rot_i)

                input = current_state[indices]

                # NN outputs 1 value
                probs += list(nn.activate(input))

                actions += [qubit_flips_rotationally_ordered[rot_i]]

        return probs, actions

    # In this case, output layer has 2*board_size**2 (# of spins) nodes
    def _get_actions(self, nn, current_state):
        indices = self.perspectives.mask_star_operators

        input = current_state[indices]

        probs = list(nn.activate(input))

        actions = self.env.state.qubit_pos

        return probs, actions

    def close(self):
        self.env.close()


import os

from PyQt5.QtWidgets import QMessageBox

from orangewidget import gui, widget
from orangewidget.settings import Setting
from oasys.widgets import gui as oasysgui, congruence

from syned.beamline.beamline import Beamline

class FileWriter(widget.OWWidget):
    name = "Syned File Writer"
    description = "Utility: Syned File Writer"
    icon = "icons/file_writer.png"
    maintainer = "Manuel Sanchez del Rio"
    maintainer_email = "srio(@at@)esrf.eu"
    priority = 2
    category = "Utility"
    keywords = ["data", "file", "load", "read"]

    want_main_area = 0

    syned_file_name = Setting("")
    is_automatic_run= Setting(1)

    inputs = [("SynedBeamline" , Beamline, "setBeamline" )]

    outputs = [{"name":"SynedBeamline",
                "type":Beamline,
                "doc":"Syned Beamline",
                "id":"data"}]

    beamline = None

    def __init__(self):
        super().__init__()

        self.runaction = widget.OWAction("Write Syned File", self)
        self.runaction.triggered.connect(self.write_file)
        self.addAction(self.runaction)

        self.setFixedWidth(590)
        self.setFixedHeight(180)

        left_box_1 = oasysgui.widgetBox(self.controlArea, "Syned File Selection", addSpace=True, orientation="vertical",
                                         width=570, height=100)

        gui.checkBox(left_box_1, self, 'is_automatic_run', 'Automatic Execution')

        gui.separator(left_box_1, height=10)

        figure_box = oasysgui.widgetBox(left_box_1, "", addSpace=True, orientation="horizontal", width=550, height=50)

        self.le_syned_file_name = oasysgui.lineEdit(figure_box, self, "syned_file_name", "Syned File Name",
                                                    labelWidth=120, valueType=str, orientation="horizontal")
        self.le_syned_file_name.setFixedWidth(330)

        gui.button(figure_box, self, "...", callback=self.selectFile)

        gui.separator(left_box_1, height=10)

        button = gui.button(self.controlArea, self, "Write Syned File", callback=self.write_file)
        button.setFixedHeight(45)

        gui.rubber(self.controlArea)

    def selectFile(self):
        self.le_syned_file_name.setText(oasysgui.selectFileFromDialog(self, self.syned_file_name, "Open Syned File"))

    def setBeamline(self, data):
        if not data is None:
            self.beamline = data

            if self.is_automatic_run:
                self.write_file()

    def write_file(self):
        self.setStatusMessage("")

        try:
            if not self.beamline is None:
                congruence.checkDir(self.syned_file_name)

                self.beamline.to_json(self.syned_file_name)

                path, file_name = os.path.split(self.syned_file_name)

                self.setStatusMessage("File Out: " + file_name)

                self.send("SynedBeamline", self.beamline)
            else:
                QMessageBox.critical(self, "Error",
                                     "Syned Data not present",
                                     QMessageBox.Ok)
        except Exception as exception:
            QMessageBox.critical(self, "Error", str(exception), QMessageBox.Ok)

if __name__ == "__main__":
    from PyQt5.QtWidgets import QApplication
    import sys

    a = QApplication(sys.argv)
    ow = FileWriter()

    ow.show()
    a.exec_()


from __future__ import annotations

import pytest

from marimo._ast.app import App
from marimo._ast.cell import cell_factory, parse_cell

class TestParseCell:
    @staticmethod
    def test_parse_simple() -> None:
        code = "x = 0\nz = y"
        cell = parse_cell(code)
        assert cell.key == hash(code)
        assert cell.code == code
        assert cell.defs == set(["x", "z"])
        assert cell.refs == set(["y"])

    @staticmethod
    def test_local_variables() -> None:
        code = "_, y = f(x)\ndef _foo():\n  _bar = 0\nimport _secret_module"
        cell = parse_cell(code)
        assert cell.defs == {"y"}
        assert cell.refs == {"f", "x"}

    @staticmethod
    def test_dunder_dunder_excluded() -> None:
        code = "__ = 10; __name__ = 20"
        cell = parse_cell(code)
        assert cell.defs == {"__", "__name__"}
        assert cell.refs == set()

    @staticmethod
    def test_local_class() -> None:
        code = "class _A: pass"
        cell = parse_cell(code)
        assert cell.defs == set()
        assert cell.refs == set()

    @staticmethod
    def test_alias_underscored_name() -> None:
        code = "import _m as m"
        cell = parse_cell(code)
        assert cell.defs == {"m"}
        assert cell.refs == set()

    @staticmethod
    def test_ref_local_var() -> None:
        code = "x = _y"
        cell = parse_cell(code)
        assert cell.defs == {"x"}
        assert cell.refs == set()

        code = "def f(x= _y): pass"
        cell = parse_cell(code)
        assert cell.defs == {"f"}
        assert cell.refs == set()

class TestCellFactory:
    @staticmethod
    def test_missing_return() -> None:
        def f() -> None:
            x = 10  # noqa: F841

        with pytest.raises(ValueError) as e:
            cell_factory(f)

        assert "missing a return statement" in str(e.value)

    @staticmethod
    def test_not_tuple() -> None:
        def f() -> int:
            x = 10  # noqa: F841
            return x

        with pytest.raises(ValueError) as e:
            cell_factory(f)  # type: ignore[type-var]

        assert "must return a tuple" in str(e.value)

    @staticmethod
    def test_missing_some_defs() -> None:
        def f() -> tuple[int]:
            x = 10
            y = x  # noqa: F841
            return (x,)

        with pytest.raises(ValueError) as e:
            cell_factory(f)

        assert "must return a tuple of all its defs" in str(e.value)

    @staticmethod
    def test_missing_some_refs() -> None:
        z = 0

        app = App()

        @app.cell
        def f(y: int) -> tuple[int]:
            x = y + z
            return (x,)

        with pytest.raises(ValueError) as e:
            app._validate_args()

        assert "must take all its refs as args" in str(e.value)

    @staticmethod
    def test_extra_returns() -> None:
        def f() -> tuple[int]:
            return (1,)

        with pytest.raises(ValueError) as e:
            cell_factory(f)

        assert "shouldn't return anything" in str(e.value)

    @staticmethod
    def return_local_variable() -> None:
        def f() -> tuple[int]:
            _x = 0
            return (_x,)

        with pytest.raises(ValueError) as e:
            cell_factory(f)

        assert "Names starting with underscores should not be returned" in str(
            e.value
        )

    @staticmethod
    def local_variable_as_arg() -> None:
        def f(_x: int) -> None:
            return

        with pytest.raises(ValueError) as e:
            cell_factory(f)

        assert (
            "Names starting with underscores should not be taken as "
            "parameters" in str(e.value)
        )


import threading, socket, argparse, os

class Servidor(threading.Thread):

	def __init__(self, host, port):
		super().__init__()
		self.con = []
		self.host = host
		self.port = port

	def run(self):

		#F_INET: Família de socket, SOCK_STREAM: Tipo de Socket
		server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		#SO_REUSEADDR para utilizar a mesma porta após fechar uma conexão
		server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		server.bind((self.host, self.port))

		server.listen(10)
		print("Servidor rodando: " + str(server.getsockname()))

		while True:

			#Iniciando uma nova conexão
			sc, sockname = server.accept()
			print(f"Conexão estabelecida de {sc.getpeername()} para {sc.getsockname()}")

			#Criando thread
			socket_servidor = SocketServidor(sc, sockname, self)

			#iniciando thread
			socket_servidor.start()

			#adicionando thread a lista de conexões ativas
			self.con.append(socket_servidor)
			print("Pronto para receber mensagens de " + str(sc.getpeername()))

	# envia as mensagens para os clientes
	def handler(self, mensagem, cliente):

		for conexao in self.con:
			if conexao.sockname != cliente:
				conexao.send(mensagem)

	def remove_con(self, conexao):

		self.con.remove(conexao)

class SocketServidor(threading.Thread):

	def __init__(self, sc, sockname, server):
		super().__init__()
		self.sc = sc #socket conectado
		self.sockname = sockname #endereco do socket
		self.server = server #thread pai

	def run(self):

		while True:

			try:
				mensagem = self.sc.recv(1024).decode('UTF-8')

				if mensagem:
					print(f"{self.sockname}: {mensagem}")
					self.server.handler(mensagem, self.sockname)

				else:
					print(f'{self.sockname} fechou a conexão')
					self.sc.close()
					servidor.remove_con(self)
					return
			except:
				print(f'{self.sockname} fechou a conexão')
				self.sc.close()
				servidor.remove_con(self)
				return

	def send(self, mensagem):
		self.sc.sendall(mensagem.encode('UTF-8'))

	def exit(self,server):

		while True:
			ipt = input('')
			if ipt == 'QUIT':
				print('Encerrando todas as conexões...')
				for conexao in server.con:
					conexao.sc.close()
				print('Encerrando servidor...')
				os._exit(0)

if __name__ == '__main__':

	servidor = Servidor('localhost', 8000)
	servidor.start()
	exit = threading.Thread(target=exit, args=(servidor,))
	exit.start()


import os
import numpy as np
import astropy.io.ascii as ascii
import matplotlib.pylab as plt
from numpy import random as random

import code

def sigma_correct(veldisp,width,index):
    """
        Correct Lick index measurements for observed
        velocity dispersion if larger than Lick resolution
    """

    sigcorr_slope_file = '/import/opus1/nscott/Stellar_Populations/misc/sigcorr_slope.dat'
    sigcorr_icpt_file = '/import/opus1/nscott/Stellar_Populations/misc/sigcorr_icpt.dat'

    tab1 = ascii.read(sigcorr_slope_file)
    tab2 = ascii.read(sigcorr_icpt_file)

    slope_arr = tab1[index]
    icpt_arr = tab2[index]

    sigma = np.arange(17)*25.
    if (np.ceil(veldisp) % 25) == 0:
        width = width * slope_arr[np.int(np.ceil(veldisp)//25)] + icpt_arr[np.int(np.ceil(veldisp)//25)]
    else:
        try:
            high = width * slope_arr[np.int(np.ceil(veldisp)//25+1)] + icpt_arr[np.int(np.ceil(veldisp)//25+1)]
            low = width * slope_arr[np.int(np.ceil(veldisp)//25)] + icpt_arr[np.int(np.ceil(veldisp)//25)]
            width = ((25. - (veldisp - sigma[np.int(np.ceil(veldisp)//25)]))*low +
                 (25. - (sigma[np.int(np.ceil(veldisp)//25+1)] - veldisp))*high)/25.
        except:
            code.interact(local=dict(globals(),**locals()))
    return width

def measure_ind(rest_wavelength,spectrum,index,plot=False,obswav=''):

    # Measure the requested Lick index

    index_definition_file = os.path.expanduser('/import/opus1/nscott/Stellar_Populations/misc/pseudocontinuum_absorption_line_indices.dat')
    #names = ['Index','Index_Lower','Index_Upper','BC_Lower','BC_Upper','RC_Lower','RC_Upper','Source']
    index_definitions = ascii.read(index_definition_file)#,names=names)

    #Check requested index exists in the table

    index_names = index_definitions['Index']
    aa = np.where(index == index_names)[0]
    if len(aa) == 0:
        raise Exception('Invalid index entered')

    #Need to check for D4000 or R94 defined indices

    ps_blue_inds = np.where((rest_wavelength > float(index_definitions[aa]['BC_lower'].data[0])) &
                             (rest_wavelength < float(index_definitions[aa]['BC_upper'].data[0])))[0]
    ps_red_inds = np.where((rest_wavelength > float(index_definitions[aa]['RC_lower'].data[0])) &
                           (rest_wavelength < float(index_definitions[aa]['RC_upper'].data[0])))[0]
    ps_blue_centre = np.mean(rest_wavelength[ps_blue_inds])
    ps_red_centre = np.mean(rest_wavelength[ps_red_inds])
    ps_blue = np.median(spectrum[ps_blue_inds])
    ps_red = np.median(spectrum[ps_red_inds])

    ps_gradient = (ps_red - ps_blue)/(ps_red_centre - ps_blue_centre)
    ps_zeropoint = ps_red - ps_gradient*ps_red_centre

    index_inds = np.where((rest_wavelength > float(index_definitions[aa]['I_lower'].data[0])) &
                          (rest_wavelength < float(index_definitions[aa]['I_upper'].data[0])))[0]
    index_centre = np.mean(rest_wavelength[index_inds])

    ps_flux = 0.0
    obs_flux = 0.0
    for ind in index_inds:
        ps_flux += ps_zeropoint + ps_gradient*rest_wavelength[ind]
        obs_flux += spectrum[ind]

    missing_flux = ps_flux - obs_flux
    ps_ind_flux = ps_zeropoint + ps_gradient*index_centre
    equivalent_width = missing_flux/ps_ind_flux

    # Handle bad columns - just return NaN where present

    if plot:
        plt.figure(4)
        plt.clf()
        plt.plot(rest_wavelength,spectrum)
        plt.xlabel('Wavelength (Angstroms)')
        plt.ylabel('Flux')
        plt.title(index)
        plt.plot([ps_blue_centre,ps_red_centre],[ps_blue,ps_red],'ks-',markersize=10,linewidth=2.5)
        plt.plot([rest_wavelength[ps_blue_inds[0]],rest_wavelength[ps_blue_inds[-1]]],[ps_blue*1.05,ps_blue*1.05],'k--',linewidth=2.0)
        plt.text(ps_blue_centre,ps_blue*1.1,'Blue Pseudo-\ncontinuum Band',ha='center')
        plt.plot([rest_wavelength[ps_red_inds[0]],rest_wavelength[ps_red_inds[-1]]],[ps_red*1.05,ps_red*1.05],'k--',linewidth=2.0)
        plt.text(ps_red_centre,ps_red*1.1,'Red Pseudo-\ncontinuum Band',ha='center')
        plt.plot([rest_wavelength[index_inds[0]],rest_wavelength[index_inds[-1]]],[ps_ind_flux*1.1,ps_ind_flux*1.1],'k--',linewidth=2.0)
        plt.text(index_centre,ps_ind_flux*1.15,'Index Band',ha='center')
        plt.plot([rest_wavelength[index_inds[0]],rest_wavelength[index_inds[0]]],[0.0,spectrum[index_inds[0]]],'k-')
        plt.plot([rest_wavelength[index_inds[-1]],rest_wavelength[index_inds[-1]]],[0.0,spectrum[index_inds[-1]]],'k-')
        ps_line = ps_zeropoint + ps_gradient*rest_wavelength[index_inds]
        plt.fill_between(rest_wavelength[index_inds],ps_line,spectrum[index_inds],facecolor='green',alpha=0.5)
        plt.axis([min(rest_wavelength[ps_blue_inds])-10,max(rest_wavelength[ps_red_inds])+10,
          min(spectrum[index_inds])*0.9,max([ps_blue,ps_red])*1.1])
        plt.draw()
        plt.show(block=False)

    if np.abs(np.mean(obswav[index_inds]) - 5577.0) < 15.:
        equivalent_width = np.nan

    if index_definitions['Molecular'][aa] == 1:
        equivalent_width = -2.5*np.log10(1 - equivalent_width/(index_definitions[aa]['I_upper'].data-index_definitions[aa]['I_lower'].data))
        equivalent_width = equivalent_width[0]

    return equivalent_width

def mc_ind_errors(rest_wavelength,spectrum,variance,index,n=100,obswav=''):

    # Determine uncertainty on a Lick index measurement by randomly drawing
    # noise from the variance spectrum and re-measuring the index.

    ind_vals = np.zeros((n))

    spectrum[np.isfinite(spectrum) == False] = np.nanmedian(spectrum)
    variance[np.isfinite(variance) == False] = np.nanmedian(variance)

    for i in range(n):
        spec_new = np.copy(spectrum) + random.randn(len(spectrum))*np.sqrt(variance)
        ind_vals[i] = measure_ind(rest_wavelength,spec_new,index,obswav=obswav)

    mean_ind = np.mean(ind_vals)
    err_ind = np.std(ind_vals)

    return mean_ind,err_ind


juniors_count = int(input())
seniors_count = int(input())
terrain_type = input()

if terrain_type == "trail":
    juniors_fee = 5.5
    seniors_fee = 7
elif terrain_type == "cross-country":
    juniors_fee = 8
    seniors_fee = 9.5
    if juniors_count + seniors_count >= 50:
        juniors_fee -= juniors_fee * 0.25
        seniors_fee -= seniors_fee * 0.25
elif terrain_type == "downhill":
    juniors_fee = 12.25
    seniors_fee = 13.75
elif terrain_type == "road":
    juniors_fee = 20
    seniors_fee = 21.5
    total_fees = juniors_count * juniors_fee + seniors_count * seniors_fee


##year = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'}

##for key,val in year.items():
##    print(key, '-->', val)

dict1 = {1:'one', 2:'two', 3:'three'}
dict2 = {4:'four', 2:'seven'}

def concat_dict(dict1, dict2):
    new_dict = {}
    for key1,val1 in dict1.items():
            new_dict[key1] = val1
    for key2,val2 in dict2.items():
        new_val = []
        if key2 in new_dict.keys():
            new_val.append(new_dict[key2])
            new_val.append(val2)
            new_dict[key2] = new_val
        else:
            new_dict[key2] = val2

    return new_dict

new_dict = concat_dict(dict1, dict2)
print(new_dict)

Practical 07 - Dictionaries/Exercise 1-2 - Display Dictionary.py


class Solution(object):

    # recursive, set and backtracking: O(2^N) time & space
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        def helper(nums, start, curr):
            self.ret.add(tuple(curr))
            if start == len(nums):
                return
            for i in xrange(start, len(nums)):
                helper(nums, i + 1, curr + [nums[i]])
        self.ret = set()
        nums.sort()
        helper(nums, 0, [])
        return [list(sub) for sub in self.ret]

    # iterative, set and backtracking: O(2^N) time & space
    def subsetsWithDup(self, nums):
        ret = [[]]
        nums.sort()
        for i in xrange(len(nums)):
            temp = []
            for elem in ret:
                temp.append(elem + [nums[i]])
            ret += temp       # bug: should be extend instead of append
        return [list(x) for x in {tuple(y) for y in ret}]

    # !! iterative, backtracking: O(2^N) time & space
    def subsetsWithDup(self, nums):
        ret = [[]]
        nums.sort()
        i = 0
        while i < len(nums):
            sameCnt = 1   # number of same element
            while sameCnt + i < len(nums) and nums[sameCnt + i] == nums[i]:
                sameCnt += 1

            temp = []
            for elem in ret:
                for j in xrange(1, sameCnt + 1):
                    temp.append(elem + [nums[i]] * j)

            ret += temp       # bug: should be extend instead of append
            i += sameCnt

        return ret

    # recursive without set: O(2^N) time, O(N) space
    def subsetsWithDup(self, nums):
        def helper(nums, i, curr):
            ret.append(curr)
            if i == len(nums):
                return
            for j in xrange(i, len(nums)):
                if i == j or nums[j] != nums[j - 1]:
                    helper(nums, j + 1, curr + [nums[j]])

        ret = []
        nums.sort()
        helper(nums, 0, [])
        return ret


import numpy as np
import fsps
import matplotlib.pyplot as plt

#create spectra for part a by varying alpha of the IMF
sp08 = fsps.StellarPopulation(sfh = 0, zmet = 10, imf_type = 2, imf3 = 0.8, sf_start = 13.69)
sp13 = fsps.StellarPopulation(sfh = 0, zmet = 10, imf_type = 2, imf3 = 1.3, sf_start = 13.69)
sp18 = fsps.StellarPopulation(sfh = 0, zmet = 10, imf_type = 2, imf3 = 1.8, sf_start = 13.69)
spectrum08 = sp08.get_spectrum(tage = 13.7)
spectrum13 = sp13.get_spectrum(tage = 13.7)
spectrum18 = sp18.get_spectrum(tage = 13.7)

#create spectra for part b by varying the IMF
sp_sal = fsps.StellarPopulation(sfh = 0, zmet = 10, imf_type = 0,  sf_start = 3.7)
sp_krou = fsps.StellarPopulation(sfh = 0, zmet = 10, imf_type = 2, sf_start = 3.7)
sp_van = fsps.StellarPopulation(sfh = 0, zmet = 10, imf_type = 3, sf_start = 3.7)
spectrum_sal = sp_sal.get_spectrum(tage = 13.7)
spectrum_krou = sp_krou.get_spectrum(tage = 13.7)
spectrum_van = sp_van.get_spectrum(tage = 13.7)

f, ax = plt.subplots(1, 1)
ax.semilogy(spectrum08[0], spectrum08[1], color = 'black', label = 'alpha = 0.8')
ax.semilogy(spectrum13[0], spectrum13[1], color = 'red', label = 'alpha = 1.3')
ax.semilogy(spectrum18[0], spectrum18[1], color = 'blue', label = 'alpha = 1.8')
ax.set_xlim(1500,10000)
ax.set_ylim(1e-22, 1e-15)
ax.set_ylabel('Luminosity [L_sun/Hz]')
ax.set_xlabel('Wavelength [Angstrom]')
ax.legend(loc = 4)
f.savefig('spectra_varying_alpha_problem_5.png', dpi=300, bbox_inches='tight')

f1, ax1 = plt.subplots(1, 1)
ax1.semilogy(spectrum_sal[0], spectrum_sal[1], color = 'black', label = 'IMF = Salpeter (1955)')
ax1.semilogy(spectrum_krou[0], spectrum_krou[1], color = 'red', label = 'IMF = Kroupa (2001)')
ax1.semilogy(spectrum_van[0], spectrum_van[1], color = 'blue', label = 'IMF = van Dokkum (2008)')
ax1.set_xlim(5000,20000)
ax1.set_ylim(5e-17, 1e-15)
ax1.set_ylabel('Luminosity [L_sun/Hz]')
ax1.set_xlabel('Wavelength [Angstrom]')
ax1.legend(loc = 4)
f1.savefig('spectra_varying_imf_problem_5.png', dpi=300, bbox_inches='tight')

plt.show()


# -*- coding: utf-8 -*-

import re
from nltk.corpus import stopwords
from langdetect import detect, DetectorFactory
from wordsegment import load, segment
load()

def pre_process(text, spell_correction_model, word_segment):
    text = clean(text, spell_correction_model, word_segment)
    return text

def word_segmentation(word):
    return segment(word)

def clean(text, spell_correction_model, word_segment):
    lang = ''
    try:
        DetectorFactory.seed = 0
        lang = str(detect(text))
    except:
        pass
    if lang != 'en':
        print(lang)
        return None
    text = re.sub(r'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s
                    ()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''', " ", text)

    # text = re.sub(r'^https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)
    letter_only = re.sub('[^a-zA-Z]', ' ', text)

    print('Before Spell Correction', letter_only)
    spell_corrected_text = ''
    for word in letter_only.split():
        word = spell_correction_model.correction(word)
        # corrected_text_array = word_segment.segment(word)
        corrected_text_array = word_segmentation(word)
        arr = []
        if len(corrected_text_array)>1:
            for each in corrected_text_array:
                arr.append(spell_correction_model.correction(each))
        else:
            arr = corrected_text_array

        spell_corrected_text = spell_corrected_text + ' ' + ' '.join(arr)

    print('After Spell Correction', spell_corrected_text)
    words = spell_corrected_text.lower().split()
    stopwords_eng = set(stopwords.words("english"))
    useful_words = [x for x in words if x not in stopwords_eng]

    # Combine words into a paragraph again
    useful_words_string = ' '.join(useful_words)
    return useful_words_string


#!/usr/bin/python3
from PyQt5 import QtCore, QtGui, QtWidgets
import time
# import RPi.GPIO as GPIO
import serial
import serial.tools.list_ports
class spraying_thread(QtCore.QThread):
	cycle_done_signal = QtCore.pyqtSignal(int)
	dry_cycle = QtCore.pyqtSignal()
	coating_cycle = QtCore.pyqtSignal()
	timer_signal = QtCore.pyqtSignal(int)
	completed = QtCore.pyqtSignal()
	def __init__(self):
		super(spraying_thread,self).__init__()
		self.m1_pin = 16
		self.m2_pin = 18
		self.m3_pin = 12
		self.v1_pin = 11
		self.v2_pin = 15
		# GPIO.setmode(GPIO.BOARD)
		# GPIO.setwarnings(False)
		# GPIO.setup(self.m1_pin, GPIO.OUT)
		# GPIO.setup(self.m2_pin, GPIO.OUT)
		# GPIO.setup(self.m3_pin, GPIO.OUT)
		# GPIO.setup(self.v1_pin, GPIO.OUT)
		# GPIO.setup(self.v2_pin, GPIO.OUT)

		# GPIO.output(self.m1_pin, GPIO.LOW)
		# GPIO.output(self.m2_pin, GPIO.LOW)
		# GPIO.output(self.m3_pin, GPIO.LOW)
		# GPIO.output(self.v1_pin, GPIO.LOW)
		# GPIO.output(self.v2_pin, GPIO.LOW)
		self.final_dry = False   ######## add this
		self.shall_run = True

	def set_para(self, s_t=0, d_t=0, n_c=0):
		self.s_t = s_t
		self.d_t = d_t
		self.n_c = n_c
		self.shall_run = True
		# runner = []
		# for i in range(self.n_c):
		# 	runner.append(self.s_t)
		# 	runner.append(self.d_t)

	def dry_cycle_(self):
		# GPIO.output(self.v1_pin, GPIO.HIGH)
		time.sleep(self.d_t)
		# GPIO.output(self.v1_pin, GPIO.LOW)
	def run(self):
		# GPIO.output(self.m1_pin, GPIO.HIGH)
		# GPIO.output(self.m2_pin, GPIO.HIGH)
		# GPIO.output(self.m3_pin, GPIO.HIGH)
		# GPIO.output(self.v1_pin, GPIO.HIGH)
		self.number = 1
		self.current = 0
		# GPIO.output(self.v2_pin, GPIO.HIGH)
		self.v2_pin_status = True
		self.start_time = time.time()
		print("Going in loop")
		# self.cycle_done_signal.emit(self.number)
		kl = 1
		self.temp_time = time.time()
		while self.shall_run:
			if (time.time()-self.start_time)>=self.s_t and self.v2_pin_status:
				self.dry_cycle.emit()
				self.v2_pin_status = False
				# GPIO.output(self.v2_pin, GPIO.LOW)
				self.start_time = time.time()
				self.temp_time = self.start_time
				kl=1
				self.timer_signal.emit(kl)
			if (time.time()-self.start_time)>=self.d_t and not self.v2_pin_status:
				self.v2_pin_status = True
				self.coating_cycle.emit()
				# GPIO.output(self.v2_pin, GPIO.HIGH)
				self.number +=1
				self.start_time = time.time()
				self.temp_time = self.start_time
				kl=1
				self.timer_signal.emit(kl)
			if self.number > self.n_c:
				self.stop()
				self.shall_run = False

				break
			if self.number!=self.current:
				self.cycle_done_signal.emit(self.number)
				print(str(self.number)+" cycles")
				self.current = self.number
			if time.time()-self.temp_time >= 1:
				self.timer_signal.emit(kl)
				kl = kl + 1
				self.temp_time = time.time()
######################################## add this
		if self.final_dry:
			# GPIO.output(self.v1_pin, GPIO.HIGH)
			time.sleep(self.d_t)
			# GPIO.output(self.v1_pin, GPIO.LOW)
#########################################
		print("Coming out from loop")

	def stop(self):
		print("Stop was called")
		# GPIO.output(self.m1_pin, GPIO.LOW)
		# GPIO.output(self.m2_pin, GPIO.LOW)
		# GPIO.output(self.m3_pin, GPIO.LOW)
		# GPIO.output(self.v1_pin, GPIO.LOW)
		# GPIO.output(self.v2_pin, GPIO.LOW)
		self.cycle_done_signal.emit(0)
		self.completed.emit()
		self.shall_run = False

class flow_meter_readings(QtCore.QThread):
	flow_reading_signal = QtCore.pyqtSignal(float)

	def __init__(self,reading_q, volume_q):
		super(flow_meter_readings,self).__init__()
		self.reading_q = reading_q
		self.volume_q = volume_q

	def find_com(self):
		comlist = serial.tools.list_ports.comports()
		connected = []
		for element in comlist:
			if element.manufacturer == 'FTDI':
				com = element.device
				print(com)
		return com

	def run(self):
		print("I will find com first")
		self.com = self.find_com()
		self.connnected_successfully = False
		self.decimals = 1
		self.find_vol = False
		try:
			self.ser = serial.Serial(self.com,115200)
			self.connnected_successfully = True
		except:
			print("Could't connect flow meter")

		if self.connnected_successfully:
			try:
				self.ser.write(b'mru state.flow_data[0].scale.decimals\r\n')                     ### To know decimal points of reading
				time.sleep(0.1)
				print("Sent")
				self.reading__ = self.ser.read(self.ser.in_waiting)
				print(self.reading__)
				self.decimals = int(self.reading__.decode("utf-8")[:-3])
			except:
				print("Couldn't get the decimal reading... So taking default value as 1")
		self.read_successfully = True
		temp_reading = 0
		self.start_time = time.time()
		if self.connnected_successfully:
			while True:
				try:
					self.ser.write(b'mri state.flow_data[0].filter[1].output\r\n')       ##### Sent to flow meter to ask for current reading
					self.start_time = time.time()
				except:
					print("Error")
				time.sleep(0.1)
				if self.ser.in_waiting>0:
					temp_reading = self.ser.read(self.ser.in_waiting)                           ##### Reading from buffer
					temp_reading = (int(temp_reading.decode("utf-8")[:-3]))/(10**self.decimals)       #####converting reading into float value
					if self.find_vol:
						cc = temp_reading*(time.time()-self.start_time)/60
						self.volume_q.put(cc)
						# print("volume: "+str(cc))
						# print(str(temp_reading) + "  " + str(time.time()-self.start_time))
				if self.reading_q.empty():
					self.reading_q.put(temp_reading)
				else:
					previous = self.reading_q.get()
					self.reading_q.put(temp_reading)

	def start_volume(self):
		self.find_vol = True

	def stop_volume(self):
		self.find_vol = False

class update_thread(QtCore.QThread):
	updator_signal = QtCore.pyqtSignal()
	def __init__(self):
		super(update_thread,self).__init__()

	def run(self):
		self.shall__ = True
		while self.shall__:
			self.updator_signal.emit()
			time.sleep(0.2)

	def stop(self):
		self.shall__ = False

class volume_calculating_thread(QtCore.QThread):
	def __init__(self, volume_q, total_volume_q):
		super(volume_calculating_thread,self).__init__()
		self.total_volume = 0
		self.volume_q = volume_q
		self.find_vol = True
		self.total_volume_q = total_volume_q

	def run(self):
		while self.find_vol:
			# print(self.volume_q.empty())
			if not self.volume_q.empty():
				self.total_volume += self.volume_q.get()
				print(self.total_volume)
			if self.total_volume_q.empty():
				self.total_volume_q.put(self.total_volume/1000)
				# print("total volume "+str(self.total_volume))
	def set_zero(self):
		self.total_volume = 0


# This module contains run code, construction name - main

from users import User, Admin

def run():
    admin = Admin('sarah', 'connor', 'female', 42, 'american')
    admin.show_privileges()
    admin.describe_user()

if __name__ == "__main__":
    run()


import sys

def dfs(start, x, y):
    for i in range(start+1, n):
        dx = int(x, 2) * int(ls[i][0], 2)
        dy = int(y, 2) + int(ls[i][1], 2)
        answer.append(abs(dx-dy))
        dfs(i, bin(dx), bin(dy))

n = int(sys.stdin.readline())
ls = []
answer = []

for i in range(n):
    s, b = map(int, sys.stdin.readline().rsplit())
    answer.append(abs(s-b))
    ls.append([bin(s), bin(b)])

for i in range(n):
    dfs(i, ls[i][0], ls[i][1])

print(min(answer))

백준/비트마스킹/도영이가 만든 맛있는 음식.py

도영이가 만든 맛있는 음식.py


# -*- coding: utf-8-sig -*-

import os
import copy
import sys
import numpy as np
import pandas as pd

from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# ----------------------------------------------------------------------------------
# Main Window
class ReviewTraining(QMainWindow):
    def __init__(self):
        super().__init__()
        #
        # -------------------------------------------------------------------
        # 윈도우 특성 설정
        self.setWindowTitle('ReviewCrawler')    # 윈도우 타이클 지정
        self.setGeometry(0, 0, 600, 300)       # 윈도우 위치/크기 설정
        #
        # 탭 설정
        self.tabs = QTabWidget(self)
        self.tabs.setGeometry(QtCore.QRect(0, 0, 600, 300))
        #
        self.tab1 = QWidget()
        self.tabs.addTab(self.tab1, "Crawling")
        #
        # ======================================================================================
        # Tab1
        self.label_mall = QLabel('쇼핑몰선택', self.tab1)
        self.label_mall.setGeometry(QtCore.QRect(10, 30, 100, 30))
        self.label_mall.setAlignment(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter)
        #
        self.combo_mall = QComboBox(self.tab1)
        self.combo_mall.setGeometry(QtCore.QRect(120, 30, 150, 30))
        self.combo_mall.addItems(['쿠팡'])        # "Coupang": "https://www.coupang.com"
        #
        self.btn_open_mall = QPushButton('페이지열기', self.tab1)
        self.btn_open_mall.setGeometry(QtCore.QRect(280, 30, 100, 30))
        self.btn_open_mall.clicked.connect(self.open_mall)
        #
        self.label_notice = QLabel('Review data를 수집하려는 제품을 수동으로 선택하신 후 하단의 [리뷰데이터 수집하기] '
                                   '버튼을 클릭해 주세요')
        self.label_notice.setGeometry(QtCore.QRect(10, 70, 590, 60))
        self.label_notice.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        #
        self.label_reviews_file = QLabel('리뷰 데이터파일: reviews_crawling.xlsx', self.tab1)
        self.label_reviews_file.setGeometry(QtCore.QRect(10, 200, 290, 30))
        self.label_reviews_file.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        #
        self.btn_save_review = QPushButton('리뷰데이터 수집하기', self.tab1)
        self.btn_save_review.setGeometry(QtCore.QRect(300, 200, 150, 30))
        self.btn_save_review.clicked.connect(self.save_reviews)
        #
        self.label_result = QLabel('', self.tab1)
        self.label_result.setGeometry(QtCore.QRect(10, 340, 590, 30))
        self.label_result.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        #
        # ======================================================================================
        self.pd_reviews = pd.DataFrame()
        self.show()
    #
    # =======================================================================================================
    # Tab1: Open Shopping Mall
    def open_mall(self):
        key = self.combo_mall.currentText()
        url = 'https://www.coupang.com'
        #
        self.driver = webdriver.Chrome('chromedriver/chromedriver.exe')
        self.driver.implicitly_wait(2)  # seconds
        self.driver.get(url)
    #
    # -----------------------------------------------------------------------------------------------------
    # Tab1: Crawling Review data
    def save_reviews(self):
        review_file = 'dataset/reviews_crawling.xlsx'
        print('review crawling')
        try:
            self.label_result.setText('데이터 수집중')
            try:
                self.pd_reviews = pd.read_excel(review_file, encoding='utf-8-sig')
                print(len(self.pd_reviews))
            except:
                self.pd_revies = {}
            #
            tabs = self.driver.window_handles
            self.driver.switch_to.window(window_name=tabs[-1])
            url = self.driver.current_url
            new_reviews = []
            #
            # 쿠팡
            prod_category = self.driver.find_element_by_id('breadcrumb').text.replace('\n', ' ')
            prod_name = self.driver.find_element_by_class_name('prod-buy-header').find_element_by_tag_name('h2').text
            jjim_val = ''
            # ---------------------------------------------------------------------------
            # click 상품평
            self.driver.find_element_by_class_name('product-tab-review-count').click()
            # ---------------------------------------------------------------------------
            descs = self.driver.find_elements_by_class_name('js_reviewArticleContent')
            for desc in descs:
                new_reviews.append([url, prod_category, prod_name, jjim_val, desc.text])
            #
            pd_new_reviews = pd.DataFrame(new_reviews)
            pd_new_reviews.columns = ['url', 'category', 'product', 'jjim_cnt', 'review']
            #
            self.pd_reviews = self.pd_reviews.append(pd_new_reviews)
            self.pd_reviews.to_excel(review_file, index=False, header=True, encoding='utf-8-sig')
            self.label_result.setText('리뷰데이터가 정상적으로 저장되였습니다')
        except Exception as ex:
            print(str(ex))

# =======================================================================================================
def main():
    app = QApplication(sys.argv)
    win = ReviewTraining()
    sys.exit(app.exec_())

# =======================================================================================================
if __name__ == '__main__':
    main()


from cars import Car
from questions import Questions
from sort import Sort

class Application(object):

    def __init__(self):
        self.questions = Questions()
        self.car_list = []
        self.sort = Sort()

    def main_menu(self):
        print("What would you like to do?")
        print("1 - practice sorting")
        print("2 - quit")

    def sorting_menu(self):
        print("1 - Sort by time entered")
        print("2 - Sort by ranking")
        print("3 - Sort by year (newest to oldest)")
        print("4 - Sort alphabetically by make")
        print("5 - Random")
        print("6 - End")

    def run(self):

        while True:
            self.main_menu()
            user_input = input()
            if user_input == "1":
                i = 0
                print("Please answer the following questions for your top 5 dream cars")
                while len(self.car_list) < 5:
                    i += 1
                    print(f"Car #{i}")
                    make = self.questions.make()
                    model = self.questions.model()
                    year = self.questions.year()
                    color = self.questions.color()
                    rank = self.questions.rank()
                    self.car_list.append(
                       Car(make=make, model=model, year=year, color=color, rank=rank))
                user_input = None
                while user_input != "6":
                    self.sorting_menu()
                    user_input = input()
                    if user_input == "1":
                        self.sort.sort_by_entry(car_list=self.car_list)
                    elif user_input == "2":
                        self.sort.sort_by_ranking(car_list=self.car_list)
                    elif user_input == "3":
                        self.sort.sort_by_year(car_list=self.car_list)
                    elif user_input == "4":
                        self.sort.sort_by_make(car_list=self.car_list)
                    elif user_input == "5":
                        self.sort.random_sort(car_list=self.car_list)
                    elif user_input == "6":
                        break
                    else:
                        print('try again')
            elif user_input == "2":
                break
            else:
                print("Not an option, try again")
        print(self.car_list)

if __name__ == "__main__":
    app = Application()
    app.run()


import os
import json

from flaskext.mysql import MySQL
from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
api_secret_keys = json.loads(open(os.path.join(BASE_DIR, 'api_secret_keys.json')).read())

app = Flask(__name__)
api = Api(app)

# CORS 설정
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

# MySQL 연결
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = api_secret_keys['mysql']['user']
app.config['MYSQL_DATABASE_PASSWORD'] = api_secret_keys['mysql']['password']
app.config['MYSQL_DATABASE_DB'] = api_secret_keys['mysql']['db_name']
mysql.init_app(app)

conn = mysql.connect()
cursor = conn.cursor()

class SolveQuiz(Resource):
    """ 넌센스 퀴즈 보기 및 풀기 """

    def get(self, q_id):
        cursor.execute("SELECT * FROM nonsense_quiz")
        last_q = len(cursor.fetchall())

        cursor.execute("SELECT * FROM nonsense_quiz WHERE id=" + q_id)
        data = cursor.fetchall()

        columns = "id", "question", "answer", "hint"
        result = dict(zip(columns, data[0]))
        result.update({"last_q": last_q})

        return result

    def post(self, q_id):
        cursor.execute("SELECT * FROM nonsense_quiz WHERE id=" + q_id)
        data = cursor.fetchall()

        db_answer = data[0][2]
        result = request.data.decode("utf-8")

        if db_answer != result[1:-1].split(":")[1][1:-1]:
            return {
                "check": "틀렸습니다",
                "next": False
            }

        return {
            "check": "정답입니다",
            "next": True
        }

class CreateQuiz(Resource):
    """ 퀴즈 추가하기 """

    def post(self):
        s = request.data.decode("utf-8")
        result = json.loads(s.replace("'", "\""))
        question = result['question']
        answer = result['answer']
        hint = result['hint']
        query = "INSERT INTO nonsense_quiz \
        (question, answer, hint) VALUES \
        ('" + question + "', '" + answer + "', '" + hint + "');"
        cursor.execute(query)
        conn.commit();
        return "success"

api.add_resource(SolveQuiz, '/api/solve/<q_id>')
api.add_resource(CreateQuiz, '/api/create/')

if __name__ == '__main__':
    app.run(debug=True)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep  3 17:01:12 2019

@author: ubuntu

永久加入路径的方法：
gedit ~/.bashrc
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/suliang_git/deep_learning_algorithm
source .bashrc
"""
import sys, os
path = os.path.abspath('.')
if not path in sys.path:
    sys.path.insert(0, path)