import copy

from .common.codegen import CodeGen, VulkanWrapperGenerator
from .common.vulkantypes import \
        VulkanAPI, makeVulkanTypeSimple, iterateVulkanType

from .marshaling import VulkanMarshalingCodegen
from .reservedmarshaling import VulkanReservedMarshalingCodegen
from .counting import VulkanCountingCodegen
from .handlemap import HandleMapCodegen
from .deepcopy import DeepcopyCodegen
from .transform import TransformCodegen, genTransformsForVulkanType

from .wrapperdefs import API_PREFIX_RESERVEDMARSHAL
from .wrapperdefs import API_PREFIX_MARSHAL
from .wrapperdefs import API_PREFIX_UNMARSHAL
from .wrapperdefs import ROOT_TYPE_DEFAULT_VALUE
from .wrapperdefs import VULKAN_STREAM_TYPE_GUEST

encoder_decl_preamble = """
class VkEncoder {
public:
    VkEncoder(IOStream* stream);
    ~VkEncoder();

#include "VkEncoder.h.inl"
"""

encoder_decl_postamble = """
private:
    class Impl;
    std::unique_ptr<Impl> mImpl;
};
"""

encoder_impl_preamble ="""

using namespace goldfish_vk;

using android::aligned_buf_alloc;
using android::aligned_buf_free;
using android::base::guest::AutoLock;
using android::base::guest::Lock;
using android::base::BumpPool;

#include "VkEncoder.cpp.inl"

#define VALIDATE_RET(retType, success, validate) \\
    retType goldfish_vk_validateResult = validate; \\
    if (goldfish_vk_validateResult != success) return goldfish_vk_validateResult; \\

#define VALIDATE_VOID(validate) \\
    VkResult goldfish_vk_validateResult = validate; \\
    if (goldfish_vk_validateResult != VK_SUCCESS) return; \\

"""

STREAM = "stream"
RESOURCES = "sResourceTracker"
POOL = "pool"

ENCODER_PREVALIDATED_APIS = [
    "vkFlushMappedMemoryRanges",
    "vkInvalidateMappedMemoryRanges",
]

ENCODER_CUSTOM_RESOURCE_PREPROCESS = [
    "vkMapMemoryIntoAddressSpaceGOOGLE",
    "vkDestroyDevice",
]

ENCODER_CUSTOM_RESOURCE_POSTPROCESS = [
    "vkCreateInstance",
    "vkCreateDevice",
    "vkMapMemoryIntoAddressSpaceGOOGLE",
    "vkGetPhysicalDeviceProperties",
    "vkGetPhysicalDeviceProperties2",
    "vkGetPhysicalDeviceProperties2KHR",
    "vkGetPhysicalDeviceMemoryProperties",
    "vkGetPhysicalDeviceMemoryProperties2",
    "vkGetPhysicalDeviceMemoryProperties2KHR",
    "vkCreateDescriptorUpdateTemplate",
    "vkCreateDescriptorUpdateTemplateKHR",
    "vkGetPhysicalDeviceExternalSemaphoreProperties",
    "vkGetPhysicalDeviceExternalSemaphorePropertiesKHR",
]

ENCODER_EXPLICIT_FLUSHED_APIS = [
    "vkEndCommandBufferAsyncGOOGLE",
    "vkQueueSubmitAsyncGOOGLE",
    "vkQueueBindSparseAsyncGOOGLE",
    "vkQueueWaitIdleAsyncGOOGLE",
    "vkQueueSignalReleaseImageANDROID",
    "vkDestroyDevice",
]

SUCCESS_RET_TYPES = {
    "VkResult" : "VK_SUCCESS",
    "void" : None,
    # TODO: Put up success results for other return types here.
}

ENCODER_THIS_PARAM = makeVulkanTypeSimple(False, "VkEncoder", 1, "this")

# Common components of encoding a Vulkan API call
def make_event_handler_call(
    handler_access,
    api,
    context_param,
    input_result_param,
    cgen,
    suffix=""):
    extraParams = [context_param.paramName]
    if input_result_param:
        extraParams.append(input_result_param)
    return cgen.makeCallExpr( \
               "%s->on_%s%s" % (handler_access, api.name, suffix),
               extraParams + \
                       [p.paramName for p in api.parameters[:-1]])

def emit_custom_pre_validate(typeInfo, api, cgen):
    if api.name in ENCODER_PREVALIDATED_APIS:
        callExpr = \
            make_event_handler_call( \
                "mImpl->validation()", api,
                ENCODER_THIS_PARAM,
                SUCCESS_RET_TYPES[api.getRetTypeExpr()],
                cgen)

        if api.getRetTypeExpr() == "void":
            cgen.stmt("VALIDATE_VOID(%s)" % callExpr)
        else:
            cgen.stmt("VALIDATE_RET(%s, %s, %s)" % \
                (api.getRetTypeExpr(),
                 SUCCESS_RET_TYPES[api.getRetTypeExpr()],
                 callExpr))

def emit_custom_resource_preprocess(typeInfo, api, cgen):
    if api.name in ENCODER_CUSTOM_RESOURCE_PREPROCESS:
        cgen.stmt( \
            make_event_handler_call( \
                "sResourceTracker", api,
                ENCODER_THIS_PARAM,
                SUCCESS_RET_TYPES[api.getRetTypeExpr()],
                cgen, suffix="_pre"))

def emit_custom_resource_postprocess(typeInfo, api, cgen):
    if api.name in ENCODER_CUSTOM_RESOURCE_POSTPROCESS:
        cgen.stmt(make_event_handler_call( \
            "sResourceTracker",
            api,
            ENCODER_THIS_PARAM,
            api.getRetVarExpr(),
            cgen))

def emit_count_marshal(typeInfo, param, cgen):
    res = \
        iterateVulkanType(
            typeInfo, param,
            VulkanCountingCodegen( \
                cgen, "sFeatureBits", param.paramName, "countPtr", ROOT_TYPE_DEFAULT_VALUE,
               "count_"))
    if not res:
        cgen.stmt("(void)%s" % param.paramName)

def emit_marshal(typeInfo, param, cgen):
    forOutput = param.isHandleType() and ("out" in param.inout)
    if forOutput:
        cgen.stmt("/* is handle, possibly out */")

    res = \
        iterateVulkanType(
            typeInfo, param,
            VulkanReservedMarshalingCodegen( \
                cgen, STREAM, ROOT_TYPE_DEFAULT_VALUE, param.paramName, "streamPtrPtr",
               API_PREFIX_RESERVEDMARSHAL,
               "" if forOutput else "get_host_u64_",
               direction="write"))
    if not res:
        cgen.stmt("(void)%s" % param.paramName)

    if forOutput:
        cgen.stmt("/* is handle, possibly out */")

def emit_unmarshal(typeInfo, param, cgen):
    iterateVulkanType(
        typeInfo, param,
        VulkanMarshalingCodegen( \
            cgen, STREAM, ROOT_TYPE_DEFAULT_VALUE, param.paramName,
           API_PREFIX_UNMARSHAL, direction="read"))

def emit_deepcopy(typeInfo, param, cgen):
    res = \
        iterateVulkanType(typeInfo, param, DeepcopyCodegen(
            cgen, [param.paramName, "local_" + param.paramName], "pool", ROOT_TYPE_DEFAULT_VALUE, "deepcopy_"))
    if not res:
        cgen.stmt("(void)%s" % param.paramName)

def emit_transform(typeInfo, param, cgen, variant="tohost"):
    res = \
        iterateVulkanType(typeInfo, param, TransformCodegen( \
            cgen, param.paramName, "sResourceTracker", "transform_%s_" % variant, variant))
    if not res:
        cgen.stmt("(void)%s" % param.paramName)

def emit_handlemap_create(typeInfo, param, cgen):
    iterateVulkanType(typeInfo, param, HandleMapCodegen(
        cgen, None, "sResourceTracker", "handlemap_",
        lambda vtype: typeInfo.isHandleType(vtype.typeName)
    ))

def custom_encoder_args(api):
    params = ["this"]
    if api.getRetVarExpr() is not None:
        params.append(api.getRetVarExpr())
    return params

def emit_handlemap_destroy(typeInfo, param, cgen):
    iterateVulkanType(typeInfo, param, HandleMapCodegen(
        cgen, None, "sResourceTracker->destroyMapping()", "handlemap_",
        lambda vtype: typeInfo.isHandleType(vtype.typeName)
    ))

class EncodingParameters(object):
    def __init__(self, api):
        self.localCopied = []
        self.toWrite = []
        self.toRead = []
        self.toCreate = []
        self.toDestroy = []

        for param in api.parameters:
            param.action = None
            param.inout = "in"

            if param.paramName == "doLock":
                continue

            if param.possiblyOutput():
                param.inout += "out"
                self.toWrite.append(param)
                self.toRead.append(param)
                if param.isCreatedBy(api):
                    self.toCreate.append(param)
                    param.action = "create"
            else:

                if param.paramName == "doLock":
                    continue

                if param.isDestroyedBy(api):
                    self.toDestroy.append(param)
                    param.action = "destroy"
                localCopyParam = \
                    param.getForNonConstAccess().withModifiedName( \
                        "local_" + param.paramName)
                self.localCopied.append((param, localCopyParam))
                self.toWrite.append(localCopyParam)

def emit_parameter_encode_preamble_write(typeInfo, api, cgen):
    emit_custom_pre_validate(typeInfo, api, cgen);
    emit_custom_resource_preprocess(typeInfo, api, cgen);

    cgen.stmt("auto %s = mImpl->stream()" % STREAM)
    cgen.stmt("auto %s = mImpl->pool()" % POOL)
    # cgen.stmt("%s->setHandleMapping(%s->unwrapMapping())" % (STREAM, RESOURCES))

    encodingParams = EncodingParameters(api)
    for (_, localCopyParam) in encodingParams.localCopied:
        cgen.stmt(cgen.makeRichCTypeDecl(localCopyParam))

def emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen, customUnwrap=None):
    encodingParams = EncodingParameters(api)

    for (origParam, localCopyParam) in encodingParams.localCopied:
        shouldCustomCopy = \
            customUnwrap and \
            origParam.paramName in customUnwrap and \
            "copyOp" in customUnwrap[origParam.paramName]

        shouldCustomMap = \
            customUnwrap and \
            origParam.paramName in customUnwrap and \
            "mapOp" in customUnwrap[origParam.paramName]

        if shouldCustomCopy:
            customUnwrap[origParam.paramName]["copyOp"](cgen, origParam, localCopyParam)
        else:
            # if this is a pointer type and we don't do custom copy nor unwrap,
            # and the transform doesn't end up doing anything,
            # don't deepcopy, just cast it.

            avoidDeepcopy = False

            if origParam.pointerIndirectionLevels > 0:
                testCgen = CodeGen()
                genTransformsForVulkanType("sResourceTracker", origParam, lambda p: testCgen.generalAccess(p, parentVarName = None, asPtr = True), lambda p: testCgen.generalLengthAccess(p, parentVarName = None), testCgen)
                emit_transform(typeInfo, origParam, testCgen, variant="tohost")
                if "" == testCgen.swapCode():
                    avoidDeepcopy = True
            if avoidDeepcopy:
                cgen.line("// Avoiding deepcopy for %s" % origParam.paramName)
                cgen.stmt("%s = (%s%s)%s" % (localCopyParam.paramName, localCopyParam.typeName, "*" * origParam.pointerIndirectionLevels, origParam.paramName))
            else:
                emit_deepcopy(typeInfo, origParam, cgen)

    for (origParam, localCopyParam) in encodingParams.localCopied:
        shouldCustomMap = \
            customUnwrap and \
            origParam.paramName in customUnwrap and \
            "mapOp" in customUnwrap[origParam.paramName]

        if shouldCustomMap:
            customUnwrap[origParam.paramName]["mapOp"](cgen, origParam, localCopyParam)
        else:
            if localCopyParam.typeName == "VkAllocationCallbacks":
                cgen.stmt("%s = nullptr" % localCopyParam.paramName)

    apiForTransform = \
        api.withCustomParameters( \
            map(lambda p: p[1], \
                encodingParams.localCopied))

    # Apply transforms if applicable.
    # Apply transform to API itself:
    genTransformsForVulkanType(
        "sResourceTracker",
        apiForTransform,
        lambda p: cgen.generalAccess(p, parentVarName = None, asPtr = True),
        lambda p: cgen.generalLengthAccess(p, parentVarName = None),
        cgen)

    # For all local copied parameters, run the transforms
    for localParam in apiForTransform.parameters:
        if "doLock" in localParam.paramName:
            continue
        emit_transform(typeInfo, localParam, cgen, variant="tohost")

    cgen.stmt("size_t count = 0")
    cgen.stmt("size_t* countPtr = &count")
    cgen.beginBlock()

    # Use counting stream to calculate the packet size.
    for p in encodingParams.toWrite:
        emit_count_marshal(typeInfo, p, cgen)

    cgen.endBlock()

def is_cmdbuf_dispatch(api):
    return "VkCommandBuffer" == api.parameters[0].typeName

def emit_parameter_encode_write_packet_info(typeInfo, api, cgen):
    # Seqno and skipping dispatch serialize are for use with VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT
    doSeqno = True
    doDispatchSerialize = True

    if is_cmdbuf_dispatch(api):
        doSeqno = False
        doDispatchSerialize = False

    if doSeqno:
        cgen.stmt("uint32_t packetSize_%s = 4 + 4 + (queueSubmitWithCommandsEnabled ? 4 : 0) + count" % (api.name))
    else:
        cgen.stmt("uint32_t packetSize_%s = 4 + 4 + count" % (api.name))

    if not doDispatchSerialize:
        cgen.stmt("if (queueSubmitWithCommandsEnabled) packetSize_%s -= 8" % api.name)

    cgen.stmt("uint8_t* streamPtr = %s->reserve(packetSize_%s)" % (STREAM, api.name))
    cgen.stmt("uint8_t** streamPtrPtr = &streamPtr")
    cgen.stmt("uint32_t opcode_%s = OP_%s" % (api.name, api.name))

    if doSeqno:
        cgen.stmt("uint32_t seqno; if (queueSubmitWithCommandsEnabled) seqno = ResourceTracker::nextSeqno()")

    cgen.stmt("memcpy(streamPtr, &opcode_%s, sizeof(uint32_t)); streamPtr += sizeof(uint32_t)" % api.name)
    cgen.stmt("memcpy(streamPtr, &packetSize_%s, sizeof(uint32_t)); streamPtr += sizeof(uint32_t)" % api.name)

    if doSeqno:
        cgen.line("if (queueSubmitWithCommandsEnabled) { memcpy(streamPtr, &seqno, sizeof(uint32_t)); streamPtr += sizeof(uint32_t); }")

def emit_parameter_encode_do_parameter_write(typeInfo, api, cgen):
    encodingParams = EncodingParameters(api)

    dispatchDone = False

    for p in encodingParams.toWrite:
        if is_cmdbuf_dispatch(api) and not dispatchDone:
            cgen.beginIf("!queueSubmitWithCommandsEnabled")
            emit_marshal(typeInfo, p, cgen)
            cgen.endIf()
        else:
            emit_marshal(typeInfo, p, cgen)

        dispatchDone = True

def emit_parameter_encode_read(typeInfo, api, cgen):
    encodingParams = EncodingParameters(api)

    for p in encodingParams.toRead:
        if p.action == "create":
            cgen.stmt(
                "%s->setHandleMapping(%s->createMapping())" % \
                (STREAM, RESOURCES))
        emit_unmarshal(typeInfo, p, cgen)
        if p.action == "create":
            cgen.stmt(
                "%s->unsetHandleMapping()" % STREAM)
        emit_transform(typeInfo, p, cgen, variant="fromhost")

def emit_post(typeInfo, api, cgen):
    encodingParams = EncodingParameters(api)

    emit_custom_resource_postprocess(typeInfo, api, cgen)

    for p in encodingParams.toDestroy:
        emit_handlemap_destroy(typeInfo, p, cgen)

    doSeqno = True
    if is_cmdbuf_dispatch(api):
        doSeqno = False

    retType = api.getRetTypeExpr()

    if api.name in ENCODER_EXPLICIT_FLUSHED_APIS:
        cgen.stmt("stream->flush()");
        return

    if doSeqno:
        if retType == "void":
            encodingParams = EncodingParameters(api)
            if 0 == len(encodingParams.toRead):
                cgen.stmt("stream->flush()");

def emit_pool_free(cgen):
    cgen.stmt("++encodeCount;")
    cgen.beginIf("0 == encodeCount % POOL_CLEAR_INTERVAL")
    cgen.stmt("pool->freeAll()")
    cgen.stmt("%s->clearPool()" % STREAM)
    cgen.endIf()

def emit_return_unmarshal(typeInfo, api, cgen):

    retType = api.getRetTypeExpr()

    if retType == "void":
        return

    retVar = api.getRetVarExpr()
    cgen.stmt("%s %s = (%s)0" % (retType, retVar, retType))
    cgen.stmt("%s->read(&%s, %s)" % \
              (STREAM, retVar, cgen.sizeofExpr(api.retType)))

def emit_return(typeInfo, api, cgen):
    if api.getRetTypeExpr() == "void":
        return

    retVar = api.getRetVarExpr()
    cgen.stmt("return %s" % retVar)

def emit_lock(cgen):
    cgen.stmt("(void)doLock");
    cgen.stmt("bool queueSubmitWithCommandsEnabled = sFeatureBits & VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT")
    cgen.stmt("if (!queueSubmitWithCommandsEnabled && doLock) this->lock()")

def emit_unlock(cgen):
    cgen.stmt("if (!queueSubmitWithCommandsEnabled && doLock) this->unlock()")

def emit_default_encoding(typeInfo, api, cgen):
    emit_lock(cgen)
    emit_parameter_encode_preamble_write(typeInfo, api, cgen)
    emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen)
    emit_parameter_encode_write_packet_info(typeInfo, api, cgen)
    emit_parameter_encode_do_parameter_write(typeInfo, api, cgen)
    emit_parameter_encode_read(typeInfo, api, cgen)
    emit_return_unmarshal(typeInfo, api, cgen)
    emit_post(typeInfo, api, cgen)
    emit_pool_free(cgen)
    emit_unlock(cgen)
    emit_return(typeInfo, api, cgen)

## Custom encoding definitions##################################################

def emit_only_goldfish_custom(typeInfo, api, cgen):
    emit_lock(cgen)
    cgen.vkApiCall( \
        api,
        customPrefix="sResourceTracker->on_",
        customParameters=custom_encoder_args(api) + \
                [p.paramName for p in api.parameters[:-1]])
    emit_unlock(cgen)
    emit_return(typeInfo, api, cgen)

def emit_only_resource_event(typeInfo, api, cgen):
    cgen.stmt("(void)doLock");
    input_result = None
    retExpr = api.getRetVarExpr()

    if retExpr:
        retType = api.getRetTypeExpr()
        input_result = SUCCESS_RET_TYPES[retType]
        cgen.stmt("%s %s = (%s)0" % (retType, retExpr, retType))

    cgen.stmt(
        (("%s = " % retExpr) if retExpr else "") +
        make_event_handler_call(
            "sResourceTracker",
            api,
            ENCODER_THIS_PARAM,
            input_result, cgen))

    if retExpr:
        emit_return(typeInfo, api, cgen)

def emit_with_custom_unwrap(custom):
    def call(typeInfo, api, cgen):
        emit_lock(cgen)
        emit_parameter_encode_preamble_write(typeInfo, api, cgen)
        emit_parameter_encode_copy_unwrap_count(
            typeInfo, api, cgen, customUnwrap=custom)
        emit_parameter_encode_write_packet_info(typeInfo, api, cgen)
        emit_parameter_encode_do_parameter_write(typeInfo, api, cgen)
        emit_parameter_encode_read(typeInfo, api, cgen)
        emit_return_unmarshal(typeInfo, api, cgen)
        emit_pool_free(cgen)
        emit_unlock(cgen)
        emit_return(typeInfo, api, cgen)
    return call

def encode_vkFlushMappedMemoryRanges(typeInfo, api, cgen):
    emit_lock(cgen)
    emit_parameter_encode_preamble_write(typeInfo, api, cgen)
    emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen)

    def emit_flush_ranges(streamVar):
        cgen.beginIf("!sResourceTracker->usingDirectMapping()")
        cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i")
        cgen.stmt("auto range = pMemoryRanges[i]")
        cgen.stmt("auto memory = pMemoryRanges[i].memory")
        cgen.stmt("auto size = pMemoryRanges[i].size")
        cgen.stmt("auto offset = pMemoryRanges[i].offset")
        cgen.stmt("uint64_t streamSize = 0")
        cgen.stmt("if (!memory) { %s->write(&streamSize, sizeof(uint64_t)); continue; }" % streamVar)
        cgen.stmt("auto hostPtr = sResourceTracker->getMappedPointer(memory)")
        cgen.stmt("auto actualSize = size == VK_WHOLE_SIZE ? sResourceTracker->getMappedSize(memory) : size")
        cgen.stmt("if (!hostPtr) { %s->write(&streamSize, sizeof(uint64_t)); continue; }" % streamVar)
        cgen.stmt("streamSize = actualSize")
        cgen.stmt("%s->write(&streamSize, sizeof(uint64_t))" % streamVar)
        cgen.stmt("uint8_t* targetRange = hostPtr + offset")
        cgen.stmt("%s->write(targetRange, actualSize)" % streamVar)
        cgen.endFor()
        cgen.endIf()

    emit_parameter_encode_write_packet_info(typeInfo, api, cgen)
    emit_parameter_encode_do_parameter_write(typeInfo, api, cgen)

    emit_flush_ranges(STREAM)

    emit_parameter_encode_read(typeInfo, api, cgen)
    emit_return_unmarshal(typeInfo, api, cgen)
    emit_pool_free(cgen)
    emit_unlock(cgen)
    emit_return(typeInfo, api, cgen)

def encode_vkInvalidateMappedMemoryRanges(typeInfo, api, cgen):
    emit_lock(cgen)
    emit_parameter_encode_preamble_write(typeInfo, api, cgen)
    emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen)
    emit_parameter_encode_write_packet_info(typeInfo, api, cgen)
    emit_parameter_encode_do_parameter_write(typeInfo, api, cgen)
    emit_parameter_encode_read(typeInfo, api, cgen)
    emit_return_unmarshal(typeInfo, api, cgen)

    def emit_invalidate_ranges(streamVar):
        cgen.beginIf("!sResourceTracker->usingDirectMapping()")
        cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i")
        cgen.stmt("auto range = pMemoryRanges[i]")
        cgen.stmt("auto memory = pMemoryRanges[i].memory")
        cgen.stmt("auto size = pMemoryRanges[i].size")
        cgen.stmt("auto offset = pMemoryRanges[i].offset")
        cgen.stmt("uint64_t streamSize = 0")
        cgen.stmt("if (!memory) { %s->read(&streamSize, sizeof(uint64_t)); continue; }" % streamVar)
        cgen.stmt("auto hostPtr = sResourceTracker->getMappedPointer(memory)")
        cgen.stmt("auto actualSize = size == VK_WHOLE_SIZE ? sResourceTracker->getMappedSize(memory) : size")
        cgen.stmt("if (!hostPtr) { %s->read(&streamSize, sizeof(uint64_t)); continue; }" % streamVar)
        cgen.stmt("streamSize = actualSize")
        cgen.stmt("%s->read(&streamSize, sizeof(uint64_t))" % streamVar)
        cgen.stmt("uint8_t* targetRange = hostPtr + offset")
        cgen.stmt("%s->read(targetRange, actualSize)" % streamVar)
        cgen.endFor()
        cgen.endIf()

    emit_invalidate_ranges(STREAM)
    emit_pool_free(cgen)
    emit_unlock(cgen)
    emit_return(typeInfo, api, cgen)

def emit_manual_inline(typeInfo, api, cgen):
    cgen.line("#include \"%s_encode_impl.cpp.inl\"" % api.name)

def unwrap_VkNativeBufferANDROID():
    def mapOp(cgen, orig, local):
        cgen.stmt("sResourceTracker->unwrap_VkNativeBufferANDROID(%s, %s)" %
                  (orig.paramName, local.paramName))
    return { "pCreateInfo" : { "mapOp" : mapOp } }

def unwrap_vkAcquireImageANDROID_nativeFenceFd():
    def mapOp(cgen, orig, local):
        cgen.stmt("sResourceTracker->unwrap_vkAcquireImageANDROID_nativeFenceFd(%s, &%s)" %
                  (orig.paramName, local.paramName))
    return { "nativeFenceFd" : { "mapOp" : mapOp } }

custom_encodes = {
    "vkMapMemory" : emit_only_resource_event,
    "vkUnmapMemory" : emit_only_resource_event,
    "vkFlushMappedMemoryRanges" : encode_vkFlushMappedMemoryRanges,
    "vkInvalidateMappedMemoryRanges" : encode_vkInvalidateMappedMemoryRanges,
    "vkCreateImage" : emit_with_custom_unwrap(unwrap_VkNativeBufferANDROID()),
    "vkCreateImageWithRequirementsGOOGLE" : emit_with_custom_unwrap(unwrap_VkNativeBufferANDROID()),
    "vkAcquireImageANDROID" : emit_with_custom_unwrap(unwrap_vkAcquireImageANDROID_nativeFenceFd()),
    "vkQueueFlushCommandsGOOGLE" : emit_manual_inline,
}

class VulkanEncoder(VulkanWrapperGenerator):
    def __init__(self, module, typeInfo):
        VulkanWrapperGenerator.__init__(self, module, typeInfo)

        self.typeInfo = typeInfo

        self.cgenHeader = CodeGen()
        self.cgenHeader.incrIndent()

        self.cgenImpl = CodeGen()

    def onBegin(self,):
        self.module.appendHeader(encoder_decl_preamble)
        self.module.appendImpl(encoder_impl_preamble)

    def onGenCmd(self, cmdinfo, name, alias):
        VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias)

        api = copy.deepcopy(self.typeInfo.apis[name])
        api.parameters.append(makeVulkanTypeSimple(False, "uint32_t", 0, "doLock"))

        self.cgenHeader.stmt(self.cgenHeader.makeFuncProto(api))
        apiImpl = api.withModifiedName("VkEncoder::" + api.name)

        self.module.appendHeader(self.cgenHeader.swapCode())

        if api.name in custom_encodes.keys():
            self.module.appendImpl(self.cgenImpl.makeFuncImpl(
                apiImpl, lambda cgen: custom_encodes[api.name](self.typeInfo, api, cgen)))
        else:
            self.module.appendImpl(self.cgenImpl.makeFuncImpl(apiImpl,
                lambda cgen: emit_default_encoding(self.typeInfo, api, cgen)))

    def onEnd(self,):
        self.module.appendHeader(encoder_decl_postamble)
        self.cgenHeader.decrIndent()


from mininet.topo import Topo

class MyTopo ( Topo ):
    def __init__( self ):
        Topo.__init__( self )

        self.numLayer = 2
        self.numChild = 3
        self.switchCnt = 0
        self.hostCnt = 0

        # Add root switches
        root = self.addSwitch( self.naming('s') )
        self.expansion(root, self.numLayer-1)

    def expansion(self, node, layerTh):
        if layerTh == 0:
            for _ in range(self.numChild):
                self.hostCnt += 1
                child = self.addHost(self.naming('h'))
                self.addLink( node, child )
            return

        for _ in range(self.numChild):
            self.switchCnt += 1
            child = self.addSwitch(self.naming('s'))
            self.addLink( node, child )
            self.expansion(child, layerTh-1)

    def naming(self, device):
        if device == 's':
            return device+str(self.switchCnt)
        elif device == 'h':
            return device+str(self.hostCnt)

topos = { 'topo': MyTopo }


import scanner.token as st
import scanner.scannerprocessor as sp

f_name = 'test.txt'
scanner = sp.Scanner(f_name)

if scanner.filepointer is not None:
    print("token_type           lexeme          value       ptr")
    print("------------------------------------------------------")
    while True:                                                             # 一次读入一个token
        token = scanner.GetToken()                                          # 以空格分开
        if token.type != st.Token_Type.NONTOKEN:
            print("{:20s}|{:12s}|{:12f}|{}".format(
                token.type, token.lexeme, token.value, token.funcptr))
        else:
            break
    print("The text has", scanner.LineNo, "lines.")
else:
    print("File open failed")


from datetime import datetime
import crython
from loguru import logger

from check_holiday import is_workday
import sys
import os

root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

chat_code_path = os.path.join(root_path,'威凡/信息交互自动化')
sys.path.insert(
    0, chat_code_path
)
from chat_robot_2 import send_msg_to_feishu
from tushare_stock2 import auto_get_tushare_data

logger.add("logs/{time}.log")

@crython.job(expr="*/3 * * * * * *")
def run_daily_weekday():
    if is_workday():
        day = datetime.today()
        logger.info(f"今天是{day}")
        auto_get_tushare_data()

@crython.job(expr="*/10 * * * * * *")
def run_daily_weekend():
    # if not is_workday():
    day = datetime.today()
    logger.info(f"今天是{day}")
    logger.info("温馨提醒:今天不是工作日")
    result = auto_get_tushare_data()
    send_msg_to_feishu(result)

if __name__ == "__main__":
    crython.start()
    crython.join()

分布式任务/任务调度/金融接口调度.py


import os
import cv2
import json
from tqdm import tqdm
from DataExtractor import DataExtractor
from OpticalFlow import OpticalFlow
from utils import read_file, write_data

def handle_windows_path(path):
    drive = path.split(':')[0]
    path = path.replace('\\', '/')
    return path.replace(f'{drive}:/', f'/mnt/{drive.lower()}/')

config = read_file('config.json')
config['data_dir'] = handle_windows_path(config['data_dir'])
config['facs_dir']['SAMM'] = handle_windows_path(config['facs_dir']['SAMM'])
config['facs_dir']['MMEW'] = handle_windows_path(config['facs_dir']['MMEW'])
config['facs_dir']['CASME_II'] = handle_windows_path(config['facs_dir']['CASME_II'])
config['out_dir'] = handle_windows_path(config['out_dir'])
config['raw_file'] = f"{config['out_dir']}/{config['raw_file']}"
config['dataset'] = f"{config['out_dir']}/{config['dataset']}"
config['face_bounding_box'] = f"{config['out_dir']}/{config['face_bounding_box']}"

extractor = DataExtractor(config['emotions'],
                        config['data_dir'],
                        config['facs_dir'])
OpF = OpticalFlow(config['emotions'], config['db_OF'])

def get_onset_apex(emotions, dbs):
    print('\nExtracting onset and apex frames...')
    data = {}

    for emotion in tqdm(emotions):
        if not emotion in data.keys():
            data[emotion] = {
                'onset': [],
                'apex': []
            }

        for db in dbs:
            data[emotion] = extractor.get_data(db, emotion, data[emotion].copy())

    with open(f'{config["raw_file"]}', 'w') as f:
        json.dump(data, f, indent=4)

def generate_features(emotions, out_dir):
    print('\nGenerating features...')
    data = read_file(config['raw_file'])

    for expr in emotions:
        for i, (onset, apex) in enumerate(zip(data[expr]['onset'], data[expr]['apex'])):
            subject = onset[0] or apex[0]

            if 'SAMM' in onset[1] or 'SAMM' in apex[1]:
                db = 'SAMM'
            elif 'MMEW' in onset[1] or 'MMEW' in apex[1]:
                db = 'MMEW'
            elif 'CASME_II' in onset[1] or 'CASME_II' in apex[1]:
                db = 'CASME_II'

            print(f"{expr} - ({i+1}/{len(data[expr]['onset'])})", end=' | ')
            os.makedirs(f"{out_dir}/{db}", exist_ok=True)
            OpF.generate_optical_flow(i+1, f"{out_dir}/{db}", db, expr, subject, onset[1], apex[1])

    dataset = OpF.get_dataset()
    face_bounding_box = OpF.get_face_bounding_box()

    write_data(dataset, config['dataset'])
    write_data(face_bounding_box, config['face_bounding_box'])

def crop_resize(dim):
    print('\nCropping and resizing images...')
    dataset = read_file(config['dataset'])
    face_bounding_box = read_file(config['face_bounding_box'])

    for (OF, OS, H, V) in tqdm(dataset['X']):
        if 'SAMM' in OF or 'CASME_II' in OF:
            crop = True

            img = cv2.imread(OF)
            x, y, w, h = face_bounding_box[OF]
            OpF.crop_resize(OF, img, int(x), int(y), int(w), int(h), dim, crop)
            img = cv2.imread(OS)
            x, y, w, h = face_bounding_box[OS]
            OpF.crop_resize(OS, img, int(x), int(y), int(w), int(h), dim, crop)
            img = cv2.imread(H)
            x, y, w, h = face_bounding_box[H]
            OpF.crop_resize(H, img, int(x), int(y), int(w), int(h), dim, crop)
            img = cv2.imread(V)
            x, y, w, h = face_bounding_box[V]
            OpF.crop_resize(V, img, int(x), int(y), int(w), int(h), dim, crop)
        elif 'MMEW' in OF:
            crop = False

            img = cv2.imread(OF)
            OpF.crop_resize(OF, img, None, None, None, None, dim, crop)
            img = cv2.imread(OS)
            OpF.crop_resize(OS, img, None, None, None, None, dim, crop)
            img = cv2.imread(H)
            OpF.crop_resize(H, img, None, None, None, None, dim, crop)
            img = cv2.imread(V)
            OpF.crop_resize(V, img, None, None, None, None, dim, crop)

def main():
    get_onset_apex(config['emotions'], config['dbs'])
    generate_features(config['emotions'], config['out_dir'])
    crop_resize(config['img_dim'])

if __name__ == '__main__':
    main()


import os
os.environ['GDAL_SKIP'] = 'DODS'
import logging
import isce
root_logger = logging.getLogger()
root_logger.setLevel('WARNING')
from carst import SingleRaster, ConfParams
from carst.libft import ampcor_task, writeout_ampcor_task
from carst.libxyz import AmpcoroffFile

import pandas as pd
import ipyleaflet as ilfl
import ipywidgets as iwg
from geostacks import SpatialIndexLS8, SpatialIndexITSLIVE
import xarray as xr
import rasterio
import fsspec
from datetime import datetime

import subprocess

# CARST monkey patch (to temporarily resolve .tiff vs .tif issue)

def Unify(self, params):
    """ Use gdalwarp to clip, reproject, and resample a DEM using a given set of params (which can 'unify' all DEMs). """

    print('Calling Gdalwarp...')
    fpath_warped = self.fpath[:-5] + '_warped' + self.fpath[-5:]
    if 'output_dir' in params:
        newpath = '/'.join([params['output_dir'], fpath_warped.split('/')[-1]])
    else:
        # for pixel tracking (temporarily)
        newfolder = 'test_folder'
        if not os.path.exists(newfolder):
            os.makedirs(newfolder)
        newpath = '/'.join([newfolder, fpath_warped.split('/')[-1]])
        newpath = newpath.replace(newpath.split('.')[-1], 'img')
    gdalwarp_cmd = 'gdalwarp -t_srs ' + params['t_srs'] + ' -tr ' + params['tr'] + ' -te ' + params['te']
    if 'of' in params:
        gdalwarp_cmd += ' -of ' + params['of']
    if 'ot' in params:
        gdalwarp_cmd += ' -ot ' + params['ot']
    gdalwarp_cmd += ' -overwrite ' + self.fpath + ' ' + newpath
    print(gdalwarp_cmd)
    retcode = subprocess.call(gdalwarp_cmd, shell=True)
    if retcode != 0:
        print('Gdalwarp failed. Please check if all the input parameters are properly set.')
        sys.exit(retcode)
    self.fpath = newpath

SingleRaster.Unify = Unify

# Adapted from Alice's AGU talk

class eztrack_ui():

    def __init__(self, zoom=4, query_pt=[-50., 69.], spatial_index=None):

        self.zoom = zoom
        self.query_pt = query_pt
        self.mainmap = None
        self.marker = None
        self.idxs = None
        self.ui_title = None
        self.prlist = []
        self.menuleft = None     # path / row menu
        self.pr_selection = None
        self.kernelselection = None
        self.datesearch_btn = None
        self.scenelist = []
        self.menuright = None    # scene menu
        self.bandselection = None
        self.runft_btn = None
        self.map_polygon = None
        self.spatial_index = spatial_index
        self.output = iwg.Output()   # print message output
        self.results = None
        self.ft_params = None     # feature tracking parameters
        self.sld1 = None         # param slider #1
        self.sld2 = None         # param slider #2
        self.sld3 = None         # param slider #3

    def init_panelleft(self):
        self.ui_title = iwg.HTML("
Drag the marker to your region of interest
")
        self.idxs = self.spatial_index.query_pathrow(self.query_pt)
        self.prlist = [('{:03d}/{:03d}'.format(self.spatial_index.footprint.loc[i, 'path'], self.spatial_index.footprint.loc[i, 'row']) ,i) for i in self.idxs]
        self.menuleft = iwg.Select(options=self.prlist, description='Path/Row:', rows=15)
        self.kernelselection = iwg.RadioButtons(options=['ITS_LIVE (online ready)', 'CARST'], value='ITS_LIVE (online ready)', description='Data / Kernel:')
        self.datesearch_btn = iwg.Button(description='Search for dates')

    def init_panelright(self):
        self.menuright = iwg.SelectMultiple(options=self.scenelist, description='Data entries:', rows=20)
        self.bandselection = iwg.RadioButtons(options=['B4', 'B8'], value='B8', description='Band (LS8):')
        self.runft_btn = iwg.Button(description='Get data / Start feature tracking')

    def init_map(self):
        self.mainmap = ilfl.Map(basemap=ilfl.basemaps.Gaode.Satellite, center=[self.query_pt[-1], self.query_pt[0]], zoom=self.zoom)
        self.marker = ilfl.Marker(location=[self.query_pt[-1], self.query_pt[0]], draggable=True)
        self.mainmap.add_layer(self.marker)
        self.pr_selection = self.idxs[0]
        self.scene_list = pd.DataFrame(columns=('prefix', 'time', 'tier'))
        self.map_polygon = ilfl.WKTLayer(wkt_string=self.spatial_index.footprint.loc[self.pr_selection].geometry.wkt)
        self.mainmap.add_layer(self.map_polygon)

    def gen_ui(self, spatial_index=None, ft_params=None):
        if self.spatial_index is None:
            self.spatial_index = spatial_index

        if ft_params is None:
            self.init_ft_params()
        else:
            self.ft_params = ft_params

        self.init_panelleft()
        self.init_panelright()
        self.init_map()

        self.marker.observe(self._on_location_changed, 'location')
        self.menuleft.observe(self._on_menuleft_selection_changed, names='value')
        self.datesearch_btn.on_click(self._on_searchbutton_clicked)
        self.runft_btn.on_click(self._on_ftbutton_clicked)

        leftside = iwg.VBox([self.ui_title, self.menuleft, self.kernelselection, self.datesearch_btn])
        leftside.layout.align_items = 'center'
        rightside = iwg.VBox([self.menuright, self.bandselection, self.runft_btn])
        rightside.layout.align_items = 'center'
        return iwg.AppLayout(left_sidebar=leftside, center=self.mainmap, right_sidebar=rightside)

    # ==== leftmenu update when map marker loc changes

    def _on_location_changed(self, event):
        # global query_pt, idx
        self.query_pt = [self.marker.location[-1], self.marker.location[0]]
        self.idxs = self.spatial_index.query_pathrow(self.query_pt)
        self.prlist = [('{:03d}/{:03d}'.format(self.spatial_index.footprint.loc[i, 'path'], self.spatial_index.footprint.loc[i, 'row']) ,i) for i in self.idxs]
        self.menuleft.options = self.prlist

    # ==== map polygon update when leftmenu selection changes

    def _on_menuleft_selection_changed(self, change):
        # global pr_selection
        self.pr_selection = change['new']
        self.map_polygon.wkt_string=self.spatial_index.footprint.loc[self.pr_selection].geometry.wkt

    # ==== search button click callback

    def _on_searchbutton_clicked(self, event):
        # global pr_scene_list
        with self.output:
            if self.kernelselection.value == 'CARST':
                s3_prefix, self.scenelist = self.spatial_index.search_s3(self.pr_selection)
                # print(s3_prefix)
                self.menuright.options = [(record['time'], idx) for idx, record in self.scenelist.loc[self.scenelist['tier'] == 'T1'].iterrows()]
            elif self.kernelselection.value == 'ITS_LIVE (online ready)':
                # query_pt = [mker.location[-1], mker.location[0]]
                polygon_coords = SpatialIndexITSLIVE.get_minimal_bbox(self.query_pt)
                params = {'polygon': polygon_coords, 'percent_valid_pixels': 1, 'start': '2015-01-01', 'end': '2020-01-01'}
                urls = SpatialIndexITSLIVE.get_granule_urls(params)
                self.scenelist = SpatialIndexITSLIVE.parse_urls(urls)
                pr_dict_key = '{:03d}/{:03d}'.format(self.spatial_index.footprint.loc[self.pr_selection, 'path'], self.spatial_index.footprint.loc[self.pr_selection, 'row'])
                self.menuright.options = [(i['entrystr'], i['url']) for i in self.scenelist[pr_dict_key]]

    # ==== feature tracking button click callback

    # file1_url  = None
    # file1_date = None
    # file2_url  = None
    # file2_date = None

    def _on_ftbutton_clicked(self, ft):
        # global file1_url, file1_date, file2_url, file2_date
        with self.output:
            if self.kernelselection.value == 'CARST':
                selected_list = self.scenelist.loc[list(self.menuright.value)]
                selected_list_prefix = selected_list['prefix'].tolist()
                selected_list_time = selected_list['time'].tolist()
                file1_url = 'https://landsat-pds.s3.amazonaws.com/' + selected_list_prefix[0] + os.path.basename(selected_list_prefix[0][:-1]) + '_' + self.bandselection.value + '.TIF'
                file2_url = 'https://landsat-pds.s3.amazonaws.com/' + selected_list_prefix[1] + os.path.basename(selected_list_prefix[1][:-1]) + '_' + self.bandselection.value + '.TIF'
                file1_date = selected_list_time[0].strftime('%Y-%m-%d')
                file2_date = selected_list_time[1].strftime('%Y-%m-%d')
                print(file1_url, file1_date, file2_url, file2_date)
                carst_featuretrack(file1_url, file1_date, file2_url, file2_date, params=self.ft_params)
                self.results = rasterio.open(selected_list_time[0].strftime('%Y%m%d') + '-' + selected_list_time[1].strftime('%Y%m%d') + '_velo-raw_mag.tif')
            elif self.kernelselection.value == 'ITS_LIVE (online ready)':
                print(self.menuright.value)
                with fsspec.open(self.menuright.value[0]) as fobj:
                    self.results = xr.open_dataset(fobj)

    # ==== Initialize feature tracking parameters

    def init_ft_params(self):
        self.ft_params = FTParams()

    def show_ft_params(self):
        self.sld1 = iwg.IntSlider(value=self.ft_params.pxsettings['refwindow_x'], step=16, min=16, max=256, description='reference window size')
        self.sld2 = iwg.IntSlider(value=self.ft_params.pxsettings['searchwindow_x'], step=1, min=16, max=128, description='search window size')
        self.sld3 = iwg.IntSlider(value=self.ft_params.pxsettings['skip_across'], step=1, min=1, max=256, description='skip size')
        self.sld1.observe(self._on_param_change_sld1, names='value')
        self.sld2.observe(self._on_param_change_sld2, names='value')
        self.sld3.observe(self._on_param_change_sld3, names='value')
        return iwg.VBox([self.sld1, self.sld2, self.sld3])

    def _on_param_change_sld1(self, change):
        self.ft_params.pxsettings['refwindow_x'] = self.sld1.value
        self.ft_params.pxsettings['refwindow_y'] = self.sld1.value

    def _on_param_change_sld2(self, change):
        self.ft_params.pxsettings['searchwindow_x'] = self.sld2.value
        self.ft_params.pxsettings['searchwindow_y'] = self.sld2.value

    def _on_param_change_sld3(self, change):
        self.ft_params.pxsettings['skip_across'] = self.sld3.value
        self.ft_params.pxsettings['skip_down'] = self.sld3.value

class FTParams:

    def __init__(self):
        self.imagepair     = {'image1': None,
                              'image1_date': None,
                              'image2': None,
                              'image2_date': None}
        self.pxsettings    = {'refwindow_x': 64,
                              'refwindow_y': 64,
                              'searchwindow_x': 20,
                              'searchwindow_y': 20,
                              'skip_across': 64,
                              'skip_down': 64,
                              'oversampling': 16,
                              'threads': 8,
                              'gaussian_hp': False,
                              'gaussian_hp_sigma': 3.0}
        self.outputcontrol = {'datepair_prefix': True,
                              'output_folder': '.'}
        self.rawoutput     = {'if_generate_ampofftxt': False,
                              'if_generate_xyztext': False,
                              'label_ampcor': 'ampoff',
                              'label_geotiff': 'velo-raw'}

    def VerifyParams(self):

        """
        Verify params and modify them to proper types.
        """

        if hasattr(self, 'pxsettings'):
            if 'size_across' not in self.pxsettings:
                self.pxsettings['size_across'] = None
            if 'size_down' not in self.pxsettings:
                self.pxsettings['size_down'] = None

        if hasattr(self, 'outputcontrol'):
            if 'datepair_prefix' in self.outputcontrol:
                if self.outputcontrol['datepair_prefix'] in ['false', 'f', 'no', 'n', '0']:
                    self.outputcontrol['if_generate_xyztext'] = False
                else:
                    self.outputcontrol['datepair_prefix'] = bool(int(self.outputcontrol['datepair_prefix']))
                    atime = datetime.strptime(self.imagepair['image1_date'], '%Y-%m-%d')
                    btime = datetime.strptime(self.imagepair['image2_date'], '%Y-%m-%d')
                    self.outputcontrol['label_datepair'] = atime.strftime('%Y%m%d') + '-' + btime.strftime('%Y%m%d' + '_')
        if hasattr(self, 'rawoutput'):
            if 'label_ampcor' in self.rawoutput:
                if self.outputcontrol['datepair_prefix'] not in ['false', 'f', 'no', 'n', '0']:
                    self.rawoutput['label_ampcor'] = os.path.join(self.outputcontrol['output_folder'], self.outputcontrol['label_datepair'] + self.rawoutput['label_ampcor'])
                else:
                    self.rawoutput['label_ampcor'] = os.path.join(self.outputcontrol['output_folder'], self.rawoutput['label_ampcor'])
            if 'label_geotiff' in self.rawoutput:
                if self.outputcontrol['datepair_prefix'] not in ['false', 'f', 'no', 'n', '0']:
                    self.rawoutput['label_geotiff'] = os.path.join(self.outputcontrol['output_folder'], self.outputcontrol['label_datepair'] + self.rawoutput['label_geotiff'])
                else:
                    self.rawoutput['label_geotiff'] = os.path.join(self.outputcontrol['output_folder'], self.rawoutput['label_geotiff'])

# CARST feature tracking workflow

def carst_featuretrack(file1_url, file1_date, file2_url, file2_date, params=None, inipath='param.ini'):
    if params is not None:
        ini = params
    else:
        ini = ConfParams(inipath)
        ini.ReadParam()
    ini.imagepair['image1'] = file1_url
    ini.imagepair['image2'] = file2_url
    ini.imagepair['image1_date'] = file1_date
    ini.imagepair['image2_date'] = file2_date
    ini.VerifyParams()
    a = SingleRaster(file1_url, date=file1_date)
    b = SingleRaster(file2_url, date=file2_date)
    # ========== Unifying LS8 image extent ==========
    te_a = a.GetExtent()
    tr_a = (a.GetXRes(), -a.GetYRes())
    t_srs_a = a.GetProj4()
    params_unify = {'output_dir': '.', 't_srs': '"' + t_srs_a + '"', 'tr': '{} {}'.format(*tr_a), 'te': '{} {} {} {}'.format(te_a[0], te_a[3], te_a[2], te_a[1])}
    b.Unify(params_unify)
    # ===============================================
    if ini.pxsettings['gaussian_hp']:
        a.GaussianHighPass(sigma=ini.pxsettings['gaussian_hp_sigma'])
        b.GaussianHighPass(sigma=ini.pxsettings['gaussian_hp_sigma'])
    a.AmpcorPrep()
    b.AmpcorPrep()
    task = ampcor_task([a, b], ini)
    writeout_ampcor_task(task, ini)
    ampoff = AmpcoroffFile(ini.rawoutput['label_ampcor'] + '.p')
    ampoff.Load()
    ampoff.SetIni(ini)
    ampoff.FillwithNAN()   # fill holes with nan
    ampoff.Ampcoroff2Velo()
    ampoff.Velo2XYV(generate_xyztext=ini.rawoutput['if_generate_xyztext'])
    ampoff.XYV2Raster()


import requests
from pyquery import PyQuery as pyq
import os

if not os.path.exists('papers'):
    os.mkdir('papers')

url_base = "http://openaccess.thecvf.com/%s"

src = pyq(url=url_base % '/CVPR2018.py')('#content')('dl').eq(0).children()

for i in range(0, src.length, 3):
    paper_name = src.eq(i)('a').html()
    download_url = url_base % src.eq(i+2)('a').eq(0).attr('href')
    print('Downloading paper #%d: %s' % (i/3+1, src.eq(i)('a').html()))

    r = requests.get(download_url)
    with open("papers/%s.pdf" % paper_name.replace('/', '\\'), 'wb') as f:
        f.write(r.content)


from dotenv import load_dotenv
import datetime
import os

class Car():
    load_dotenv()

    def __init__(self):    #constractor
        self.feul = int(os.getenv('feul'))
        self.feulco =  float(os.getenv('feulco'))
        self.money =   int(os.getenv('money'))
        self.gear =    int(os.getenv('gear'))
        self.speed =   int(os.getenv('speed'))
        self.status = int(os.getenv('status'))

    def start(self):
        '''
        :name : artium
        : date : 23.1
        :desc : the func start the car and do ->  gear up from 0 to 1
        :return : car status 1/0
        '''
        if  self.status ==0 :
            if self.feul > 0:
                self.status = 1
                self.gear  = 0
                return self.status
        else:
            return 'car alredy driven  '

    def close(self):
        '''
        :name : artium brovarnik
        date : 23.1.23
        desc : the func close the car -> gear,speed,status = 0
        :return: car status
        '''
        if self.status !=0:
            self.gear = 0
            self.speed = 0
            self.status = 0
            return self.status
        else :
            return 'the car alredy in turned off'

    def get_speed(self,gear):
        '''
        :name : artium brovarnik
        :date : 24.1.23
        :param gear:
        :return: speed of the car
        '''
        x = os.getenv(f'max_gear')
        if (gear>int(os.getenv('max_gear')) or gear < 0):
            raise ValueError(x.format(os.getenv('error_value_range')))

        else:
            self.speed = gear * int(os.getenv('mph'))
            return self.speed

    def how_much_liter_to_destination(self,destination):
        '''
        : desc : the func culc how much liters feul we need by distans
        :param destination
        :return: liters by km
        '''
        return destination / int(os.getenv('feulco'))

    def drive(self,km):
        '''
        :desc : the func get km and culc how much fuel you have after driving
        :param km that you want drive:
        :return: feul amount after drive
        '''
        if  self.status ==0:
            self.start()
        if self.feul > self.how_much_liter_to_destination(km):
            self.feul= self.feul- self.how_much_liter_to_destination(km)
        elif self.feul < self.how_much_liter_to_destination(km):
            self.feul_for_drive(km)
        else:
            raise OverflowError( os.getenv('er_feu'))

    def feul_for_drive(self,km):
        '''
        :name artium brovarnik
        :data 23.1.23
        desc: calc if you have feul to this distance , if not buy and update your money
        :param km
        :return: money after buying
        '''
        if (self.feul< self.how_much_liter_to_destination(km)):

            liter_ineed_buy = self.how_much_liter_to_destination(km) - self.feul
            if (self.money>= liter_ineed_buy*int(os.getenv('liter_price'))):
                self.buy_benzin(liter_ineed_buy)
                return self.money
            else:
                raise(os.getenv('error_value_money'))
        else:
            self.feul = self.feul- self.how_much_liter_to_destination(km)
            return self.money

    def buy_benzin(self,liter):
        '''
        :name: artium brovarnik
        :date : 23.1.23
        :desc: calc how much money i have after buying fuel , check that you have this sum of money
        :param liter
        :return: money after buying
        '''
        money_i_need = liter*int(os.getenv('liter_price'))
        if self.money >= money_i_need:
            self.money = self.money - money_i_need
            return self.money
        else:
            raise ValueError(os.getenv('error_value_money'))

    def get_gear_by_speed(self,sp):
        '''
        desc: func return your gear by speed
        :param sp:
        :return: your gear level
        '''
        if sp<=0:
            raise ValueError(os.getenv('error_value_speed'))
        elif sp > 0 and sp<30:
            return 1
        elif sp>30 and sp<=60:
            return 2
        elif sp>60 and sp<=90:
            return 3
        elif sp>90 and sp<=120:
            return 4
        elif sp>120 and sp<=150:
            return 5
        else: os.getenv('max_gear')

    def gear_up(self):
        '''
        : name : artium brovarnik
        : date : 23.1.23
        decs : the func add 1 to gear , check that cannot up from level 6
        :return: gear level
        '''
        if self.gear ==int(os.getenv('max_gear')):
            x = os.getenv(f'error_value_maxGear')
            raise OverflowError(x.format(int(os.getenv('max_gear'))))
        else:
            if (self.status==1):
                self.gear = self.gear+1
                self.speed = self.speed +int(os.getenv('mph'))
                return self.gear
            else: raise ValueError(os.getenv('error_value_startcar'))

    def write_to_log_file(self,str):
        """
        : name : artium brovarnik
        :date : 23.1.23
        :desc : write all test result Pass / Fail  in log file
        :param str :string that describe the error
        :return: new line in log file
        """
        f= open('logfsfile.txt', 'a')
        f.write(f"{str}, {datetime.datetime.now()}, \n")
        f.close()


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
from tkinter import *
from tkinter import ttk

root = Tk()
root.title(u"Software Title")
root.geometry("400x300")

#
# ボタンが押されるとここが呼び出される
#
def DeleteEntryValue(event):
    #エントリーの中身を削除
    EditBox.delete(0, tk.END)

#エントリー
EditBox = ttk.Entry(width=10)
EditBox.insert(tk.END,"挿入する文字列")
EditBox.pack()

#ボタン
Button = ttk.Button(text=u'ボタンです', width=50)
Button.bind("<Button-1>",DeleteEntryValue)
#左クリック（<Button-1>）されると，DeleteEntryValue関数を呼び出すようにバインド
Button.pack()

root.mainloop()


import re
import time
import logging
import numpy as np
import pandas as pd

from selenium import webdriver
from selenium.webdriver.common.by import By
from web_scraper.version import __appName__
from selenium.webdriver.common.action_chains import ActionChains

class WebCollector():
    def __init__(self, web_endpoint: str = '', output_csv_file: str = '', show_graphs: bool = False, driver_path: str = None):
        self.logger = self.setup_logggin()
        self.web_endpoint = web_endpoint
        self.show_graphs = show_graphs
        self.driver_path = driver_path
        self.output_csv_file = output_csv_file
        self.output_fig_path = "../doc/"

        self._main_window_handler = None
        self._secondary_window_handler = None
        self._tertiary_window_handler = None
        self.regex = re.compile('\d{4}S\d')

    # Logger initializier
    def setup_logggin(self) -> logging.Logger:
        log_format = "%(levelname)s %(asctime)s - %(message)s"
        logging.basicConfig(format=log_format, level=logging.INFO)
        logger = logging.getLogger(__appName__)
        return logger

    def load_all_available_dates(self, driver):
        # Find the time button
        time_elem = driver.find_element(by=By.ID, value="TIME")
        time_button = time_elem.find_element(By.TAG_NAME, value='button')

        # We save the main browser window
        self._main_window_handler = driver.current_window_handle
        # Focus on the elements we want to interact
        action = ActionChains(driver)
        action.move_to_element(time_button).perform()
        time_button.click()
        time.sleep(5)

        # Get the secondary window (pop up window)
        while not self._secondary_window_handler:
            for handle in driver.window_handles:
                if handle != self._main_window_handler:
                    self._secondary_window_handler = handle
                    break

        # We change the main window for the pop-up one
        driver.switch_to.window(self._secondary_window_handler)
        # We look for the button to enable all dates
        driver.find_element(by=By.ID, value="checkUncheckAllCheckboxTable").click()
        time.sleep(3)
        # And update the main table which is in the main window
        driver.find_element(by=By.ID, value="updateExtractionButton").click()
        time.sleep(3)
        # Change again to the main window
        driver.switch_to.window(self._main_window_handler)
        time.sleep(3)

    def create_data_table_as_dataframe(self, driver):
        # First we select the preview option so we can iterate over the whole dataset and not only the visible one
        nav_elem = driver.find_element(by=By.ID, value="nav")
        preview_button = nav_elem.find_element(By.CLASS_NAME, value="preview")
        preview_button.click()
        time.sleep(5)

        # Get the third window (pop-up)
        while not self._tertiary_window_handler:
            for handle in driver.window_handles:
                if handle != self._main_window_handler and handle != self._secondary_window_handler:
                    self._tertiary_window_handler = handle
                    break

        driver.switch_to.window(self._tertiary_window_handler)

        table_elem = driver.find_elements(by=By.TAG_NAME, value="table")
        self.logger.info(f"Number of sub-tables {len(table_elem)}")
        self.logger.info("Collecting values from the web site")
        set_columns = []
        table_df = pd.DataFrame()
        # loop over results
        for sub_table in table_elem:
            data = []
            # Remove the "TIME" and "GEO" header and split the table values
            table_values = sub_table.text.split("\n", 1)[1].split("\n")

            # Counting the number of year columns and adding an extra one for the countries
            num_year_cols = len(list(filter(self.regex.match, table_values))) + 1
            # Get the column names
            set_columns = table_values[0:num_year_cols]

            # Iterate over the cost values and jump every num_year_cols columns
            for i in range(num_year_cols, len(table_values), num_year_cols):
                tmp_value = []
                for j in range(0, num_year_cols):
                    tmp_value.append(table_values[i + j])
                data.append(tmp_value)
            # Create a temporal dataframe which is the sub table data
            temporal_df = pd.DataFrame(data=data, columns=set_columns)
            # Merge with the final dataframe
            if table_df.empty:
                table_df = temporal_df
            else:
                table_df = pd.merge(table_df, temporal_df, how="left", on=["GEO"])

        return table_df

    def generate_graph_spain(self, input_csv):
        input_df = pd.read_csv(input_csv, index_col=0)
        year_cols = [x for x in input_df.columns if "GEO" not in x]
        input_df[year_cols] = input_df[year_cols].replace(np.nan, np.float64(0))

        spain_values = input_df.loc[input_df["GEO"] == "Spain"]
        input_df.loc[input_df.index.max() + 1] = input_df[year_cols].mean().replace(np.nan, "European-Mean")
        input_df.iloc[input_df.index.max(), input_df.columns.get_loc('GEO')] = "European-Mean"

        mean_values = input_df.loc[input_df["GEO"] == "European-Mean"]

        spain_values = spain_values.append(mean_values)
        plot_values = spain_values[year_cols].T
        plot_values.columns = ["Spain", "European-Mean"]

        # mean_line = plot_values["European-Mean"].plot(linestyle='-', marker='o')
        plot = plot_values.plot(kind="bar", title="Comparativa de la evolución de precios de la energia Europa vs. España")
        plot.set_xlabel("Año")
        plot.set_ylabel("Precio kWh")
        fig = plot.get_figure()
        fig.savefig(self.output_fig_path + "figure_spain.png")

    def generate_graph_top_expensive(self, input_csv):
        input_df = pd.read_csv(input_csv, index_col=0)
        year_cols = [x for x in input_df.columns if "GEO" not in x]
        input_df[year_cols] = input_df[year_cols].replace(np.nan, np.float64(0))

        countries_to_filter = ["Ireland", "Liechtenstein", "Belgium",
                               "Germany (until 1990 former territory of the FRG)", "Luxembourg", "Italy"]
        input_df = input_df[input_df["GEO"].isin(countries_to_filter)]

        new_col_names = input_df["GEO"]

        plot_values = input_df[year_cols].T
        plot_values.columns = new_col_names

        plot = plot_values.plot(kind="bar", title="Comparativa de la evolución de precios de la energia en Europa para los paises más caros")
        plot.set_xlabel("Año")
        plot.set_ylabel("Precio kWh")
        fig = plot.get_figure()
        fig.savefig(self.output_fig_path + "figure_top_expensives.png")

    def start(self):
        self.logger.info("Starting the web data collector!")
        self.logger.info(f"The website used to do the scraping is: {self.web_endpoint}")

        try:
            self.logger.info("We create the firefox web driver from the path of the executable that we want (if needed)")
            driver = webdriver.Firefox(executable_path=self.driver_path) if self.driver_path else webdriver.Firefox()
            # Maximize the size of the browser to load more JS data
            driver.maximize_window()
            # Selenium don't contain API for request headers
            self.logger.info("Querying the website")
            driver.get(self.web_endpoint)

            self.logger.info("We load all possible years for the data")
            self.load_all_available_dates(driver)

            self.logger.info("We select the preview option to iterate over the table data")
            final_df = self.create_data_table_as_dataframe(driver)

            time.sleep(5)
            self.logger.info("Closing the browser")
            driver.quit()

            self.logger.info("Clean the output dataframe")
            year_cols = [x for x in final_df.columns if "GEO" not in x]
            # Removing strings in price columns and assigning NA for nonexistent ones
            final_df[year_cols] = final_df[year_cols].astype(str).applymap(
                lambda x: re.sub(r'[^0-9^\-\.]+', '', x)).replace('', np.nan).astype('float64')

            self.logger.info(f"Saving the final dataframe in {self.output_csv_file}")
            final_df.to_csv(self.output_csv_file)

            if self.show_graphs:
                self.logger.info("Generating graphs!")
                self.generate_graph_spain(self.output_csv_file)
                self.generate_graph_top_expensive(self.output_csv_file)

        except Exception as e:
            self.logger.warning(f"Exception during the data collection - [{e.args[0]}].")


#!/usr/bin/env python3

import unittest

from pcost import Portfolio

class TestPortfolio(unittest.TestCase):
    def test_Portfolio_position(self):
        p = Portfolio(path='Data/portfolio3.dat')
        self.assertEqual(p.portfolio_value, 12597.479999999998)

        p = Portfolio(path='Data/portfolio2.dat')
        self.assertEqual(p.portfolio_value, 19908.75)

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


# -*- coding: utf-8 -*-
# This code reconstructs an in-line hologram captured with the Raspberry Pi Setup

# manage the imports
import numpy as np
import matplotlib.pyplot as plt
import os
import time
import FresnelPropagator as FP
import MultiSliceViewer as MSV

# Taken from this link: https://matplotlib.org/2.1.2/gallery/animation/image_slices_viewer.html

class IndexTracker(object):
    def __init__(self, ax, X):
        self.ax = ax
        ax.set_title('use scroll wheel to navigate images')

        self.X = X
        rows, cols, self.slices = X.shape
        self.ind = self.slices//2

        self.im = ax.imshow(self.X[:, :, self.ind])
        self.update()

    def onscroll(self, event):
        print("%s %s" % (event.button, event.step))
        if event.button == 'up':
            self.ind = (self.ind + 1) % self.slices
        else:
            self.ind = (self.ind - 1) % self.slices
        self.update()

    def update(self):
        self.im.set_data(self.X[:, :, self.ind])
        ax.set_ylabel('slice %s' % self.ind)
        self.im.axes.figure.canvas.draw()

# define some experimental parameters
my_holo_file = 'testholo.jpg' #'test_microplastic.jpg'
my_background_file = 'test2bg.jpg'
mychannel = 2 # select the color channel 0,1,2
mysize = None #1024 # region of interest around the center coordinate

# define acquisition parameters
pixelsize = 1.4e-6; #0.6e-6#.2e-6#3.000e-6;
lambda0 = 450e-9; # in nanometer #530e-9;

# start and stop z-focus measure
stepsize = 0.001;

# read the raw-hologram
myholo = 1.*plt.imread(my_holo_file);

# select only the blue channel (because we used the blue LED)
myholo = myholo[:,:,mychannel ]

# if we acquired the background, subtract it!
if(0):
    mybak = 1.*imread(my_background_file)
    mybak = mybak[:,:,mychannel ]
    myholo = myholo/(20+mybak) # add some value to avoid dividing by zero

# crop out the ROI around the center to speed up computation (RADIX 2 please!)
holosize = myholo.shape
if mysize is not None:
    myholo =  myholo[int(holosize[0]/2-mysize/2):int(holosize[0]/2+mysize/2), int(holosize[0]/2-mysize/2):int(holosize[0]/2+mysize/2)]
else:
    # make square
    mysize = np.min(holosize)
    myholo =  myholo[int(holosize[0]/2-mysize/2):int(holosize[0]/2+mysize/2), int(holosize[0]/2-mysize/2):int(holosize[0]/2+mysize/2)]

# display intermediate result
plt.imshow(myholo)
plt.colorbar()
plt.show()

# normalize hologram
myholo = myholo/np.max(myholo);

# estimate hologram's amplitude
myamplitude = np.sqrt(myholo);

j = 0;
## Backpropagate the Hologram
# Fresnel Propagation - test out a range of z-values to find the one that
# brings the bug into focus
myholo_rec = [];
for i in range(0,20):
    #     [Ef, H] = fresnelprop(hologram,lambda,i,ps);
    zpos = i*stepsize;
    Ef = FP.FresnelPropagator(myholo, pixelsize, lambda0, zpos)

    myholo_rec.append(FP.abssqr(Ef))

    print("Now processed z-slice number"+str(i)+" at z-position (mum) "+str(zpos))

# convert list to numpy array
myholo_rec = np.array(myholo_rec)
#myholo_rec = np.transpose(myholo_rec, [1,2,0]) # make sure Z-axis is the last one

import NanoImagingPack as nip
nip.image(myholo_rec)
# now create a 3D image-stack viewer
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, myholo_rec)

fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()


import kfp.components as comp
from typing import NamedTuple

def train_xgb(input_x_train_csv : comp.InputPath('CSV'),
              input_y_train_csv : comp.InputPath('CSV'),
              input_x_test_csv : comp.InputPath('CSV'),
              input_y_test_csv : comp.InputPath('CSV'),
              hyperopt_iterations : int
  )-> NamedTuple('Outputs', [('MSE', float), ('R2', float), ('hyperparams', dict)]):

    global best

    import pandas as pd
    import xgboost as xgb
    from sklearn.metrics import mean_squared_error, explained_variance_score, r2_score
    from sklearn.model_selection import cross_val_score, KFold
    from sklearn.svm import SVR
    import hyperopt
    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
    from random import seed

    X_train = pd.read_csv(input_x_train_csv)
    y_train = pd.read_csv(input_y_train_csv)
    X_test = pd.read_csv(input_x_test_csv)
    y_test = pd.read_csv(input_y_test_csv)

    seed(42)
    def model_accuracy(params):
        xgb_reg = xgb.XGBRegressor(objective = "reg:squarederror",
                                   tree_method = 'hist',
                                   eval_metric = ["rmse"],
                                   **params)
        return cross_val_score(xgb_reg, X_train, y_train).mean()

    space = {
        'max_depth' : hp.choice('max_depth', range(1, 30, 1)),
        'learning_rate' : hp.quniform('learning_rate', 0.01, 0.5, 0.01),
        'n_estimators' : hp.choice('n_estimators', range(20, 205, 5)),
        'gamma' : hp.quniform('gamma', 0, 0.50, 0.01),
        'min_child_weight' : hp.quniform('min_child_weight', 1, 10, 1),
        'subsample' : hp.quniform('subsample', 0.1, 1, 0.01),
        'colsample_bytree' : hp.quniform('colsample_bytree', 0.1, 1.0, 0.01),
        'colsample_bylevel' : hp.quniform('colsample_bylevel', 0.1, 1.0, 0.01),
        'colsample_bynode' : hp.quniform('colsample_bynode', 0.1, 1.0, 0.01),
        'max_delta_step' : hp.choice('max_delta_step', range(0, 11, 1))
    }

    best=0
    def hyperparameter_tuning(space):
        global best
        acc = model_accuracy(space)

        if acc > best:
            best = acc
            print ('new best:', best, space)

        return {'loss': 1-acc, 'status': STATUS_OK}

    trials = Trials()
    best = fmin(hyperparameter_tuning, space, algo=tpe.suggest, max_evals=hyperopt_iterations, trials=trials)

    xgb_hyperparams = space_eval(space, best)

    modfit_xgb = xgb.XGBRegressor(objective = "reg:squarederror",
                                      tree_method = 'hist',
                                      eval_metric = ["rmse"],
                                      **xgb_hyperparams)

    modfit_xgb.fit(X_train, y_train)

    xgb_mse = mean_squared_error(y_test.to_numpy(), modfit_xgb.predict(X_test))
    xgb_accuracies = cross_val_score(estimator = modfit_xgb, X = X_test, y = y_test,
                                    cv = 10)
    xgb_r2 = xgb_accuracies.mean()

    return(xgb_mse, xgb_r2, xgb_hyperparams)

if __name__ == '__main__':
    from kfp.components import create_component_from_func
    base_img = "public.ecr.aws/f6t4n1w1/poc_kf_pipeline:latest"

    train_xgb_op = create_component_from_func(
        train_xgb,
        output_component_file='train_xgb_op.yaml',
        base_image=base_img,
        annotations={
            "author": "Antoine Villatte"}
    )


instruction = '''This program provide two ways to search for food/restaurant in the United state
choice 1:
'''

while True:
    while True:
        query_name = input("please input the food/restaurant")
        query_name = query_name.lower().strip(" ")
        if query_name == 'exit':
            print("Goodbye! Thank you!")
            exit()
    #time.sleep(0.5)
        query_location = input("please input the location")
        query_location = query_location.lower().strip(" ")
        if query_location == 'exit':
            print("Goodbye! Thank you!")
            exit()
        elif query_location == "":
            print("location is required")
            break
        query_string = query_name + query_location
        #print(query_string)
        result = path_decision(query_name, query_location)
        yt_search(query_string)
        show_related_loc(query_name,query_location)
        go_direction(query_name, query_location,query_string)
    print("midpoint")
    while True:

        break
        if usr.isnumeric():
            print("AAAAAA")
            break
        else:
            print("invalid for section2")
    while True:
        usr = input("enter a number1")
        if usr.isnumeric():
            print("AAAAAA")
            break
        else:
            print("invalid for section3")
    print("end")
    break
    #time.sleep(0.5)

#path_decision(query_name, query_location) # search the yelp and update the sql and ger the list
#yt_search(query_string) # search the video on YOutube
#result = show_related_loc(query_name,query_location) # generate the linklist for open_static_map
#go_direction(query_name, query_location,query_string) # choose one of the option and direct to it
'''
r = sql_query() # output the query string for sql database
print(r)
r1 = sql_execute(r) # launch the search sql and return list in list [[] []]
print(r1)
r2 = array_output(r1) # print in the desire format
'''
#r3 = scale_convert(r1)
#draw_radarplot(r1) #draw the radar plot
#draw_barplot(r1) # draw the barplot

##==============used====================
#query_name = input("please input the food/restaurant")
#query_name = query_name.lower().strip(" ")
#time.sleep(1)
#query_location = input("please input the location")
#query_location = query_location.lower().strip(" ")
#query_string = query_name + query_location
#time.sleep(1)


from rest_framework import serializers
from django.db.models import Q, QuerySet

from certificates.models import StudentCertificate
from courses.models import UserCourse
from users.serializers import UserMinSerializer
from .models import Program, Field, UserBuyProgram, StudentProgram, UserViewProgram
from courses.serializers import CourseSerializer, CourseSearchSerializer, CourseMinSerializer, CourseProcessSerializer, \
    UserCourseSerializer

class ProgramSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    field = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
    created_by = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
    program_course = CourseSerializer(many=True, read_only=True)

    class Meta:
        model = Program
        fields = [
            'id', 'name',  'icon', 'slug', 'program_course',
            'status', 'field', 'created_date', 'discount_percentage',
            'short_description', 'full_description', 'created_date', 'created_by',
            'created_by_name', 'modified_date'
        ]
        read_only_fields = ('created_date', 'modified_date',
                            'created_by', 'created_by_name')

class ProgramDetailSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    field = serializers.StringRelatedField(read_only=True)
    program_course = CourseSearchSerializer(many=True, read_only=True)
    courses_count = serializers.IntegerField(read_only=True)
    is_my_program = serializers.SerializerMethodField()
    price = serializers.SerializerMethodField()
    bought_courses = serializers.SerializerMethodField(read_only=True)
    completed_courses = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Program
        fields = [
            'id', 'name', 'icon', 'slug', 'discount_percentage', 'price',
            'program_course', 'benefits', 'pre_requisites', 'completed_courses',
            'courses_count', 'status', 'field', 'bought_courses',
            'short_description', 'full_description', 'is_my_program'
        ]

    def get_is_my_program(self, obj):
        user = self.context.get('user')
        return obj.user_buy.filter(pk=user.id).count() > 0

    def get_price(self, obj):
        price = 0
        user = self.context.get('user')
        program_courses = obj.program_course.all()
        for course in program_courses:
            if course.check_is_bought(user) is not True:
                price += int(course.get_price())
        # if obj.discount:
        #     price -= int(obj.discount)
        if obj.discount_percentage:
            price *= (100-obj.discount_percentage)/100
        return int(price)

    def get_bought_courses(self, obj):
        user = self.context.get('user')
        if user and not user.is_anonymous:
            program_courses = obj.program_course.all()
            queryset = [course for course in program_courses if course.check_is_bought(student=user)]
            return CourseMinSerializer(instance=queryset, many=True).data
        return []

    def get_completed_courses(self, obj):
        user = self.context.get('user')
        if user and not user.is_anonymous:
            program_courses = obj.program_course.all()
            queryset = [course for course in program_courses if course.check_is_completed(student=user)]
            return CourseMinSerializer(instance=queryset, many=True).data
        return []

class ProgramSearchSerializer(serializers.ModelSerializer):

    id = serializers.IntegerField(read_only=True)
    field = serializers.StringRelatedField(read_only=True)
    courses_count = serializers.IntegerField(read_only=True)
    program_course = CourseSearchSerializer(many=True, read_only=True)
    status = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Program
        fields = [
            'id', 'name', 'icon', 'slug', 'discount_percentage', 'status',
            'courses_count', 'status', 'field', 'program_course'
        ]

    def get_status(self, obj):
        request = self.context.get('request', None)
        if request and request.user is not None and not request.user.is_anonymous:
            try:
                student_program = StudentProgram.objects.get(student=request.user, program_id=obj.id)
                return student_program.status
            except StudentProgram.DoesNotExist:
                return None
        return None

class ProgramMinSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    field = serializers.StringRelatedField(read_only=True)
    courses_count = serializers.IntegerField(read_only=True)
    program_course = CourseMinSerializer(many=True, read_only=True)
    bought_date = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Program
        fields = [
            'id', 'name', 'icon', 'slug', 'bought_date', 'discount_percentage',
            'courses_count', 'status', 'field', 'program_course'
        ]

    def get_bought_date(self, obj):
        user = self.context.get('user')
        if user is not None:
            try:
                instance = UserBuyProgram.objects.get(user_id=user.id, program_id=obj.id)
            except UserBuyProgram.DoesNotExist:
                return None
            return instance.bought_date
        return None

class ProgramProcessSerializer(serializers.ModelSerializer):

    id = serializers.IntegerField(read_only=True)
    field = serializers.StringRelatedField(read_only=True)
    courses_count = serializers.IntegerField(read_only=True)
    program_course = CourseProcessSerializer(many=True, read_only=True)
    student_program = serializers.SerializerMethodField(read_only=True)
    student_course = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Program
        fields = [
            'id', 'name', 'icon', 'slug', 'discount_percentage', 'student_program', 'student_course',
            'courses_count', 'status', 'field', 'program_course',
        ]

    def get_student_program(self, obj):
        user = self.context.get('user')
        request = self.context.get('request')
        context = {"user": user, "request": request}
        try:
            instance = StudentProgram.objects.get(student_id=user.id, program_id=obj.id)
            return StudentProgramSerializer(instance=instance, context=context).data
        except StudentProgram.DoesNotExist:
            return None

    def get_student_course(self, obj):
        user = self.context.get('user')
        queryset = list()
        program_courses = obj.program_course.all()
        for course in program_courses:
            try:
                instance = UserCourse.objects.get(Q(user_id=user) & Q(course_id=course.id))
                queryset.append(instance)
            except UserCourse.DoesNotExist:
                pass
        return UserCourseSerializer(instance=queryset, many=True).data

class ProgramDataSerializer(serializers.ModelSerializer):
    view_count = serializers.SerializerMethodField(read_only=True)
    courses_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = Program
        fields = ['id', 'created_date', 'name', 'view_count',
                  'courses_count', 'field', 'status', 'discount_percentage', 'discount_percentage']

    @staticmethod
    def get_view_count(obj):
        return UserViewProgram.objects.filter(program_id=obj.id).count()

class FieldSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    field_programs = ProgramSearchSerializer(many=True, read_only=True)
    field_courses = CourseSearchSerializer(many=True, read_only=True)
    field = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Field
        fields = [
            'id', 'name', 'icon', 'description', 'field_programs', 'field_courses', 'field', 'created_date'
        ]
        read_only_fields = ('created_date',)

class FieldMinSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    slug = serializers.SlugField(read_only=True)

    class Meta:
        model = Field
        fields = [
            'id', 'name',  'slug', 'icon', 'description'
        ]

class UserBuyProgramSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True, required=False)
    program = serializers.StringRelatedField()

    class Meta:
        model = UserBuyProgram
        fields = [
            'id', 'user', 'program', 'bought_date', 'money'
        ]

class StudentProgramSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    student = UserMinSerializer(required=False)
    program = ProgramMinSerializer(required=False)
    course_num = serializers.SerializerMethodField(read_only=True)
    # file = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = StudentProgram
        fields = [
            'id', 'file', 'student', 'program', 'status',
            'started_date', 'completed_date', 'received_certificate', 'course_num'
        ]

    @staticmethod
    def get_course_num(obj):
        return obj.program.program_course.all().count()

    # def get_file(self, obj):
    #     user = self.context.get('user')
    #     if user:
    #         try:
    #             instance = StudentCertificate.objects.get(student_id=user.id, program_id=obj.program.id)
    #             return instance.file
    #         except StudentCertificate.DoesNotExist:
    #             return None
    #     return None

class StudentProgramCertificateSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    student = UserMinSerializer(required=False)
    # program = ProgramMinSerializer(required=False)

    class Meta:
        model = StudentProgram
        fields = [
            'id', 'file', 'student', 'status', 'started_date', 'completed_date', 'received_certificate'
        ]

class ProgramCertificateSerializer(serializers.ModelSerializer):

    id = serializers.IntegerField(read_only=True)
    courses_count = serializers.IntegerField(read_only=True)
    student_program = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Program
        fields = [
            'id', 'name', 'icon', 'slug', 'student_program', 'courses_count', 'status', 'field',
        ]

    @staticmethod
    def get_student_program(obj):
        queryset = StudentProgram.objects.filter(program_id=obj.id)
        return StudentProgramCertificateSerializer(instance=queryset, many=True).data


import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.utils import save_image

from utils import InstanceNorm2d

class NoiseMappingNetwork(nn.Module):
    def __init__(self, z_dim, hidden_dim, w_dim):
        super(NoiseMappingNetwork, self).__init__()

        # Define the hidden layers using nn.ModuleList
        self.hidden_layers = nn.ModuleList([
            nn.Sequential(
                nn.Linear(hidden_dim, hidden_dim, bias=True),
                nn.LeakyReLU(0.2)
            ) for _ in range(6)
        ])

        # Define the mapping layers using nn.Sequential
        self.mapping = nn.Sequential(
            nn.Linear(z_dim, hidden_dim, bias=True),
            nn.LeakyReLU(0.2)
        )

        for i in range(6):
            self.mapping.add_module(f"hidden_{i}", self.hidden_layers[i])

        self.mapping.add_module("final_layer", nn.Linear(hidden_dim, w_dim, bias=True))

    def forward(self, noise):
        '''
        Function for completing a forward pass of MappingLayers:
        Given an initial noise tensor, returns the intermediate noise tensor.
        '''
        return self.mapping(noise)

class InjectNoise(nn.Module):
    def __init__(self, channels):
        super().__init__()
        self.weight = nn.Parameter(
            torch.randn(channels)[None, :, None, None]
        )

    def forward(self, image):
        '''
        Function for completing a forward pass of InjectNoise: Given an image,
        returns the image with random noise added.
        '''
        # Set the appropriate shape for the noise!
        noise_shape = (image.shape[0], 1, image.shape[2], image.shape[3])

        noise = torch.randn(noise_shape, device=image.device) # Creates the random noise
        return image + self.weight * noise # Applies to image after multiplying by the weight for each channel

class AdaIN(nn.Module):
    def __init__(self, channels, w_dim):
        super(AdaIN, self).__init__()

        # Normalize the input per-dimension
        self.instance_norm = InstanceNorm2d(channels)

        self.style_scale_transform = nn.Linear(w_dim, channels)
        self.style_shift_transform = nn.Linear(w_dim, channels)

    def forward(self, image, w):
        '''
        Function for completing a forward pass of AdaIN: Given an image and intermediate noise vector w,
        returns the normalized image that has been scaled and shifted by the style.
        '''
        normalized_image = self.instance_norm(image)
        style_scale = self.style_scale_transform(w)[:, :, None, None]
        style_shift = self.style_shift_transform(w)[:, :, None, None]

        transformed_image = style_scale * normalized_image + style_shift

        return transformed_image

class StyleGANGeneratorBlock(nn.Module):

    def __init__(self, in_chan, out_chan, w_dim, kernel_size=3, use_upsample=True):
        super(StyleGANGeneratorBlock, self).__init__()
        self.use_upsample = use_upsample

        if self.use_upsample:
            self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)

        self.conv1 = nn.Conv2d(in_chan, out_chan, kernel_size, stride=1, padding=1) # Padding is used to maintain the image size
        self.conv2 = nn.Conv2d(out_chan, out_chan, kernel_size, stride=1, padding=1) # Padding is used to maintain the image size

        self.inject_noise1 = InjectNoise(out_chan)
        self.adain1 = AdaIN(out_chan, w_dim)

        self.inject_noise2 = InjectNoise(out_chan)
        self.adain2 = AdaIN(out_chan, w_dim)

        self.activation = nn.LeakyReLU(0.2)

    def forward(self, x, w):
        '''
        Function for completing a forward pass of MicroStyleGANGeneratorBlock: Given an x and w,
        computes a StyleGAN generator block.
        '''
        if self.use_upsample:
            x = self.upsample(x)
        x = self.conv1(x)
        x = self.inject_noise1(x)
        x = self.activation(x)
        x = self.adain1(x, w)

        x = self.conv2(x)
        x = self.inject_noise2(x)
        x = self.activation(x)
        x = self.adain2(x, w)

        return x

class StyleGANGenerator(nn.Module):
    '''
    StyleGAN Generator Class
    Values:
        z_dim: the dimension of the noise vector, a scalar
        map_hidden_dim: the mapping inner dimension, a scalar
        w_dim: the dimension of the intermediate noise vector, a scalar
        channels: a list of channel sizes for each block in the generator
    '''
    def __init__(self, z_dim, map_hidden_dim, w_dim, channels=[512, 256, 128, 64, 32, 16, 8]):
        super(StyleGANGenerator, self).__init__()

        self.num_layers = len(channels) - 1
        self.map = NoiseMappingNetwork(z_dim, map_hidden_dim, w_dim)

        self.initial_block = nn.Parameter(torch.randn(1, channels[0], 4, 4))

        self.blocks = nn.ModuleList([])
        for i in range(self.num_layers):
            in_channels = channels[i]
            out_channels = channels[i + 1]
            block = StyleGANGeneratorBlock(in_channels, out_channels, w_dim, use_upsample=True)  # Fix typo
            self.blocks.append(block)

        self.output_conv_1 = nn.Conv2d(channels[-2], 3, kernel_size=1, stride=1)
        self.output_conv_2 = nn.Conv2d(channels[-1], 3, kernel_size=1, stride=1)
        self.output_tanh = nn.Tanh()

    def upsample_to_match_size(self, smaller_image, bigger_image):
        '''
        Function for upsampling an image to the size of another: Given a two images (smaller and bigger),
        upsamples the first to have the same dimensions as the second.
        Parameters:
            smaller_image: the smaller image to upsample
            bigger_image: the bigger image whose dimensions will be upsampled to
        '''
        return F.interpolate(smaller_image, size=bigger_image.shape[-2:], mode='bilinear')

    def forward(self, noise, return_intermediate=False, alpha=0.2):
        '''
        Function for completing a forward pass of StyleGANGenerator: Given noise,
        computes a StyleGAN iteration.
        '''
        x = self.initial_block
        w = self.map(noise)
        intermediates = []

        for i in range(self.num_layers):
            x = self.blocks[i](x, w)
            if i == self.num_layers - 2:
                x_small = x
                x_small_image = self.output_tanh(self.output_conv_1(x_small))
                intermediates.append(x_small_image)
            if i == self.num_layers - 1:
                x_big = x
                x_big_image = self.output_tanh(self.output_conv_2(x_big))
                intermediates.append(x_big_image)

        x_small_upsample = self.upsample_to_match_size(intermediates[0], intermediates[1])

        interpolation = alpha * x_big_image + (1 - alpha) * x_small_upsample

        if return_intermediate:
            return interpolation, x_small_upsample, intermediates
        else:
            return interpolation

if __name__ == "__main__":
    # Set the required parameters
    z_dim = 512
    map_hidden_dim = 1024
    w_dim = 512
    channels = [512, 256, 128, 64, 32, 16, 8]

    # Create an instance of the generator
    generator = StyleGANGenerator(z_dim, map_hidden_dim, w_dim, channels)

    # Generate a random noise vector
    batch_size = 4
    noise = torch.randn(batch_size, z_dim)

    # Generate images using the generator blocks
    images = generator(noise)
    print(images.size())

    # Save the generated images
    save_image(images, 'generated_images.png', nrow=2, normalize=True)


from __future__ import print_function

def main():
    #assume all inputs are integers
    val = int(input("Enter a single digit integer (1-9): "))
    while ((val < 1) or (val > 9)):
        val = int(input("Enter a single digit integer (1-9): "))
    n1 = val
    #print(n1)
    n2 = n1 * 10 + n1
    #print(n2)
    n3 = n2 * 10 + n1
    #print(n3)
    n4 = n3 * 10 + n1
    #print(n4)
    n5 = n4 * 10 + n1

    sn2 = str(n1) + str(n1)
    sn3 = sn2 + str(n1)
    sn4 = sn3 + str(n1)
    sn5 = sn4 + str(n1)

    print("The sum of", n1, "+", sn2, "+", sn3, "+", sn4, "+", sn5, "is", n1 + n2 + n3 + n4 + n5)

if __name__ == "__main__":
    main()

exercises/111 - Introduction to Programming/repeating_sum.py


from __future__ import annotations

from .account_meta import AccountMeta
from .address import Address
from .public_account import PublicAccount
from ..blockchain.network_type import OptionalNetworkType
from ..mosaic.mosaic import Mosaic, MosaicList
from ... import util

__all__ = ['AccountInfo']

@util.inherit_doc
@util.dataclass(frozen=True)
class AccountInfo(util.DTO):
    """
    Basic information describing an account.

    :param meta: Account metadata.
    :param address: Account address.
    :param address_height: Chain height when address was published.
    :param public_key: Account public key.
    :param public_key_height: Chain height when public key was published.
    :param mosaics: List of mosaics owned by account.

    DTO Format:
        .. code-block:: yaml

            AccountDTO:
                # Hex(Address) (50-bytes)
                address: string
                addressHeight: UInt64DTO
                # Hex(PublicKey) (64-bytes)
                publicKey: string
                publicKeyHeight: UInt64DTO
                accountType: UInt64DTO
                linkedAccountType: string
                mosaics: MosaicDTO[]

            AccountInfoDTO:
                meta: AccountMetaDTO
                account: AccountDTO
    """

    meta: AccountMeta
    address: Address
    address_height: int
    public_key: str
    public_key_height: int
    account_type: int
    linked_account_key: str
    mosaics: MosaicList
    snapshots: dict

    @property
    def public_account(self) -> PublicAccount:
        """Get public account."""
        return PublicAccount(self.address, self.public_key)

    @classmethod
    def validate_dto(cls, data: dict) -> bool:
        """Validate the data-transfer object."""

        required_l1 = {'meta', 'account'}
        required_l2 = {
            'address',
            'addressHeight',
            'publicKey',
            'publicKeyHeight',
            'accountType',
            'linkedAccountKey',
            'mosaics',
            'snapshots'
        }
        return (
            # Level 1
            cls.validate_dto_required(data, required_l1)
            and cls.validate_dto_all(data, required_l1)
            # Level 2
            and cls.validate_dto_required(data['account'], required_l2)
            and cls.validate_dto_all(data['account'], required_l2)
        )

    def to_dto(
        self,
        network_type: OptionalNetworkType = None,
    ) -> dict:
        meta = self.meta.to_dto(network_type)
        account = {
            'address': util.hexlify(self.address.encoded),
            'addressHeight': util.u64_to_dto(self.address_height),
            'publicKey': self.public_key,
            'publicKeyHeight': util.u64_to_dto(self.public_key_height),
            'accountType': self.account_type,
            'linkedAccountKey': self.linked_account_key,
            'mosaics': Mosaic.sequence_to_dto(self.mosaics, network_type),
            'snapshots': self.snapshots
        }

        return {
            'meta': meta,
            'account': account,
        }

    @classmethod
    def create_from_dto(
        cls,
        data: dict,
        network_type: OptionalNetworkType = None,
    ):
        if not cls.validate_dto(data):
            raise ValueError('Invalid data-transfer object.')

        meta = data['meta']
        account = data['account']
        return cls(
            meta=AccountMeta.create_from_dto(meta, network_type),
            address=Address.create_from_encoded(account['address']),
            address_height=util.u64_from_dto(account.get('addressHeight', [0, 0])),
            public_key=account['publicKey'],
            public_key_height=util.u64_from_dto(account.get('publicKeyHeight', [0, 0])),
            account_type=account['accountType'],
            linked_account_key=account['linkedAccountKey'],
            mosaics=Mosaic.sequence_from_dto(account.get('mosaics', []), network_type),
            snapshots=account.get('snapshots', [])
        )


from modules import *

def beginning():
	print(text.throw_it)

def get_number(start, stop):
	number = random.randint(start, stop)
	return number

def get_sides():
  while True:
    sides = input(text.sides)
    if sides.isdigit() and int(sides) > 0:
      return int(sides)
    print(text.num_need)

def get_dices():
  while True:
    dices = input(text.dices)
    if dices.isdigit() and int(dices) > 0:
      return int(dices)
    print(text.num_need)

def throw(amount_of_dices:int, amount_of_sides:int):
	numbers = []
	for _ in range(amount_of_dices):
		numbers.append(get_number(1, amount_of_sides))
	if amount_of_sides != 6:
		return numbers
	else:
		six.six_sided(numbers)
		return numbers

def try_again():
	user_input = input(text.throw_again)
	if 'y' in user_input.lower():
		return True
	else:
		return False

def main():
	beginning()
	ready = True
	while ready:
		print(*(throw(get_dices(), get_sides())), sep=' ')
		ready = try_again()

if __name__== "__main__":
    main()


import random

class MarkovState:

	def __init__(self,charsToEmit, emissionProbs,transitionProbs):
		self.charsToEmit = charsToEmit
		self.emissionProbs = emissionProbs
		self.transitionProbs = transitionProbs

	def getEmissionIndex(self):
		aRand = random.random()
		cumulative = 0
		index =0
		for val in self.emissionProbs:
			cumulative += val
			if aRand <= cumulative:
				return index
			index = index + 1
		return len(self.emissionProbs) - 1

	def getIndexOfEmission(self, char):
		for i in range(0, len(self.charsToEmit) ):
			if str(self.charsToEmit[i]) == str(char):
				return i
		raise Exception("Cound not find " + str(char) )

	def getTransitionIndex(self):
		aRand = random.random()
		cumulative = 0
		index =0
		for val in self.transitionProbs:
			cumulative += val
			if aRand <= cumulative:
				return index
			index = index + 1
		return len(self.transitionProbs) - 1

def getMaxIndex( iterable ):
	val = iterable[0]
	index =0
	returnVal =0
	for i in iterable:
		if i > val:
			returnVal = index
		index = index+1
	return returnVal

def getViterbiPath( markovStates, output):
	returnPath= []
	oldViterbiProbs = []
	oldViterbiProbs.append(1) # we are 100% sure we start in the first state
	for i in range( 1, len(markovStates) ):
		oldViterbiProbs.append( 0)
	aTuple = ( oldViterbiProbs, 0)
	returnPath.append( aTuple )

	for i in range( 0,len(output)):
		newViterbiProbs = []
		for j in range( 0, len(markovStates)):
			state = markovStates[j]
			emissionProb = state.emissionProbs[state.getIndexOfEmission(output[i])]
			vTimesA=[]
			for k in range(0, len(markovStates)):
				vTimesA.append (oldViterbiProbs[k] * markovStates[k].transitionProbs[j])
			#print( "vTimesA" + str( vTimesA))
			maxVal = vTimesA[ getMaxIndex(vTimesA) ]
			newViterbiProbs.append( emissionProb * maxVal)
		aTuple = (newViterbiProbs,getMaxIndex(newViterbiProbs))
		returnPath.append( aTuple)
		oldViterbiProbs = newViterbiProbs
	return returnPath

dice = ( 1,2,3,4,5,6 )

fairState = MarkovState( dice, (1/6,1/6,1/6,1/6,1/6,1/6), ( 0.95, 0.05) )
loadedState = MarkovState( dice, (1/10,1/10,1/10,1/10,1/10,5/10), ( 0.10, 0.90) )

states = ( fairState, loadedState )

################################################

rolls = "266666"
getViterbiPath( states, rolls)

################################################
rolls = ""
trueStates = ""
state = states[0]

for i in range( 1, 100):
	nextState = state.getTransitionIndex()
	state = states[ nextState]
	trueStates = trueStates + str(nextState)
	rolls = rolls + str( dice[ state.getEmissionIndex()] )

rolls
trueStates

viterbiPath = getViterbiPath( states, rolls)

for i in range(0, len(rolls)):
	print( str(rolls[i]) + " " + str(trueStates[i])+ " " + str(viterbiPath[i][1]))

################################################


# Utility, to go through a scan, and see which exact labels were classified incorrectly.

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

import classifier
import cnn
import files
import util

def generateErrorForSingleBrain(scanID):
  data, labelsTrain, labelsTest = files.loadAllInputsUpdated(scanID, classifier.ALL_FEAT, classifier.MORE_FEAT, oneFeat=classifier.ONE_FEAT_NAME)

  trainX, trainY = files.convertToInputs(data, labelsTrain, classifier.PAD, classifier.FLIP_X, classifier.FLIP_Y, classifier.FLIP_Z)
  testX,   testY = files.convertToInputs(data,  labelsTest, classifier.PAD, False, False, False)

  print ("%d train samples, %d test" % (len(trainX), len(testX)))
  _, _, scores, predictions = cnn.runOne(trainX, trainY, testX, testY, scanID, None)
  print ("  Results Train -> Test\n  -------\n" + util.formatScores(scores))

  delta = labelsTest[:, 3] - np.array(predictions)
  errorTest = np.copy(labelsTest.astype(np.float16))
  errorTest[:, 3] = labelsTest[:, 3] - np.array(predictions)
  print ("%f -> %f" % (np.min(errorTest[:, 3]), np.max(errorTest[:, 3])))

  print ("Switched: %d train samples, %d test" % (len(testX), len(trainX)))
  _, _, scores, predictions = cnn.runOne(testX, testY, trainX, trainY, scanID, None)
  print ("  Results Test -> Train\n  -------\n" + util.formatScores(scores))

  delta = labelsTrain[:, 3] - np.array(predictions)
  errorTrain = np.copy(labelsTrain.astype(np.float16))
  errorTrain[:, 3] = labelsTrain[:, 3] - np.array(predictions)
  print ("%f -> %f" % (np.min(errorTrain[:, 3]), np.max(errorTrain[:, 3])))

  errorPath  = "%s/%s/Normal%s-MRA_annotationAll_errors.mat" % (files.BASE_PATH, scanID, scanID)
  print (errorTest.shape)
  print (errorTrain.shape)
  allErrors = np.vstack((errorTrain, errorTest))
  print (allErrors.shape)
  files.writePrediction(errorPath, 'coordTable', allErrors)
  plt.hist(allErrors[:, 3].ravel())
  plt.show()

def main():
  generateErrorForSingleBrain('034')

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