# Python bytecode 2.7 (decompiled from Python 2.7)
# Embedded file name: scripts/client/gui/customization/context.py
from collections import defaultdict
from functools import partial
from collections import namedtuple
import Event
from CurrentVehicle import g_currentVehicle
from gui.shared.utils.decorators import process
from gui import g_tankActiveCamouflage, SystemMessages
from gui.Scaleform.daapi.view.lobby.customization.customization_carousel import comparisonKey
from gui.Scaleform.daapi.view.lobby.customization.shared import C11nMode, C11nTabs, TABS_ITEM_MAPPING, TYPE_TO_TAB_IDX, SEASON_IDX_TO_TYPE, SEASON_TYPE_TO_IDX, getStylePurchaseItems, getStyleInventoryCount, getItemInventoryCount, SEASONS_ORDER, OutfitInfo, getCustomPurchaseItems, AdditionalPurchaseGroups, getOutfitWithoutItems
from gui.Scaleform.locale.SYSTEM_MESSAGES import SYSTEM_MESSAGES
from gui.shared.gui_items import GUI_ITEM_TYPE
from gui.shared.gui_items.processors.common import OutfitApplier, StyleApplier, CustomizationsSeller
from gui.shared.gui_items.customization.outfit import Area
from items.components.c11n_constants import SeasonType
from helpers import dependency
from shared_utils import nextTick
from skeletons.gui.shared import IItemsCache
from skeletons.gui.customization import ICustomizationService
from gui.customization.shared import createCustomizationBaseRequestCriteria, isServiceItem
from skeletons.gui.server_events import IEventsCache
from gui.customization.shared import getAppliedRegionsForCurrentHangarVehicle
from shared_utils import first
CustomizationRegion = namedtuple('CustomizationRegion', ('slotType', 'areaId', 'regionIdx'))
CustomizationRegion = partial(CustomizationRegion, slotType=-1, areaId=-1, regionIdx=-1)
CaruselItemData = namedtuple('CaruselItemData', ('index', 'intCD'))
CaruselItemData = partial(CaruselItemData, index=-1, intCD=-1)

class CustomizationContext(object):
    itemsCache = dependency.descriptor(IItemsCache)
    service = dependency.descriptor(ICustomizationService)
    eventsCache = dependency.descriptor(IEventsCache)

    @property
    def visibleTabs(self):
        return self.__visibleTabs[self._currentSeason]

    @property
    def currentTab(self):
        return self._tabIndex

    @property
    def currentSeason(self):
        return self._currentSeason

    @property
    def currentOutfit(self):
        return self._currentOutfit

    @property
    def originalOutfit(self):
        return self._originalOutfits[self._currentSeason]

    @property
    def originalMode(self):
        return self._originalMode

    @property
    def mode(self):
        return self._mode

    @property
    def originalStyle(self):
        return self._originalStyle

    @property
    def modifiedStyle(self):
        return self._modifiedStyle

    @property
    def selectedRegion(self):
        return self._selectedRegion

    @property
    def selectedCaruselItem(self):
        return self._selectedCaruselItem

    def __init__(self):
        self._currentSeason = SeasonType.SUMMER
        self.__visibleTabs = defaultdict(set)
        self._tabIndex = C11nTabs.PAINT
        self._lastTab = C11nTabs.PAINT
        self._originalStyle = None
        self._modifiedStyle = None
        self._originalOutfits = {}
        self._modifiedOutfits = {}
        self._currentOutfit = None
        self._originalMode = C11nMode.CUSTOM
        self._mode = self._originalMode
        self._state = {}
        self._selectedCaruselItem = CaruselItemData()
        self._selectedRegion = CustomizationRegion()
        self._eventsManager = Event.EventManager()
        self.onCustomizationSeasonChanged = Event.Event(self._eventsManager)
        self.onCustomizationModeChanged = Event.Event(self._eventsManager)
        self.onCustomizationTabChanged = Event.Event(self._eventsManager)
        self.onCustomizationItemInstalled = Event.Event(self._eventsManager)
        self.onCustomizationItemsRemoved = Event.Event(self._eventsManager)
        self.onCustomizationCamouflageColorChanged = Event.Event(self._eventsManager)
        self.onCustomizationCamouflageScaleChanged = Event.Event(self._eventsManager)
        self.onCustomizationItemsBought = Event.Event(self._eventsManager)
        self.onCustomizationItemSold = Event.Event(self._eventsManager)
        self.onCacheResync = Event.Event(self._eventsManager)
        self.onChangesCanceled = Event.Event(self._eventsManager)
        self.onCaruselItemSelected = Event.Event(self._eventsManager)
        self.onCaruselItemUnselected = Event.Event(self._eventsManager)
        self.onCarouselFilter = Event.Event(self._eventsManager)
        self.onCustomizationRegionSelected = Event.Event(self._eventsManager)
        self.onPropertySheetShown = Event.Event(self._eventsManager)
        self.onPropertySheetHidden = Event.Event(self._eventsManager)
        return

    def changeSeason(self, seasonIdx):
        self._currentSeason = SEASON_IDX_TO_TYPE[seasonIdx]
        self.refreshOutfit()
        self.onCustomizationSeasonChanged(self._currentSeason)

    def refreshOutfit(self):
        if self._mode == C11nMode.STYLE:
            if self._modifiedStyle:
                self._currentOutfit = self._modifiedStyle.getOutfit(self._currentSeason)
            else:
                self._currentOutfit = self.service.getEmptyOutfit()
        else:
            self._currentOutfit = self._modifiedOutfits[self._currentSeason]
        self.service.tryOnOutfit(self._currentOutfit)
        g_tankActiveCamouflage[g_currentVehicle.item.intCD] = self._currentSeason

    def tabChanged(self, tabIndex):
        self._tabIndex = tabIndex
        if self._tabIndex == C11nTabs.EFFECT:
            self._selectedRegion = CustomizationRegion(slotType=GUI_ITEM_TYPE.MODIFICATION, areaId=Area.MISC, regionIdx=0)
        elif self._tabIndex == C11nTabs.STYLE:
            self._selectedRegion = CustomizationRegion(slotType=GUI_ITEM_TYPE.STYLE, areaId=Area.CHASSIS, regionIdx=0)
        else:
            self._selectedRegion = CustomizationRegion()
        self._selectedCaruselItem = CaruselItemData()
        self.onCustomizationTabChanged(tabIndex)

    def regionSelected(self, slotType, areaId, regionIdx):
        if self._tabIndex in (C11nTabs.EFFECT, C11nTabs.STYLE):
            return
        self._selectedRegion = CustomizationRegion(slotType=slotType, areaId=areaId, regionIdx=regionIdx)
        self.onCustomizationRegionSelected(self._selectedRegion.slotType, self._selectedRegion.areaId, self._selectedRegion.regionIdx)
        if areaId != -1 and regionIdx != -1 and self._selectedCaruselItem.intCD != -1:
            self.installItem(self._selectedCaruselItem.intCD, areaId, slotType, regionIdx, SEASON_TYPE_TO_IDX[self.currentSeason])

    def installItem(self, intCD, areaId, slotType, regionId, seasonIdx, component=None):
        item = self.service.getItemByCD(intCD)
        if item.isHidden and not self.getItemInventoryCount(item):
            SystemMessages.pushI18nMessage(SYSTEM_MESSAGES.CUSTOMIZATION_PROHIBITED, type=SystemMessages.SM_TYPE.Warning, itemName=item.userName)
            return False
        if slotType == GUI_ITEM_TYPE.STYLE:
            self._modifiedStyle = item
        else:
            season = SEASON_IDX_TO_TYPE.get(seasonIdx, self._currentSeason)
            outfit = self._modifiedOutfits[season]
            outfit.getContainer(areaId).slotFor(slotType).set(item, idx=regionId, component=component)
            outfit.invalidate()
        self.refreshOutfit()
        self.onCustomizationItemInstalled()
        return True

    def installItemToAllTankAreas(self, season, slotType, currentSlotData):
        currentSeasonIdx = SEASON_TYPE_TO_IDX[season]
        additionalyApplyedItems = 0
        for areaId in Area.TANK_PARTS:
            regionsIndexes = getAppliedRegionsForCurrentHangarVehicle(areaId, slotType)
            multiSlot = self.currentOutfit.getContainer(areaId).slotFor(slotType)
            for regionIdx in regionsIndexes:
                slotData = multiSlot.getSlotData(regionIdx)
                df = currentSlotData.weakDiff(slotData)
                if slotData.item is None or df.item is not None:
                    self.installItem(currentSlotData.item.intCD, areaId, slotType, regionIdx, currentSeasonIdx, currentSlotData.component)
                    additionalyApplyedItems += 1

        return additionalyApplyedItems

    def installItemForAllSeasons(self, areaID, slotType, regionIdx, currentSlotData):
        additionalyApplyedItems = 0
        for season in SeasonType.COMMON_SEASONS:
            slotData = self.getModifiedOutfit(season).getContainer(areaID).slotFor(slotType).getSlotData(regionIdx)
            df = currentSlotData.weakDiff(slotData)
            if slotData.item is None or df.item is not None:
                seasonIdx = SEASON_TYPE_TO_IDX[season]
                self.installItem(currentSlotData.item.intCD, areaID, slotType, regionIdx, seasonIdx)
                additionalyApplyedItems += 1

        return additionalyApplyedItems

    def removeItemFromRegion(self, season, areaId, slotType, regionId):
        outfit = self._modifiedOutfits[season]
        slot = outfit.getContainer(areaId).slotFor(slotType)
        if slot.capacity() > regionId:
            if not isServiceItem(slot.getItem(regionId)):
                slot.remove(idx=regionId)
        self.refreshOutfit()
        self.onCustomizationItemsRemoved()

    def removeItemFromAllTankAreas(self, season, slotType):
        for areaId in Area.TANK_PARTS:
            regionsIndexes = getAppliedRegionsForCurrentHangarVehicle(areaId, slotType)
            for regionIdx in regionsIndexes:
                self.removeItemFromRegion(season, areaId, slotType, regionIdx)

    def removeItemForAllSeasons(self, areaID, slotType, regionIdx):
        for season in SeasonType.COMMON_SEASONS:
            self.removeItemFromRegion(season, areaID, slotType, regionIdx)

    def removeStyle(self, intCD):
        if self._modifiedStyle and self._modifiedStyle.intCD == intCD:
            self._modifiedStyle = None
        self.refreshOutfit()
        self.onCustomizationItemsRemoved()
        return

    def removeItems(self, onlyCurrent, *intCDs):

        def intCdFilter(item):
            return item.intCD in intCDs

        if onlyCurrent:
            self.removeItemsFromOutfit(self._currentOutfit, intCdFilter, False)
        else:
            for outfit in self._modifiedOutfits.itervalues():
                self.removeItemsFromOutfit(outfit, intCdFilter, False)

        self.refreshOutfit()
        self.onCustomizationItemsRemoved()

    def removeItemsFromOutfit(self, outfit, filterMethod=None, refresh=True):
        for slot in outfit.slots():
            for idx in range(slot.capacity()):
                item = slot.getItem(idx)
                if item and (not filterMethod or filterMethod(item)) and not isServiceItem(item):
                    slot.remove(idx)

        if refresh:
            self.refreshOutfit()
            self.onCustomizationItemsRemoved()

    def switchToCustom(self):
        if self._mode != C11nMode.CUSTOM:
            self._mode = C11nMode.CUSTOM
            self._tabIndex = self._lastTab
        self.refreshOutfit()
        self.onCustomizationModeChanged(self._mode)

    def switchToStyle(self):
        if self._mode != C11nMode.STYLE:
            self._mode = C11nMode.STYLE
            self._lastTab = self._tabIndex
            self._tabIndex = C11nTabs.STYLE
        self.refreshOutfit()
        self.onCustomizationModeChanged(self._mode)

    def cancelChanges(self):
        if self._mode == C11nMode.STYLE:
            self.__cancelModifiedStyle()
        else:
            self.__cancelModifiedOufits()
        self.refreshOutfit()
        self.onChangesCanceled()

    def caruselItemSelected(self, index, intCD):
        self._selectedCaruselItem = CaruselItemData(index=index, intCD=intCD)
        if self.isRegionSelected() and self._selectedCaruselItem.intCD != -1:
            self.installItem(self._selectedCaruselItem.intCD, self._selectedRegion.areaId, self._selectedRegion.slotType, self._selectedRegion.regionIdx, SEASON_TYPE_TO_IDX[self.currentSeason])
        self.onCaruselItemSelected(index, intCD)

    def caruselItemUnselected(self):
        if self._selectedCaruselItem.index != -1:
            prevSelectCaruselItem = self._selectedCaruselItem
            self._selectedCaruselItem = CaruselItemData()
            self.onCaruselItemUnselected(prevSelectCaruselItem.index, prevSelectCaruselItem.intCD)

    def applyCarouselFilter(self, **kwargs):
        self.onCarouselFilter(**kwargs)

    def changeCamouflageColor(self, areaId, regionIdx, paletteIdx):
        component = self.currentOutfit.getContainer(areaId).slotFor(GUI_ITEM_TYPE.CAMOUFLAGE).getComponent(regionIdx)
        if component.palette != paletteIdx:
            component.palette = paletteIdx
            self.refreshOutfit()
            self.onCustomizationCamouflageColorChanged(areaId, regionIdx, paletteIdx)

    def changeCamouflageScale(self, areaId, regionIdx, scale):
        component = self.currentOutfit.getContainer(areaId).slotFor(GUI_ITEM_TYPE.CAMOUFLAGE).getComponent(regionIdx)
        if component.patternSize != scale:
            component.patternSize = scale
            self.refreshOutfit()
            self.onCustomizationCamouflageScaleChanged(areaId, regionIdx, scale)

    def getOutfitsInfo(self):
        outfitsInfo = {}
        for season in SEASONS_ORDER:
            outfitsInfo[season] = OutfitInfo(self._originalOutfits[season], self._modifiedOutfits[season])

        return outfitsInfo

    def getStyleInfo(self):
        return OutfitInfo(self._originalStyle, self._modifiedStyle)

    def getPurchaseItems(self):
        return getCustomPurchaseItems(self.getOutfitsInfo()) if self._mode == C11nMode.CUSTOM else getStylePurchaseItems(self.getStyleInfo())

    def getItemInventoryCount(self, item):
        return getItemInventoryCount(item, self.getOutfitsInfo()) if self._mode == C11nMode.CUSTOM else getStyleInventoryCount(item, self.getStyleInfo())

    def getModifiedOutfit(self, season):
        return self._modifiedOutfits.get(season)

    def getAppliedItems(self, isOriginal=True):
        outfits = self._originalOutfits if isOriginal else self._modifiedOutfits
        style = self._originalStyle if isOriginal else self._modifiedStyle
        seasons = SeasonType.COMMON_SEASONS if isOriginal else (self._currentSeason,)
        appliedItems = set()
        for seasonType in seasons:
            outfit = outfits[seasonType]
            appliedItems.update((i.intCD for i in outfit.items()))

        if style:
            appliedItems.add(style.intCD)
        return appliedItems

    def isItemInOutfit(self, item):
        return any((outfit.has(item) for outfit in self._originalOutfits.itervalues())) or any((outfit.has(item) for outfit in self._modifiedOutfits.itervalues()))

    def getNotModifiedItems(self, season):
        df = self._modifiedOutfits[season].diff(self._originalOutfits[season])
        notModifiedItems = df.diff(self._originalOutfits[self.currentSeason])
        return notModifiedItems

    @process('customizationApply')
    def applyItems(self, purchaseItems):
        self.itemsCache.onSyncCompleted -= self.__onCacheResync
        groupHasItems = {AdditionalPurchaseGroups.STYLES_GROUP_ID: False,
         SeasonType.WINTER: False,
         SeasonType.SUMMER: False,
         SeasonType.DESERT: False}
        modifiedOutfits = {season:outfit.copy() for season, outfit in self._modifiedOutfits.iteritems()}
        for pItem in purchaseItems:
            if not pItem.selected:
                if pItem.slot:
                    slot = modifiedOutfits[pItem.group].getContainer(pItem.areaID).slotFor(pItem.slot)
                    slot.remove(pItem.regionID)
            groupHasItems[pItem.group] = True

        if self._mode == C11nMode.CUSTOM:
            groupHasItems[self._currentSeason] = True
        empty = self.service.getEmptyOutfit()
        for season in SeasonType.COMMON_SEASONS:
            if groupHasItems[season]:
                yield OutfitApplier(g_currentVehicle.item, empty, season).request()

        results = []
        for season in SeasonType.COMMON_SEASONS:
            if groupHasItems[season]:
                outfit = modifiedOutfits[season]
                result = yield OutfitApplier(g_currentVehicle.item, outfit, season).request()
                results.append(result)

        if groupHasItems[AdditionalPurchaseGroups.STYLES_GROUP_ID]:
            result = yield StyleApplier(g_currentVehicle.item, self._modifiedStyle).request()
            results.append(result)
        self.onCustomizationItemsBought(purchaseItems, results)
        self.__onCacheResync()
        self.itemsCache.onSyncCompleted += self.__onCacheResync

    @process('sellItem')
    def sellItem(self, intCD, count):
        if not count:
            return
        item = self.service.getItemByCD(intCD)
        if item.fullInventoryCount(g_currentVehicle.item) < count:
            if self._mode == C11nMode.CUSTOM:
                for season, outfit in getOutfitWithoutItems(self.getOutfitsInfo(), intCD, count):
                    yield OutfitApplier(g_currentVehicle.item, outfit, season).request()

            else:
                yield StyleApplier(g_currentVehicle.item).request()
        yield CustomizationsSeller(g_currentVehicle.item, item, count).request()
        nextTick(self.refreshOutfit)()
        nextTick(partial(self.onCustomizationItemSold, item=item, count=count))()

    def init(self):
        self.service.onOutfitChanged += self.__onOutfitChanged
        self.itemsCache.onSyncCompleted += self.__onCacheResync
        self.carveUpOutfits()
        self.__updateVisibleTabsList()
        if self._originalStyle:
            self._mode = C11nMode.STYLE
        else:
            self._mode = C11nMode.CUSTOM
            notInst = all([ not self._originalOutfits[season].isInstalled() for season in SeasonType.COMMON_SEASONS ])
            if notInst and not self.isOutfitsEmpty(self._modifiedOutfits) and not self._modifiedStyle:
                self._mode = C11nMode.STYLE
        self._originalMode = self._mode
        if self._mode == C11nMode.STYLE:
            self._tabIndex = C11nTabs.STYLE
            self._lastTab = first(self.visibleTabs, -1)
        else:
            self._tabIndex = first(self.visibleTabs, -1)
            self._lastTab = self._tabIndex
        self.refreshOutfit()

    def fini(self):
        self.itemsCache.onSyncCompleted -= self.__onCacheResync
        self.service.onOutfitChanged -= self.__onOutfitChanged
        self._eventsManager.clear()

    def checkSlotsFillingForSeason(self, season):
        checkedSlotTypes = (TABS_ITEM_MAPPING[tabId] for tabId in self.visibleTabs)
        return all((self.checkSlotsFilling(slotType, season) for slotType in checkedSlotTypes))

    def checkSlotsFilling(self, slotType, season):
        allSlotsFilled = True
        for areaId in Area.ALL:
            regionsIndexes = getAppliedRegionsForCurrentHangarVehicle(areaId, slotType)
            for regionIdx in regionsIndexes:
                item = self.getModifiedOutfit(season).getContainer(areaId).slotFor(slotType).getItem(regionIdx)
                if item is None:
                    break
            else:
                continue

            allSlotsFilled = False
            break

        return allSlotsFilled

    def carveUpOutfits(self, preserve=False):
        if preserve:
            self.__preserveState()
            self.__carveUpOutfits()
            self.__restoreState()
        else:
            self.__carveUpOutfits()

    def isOutfitsModified(self):
        if self._mode == self._originalMode:
            if self._mode == C11nMode.STYLE:
                currentStyle = self.service.getCurrentStyle()
                if self._modifiedStyle and currentStyle:
                    return self._modifiedStyle.intCD != currentStyle.intCD
                return not (self._modifiedStyle is None and currentStyle is None)
            for season in SeasonType.COMMON_SEASONS:
                outfit = self._modifiedOutfits[season]
                currOutfit = self._originalOutfits[season]
                if not currOutfit.isEqual(outfit) or not outfit.isEqual(currOutfit):
                    return True

            return False
        else:
            if self._mode == C11nMode.CUSTOM:
                if self.isOutfitsEmpty(self._modifiedOutfits) and self._originalStyle is None:
                    return False
            elif self._modifiedStyle is None and self.isOutfitsEmpty(self._originalOutfits):
                return False
            return True
            return

    @staticmethod
    def isOutfitsEmpty(outfits):
        outfitsEmpty = True
        for season in SeasonType.COMMON_SEASONS:
            outfit = outfits[season]
            if not outfit.isEmpty():
                outfitsEmpty = False
                break

        return outfitsEmpty

    def isRegionSelected(self):
        return self._selectedRegion.areaId != -1 and self._selectedRegion.regionIdx != -1

    def __carveUpOutfits(self):
        for season in SeasonType.COMMON_SEASONS:
            outfit = self.service.getCustomOutfit(season)
            self._modifiedOutfits[season] = outfit.copy()
            if outfit.isInstalled():
                self._originalOutfits[season] = outfit.copy()
            self._originalOutfits[season] = self.service.getEmptyOutfit()
            for slot in self._modifiedOutfits[season].slots():
                for idx in range(slot.capacity()):
                    item = slot.getItem(idx)
                    if item and item.isHidden and item.fullInventoryCount(g_currentVehicle.item) == 0:
                        slot.remove(idx)

        style = self.service.getCurrentStyle()
        if self.service.isCurrentStyleInstalled():
            self._originalStyle = style
            self._modifiedStyle = style
        else:
            self._originalStyle = None
            if style and style.isHidden and style.fullInventoryCount(g_currentVehicle.item) == 0:
                self._modifiedStyle = None
            else:
                self._modifiedStyle = style
        if style:
            self._currentOutfit = style.getOutfit(self._currentSeason)
        else:
            self._currentOutfit = self._modifiedOutfits[self._currentSeason]
        return

    def __cancelModifiedOufits(self):
        for season in SeasonType.COMMON_SEASONS:
            self._modifiedOutfits[season] = self._originalOutfits[season].copy()

    def __cancelModifiedStyle(self):
        self._modifiedStyle = self._originalStyle

    def __onOutfitChanged(self):
        self.refreshOutfit()

    def __preserveState(self):
        self._state.update(modifiedStyle=self._modifiedStyle, modifiedOutfits={season:outfit.copy() for season, outfit in self._modifiedOutfits.iteritems()})

    def __restoreState(self):
        self._modifiedStyle = self._state.get('modifiedStyle')
        self._modifiedOutfits = self._state.get('modifiedOutfits')
        if self._modifiedStyle:
            self._modifiedStyle = self.service.getItemByCD(self._modifiedStyle.intCD)
        self._state.clear()

    def __onCacheResync(self, *_):
        if g_currentVehicle.isPresent():
            self.carveUpOutfits(preserve=True)
            self.__updateVisibleTabsList()
            self.refreshOutfit()
        self.onCacheResync()

    def __updateVisibleTabsList(self):
        visibleTabs = defaultdict(set)
        anchorsData = g_currentVehicle.hangarSpace.getSlotPositions()
        requirement = createCustomizationBaseRequestCriteria(g_currentVehicle.item, self.eventsCache.questsProgress, self.getAppliedItems())
        items = self.service.getItems(GUI_ITEM_TYPE.CUSTOMIZATIONS, criteria=requirement)
        for item in sorted(items.itervalues(), key=comparisonKey):
            tabIndex = TYPE_TO_TAB_IDX.get(item.itemTypeID)
            if tabIndex not in C11nTabs.VISIBLE or tabIndex == C11nTabs.CAMOUFLAGE and g_currentVehicle.item.descriptor.type.hasCustomDefaultCamouflage:
                continue
            for seasonType in SeasonType.COMMON_SEASONS:
                if item.season & seasonType:
                    if item.itemTypeID in (GUI_ITEM_TYPE.INSCRIPTION, GUI_ITEM_TYPE.EMBLEM):
                        for areaData in anchorsData.itervalues():
                            if areaData.get(item.itemTypeID):
                                hasSlots = True
                                break
                        else:
                            hasSlots = False

                        if not hasSlots:
                            continue
                    visibleTabs[seasonType].add(tabIndex)

        self.__visibleTabs = visibleTabs


"""
Created on Tue Mar 03 15:57:41 2015
@author: Ching-Yen
"""
"""
This script is to make a file recording which SMILES are outliers in each combination.
1 stands for Outliers. 0 stands for fitted group.
<input> argument: percentage of outliers

DFT_(property)_(index)_(flavorName).csv
SMILES,amount of this SMILES, max, min, median, mean, MAD, SD, CV

"""
# remember to remove  "break"
filename_CountBB = "BB_test.csv"
str_property = "HOMO"

import pandas as pd
import argparse
import time
start = time.time()
import csv
import numpy as np
import matplotlib
matplotlib.use('Agg')
### the HelpFormatter and provide a special intro for the options that should be handled "raw"
### Any other calls to .add_argument() where the help does not start with R| will be wrapped as normal.
class SmartFormatter(argparse.HelpFormatter):

    def _split_lines(self, text, width):
        # this is the RawTextHelpFormatter._split_lines
        if text.startswith('R|'):
            return text[2:].splitlines()
        return argparse.HelpFormatter._split_lines(self, text, width)

parser = argparse.ArgumentParser(description="Do Linear Regression to the data",
                                 formatter_class=SmartFormatter)

#parser.add_argument("property", type=int, choices=[1,2,3,4],
#                    help="R|choose the options of property:\n"
#                    "1 = HOMO  2 = LUMO  3 = Dipole  4 = Gap")
parser.add_argument("percent_outlier", type=float, help="R|define the outlier\n"
                    "enter the percentage of data taken out to be outliers\n"
                    "input only number part")
parser.add_argument("-x", "--x_flavor", type=int, default = 1,
                    choices=[1,2], help="R|choose the flavor for x-axis:\n"
                    "1 = BP86/SVP    2 = B3LYP/SVP")
parser.add_argument("-y", "--y_flavor", type=int, default = 999,
                    choices=[1,2,3,4,5,6,7,8,9], help="R|choose the flavor for y-axis:\n"
                    "1 = BP86/SVP    2 = B3LYP/SVP  3 = PBE0/SVP \n4 = BH&HLYP/SVP 5 = M06-2X/SVP"
                    " 6 = HF/SVP \n7 = BP86/TZVP   8 = B3LYP/TZVP 9 = PBE0/TZVP")

#parser.add_argument("-rb","--result_box",action='store_false',default=True,
#                    help="Hide the result display box in all plots")
#parser.add_argument("-rb1","--result_box1",action='store_false',default=True,
#                    help="Hide the result display box only in original data pool")
#parser.add_argument("-rb2","--result_box2",action='store_false',default=True,
#                    help="Hide the result display box only in outlier")
#parser.add_argument("-rb3","--result_box3",action='store_false',default=True,
#                    help="Hide the result display box only in remaining pool")
#
#parser.add_argument("-lr","--lr_line",action='store_false',default=True,
#                    help="Hide all the LinearReg lines.")
#parser.add_argument("-lr1","--lr_line1",action='store_false',default=True,
#                    help="Hide the LinearReg line for original data pool.")
#parser.add_argument("-lr2","--lr_line2",action='store_false',default=True,
#                    help="Hide the LinearReg line for outliers.")
#parser.add_argument("-lr3","--lr_line3",action='store_false',default=True,
#                    help="Hide the LinearReg line for remaining pool.")
#
#args = parser.parse_args('1 1 -x 1 -y 6 -lr2 -lr3'.split())
args = parser.parse_args('1'.split())
#args = parser.parse_args('1 1 -x 1 -y 6 -lr2 -lr3'.split())
#args = parser.parse_args()

#args = parser.parse_args()
###############################Function #######################################
def record_time(part,start,timenow,timelast):
#    str1 = str(part)
    str2 = str(timenow - timelast)
    str3 = str(timenow - start)
    str6 = "time_record.txt"
    txt = open(str6, 'a')
    txt.write(part),txt.write("    "),txt.write(str2),txt.write("    "),
    txt.write(str3),txt.write("\n")

###################### Load building blocks dictionary file ##################
def readDict(fn):
    f=open(fn,'rb')
    dict_rap={}

    for key, val in csv.reader(f):
        dict_rap[key]=eval(val)
    f.close()
    return(dict_rap)

#dict_BB = readDict(filename_CountBB)
###############################Define Variable#################################
#if args.property == 1: #HOMO
#str_property = "HOMO"
#if args.property == 2: #LUMO
#    str_property = "LUMO"
#if args.property == 3: #Dipole
#    str_property = "Dipole"
#if args.property == 4: #eGap
#    str_property = "eGap"
num_percentage = str(args.percent_outlier)
str_f1 = "BP86/SVP"
str_f2 = "B3LYP/SVP"
str_f3 = "PBE0/SVP"
str_f4 = "BH&HLYP/SVP"
str_f5 = "M06-2X/SVP"
str_f6 = "HF/SVP"
str_f7 = "BP86/TZVP"
str_f8 = "B3LYP/TZVP"
str_f9 = "PBE0/TZVP"
str_f1_2 = "BP86s"
str_f2_2 = "B3LYPs"
str_f3_2 = "PBE0s"
str_f4_2 = "BH&HLYPs"
str_f5_2 = "M06s"
str_f6_2 = "HFs"
str_f7_2 = "BP86t"
str_f8_2 = "B3LYPt"
str_f9_2 = "PBE0t"
list_fname_1 = ["BP86/SVP","B3LYP/SVP","PBE0/SVP","BH&HLYP/SVP","M06-2X/SVP","HF/SVP",
                "BP86/TZVP","B3LYP/TZVP","PBE0/TZVP"]
list_fname_2 = ["BP86s","B3LYPs","PBE0s","BH&HLYPs","M06s","HFs",
                "BP86t","B3LYPt","PBE0t"]

"""
1 = BP86/SVP    2 = B3LYP/SVP  3 = PBE0/SVP 4 = BH&HLYP/SVP 5 = M06-2X/SVP
6 = HF/SVP      7 = BP86/TZVP   8 = B3LYP/TZVP 9 = PBE0/TZVP
"""
list_textname = ['DFT_%s_%s_%s.csv' %(str_property,'1',str_f1_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'2',str_f2_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'3',str_f3_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'4',str_f4_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'5',str_f5_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'6',str_f6_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'7',str_f7_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'8',str_f8_2),
                 'DFT_%s_%s_%s.csv' %(str_property,'9',str_f9_2),]

name_directory = "Zscore_BB"
#os.makedirs(name_directory)

#####################Import Values############################################

###read x file
def dic_SMILES_x_y(xfilename,yfilename):
    dic_SMILES_x_y = {}

    f = open(xfilename)
    csv_f = csv.reader(f)
    next(csv_f)
    for row in csv_f:
      dic_SMILES_x_y.setdefault(row[0],[]).append(row[4])
    f.close()
    f = open(yfilename)
    csv_f = csv.reader(f)
    next(csv_f)
    for row in csv_f:
        dic_SMILES_x_y.setdefault(row[0],[]).append(row[4])
    return dic_SMILES_x_y

def distrubute_xy(dic):
    for key,value in dict.items(dic):
        if len(value) == 2:
            list_SMILES.append(key)
            list_x.append(float(value[0]))
            list_y.append(float(value[1]))
        else:
            pass
def outliers(x,y):
    from scipy import stats
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
    y2 = []
    for i in list_x:
        num7 = slope * i + intercept
        y2.append(round(num7,5))
    list_1 = zip(list_y,y2)
    array1 = np.asarray(list_1)

    list_dis = []
    for i1,i2 in array1:
        num3 = i2 - i1
        list_dis.append(round(num3,5))

    list_2 = zip(list_y,list_dis)   ## ((y2-y1)/y1)*100%
    array2 = np.asarray(list_2)

    list_percentage = []
    for i_y1, i_dis in array2:
        num4 = abs(i_dis/i_y1) * 100
        list_percentage.append(round(num4,2))
    for i,line in enumerate(list_percentage):
        if line >= args.percent_outlier:
            list_outliers_SMILES.append(list_SMILES[i])
        if line < args.percent_outlier:
            list_fitted_SMILES.append(list_SMILES[i])

#def sum_BB(list_SMILES):
#    list_sum = [0] *26
#    for i in list_SMILES:
#        for index,value in enumerate(dict_BB[i]):
#            if int(value) > 0:
#                list_sum[index] += 1
#    return list_sum
#
#def get_zscore(outlierList,fittedList):
#    import math
#    list_zscore = [0]*26
#    for i in range(0,26):
#        value_upperK = float(outlierList[i] + fittedList[i])
#        value_k = float(outlierList[i])
#        value_expected_k = value_n * value_upperK / value_N
#        value_sigma = math.sqrt(value_n*value_upperK*(value_N-value_upperK)/value_N/value_N*(value_N-value_n)/(value_N-1))
#        try:
#            num_z = (value_k - value_expected_k) / value_sigma
#        except ZeroDivisionError:
#            num_z = 100.0
#        list_zscore[i] = round(num_z,2)
#    return list_zscore
#def sortZ(list_z):
#    list_1 = zip(range(1,27),list_z)
#    from operator import itemgetter
#    list_4 = sorted(list_1, key= itemgetter(1))
#    list_index = []
#    list_z = []
#    for i in list_4:
#        list_index.append(str(i[0]))
#        list_z.append(i[1])
#    list_5 = [list_index, list_z]
#    return list_5
#def Z_plot(list_z,y,x_flavorname):
#    list_1 = sortZ(list_z)
#    import matplotlib.pyplot as plt
#    N = 26
#    ind = np.arange(N)
#    width = 0.8
#    plt.bar(ind,list_1[1],width)
#    plt.xticks(ind+width/2.0, list_1[0],rotation=75 )
#    plt.yticks(np.arange(-300,301,50))
#    plt.title("%s%% %s   %s vs. %d"%(num_percentage,str_property,x_flavorname,y+1))
#    plt.xlabel("Building blocks")
#    plt.ylabel("Z-score")
##    plt.show()
#    str_1 = str_property + "_Zscore_%d%d_sorted.png"%(args.x_flavor,y+1)
#    plt.savefig(str_1)
#    plt.close()
#    ########################plot unsorted plot#################
#    plt.bar(ind,list_z,width,color="r")
#    plt.xticks(ind+width/2.0, xrange(1,27),rotation=75 )
#    plt.yticks(np.arange(-300,301,50))
#    plt.title("%s%% %s   %s vs. %d"%(num_percentage,str_property,x_flavorname,y+1))
#    plt.xlabel("Building blocks")
#    plt.ylabel("Z-score")
##    plt.show()
#    str_2 = str_property + "_Zscore_%d%d.png"%(args.x_flavor,y+1)
#    plt.savefig(str_2)
#    plt.close()
###############
if args.y_flavor == 999 and args.x_flavor == 1:
    args.y_flavor = [1,2,3,4,5,6,7,8]
elif args.y_flavor == 999 and args.x_flavor == 2:
    args.y_flavor = [2,3,4,5,6,7,8]
else:
    args.y_flavor = [args.y_flavor]
#txt_zscore = open('result_Zscore.txt', 'a')
#txt_zscore.write("\n%s   %s %% \n"%(str_property,num_percentage))
SMILES_list = []
with open(filename_CountBB) as f:
    for i,line in enumerate(f):
        values = line.strip().split(',')
        SMILES_list.append(values[0].strip())
f.close()
list_yesno = []
for y in args.y_flavor:
    filename_x = list_textname[args.x_flavor-1]
    filename_y = list_textname[y]
    #############
    if filename_y != "DFT_HOMO_8_B3LYPt.csv":   ## specify the y file
        continue
    ##############
    y_flavorname = list_fname_1[y]
    x_flavorname = list_fname_1[args.x_flavor-1]
    dic_all = dic_SMILES_x_y(filename_x,filename_y)
    list_x = []
    list_y = []
    list_SMILES = []
    list_outliers_SMILES = []
    list_fitted_SMILES = []
    distrubute_xy(dic_all) ## to get list_x and list_y
    outliers(list_x,list_y) ## to get list_outliers_SMILES and list_fitted_SMILES
    list_0_1_NA = []
    end0 = time.time()
    record_time("Part1_18_0",start,end0,start)
    for SMILES in SMILES_list:
        if SMILES in list_outliers_SMILES:
            list_0_1_NA.append(1)
        elif SMILES in list_fitted_SMILES:
            list_0_1_NA.append(0)
        else:
            list_0_1_NA.append("NA")
    list_yesno.append(list_0_1_NA)
#    list_sumBB_outliers = sum_BB(list_outliers_SMILES)
#    list_sumBB_fitted = sum_BB(list_fitted_SMILES)

    ###### Z-Score #######
#    value_n = float(len(list_outliers_SMILES))
#    value_N = float(len(list_SMILES))
#    list_zscore = get_zscore(list_sumBB_outliers,list_sumBB_fitted)
#    txt_zscore.write("\n%s vs %d\n"%(args.x_flavor,y+1))
#    txt_zscore.write(str(list_zscore))
#    Z_plot(list_zscore,y,x_flavorname)
#    break

#txt_zscore.close()
#outlierList = list_sumBB_outliers
#fittedList = list_sumBB_fitted
#import math
#list_zscore = []
#for i in range(0,2):
#    print i
#    value_upperK = float(outlierList[i] + fittedList[i])
#    value_k = float(outlierList[i])
#    value_expected_k = value_n * value_upperK / value_N
#    value_sigma = math.sqrt(value_n*value_upperK*(value_N-value_upperK)/value_N/value_N*(value_N-value_n)/(value_N-1))
#    num_z = (value_k - value_expected_k) / value_sigma

df_1 = pd.DataFrame({"SMILES": SMILES_list,
#                     "F12": list_yesno[0],
#                     "F13": list_yesno[1],
#                     "F14": list_yesno[2],
#                     "F15": list_yesno[3],
#                     "F16": list_yesno[4],
#                     "F17": list_yesno[5],
                     "F18": list_yesno[0]})
#                     "F19": list_yesno[7]})

#df_1.to_csv("DFT_HOMO_BP86s_1percent_1outlier0fitted.csv")
df_1.to_csv("DFT_HOMO_BP86s_1percent_1outlier0fitted_F18.csv")

#####
end1 = time.time()
record_time("Part1_18",start,end1,start)
#####


#!/usr/bin/env python3

"""
Copyright (c) 2016-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
"""

import argparse

from magma.policydb.rule_store import PolicyRuleDict
from lte.protos.policydb_pb2 import FlowMatch, FlowDescription, PolicyRule

def add_rule(args):
    rule_id = args.rule_id
    policy_dict = PolicyRuleDict()
    arg_list = {'ip_proto': args.ip_proto,
                'ipv4_dst': args.ipv4_dst,
                'ipv4_src': args.ipv4_src,
                'tcp_dst': args.tcp_dst,
                'tcp_src': args.tcp_src,
                'udp_dst': args.udp_dst,
                'udp_src': args.udp_src,
                'direction': args.direction}
    match = FlowMatch(**arg_list)
    flow = FlowDescription(match=match, action=args.action)
    rule = policy_dict.get(rule_id)
    if not rule or args.overwrite:
        action = 'add'
        rule = PolicyRule(id=rule_id, flow_list=[flow])
    else:
        action = 'edit'
        rule.flow_list.extend([flow])
    policy_dict[rule_id] = rule
    print("Rule '%s' successfully %sed!" % (rule_id, action))

def create_parser():
    """
    Creates the argparse parser with all the arguments.
    """
    parser = argparse.ArgumentParser(
        description='Management CLI for policydb',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    # Add subcommands
    subparsers = parser.add_subparsers(title='subcommands', dest='cmd')
    parser_add = subparsers.add_parser('add_rule', help='Add rule')
    parser_add.add_argument('rule_id', help='rule id to add')
    parser_add.add_argument('-ipd', '--ipv4_dst', help='ipv4 dst for rule')
    parser_add.add_argument('-ips', '--ipv4_src', help='ipv4 src for rule')
    parser_add.add_argument('-p', '--ip_proto', help='ip proto for rule',
                            type=int)
    parser_add.add_argument('-td', '--tcp_dst', type=int,
                            help='tcp dst for rule, set -p to IPPROTO_TCP(6)')
    parser_add.add_argument('-ts', '--tcp_src', type=int,
                            help='tcp src for rule, set -p to IPPROTO_TCP(6)')
    parser_add.add_argument('-ud', '--udp_dst', type=int,
                            help='udp dst for rule, set -p to IPPROTO_UDP(17)')
    parser_add.add_argument('-us', '--udp_src', type=int,
                            help='udp src for rule, set -p to IPPROTO_UDP(17)')
    parser_add.add_argument('-d', '--direction', type=int,
                            help='0 == UPLINK, 1 == DOWNLINK')
    parser_add.add_argument('-a', '--action', type=int,
                            help='0 == PERMIT, 1 == DENY')
    parser_add.add_argument('-o', '--overwrite', action='store_true',
                            help='overwrite existing rule')

    parser_add.set_defaults(func=add_rule)
    return parser

def main():
    parser = create_parser()

    # Parse the args
    args = parser.parse_args()
    if not args.cmd:
        parser.print_usage()
        exit(1)

    # Execute the subcommand function
    args.func(args)

if __name__ == "__main__":
    main()


"""
MIT License
-----------

Copyright (c) 2021 University of Malaga
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Authors: J. Ricardo Sánchez Ibáñez, Carlos J. Pérez del Pulgar
Affiliation: Department of Systems Engineering and Automation
Space Robotics Lab (www.uma.es/space-robotics)
"""
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from context import camis
import time

h = 1.0

#x = np.array([0.5,np.sqrt(3)/2])*h
#xj = np.array([0.0, 0.0])*h
#xk = np.array([1.0, 0.0])*h

x = np.array([1.0,np.sqrt(3)/2])*h
xj = np.array([0.0, np.sqrt(3)/2])*h
xk = np.array([0.5, 0.0])*h

Tj = 1.0
Tk = 1.0
Q1 = 1.0
Q2 = 1.0
D1 = 0.0
D2 = 0.0
aspect = np.array([1.0, 0.0])

start = time.time()
for i in range(100):
    optimizedT,dirT = camis.optimizeCost(x,xj,xk,Tj,Tk,i,i,D1,D2,aspect)
print(time.time() - start)

start = time.time()
for i in range(100):
    computedT,computedDir = camis.getEikonalCost(x,xj,xk,Tj,Tk,i)
print(time.time() - start)


import sys
from collections import defaultdict

# sys.stdin = open("input.txt",'rt')

n,m = map(int,input().split())
arr = [ int(input()) for _ in range(m)]

left = 1
#한명이 다 가져간 경우
right = max(arr)

while left<=right:
    mid = (left+right)//2
    #보석을 m명보다 많이 나눌 수 있으면
    #보석을 더 작게 나눌 수 있음
    s = 0
    for i in arr:

        if i % mid == 0:
            s += i//mid
        else:
            s += (i//mid)+1

        #묶음수가 가져갈 사람보다 더 많으면
    if s > n:
        #left를 mid보다 크게함 -> 다음 i//mid 인 s값은 더 작아지고 다시 체크
        left = mid+1
    else:
        right = mid-1

print(left)


import re
import nltk
import numpy as np
import pandas as pd
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from server_side.apps.module_interaction.additional_modules import constants
from server_side.apps.data_interaction import crud

def find_danger(id_disaster, language):
    disaster = crud.read_disaster_id(id_disaster)
    if disaster == '':
        return ''
    quantity_indicator = find_quantity_indicator(disaster)
    peak = np.random.normal(find_center_distribution(quantity_indicator, disaster.term))
    content_analysis = make_content_analysis(disaster.about, language, disaster.id)
    return peak * content_analysis

def find_quantity_indicator(disaster):
    intensity_list = [constants.MIN_INTENSITY, disaster.intensity, constants.MAX_INTENSITY]
    intensity = [float(i) / max(intensity_list) for i in intensity_list][1]
    readiness_list = [constants.MIN_READINESS, disaster.readiness_degree, constants.MAX_READINESS]
    readiness = [float(i) / max(readiness_list) for i in readiness_list][1]
    return intensity / readiness

def find_center_distribution(indicator, term):
    indicator_list = [constants.MIN_INDICATOR, indicator, constants.MAX_INDICATOR]
    indicator = [float(i) / max(indicator_list) for i in indicator_list][1]
    return term / indicator

def make_content_analysis(text, language, id_disaster):
    sid = SentimentIntensityAnalyzer()
    text = clean_text(text)
    text = process_text(text, language)
    crud.update_disaster_about(id_disaster, text)
    dictionary = make_dictionary(id_disaster)
    dictionary.insert(0, text)
    tfidf_vectorizer = TfidfVectorizer()
    values = tfidf_vectorizer.fit_transform(dictionary)
    feature_names = tfidf_vectorizer.get_feature_names()
    data = pd.DataFrame(values.toarray(), columns=feature_names)
    data = data.T[data.T[0] != 0]
    words = list(data.index)
    sum_ = 0
    sum_f = 0
    for word in words:
        polarity = sid.polarity_scores(word)['compound']
        frequency = data[0][word]
        sum_f += frequency
        sum_ += polarity * frequency
    return sum_ + sum_f

def clean_text(text):
    pattern = r"[\W\d_]"
    return re.sub(pattern, " ", text)

def process_text(text, language):
    stemmer = PorterStemmer()
    lemmatizer = WordNetLemmatizer()
    stop_words = set(stopwords.words(get_language_name(language)))
    sentences = nltk.sent_tokenize(text)
    sentences_processed = []
    for sentence in sentences:
        words_processed = []
        words = nltk.word_tokenize(sentence)
        words = [word for word in words if word not in stop_words]
        for word in words:
            word = stemmer.stem(word)
            word = lemmatizer.lemmatize(word)
            words_processed.append(word)
        words = [word for word in words_processed if word not in stop_words]
        sentences_processed.append(" ".join(words))
    text_processed = " ".join(sentences_processed)
    return text_processed

def make_dictionary(id_disaster):
    disasters = crud.read_disasters()
    dictionary = []
    for disaster in disasters:
        if disaster.about_clean != "" and disaster.id != id_disaster:
            dictionary.append(disaster.about_clean)
    return dictionary

def get_language_name(language):
    if language == "en":
        return "english"
    if language == "de":
        return "german"
    if language == "ru" or language == "be" or language == "uk":
        return "russian"


import sys

InputFile1 = sys.argv[1] # vcf file
InputFile2 = sys.argv[2] # sample list

OutputFile1 = sys.argv[3] # output file name group1

file1 = open(InputFile1, "r")
file2 = open(InputFile2, "r")
file3 = open(OutputFile1, 'w')

group_list = []
group_dict = {}
for line_1 in file2:
	group_list.append(line_1[:-1])
for line in file1:
	splt = line.split("\t")
	sample_no = len(splt)
	samples = range(9, sample_no)

	if splt[0][0:2] == "##":
		continue
	elif splt[0][0:2] == "#C":
		for x in samples:
			if x != sample_no:
				group_dict[x] = splt[x]
			elif x == sample_no:
				group_dict[x] = splt[x][:-1]
	else:
		chr = splt[0]
		pos = splt[1]
		ref = splt[3]
		alt = splt[4]
		if alt != "." and len(alt) == 1 and len(ref) ==1:

			file3.write(chr + " snp_" + pos + " " + pos + " " + ref + " " + alt)
			for j in samples:
				if group_dict[j] in group_list:
					if splt[j][0] == ".":
						allele = "N"
					else:
						allele1 = int(splt[j][0])
						allele2 = int(splt[j][2])
						if group_dict[j] in group_list:
							if allele1 == allele2 and allele1 == 0:
								allele = 1
							elif allele1 == allele2 and allele1 == 1:
								allele = 4

							else:
								allele = 5
					file3.write(" " + str(allele))
				else:
					continue
		else:
			continue
		file3.write("\n")


# script calculates the body mass index of a user
# get user height in cm and convert it to integer
height = input("Enter your height in centimeter ")

# convert height string to integer
height = int(height)

# get users weigth in kilogramme
weight = input("Enter your weight in kg ")

# convert string weight to weight int
weight = int(weight);

# calculate bmi
bmi = height / weight ** 2

# assign bmi to result
result = "Your height is {} and your weight is {}. Therefore your Body Mass Index is {}".format(height, weight, bmi)

# determine if user is obese or not
if bmi < 20:
    print(result + " Your are underweight")
elif (bmi >= 20) and (bmi < 30):
    print(result + " Your are on weight")
elif (bmi >= 30) and (bmi < 50):
    print(result + " You are a little overweight")
elif bmi >= 50:
    print(result + " You are obese")


# 你有一个用于表示一片土地的整数矩阵land，该矩阵中每个点的值代表对应地点的海拔高度。若值为0则表示水域。由垂直、水平或对角连接的水域为池塘。池塘的大小是指相连接的水域的个数。编写一个方法来计算矩阵中所有池塘的大小，返回值需要从小到大排序。

# 示例：

# 输入：
# [
#   [0,2,1,0],
#   [0,1,0,1],
#   [1,1,0,1],
#   [0,1,0,1]
# ]
# 输出： [1,2,4]
# 提示：

# 0 < len(land) <= 1000
# 0 < len(land[i]) <= 1000
# 通过次数490提交次数712

# 来源：力扣（LeetCode）
# 链接：https://leetcode-cn.com/problems/pond-sizes-lcci
# 著作权归领扣网络所有 。 商业转载请联系官方授权 ， 非商业转载请注明出处 。

class Solution:
    def pondSizes(self, land: List[List[int]]) -> List[int]:
        # BFS
        v = set()
        res = []
        rows, cols = len(land), len(land[0])
        for r in range(rows):
            for c in range(cols):
                if land[r][c] == 0 and (r, c) not in v:
                    q = [(r, c)]
                    v.add((r, c))
                    for node in q:
                        x, y = node
                        for xx, yy in ((x + 1, y), (x - 1, y), (x, y + 1),
                                       (x, y - 1), (x + 1, y + 1), (x + 1,
                                                                    y - 1),
                                       (x - 1, y + 1), (x - 1, y - 1)):
                            if 0 <= xx < rows and 0 <= yy < cols and land[xx][
                                    yy] == 0 and (xx, yy) not in v:
                                v.add((xx, yy))
                                q.append((xx, yy))

                    res.append(len(q))
        return sorted(res)


# coding:utf-8
'''
求出平均数量最大的10个年龄
'''

from mrjob.job import MRJob
from mrjob.job import MRStep

class Friend(MRJob):
    # 第一个step，得到每一个年龄的平均朋友数量
    def mapper1(self,_,line):
        row = line.split(',')
        yield row[2],int(row[3])

    def reducer1(self,age,num):
        l = list(num)
        yield age,sum(l)/len(l)

    # 第二个step：求出平均数量最大的10个年龄
    def mapper2(self,age,avernum):
        # 【很重要】mapper2的输入是reducer1的输出
        # 【重要】mapper2执行了几次？有多少个age，就执行了几次！或者说，reducer1输出了几次，mapper2就执行几次
        # 必须把平均朋友数量和相对应的年龄写在同一个元组当中，这样才能一一对应
        yield None,(avernum,age)

    def reducer2(self,_,values):
        # values是元组的列表，列表的每个元素都是平均朋友数量和相对应的年龄组成的一个元组
        # reducer2只执行了一次，因为mapper2中只输出了一个key，那就是None
        l = list(values)
        # l.sort()
        for i in l[-10:]:
            yield i[1],i[0]

    def steps(self):
        return [
            MRStep(mapper = self.mapper1,
                   reducer = self.reducer1
                   ),
            MRStep(mapper = self.mapper2,
                   reducer = self.reducer2
                   )
        ]

if __name__ == '__main__':
    Friend.run()


#!/usr//bin/env/python
# _*_ coding=utf-8 _*_

#################################################################
#            POWER by Fitz or call me WEN DA                    #
#      这个软件的功能主要是通过调取卓易健康这个api，并向其post  #
#      数据，以达到自己能修改微信运动的步数的功能！             #
#                                                               #
#                                                               #
#      API:http://www.789sk.cn/wxyd/api.html                    #
#                                                               #
#                有问题+VX：qq498289134                           #
#################################################################

import requests  # 调用requests库
import sys
keep = requests.Session()  # 用Session保持住会话
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'
}                                                                         # 利用headers伪装好防止被服务器拒绝
url = 'http://www.789sk.cn/wxyd/api.php'  # 目标网址
datas = {}
while True:                                                               #构建循环以便满足多次修改步数的需求
    try:
        datas['user'] = input('你所绑定的卓易健康账号：')
        datas['walk'] = int(input('你想要刷取的步数：'))
    except ValueError:                                                     #防止有神奇宝贝乱填
        print('非法输入，请检查所输入的步数是否符合-1至98800这个范围\n')
        print('-1表示直接刷满也即是98800，注意：这比较容易被封号请谨慎使用！！！')
    post_data = requests.post(url=url, headers=headers, data=datas)        #提交数据
    if "数据提交成功！" in post_data:                                         #以下都是根据网站返回的信息作出判断
        print('\n你已经成功修改了微信运动步数，请前往查看')
    elif "账号未绑定微信！" in post_data.text:
        print('\n你的账号没有绑定微信，请去微信绑定后再使用，谢谢！')
    elif "效验码认证失败！" in post_data.text:
        print('\n效验码认证失败！可能是网站出现问题')
    elif "请输入账号！" in post_data.text:
        print('\n你似乎忘记输入账号了')
    elif "请输入步数！" in post_data.text:
        print('\n你似乎忘记输入步数了')
    else:
        pass
    if input('请问你还要继续修改吗？如果是请按回车键，想退出按q') == 'q':           #结合开头的While True 循环做最终判断
            sys.exit()


# -*- coding: utf-8 -*-
import collections

from nltk.tokenize import word_tokenize, sent_tokenize
from trLemmer import tr
from trLemmer.attributes import RootAttribute, SecondaryPos
from trLemmer.lexicon import RootLexicon
from trLemmer.morphotactics import TurkishMorphotactics, SearchPath, pnon, nom
from trLemmer.rulebasedanalyzer import RuleBasedAnalyzer
import typing

"""Main module."""

class UDFormatter:
    posessives = {
        'P1sg': ('|Number[psor]=Sing', '|Person[psor]=1'),
        'P1pl': ('|Number[psor]=Plur', '|Person[psor]=1'),
        'P2sg': ('|Number[psor]=Sing', '|Person[psor]=2'),
        'P2pl': ('|Number[psor]=Plur', '|Person[psor]=2'),
        'P3sg': ('|Number[psor]=Sing', '|Person[psor]=3'),
        'P3pl': ('|Number[psor]=Plur', '|Person[psor]=3')
    }
    agreement_values = {
        'A1sg': ('Sing', '1'),
        'A1pl': ('Plur', '1'),
        'A2sg': ('Sing', '2'),
        'A2pl': ('Plur', '2'),
        'A3sg': ('Sing', '3'),
        'A3pl': ('Plur', '3')
    }
    cases = ["Nom", "Dat", "Acc", "Abl", "Loc", "Ins", "Gen", "Equ"]

    def __init__(self, add_surface=True):
        self.add_surface = add_surface

    def format_adj(self, analysis):
        nadj_str = "Case={case}|Number={number}|{npsor}|Person={person}{psor}"
        ids = [m.id_ for m in analysis.morphemes]
        case = ''
        number = ''
        person = ''
        npsor = ''
        psor = ''
        if 'Noun' in ids:
            for m_id in ids:
                if m_id in UDFormatter.cases:
                    case = m_id
                else:
                    possessive = UDFormatter.posessives.get(m_id)
                    if possessive is not None:
                        npsor, psor = possessive
                    else:
                        agreement = UDFormatter.agreement_values.get(m_id)
                        if agreement is not None:
                            number, person = agreement
            return nadj_str.format(case=case, number=number,
                                   npsor=npsor, person=person,
                                   psor=psor)
        else:
            return self.format(analysis)

    def format_noun(self, analysis):
        noun_str = "{}Case={}|Number={}{}|Person={}{}"
        case = ''
        number = ''
        person = ''
        number_psor = ''
        person_psor = ''
        for m in analysis.morphemes:
            morph = m[0]
            # print("\tMorph id: ", morph.id_)
            if morph.id_ in UDFormatter.cases:
                case = morph.id_
            possessive_value = UDFormatter.posessives.get(morph.id_)
            agreement_value = UDFormatter.agreement_values.get(morph.id_)
            if possessive_value is not None:
                number_psor, person_psor = possessive_value
            if agreement_value is not None:
                number, person = agreement_value

        if case == '':
            case = 'Nom'
        if analysis.dict_item.secondary_pos == SecondaryPos.Abbreviation:
            abbr = "Abbr=Yes|"
        else:
            abbr = ''
        return noun_str.format(abbr, case, number, number_psor, person, person_psor)

    def format_num(self, analysis):
        return f"NumType={analysis.dict_item.secondary_pos.value}"

    def format_pron(self, analysis):
        prontypestr = analysis.dict_item.secondary_pos.value

        if prontypestr == "Pers":
            prontype = f"|PronType=Prs"
        elif prontypestr == 'Reflex':
            prontype = "|Reflex=Yes"
        elif prontypestr == 'Demons':
            prontype = "|PronType=Dem"
        elif prontypestr == 'Ques':
            prontype = ''
        else:
            prontype = f"|PronType={prontypestr}"
        person = ''
        number = ''
        case = ''
        number_psor = ''
        person_psor = ''
        for m in analysis.morphemes:
            morph = m[0]
            agreement_value = UDFormatter.agreement_values.get(morph.id_)
            if agreement_value is not None:
                number, person = agreement_value
            if prontypestr == "Reflex" or prontypestr == 'Quant':
                psor = UDFormatter.posessives.get(morph.id_)
                if psor is not None:
                    number_psor, person_psor = psor
            if morph.id_ in UDFormatter.cases:
                case = morph.id_
        if case == '':
            case = 'Nom'
        pron_str = "Case={case}|Number={number}{number_psor}|Person={person}{person_psor}{prontype}"

        return pron_str.format(case=case, number=number, number_psor=number_psor, person_psor=person_psor,
                               person=person, prontype=prontype)

    def format_verb(self, analysis):
        aspect = ''
        mood = ''
        number = ''
        person = ''
        polarity = ''
        tense = ''
        case = ''
        verb_form = ''
        number_psor = ''
        person_psor = ''
        evident = ''
        voice = ''
        register = ''
        agreement_values = {
            'A1sg': ('Sing', '1'),
            'A1pl': ('Plur', '1'),
            'A2sg': ('Sing', '2'),
            'A2pl': ('Plur', '2'),
            'A3sg': ('Sing', '3'),
            'A3pl': ('Plur', '3')
        }
        posessives = {
            'P1sg': ('|Number[psor]=Sing', '|Person[psor]=1'),
            'P1pl': ('|Number[psor]=Plur', '|Person[psor]=1'),
            'P2sg': ('|Number[psor]=Sing', '|Person[psor]=2'),
            'P2pl': ('|Number[psor]=Plur', '|Person[psor]=2'),
            'P3sg': ('|Number[psor]=Sing', '|Person[psor]=3'),
            'P3pl': ('|Number[psor]=Plur', '|Person[psor]=3')
        }
        last_part = ''
        morph_ids = [_[0].id_ for _ in analysis.morphemes]

        for k, m in enumerate(analysis.morphemes):
            morph = m[0]
            if morph.id_ in ["Nom", "Dat", "Acc", "Abl", "Loc", "Ins", "Gen", "Equ"]:
                case = f"|Case={morph.id_}"

            agreement_value = agreement_values.get(morph.id_)
            if agreement_value is not None and k == len(analysis.morphemes) - 1:
                number, person = agreement_value
            posessive_value = posessives.get(morph.id_)
            if posessive_value is not None:
                number_psor, person_psor = posessive_value
            if morph.id_ == 'Prog1':
                register = "|Polite=Infm"
            elif morph.id_ == 'Prog2':
                register = "|Polite=Form"
            if morph.id_ in ['Past', 'Pres', 'Fut']:
                if tense == 'Past' and morph.id_ == 'Past':
                    tense = 'Pqp'

                elif tense == '':
                    tense = morph.id_

                # if verb_form == 'Part':
                #     tense = 'Pqp'
            if morph.id_ == 'PastPart':
                tense = 'Past'
            elif morph.id_ == "FutPart":
                tense = "Fut"
                verb_form = 'Part'
            if morph.id_ == 'NarrPart':
                tense = 'Past'
                verb_form = "Part"
                evident = '|Evident=Nfh'
            if morph.id_ == 'Aor':
                aspect = 'Hab'
            if morph.id_ == 'AorPart':
                aspect = 'Hab'
                verb_form = 'Part'
            if morph.id_ == 'Narr':
                if not 'Past' in morph_ids:
                    evident = "|Evident=Nfh"
                tense = 'Past'
            if morph.id_ == "Pass":
                voice = "|Voice=Pass"
            elif morph.id_ == 'Caus':
                voice = '|Voice=Cau'
            if morph.id_ == "PresPart" or morph.id_ == "PastPart":
                verb_form = "Part"
            if morph.id_ == 'Noun' and verb_form == '':
                verb_form = 'Vnoun'
            if morph.id_ == "Opt":
                mood = 'Opt'
            elif morph.id_ == "Desr" or morph.id_ == "Cond":
                mood = 'Cnd'
            elif morph.id_ == 'Unable' or morph.id_ == 'Able':
                mood = 'Pot'
            elif morph.id_ == "Imp":
                mood = "Imp"
            if morph.id_ == "Neg" or morph.id_ == 'Unable':
                polarity = "Neg"
            if morph.id_ in ['Prog1', 'Prog2']:
                aspect = 'Prog'
            if morph.id_ == 'Adv':
                verb_form = "Conv"
        if polarity.strip() == "":
            polarity = "Pos"
        if tense.strip() == '':
            tense = 'Pres'
        if mood.strip() == '':
            mood = 'Ind'
        if aspect.strip() == '':
            aspect = "Perf"

        if verb_form != '':
            last_part += f"|VerbForm={verb_form}"
            if verb_form == 'Vnoun' and case == '':
                case = '|Case=Nom'
        if number != '':
            if verb_form == '':
                number = "|Number=" + number
            else:
                number = ''
        if person != '':
            if verb_form == '':
                person = "|Person=" + person
            else:
                person = ''
        if 'Noun' in morph_ids:
            noun_deriv = morph_ids.index('Noun')
            for id_ in morph_ids[noun_deriv:]:
                if id_ in ["Nom", "Dat", "Acc", "Abl", "Loc", "Ins", "Gen", "Equ"]:
                    case = f"|Case={morph.id_}"
                if case == '':
                    case = "|Case=Nom"
        if 'Fut' in morph_ids and 'Past' in morph_ids:
            tense = 'Fut,Past'
        verb_str = "Aspect={aspect}{evident}{case}|Mood={mood}" \
                   "{number_psor}{number}{person_psor}{person}" \
                   "|Polarity={polarity}{register}|Tense={tense}{last_part}{voice}".format(
            aspect=aspect,
            evident=evident,
            case=case,
            mood=mood,
            number_psor=number_psor,
            number=number,
            person_psor=person_psor,
            person=person,
            polarity=polarity,
            register=register,
            tense=tense,
            last_part=last_part,
            voice=voice
        )
        return verb_str

    def format(self, analysis) -> str:
        pos = analysis.dict_item.primary_pos.value
        if pos == "Noun":
            return self.format_noun(analysis)
        elif pos == "Verb":
            return self.format_verb(analysis)
        elif pos == "Pron":
            return self.format_pron(analysis)
        elif pos == "Num":
            return self.format_num(analysis)

        # result = f"[{analysis.dict_item.lemma}:{analysis.dict_item.primary_pos.value}"
        # if analysis.dict_item.secondary_pos != SecondaryPos.NONE:
        #     result = (
        #         f"{result},{analysis.dict_item.secondary_pos.name}] "
        #     )  # TODO: check name works
        # else:
        #     result = f"{result}] "
        result = ""
        result += self.format_morphemes(stem=analysis.stem, surfaces=analysis.morphemes)
        return result

    def format_morphemes(self, stem, surfaces):
        result = []
        if self.add_surface:
            result.append(f"{stem}:")
        result.append(surfaces[0][0].id_)
        if len(surfaces) > 1 and not surfaces[1][0].derivational:
            result.append(" + ")
        for i, sf in enumerate(surfaces):
            if i == 0:
                continue
            m, surface = sf
            if m.derivational:
                result.append(" | ")
            if self.add_surface and len(surface) > 0:
                result.append(f"{surface}:")
            result.append(m.id_)
            if m.derivational:
                result.append("→")
            elif i < len(surfaces) - 1 and not surfaces[i + 1][0].derivational:
                result.append(" + ")
        return "".join(result)

_Parse = collections.namedtuple('Parse', 'word, lemma, morphemes, formatted_form')

class Parse(_Parse):
    """
    Parse result wrapper. Based on https://github.com/kmike/pymorphy2/blob/master/pymorphy2/analyzer.py
    TODO: Decide which methods to add
    """
    pass

class MorphAnalyzer:
    """
    Morphological analyzer for Turkish language.

    It analyzes each word to suggest all possible morphological analyses,
    as well as lemmas.

    Create a :class:`TrLemmer` object ::

        >>> import trLemmer
        >>> lemmer = trLemmer.TrLemmer()

    Analyzer uses default text dictionaries
    (TODO: sources), as well as an optional unknown word analyzer).
    You can also add your own dictionary files in .txt format, with
    each word on its own line.

        # >>> lemmer.add_dictionary(path='/path/to/file')

    TrLemmer can analyze or lemmatize words and sentences.

        >>> lemmer.lemmatize_word('beyazlaştırmak')
        ['beyaz']

    #TODO: Add analysis for words with apostrophes
    """

    formatters = {"UD": UDFormatter}

    def __init__(self, lexicon=None, formatter=None):
        self.lexicon = (
            lexicon if lexicon is not None else RootLexicon.default_text_dictionaries()
        )
        self.morphotactics = TurkishMorphotactics(self.lexicon)
        self.analyzer = RuleBasedAnalyzer(self.morphotactics)
        self.formatter = (
            DefaultFormatter(True)
            if formatter is None
            else MorphAnalyzer.formatters[formatter]()
        )

    def analyze(self, text, verbose=False):
        result = []
        sentences = sent_tokenize(text, language="turkish")
        for sentence in sentences:
            sentence_analysis = self.analyze_sentence(sentence)
            result.append((sentence, sentence_analysis))
        return result

    @staticmethod
    def normalize_word(word):
        word = tr.normalize_circumflex(tr.lower(word))
        # TODO: Decide what to do with apostrophes
        word = word.replace("'", "")
        return tr.lower(word)

    def lemmatize(self, text, by_sent=False, no_punctuation=False):
        import string

        punctuation = list(string.punctuation)
        punctuation.remove("'")
        #print(f"Punctuation: {string.punctuation}/ {punctuation}")
        result = []
        sentences = sent_tokenize(text, language="turkish")
        if by_sent:
            for sentence in sentences:
                sentence_lemmas = self.lemmatize_sentence(sentence, no_punctuation)
                result.append((sentence, sentence_lemmas))
        else:
            for word in word_tokenize(text, language="turkish"):
                res_word = []
                for letter in word:
                    if letter not in punctuation:
                        res_word.append(letter)
                word = "".join(res_word)
                if word:
                    lemmas = self.lemmatize_word(tr.lower(word))
                    result.append((word, lemmas))
        return result

    def analyze_word(self, word) -> typing.List[Parse]:
        normalized_word = self.normalize_word(word)
        analysis = self.analyzer.analyze(normalized_word)
        if len(analysis) == 0:
            return [Parse(word, 'Unk', 'Unk', 'Unk')]
        result = []
        for a in analysis:
            formatted = self.formatter.format(a)
            morpheme_list = [m[0].id_ for m in a.morphemes]
            result.append(Parse(word, a.dict_item.lemma, morpheme_list, formatted))
        return result

    def lemmatize_word(self, word):
        analysis = self.analyzer.analyze(word)
        if len(analysis) == 0:
            return [word]
        else:
            return list(set([tuple([morpheme[1] for morpheme in single_analysis.morphemes]) for single_analysis in analysis]))
            return list(set([a.dict_item.lemma for a in analysis]))

    def analyze_sentence(self, sentence):
        result = []
        for word in word_tokenize(sentence, language="turkish"):
            result.append(self.analyze_word(word))
        return result

    def lemmatize_sentence(self, sentence, no_punctuation=True):
        from trLemmer import tr
        import string

        punct = string.punctuation
        result = []
        # words = [_ for _ in re.split(r"(\W+)", sentence) if _ != ' ']
        words = word_tokenize(sentence, language="turkish")
        for word in words:
            if no_punctuation and word in punct:
                continue
            res_word = []
            for letter in word:
                if letter not in punct:
                    res_word.append(letter)
            word = "".join(res_word)
            if word:
                analysis = self.lemmatize_word(tr.lower(word))
                result.append((word, analysis))
        return result

class DefaultFormatter:
    def __init__(self, add_surface=True):
        self.add_surface = add_surface

    def format(self, analysis) -> str:
        result = f"[{analysis.dict_item.lemma}:{analysis.dict_item.primary_pos.value}"
        if analysis.dict_item.secondary_pos != SecondaryPos.NONE:
            result = (
                f"{result},{analysis.dict_item.secondary_pos.value}] "
            )  # TODO: check name works
        else:
            result = f"{result}] "
        result = ""
        result += self.format_morphemes(stem=analysis.stem, surfaces=analysis.morphemes)
        return result

    def format_morphemes(self, stem, surfaces):
        result = []
        if self.add_surface:
            result.append(f"{stem}:")
        result.append(surfaces[0][0].id_)
        if len(surfaces) > 1 and not surfaces[1][0].derivational:
            result.append(" + ")
        for i, sf in enumerate(surfaces):
            if i == 0:
                continue
            m, surface = sf
            if m.derivational:
                result.append(" | ")
            if self.add_surface and len(surface) > 0:
                result.append(f"{surface}:")
            result.append(m.id_)
            if m.derivational:
                result.append("→")
            elif i < len(surfaces) - 1 and not surfaces[i + 1][0].derivational:
                result.append(" + ")
        return "".join(result)

class SingleAnalysis:
    """
       This class represents a single morphological analysis result.
       :param dict_item: Dictionary Item of the analysis.
       :param morpheme_data_list: Contains Morphemes and their surface form (actual appearance in the normalized input)
       List also contain the root (unchanged or modified) of the Dictionary item.
       For example, for normalized input "kedilere"
       This list may contain "kedi:Noun, ler:A3pl , e:Dat" information.
       :param group_boundaries: groupBoundaries holds the index values of morphemes.
       """

    def __init__(self, search_path: SearchPath):
        self.path = search_path
        self.morphemes = []
        self.derivation_count = 0
        self.dict_item = None
        self.group_boundaries = []
        self.parse_dict_item_and_boundaries()

    def __repr__(self):
        return f"{self.dict_item}-{self.morphemes}"

    def parse_dict_item_and_boundaries(self):
        for transition in self.path.transitions:
            if transition.is_derivative:
                self.derivation_count += 1
            morpheme = transition.morpheme
            # we skip these two morphemes as they create visual noise and does not carry much information.
            if morpheme == nom or morpheme == pnon:
                continue
            if len(transition.surface) == 0:
                morpheme_data = (morpheme, "")
                self.morphemes.append(morpheme_data)
                continue
            morpheme_data = (morpheme, transition.surface)
            self.morphemes.append(morpheme_data)
        group_boundaries = [
            0 for _ in range(self.derivation_count + 1)
        ]  # we assume there is always an IG
        group_boundaries[0] = 0
        morpheme_counter = 0
        derivation_counter = 1
        for mdata in self.morphemes:
            if mdata[0].derivational:
                group_boundaries[derivation_counter] = morpheme_counter
                derivation_counter += 1
            morpheme_counter += 1

        # if dictionary item is `Dummy`, use the referenced item.
        # `Dummy` items are usually generated for some compound words. For example for `zeytinyağı`
        # a DictionaryItem is generated with root "zeytinyağ". But here we switch to the original.
        dict_item = self.path.dict_item
        if dict_item.has_attribute(RootAttribute.Dummy):
            dict_item = dict_item.ref_item
        self.dict_item = dict_item
        self.group_boundaries = group_boundaries

    @property
    def stem(self):
        return self.morphemes[0][1]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 17 14:33:06 2019

@author: tao
"""
#coding:utf-8
import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from argparse import ArgumentParser
from matrix_completion import *
from matrix_completion import gen_mask
import matplotlib.pyplot as plt
from impyute.imputation.cs import fast_knn
from impyute.imputation.cs import mice
from sklearn.metrics import mean_squared_error #均方误差
from sklearn.metrics import mean_absolute_error #平方绝对误差
from sklearn.metrics import r2_score#R square
from sklearn import preprocessing
from fancyimpute import KNN, NuclearNormMinimization, SoftImpute, BiScaler
import tensorflow as tf
from sklearn.preprocessing import scale
from sklearn.preprocessing import StandardScaler
import time

def gen_mask2():
    mat = np.ones((40,40),dtype=int)
    for i in range(15):
        for j in range(15):
            mat[i][j]=0
    return np.reshape(mat,(1,-1))

def plot_image(A):
    plt.imshow(A.T)
    plt.show()
def show():
    time_slots = 100

    data_path = "../data/DlRsrpSinrStats.txt"

    #data_set = np.loadtxt(data_path,delimiter='	',skiprows=1);
    data_set = pd.read_table(data_path,delimiter='\t')
    print(data_set.shape)

    df = pd.DataFrame(data_set)

    rsrp=[]
    #construct matrix with location and timeslot
    bias = 3200*149
    for i in range(time_slots):
        temp = np.array((df.loc[bias+i*3200:bias+(i+1)*3200-1].sort_values('IMSI'))['rsrp'])
        temp=temp[0:1600]

        row = np.array(temp)

        rsrp.append(row)
    #rsrp = np.array((df.loc[0:time_slots*total_num-1].sort_values('IMSI'))['rsrp'])
    #print('rsrp shape:',rsrp.shape)
    #print(rsrp.shape)

    mrsrp=[]
    for power in rsrp:
        temp_power = 10*np.log10(power) + 30
        mrsrp.append(temp_power)

    mrsrp = np.array(mrsrp)

#    mrsrp = scale(mrsrp)

    print('mrsrp',mrsrp.shape)

    # generate a maskmatrix
    m = 40
    n = 40

    missing_rates = [0.75]
    #[0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95]
    #random_mat = np.random.uniform(size=[])
    xaxis=[]

    knnxaxis=[]
    knnyaxis=[]

#    iiyaxis=[]
    nnmxaxis=[]
    nnmyaxis=[]
    micexaxis=[]
    miceyaxis=[]

    knntime=[]
    nnmtime=[]
    micetime=[]

    for missing_rate in missing_rates:
        knny=[]
#        iiy=[]
        nny=[]
        micey=[]
        nnmy=[]

        ktime=0
        ntime=0
        mtime=0

        for mat_rsrp in mrsrp:
            mat_rsrp = np.array(mat_rsrp).reshape(40,40)
            mask = gen_mask(m, n, missing_rate)
#            mask = gen_mask2().reshape(40,40)
            print(mask)
            sample_data = mat_rsrp* mask

            print('origin_data')
            plot_image(mat_rsrp)

            print('the sample_data')
            plot_image(sample_data)

            try:
                t1 = time.time()
                sample_data[sample_data==0] = np.nan
                knn_recover = fast_knn(sample_data,k=3)
                print('knn')
                plot_image(knn_recover)
                error_knn = mean_absolute_error(mat_rsrp,knn_recover)
                print(error_knn)
                knny.append(error_knn)
                t2 = time.time()
                ktime = ktime + (t2-t1)
            except ValueError:
                knny.append(2)
                t2 = time.time()
                ktime = ktime + 600*(1+missing_rate)

            try:
                t1 = time.time()
                mice_data = mice(sample_data)
                print('mice')
                plot_image(mice_data)
                error_mice = mean_absolute_error(mat_rsrp,mice_data)

                micey.append(error_mice)

                t2 = time.time()
                mtime = mtime + (t2-t1)
            except ValueError:
                micey.append(2)

                t2 = time.time()
                mtime = mtime + 600*(1+missing_rate)

            try:
                t1 = time.time()

                X_filled_nnm = SoftImpute().fit_transform(sample_data)
                print('NuclearNormMinimization')
                plot_image(X_filled_nnm)
                error_nuclear = mean_absolute_error(mat_rsrp,X_filled_nnm)
                nnmy.append(error_nuclear)

                t2 = time.time()
                ntime = ntime + (t2-t1)
            except:
                nnmy.append(2)

                t2 = time.time()
                ntime = ntime + 600*(1+missing_rate)

            break
#            print("\tknn:",error_knn,"\tmice:",error_mice,"\titer:",error_iter,"\tnuclear",error_nuclear)
        knntime.append(ktime)
        nnmtime.append(ntime)
        micetime.append(mtime)

        knnyaxis.append(np.mean(np.array(knny)))
        miceyaxis.append(np.mean(np.array(micey)))
#        iiyaxis.append(np.mean(np.array(iiy)))
        nnmyaxis.append(np.mean(np.array(nnmy)))
        xaxis.append(missing_rate)

#    plt.plot(xaxis,iiy,c='red',label='iter')
#    plt.plot(xaxis,knny,c='blue',label='knn')
#    plt.plot(xaxis,nnmy,c='orange',label='nnm')
#    plt.plot(xaxis,micey,c='black',label='mice')
#
#    plt.xlabel("missing rate")
#    plt.ylabel("mae")
#    plt.legend()
#    plt.show()
        res = np.array([xaxis,knnyaxis,nnmyaxis,miceyaxis, knntime,nnmtime,micetime])
    return res ;

res = show()
print(res)
out = open('test.csv','a', newline='')
#csv_write = csv.writer(out,dialect='excel')
for d in res:
    print(d)
    csv_write.writerow(d)
out.close()


import re,string,os,sys
import afs.dao.bin
from afs.dao.BaseDAO import BaseDAO

class RxOsdDAO(BaseDAO):

    """
    stuff to do with rxosd-servers
    """

    def __init__(self) :
        BaseDAO.__init__(self)
        return

    def examine(self, osd_id, fid,cellname,token,lun=0):
        """
        examine an object
        """
        CmdList=[afs.dao.bin.OSDBIN, "examine","-osd","%s" % osd_id,"-fid","%s" % fid, "-cell",  "%s" % cellname ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)

        obj_dict={}

        return obj_dict

    def getFetchQueue(self, cellname,token, osd_id=None,):
        """
        query fetchqueues of archival osd.
        """
        CmdList=[afs.dao.bin.OSDBIN, "fetchq", "-cell",  "%s" % cellname ]
        if osd_id :
            CmdList += ["-name" , "%s" % osd_id]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        FetchQDict={}
        return FetchQDict

    def getListOfServerSettings(self,osd_id,cellname,token):
        """
        query Servers for it tuneables
        """
        CmdList=[afs.dao.bin.OSDBIN, "whichvariables","-server", "%s" % osd_id, "-cell",  "%s" % cellname ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        settingsList=[]
        return settingsList

    def getServerSetting(self,osd_id,key,cellname,token):
        """
        query Servers for it tuneables
        """
        CmdList=[afs.dao.bin.OSDBIN, "getvariable","-server", "%s" % osd_id,"-variable","%s" % key, "-cell",  "%s" % cellname ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        value=""
        return value

    def setServerSettings(self,osd_id,key,value,cellname,token) :
        """
        set Server tunable. Verifies result
        """
        CmdList=[afs.dao.bin.OSDBIN, "setvariable","-server", "%s" % osd_id, "-variable", "%s" % key, "-value" % value, "-cell",  "%s" % cellname ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        newSettings=self.getServeSettings(self,osd_id,key,cellname,token)
        if newSettings[key] == value :
            return value
        else :
            raise RxOsdError("Error", "failed to set variable")

    def getObjectsofVolumeByOsd(self,osd_id,vid,lun,cellname,token) :
        """
        get all objects of a given volume on an RxOsd.
        Optional: define lun on RxOsd
        """
        CmdList=[afs.dao.bin.OSDBIN, "objects", "-osd","%s" % osd_id, "-volume", "%s" % vid, "-cell",  "%s" % cellname ]
        if osd_id :
            CmdList += ["-lun" , "%s" % lun]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        obj_list=[]
        return obj_list

    def getStatistics(self,osd_id,cellname,token,extended=False) :
        """
        get RPC-statistics of RxOsd-sever
        """
        CmdList=[afs.dao.bin.OSDBIN, "statistics", "-osd","%s" % osd_id, "-cell",  "%s" % cellname ]
        if extended :
            CmdList += ["-verbose" ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        statDict={}
        return statDict

    def resetStatistics(self,osd_id,cellname,token) :
        """
        reset RPC-statistics of a RxOsd-sever
        """
        CmdList=[afs.dao.bin.OSDBIN, "statistics", "-osd","%s" % osd_id,"-reset", "-cell",  "%s" % cellname ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        return

    def getActiveThreads(self,osd_id,cellname,token) :
        """
        get list of active threads of a RxOsd-sever
        """
        CmdList=[afs.dao.bin.OSDBIN, "threads", "-server","%s" % osd_id, "-cell",  "%s" % cellname ]
        if extended :
            CmdList += ["-verbose" ]
        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        threadList=[]
        return threadList

    def getWipeCandidates(self,osd_id,cellname,token,lun=None,maxNum=100,criteria="atime",minSizeMB=0) :
        CmdList=[afs.dao.bin.OSDBIN, "threads", "-server","%s" % osd_id, "-max" , "%s" % maxNum, "-minMB", "%s" % minSizeMB, "-cell",  "%s" % cellname ]
        if lun :
            CmdList += ["-lun", "%s" % lun ]
        if criteria == "atime" :
            CmdList += ["-crit" , "0"]
        elif criteria == "size" :
            CmdList += ["-crit" , "1"]
        elif criteria == "sizeXage" :
            CmdList += ["-crit" , "2"]
        else :
            raise RxOsdError("Error", "invalid criteria %s" % criteria)

        rc,output,outerr=self.execute(CmdList)
        if rc:
            raise RxOsdError("Error", outerr)
        candList=[]
        return candList


from requests_html import HTML, HTMLSession
import csv

with open('sample.html') as html_file:
  source = html_file.read()
  html = HTML(html=source)
  html.render()

match = html.find('#footer', first=True)
print(match.text)

# articles = html.find('div.article')
# for article in articles:
#   headline = article.find('h2', first=True).text
#   summary = article.find('p', first=True).text
#   print(headline)
#   print(summary)

# csv_file = open('cms_scrape.csv', 'w')
# csv_writer = csv.writer(csv_file)
# csv_writer.writerow(['headline', 'summary', 'video'])

# session = HTMLSession()
# r = session.get('https://coreyms.com/')

# for link in r.html.absolute_links:
#   print(link)

# article = r.html.find('article', first=True)
# # print(article.html)
# headline = article.find('.entry-title-link', first=True).text
# # print(headline)

# summary = article.find('.entry-content p', first=True).text
# # print(summary)

# video_src = article.find('iframe', first=True).attrs['src']
# video_id = video_src.split('/')[4]
# video_id = video_id.split('?')[0]

# youtube_link = f'https://youtube.com/watch?v={video_id}'
# print(youtube_link)

# articles = r.html.find('article')

# for article in articles:
#   headline = article.find('.entry-title-link', first=True).text
#   print(headline)

#   summary = article.find('.entry-content p', first=True).text
#   print(summary)

#   try:
#     video_src = article.find('iframe', first=True).attrs['src']
#     video_id = video_src.split('/')[4]
#     video_id = video_id.split('?')[0]
#   except Exception as e:
#     youtube_link = None

#   youtube_link = f'https://youtube.com/watch?v={video_id}'
#   print(youtube_link,'\n')

#   csv_writer.writerow([headline, summary, youtube_link])
# csv_file.close()


import os
import numpy as np
import copy
from pathlib import Path
from ..utils.visualization import heat_map, contour, contour_heat, draw_pr_line

def main(data: np.array):
    # contour_heat(data)
    contour(data)
    # heat_map(data)

def mean(data: np.array, i: int, j: int):
    sum = 0
    counter = 0
    dirs = [[1,0],[-1,0],[0,1],[0,-1]]
    for dir in dirs:
        newi = i + dir[0]
        newj = j + dir[1]
        if(newi >=0 and newi < len(data) and newj >=0 and newj < len(data[i])):
            sum += data[newi][newj]
            counter += 1
    return sum/counter

def process(data_origin: np.array):
    data = copy.deepcopy(data_origin)
    for i in range(len(data)):
        for j in range(len(data[i])):
            data[i][j] = mean(data, i, j)
    # for i in range(len(data)):
    #     for j in range(len(data[i])):
    #         data[i][j] /= 2
    return data

def calc_mean(data: np.array):
    sum = 0
    counter = 0
    for i in range(len(data)):
        for j in range(len(data[i])):
            if data[i][j] != 0:
                sum += data[i][j]
                counter += 1
    return sum/counter

if __name__ == '__main__':
    # get ROOT DIR
    ROOT_DIR = Path(__file__).resolve().parents[2]

    # data_dir = os.path.join(str(ROOT_DIR), 'src/LidCamFusion/eval/20220820-110456/heat_map_alpha.txt')
    data_dir = os.path.join(str(ROOT_DIR), 'src/LidCamFusion/eval/20220822-034113/heat_map_alpha_similarity.txt')
    # txt_dir = os.path.join(str(ROOT_DIR), 'src/LidCamFusion/eval/20220820-110456/tp_fp_fn.txt')
    data_origin = np.loadtxt(data_dir)
    print(calc_mean(data_origin))
    # data = process(data_origin)
    # main(data)
    # draw_pr_line(txt_dir)


"""ratemyrig URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import RedirectView
from django.views.static import serve

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('home.urls')),
    path('home/', RedirectView.as_view( pattern_name='home', permanent=False)),
    path('builds/', RedirectView.as_view( pattern_name='blog-home', permanent=False)),
    path('blog/search/', RedirectView.as_view( pattern_name='search', permanent=False)),
    path('blog/', include('blog.urls')),
    path('user/', include('users.urls')),
    path('cart/', include('cart.urls')),
    path('checkout/', include('checkout.urls')),
    path('contact/', include('sendemail.urls')),
    path('search/', include('search.urls')),
    re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]