<commit_before>#!/usr/bin/env python -*- coding: utf-8 -*-

from __future__ import unicode_literals

import io, sys
from itertools import chain
reload(sys); sys.setdefaultencoding("utf-8")

import lucene
from java.io import File
# For Indexing.
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.document import Document, Field, FieldType
from org.apache.lucene.index import FieldInfo, IndexWriter, IndexWriterConfig
from org.apache.lucene.store import FSDirectory
from org.apache.lucene.util import Version
# For Querying.
from org.apache.lucene.index import DirectoryReader
from org.apache.lucene.queryparser.classic import QueryParser
from org.apache.lucene.search import IndexSearcher

import numpy as np

from nltk.corpus import wordnet as wn
from nltk import sent_tokenize, word_tokenize

from gensim.utils import tokenize
from gensim.models import Word2Vec

from texeval import TexEval2015
from luluwiki import retrieve_wiki

texeval_corpus = TexEval2015()

wiki_index = '/home/alvas/engwiki/english-wiki'
lucene.initVM()

def filter_doc(term, doc):
    for i in sent_tokenize(doc):
        for j in i.split('\n'):
            if term.lower() in j.lower():
                yield j

def isa_doc(term, doc):
    for i in sent_tokenize(doc):
        if term+" is a" in i or "is a "+term in i or "is an "+term in i:
            yield i

def build_corpus_from_terms_with_wiki(sbc, searcher, analyzer):
    fout = io.open('WIKI_'+sbc, 'w', encoding='utf8')
    for termid, term in texeval_corpus.terms('test', sbc):
        docs = []
        for curl, doc in retrieve_wiki(term, searcher, analyzer):
            for fdoc in filter_doc(term, doc):
                docs.append(fdoc)

        print termid, term, len(docs)
        fout.write("{}\t{}\n".format(termid, term).decode('utf8'))
        for i in docs:

            fout.write(i.decode('utf8')+ '\n')
        fout.write('\n'.decode('utf8'))
    return sbc

def build_corpus(n=0):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    # Hack for parallelizing queries, uses one index per domain.
    directory = FSDirectory.open(File(wiki_index+'-'+sbc))
    searcher = IndexSearcher(DirectoryReader.open(directory))
    analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
    build_corpus_from_terms_with_wiki(sbc, searcher, analyzer)

def build_word_vector(n=0, mincount=1):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    corpus_name = 'WIKI_'+sbc
    sentences = []
    current_term = ""
    with io.open(corpus_name, 'r', encoding='utf8') as fin:
        for line in fin:
            if '\t' in line:
                current_term = line.strip().split('\t')[1]
            if line.strip().endswith('.'):
                if current_term in line:
                    # Single tokenize terms.
                    line.replace(current_term, current_term.replace(' ', '_'))
                sentences.append(list(tokenize(line)))
    model = Word2Vec(sentences, size=100, window=5,
                     min_count=mincount, workers=2, iter=10)
    model.save(corpus_name+'.10epochs.singletok.min'+str(mincount)+'.deep')

def test_vector(n=0, mincount=1):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    fname = 'WIKI_'+'.10epochs.singletok.min'+str(mincount)+'.deep'
    model = Word2Vec.load(fname)

    for termid, term in texeval_corpus.terms('test', sbc):
        if len(term.split()) == 1 and term in model:
            print termid, term, model[term]

def get_matrix(n=0):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    fname = 'WIKI_'+sbc+'.10epochs.singletok.min1'+'.deep'
    model = Word2Vec.load(fname)

    terms = [i for i in texeval_corpus.terms('test', sbc)]
    matrix_lol = [[0 for x in range(len(terms))] for x in range(len(terms))]

    for termid1, term1 in terms:
        for termid2, term2 in terms:
            print term1, term2
            term1 = term1.replace(' ', '_')
            term2 = term2.replace(' ', '_')
            similarity = model.n_similarity(term1, term2)
            distance = 2*(1-similarity)
            matrix_lol[termid1][termid2] = distance

    matrix = np.array(matrix_lol)

    print matrix

get_matrix()

'''
def build_taxo(n=0):
    pass
    # Parse cluster for hypernyms.
    # Output to file.

def main(domain_number, mincount):
    build_word_vector(int(domain_number), int(mincount))

if __name__ == '__main__':
  import sys
  main(*sys.argv[1:])

'''<commit_msg>testing get_matrix()<commit_after>#!/usr/bin/env python -*- coding: utf-8 -*-

from __future__ import unicode_literals

import io, sys
from itertools import chain
reload(sys); sys.setdefaultencoding("utf-8")

import lucene
from java.io import File
# For Indexing.
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.document import Document, Field, FieldType
from org.apache.lucene.index import FieldInfo, IndexWriter, IndexWriterConfig
from org.apache.lucene.store import FSDirectory
from org.apache.lucene.util import Version
# For Querying.
from org.apache.lucene.index import DirectoryReader
from org.apache.lucene.queryparser.classic import QueryParser
from org.apache.lucene.search import IndexSearcher

import numpy as np

from nltk.corpus import wordnet as wn
from nltk import sent_tokenize, word_tokenize

from gensim.utils import tokenize
from gensim.models import Word2Vec

from texeval import TexEval2015
from luluwiki import retrieve_wiki

texeval_corpus = TexEval2015()

wiki_index = '/home/alvas/engwiki/english-wiki'
lucene.initVM()

def filter_doc(term, doc):
    for i in sent_tokenize(doc):
        for j in i.split('\n'):
            if term.lower() in j.lower():
                yield j

def isa_doc(term, doc):
    for i in sent_tokenize(doc):
        if term+" is a" in i or "is a "+term in i or "is an "+term in i:
            yield i

def build_corpus_from_terms_with_wiki(sbc, searcher, analyzer):
    fout = io.open('WIKI_'+sbc, 'w', encoding='utf8')
    for termid, term in texeval_corpus.terms('test', sbc):
        docs = []
        for curl, doc in retrieve_wiki(term, searcher, analyzer):
            for fdoc in filter_doc(term, doc):
                docs.append(fdoc)

        print termid, term, len(docs)
        fout.write("{}\t{}\n".format(termid, term).decode('utf8'))
        for i in docs:

            fout.write(i.decode('utf8')+ '\n')
        fout.write('\n'.decode('utf8'))
    return sbc

def build_corpus(n=0):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    # Hack for parallelizing queries, uses one index per domain.
    directory = FSDirectory.open(File(wiki_index+'-'+sbc))
    searcher = IndexSearcher(DirectoryReader.open(directory))
    analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
    build_corpus_from_terms_with_wiki(sbc, searcher, analyzer)

def build_word_vector(n=0, mincount=1):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    corpus_name = 'WIKI_'+sbc
    sentences = []
    current_term = ""
    with io.open(corpus_name, 'r', encoding='utf8') as fin:
        for line in fin:
            if '\t' in line:
                current_term = line.strip().split('\t')[1]
            if line.strip().endswith('.'):
                if current_term in line:
                    # Single tokenize terms.
                    line.replace(current_term, current_term.replace(' ', '_'))
                sentences.append(list(tokenize(line)))
    model = Word2Vec(sentences, size=100, window=5,
                     min_count=mincount, workers=2, iter=10)
    model.save(corpus_name+'.10epochs.singletok.min'+str(mincount)+'.deep')

def test_vector(n=0, mincount=1):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    fname = 'WIKI_'+'.10epochs.singletok.min'+str(mincount)+'.deep'
    model = Word2Vec.load(fname)

    for termid, term in texeval_corpus.terms('test', sbc):
        if len(term.split()) == 1 and term in model:
            print termid, term, model[term]

def get_matrix(n=0):
    sbcs = texeval_corpus.test_subcorpora
    sbc = sbcs[n]
    fname = 'WIKI_'+sbc+'.10epochs.singletok.min1'+'.deep'
    model = Word2Vec.load(fname)

    terms = [i for i in texeval_corpus.terms('test', sbc)]
    matrix_lol = [[0 for x in range(len(terms))] for x in range(len(terms))]

    for termid1, term1 in terms:
        for termid2, term2 in terms:
            term1 = term1.replace(' ', '_').replace('-', ' ')
            term2 = term2.replace(' ', '_').replace('-', ' ')
            similarity = model.n_similarity(term1, term2)
            distance = 2*(1-similarity)
            matrix_lol[termid1][termid2] = distance

    matrix = np.array(matrix_lol)

    print matrix

get_matrix()

'''
def build_taxo(n=0):
    pass
    # Parse cluster for hypernyms.
    # Output to file.

def main(domain_number, mincount):
    build_word_vector(int(domain_number), int(mincount))

if __name__ == '__main__':
  import sys
  main(*sys.argv[1:])

'''<|endoftext|>


<commit_before>import pyed
import re

# extract the version number from the pexpect.py source.
doc = file ('../pexpect.py','rb').read()
d = pyed.pyed(doc)
d.first('^__version__')
r = re.search("'([0-9]\.[0-9])'", d.cur_line)
version = r.group(1)

<commit_msg>better<commit_after>import pyed
import re

# extract the version number from the pexpect.py source.
d = pyed.pyed()
d.read ("../pexpect.py")
d.first('^__version__')
r = re.search("'([0-9]\.[0-9])'", d.cur_line)
version = r.group(1)

d = pyed.pyed()
d.read ("../doc/index.html")
for cl in d.match_lines('.*VERSION.*'):
    d.cur_line = d.cur_line.replace('VERSION', version)
d.write("../doc/index.html")

<|endoftext|>


<commit_before>#!/usr/bin/env python
# pyOCD debugger
# Copyright (c) 2006-2018 Arm Limited
# Copyright (c) 2020 Cypress Semiconductor Corporation
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import sys
import os
import logging
import argparse
import json
import pkg_resources

from .. import __version__
from .. import target
from ..core.session import Session
from ..core.helpers import ConnectHelper
from ..gdbserver import GDBServer
from ..utility.cmdline import (split_command_line, VECTOR_CATCH_CHAR_MAP, convert_vector_catch,
                                convert_session_options)
from ..probe.cmsis_dap_probe import CMSISDAPProbe
from ..probe.pydapaccess import DAPAccess
from ..core.session import Session
from ..coresight.generic_mem_ap import GenericMemAPTarget

LOG = logging.getLogger(__name__)

LEVELS = {
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL
}

SUPPORTED_TARGETS = list(sorted(target.TARGET.keys()))
DEBUG_LEVELS = list(LEVELS.keys())

class GDBServerTool(object):
    def __init__(self):
        self.args = None
        self.gdb_server_settings = None
        self.echo_msg = None

    def build_parser(self):
        # Build epilog with list of targets.
        epilog = "Available targets for use with --target option: " + ", ".join(SUPPORTED_TARGETS)

        # Keep args in snyc with flash_tool.py when possible
        parser = argparse.ArgumentParser(description='PyOCD GDB Server', epilog=epilog)
        parser.add_argument('--version', action='version', version=__version__)
        parser.add_argument('--config', metavar="PATH", default=None, help="Use a YAML config file.")
        parser.add_argument("--no-config", action="store_true", default=None, help="Do not use a configuration file.")
        parser.add_argument("--pack", metavar="PATH", help="Path to a CMSIS Device Family Pack")
        parser.add_argument("-p", "--port", dest="port_number", type=int, default=3333, help="Set the port number that GDB server will open (default 3333).")
        parser.add_argument("-sc", "--semihost-console", dest="semihost_console_type", default=None, choices=('telnet', 'stdx'), help="Console for semihosting.")
        parser.add_argument("-T", "--telnet-port", dest="telnet_port", type=int, default=4444, help="Specify the telnet port for semihosting (default 4444).")
        parser.add_argument("--allow-remote", dest="serve_local_only", default=True, action="store_false", help="Allow remote TCP/IP connections (default is no).")
        parser.add_argument("-b", "--board", dest="board_id", default=None, help="Connect to board by board ID. Use -l to list all connected boards. Only a unique part of the board ID needs to be provided.")
        parser.add_argument("-l", "--list", action="store_true", dest="list_all", default=False, help="List all connected boards.")
        parser.add_argument("--list-targets", action="store_true", dest="list_targets", default=False, help="List all available targets.")
        parser.add_argument("--json", action="store_true", dest="output_json", default=False, help="Output lists in JSON format. Only applies to --list and --list-targets.")
        parser.add_argument("-d", "--debug", dest="debug_level", choices=DEBUG_LEVELS, default='info', help="Set the level of system logging output. Supported choices are: " + ", ".join(DEBUG_LEVELS), metavar="LEVEL")
        parser.add_argument("-t", "--target", dest="target_override", default=None, help="Override target to debug.", metavar="TARGET")
        parser.add_argument("-n", "--nobreak", dest="no_break_at_hardfault", action="store_true", help="Disable halt at hardfault handler. (Deprecated)")
        parser.add_argument("-r", "--reset-break", dest="break_on_reset", action="store_true", help="Halt the target when reset. (Deprecated)")
        parser.add_argument("-C", "--vector-catch", default='h', help="Enable vector catch sources, one letter per enabled source in any order, or 'all' or 'none'. (h=hard fault, b=bus fault, m=mem fault, i=irq err, s=state err, c=check err, p=nocp, r=reset, a=all, n=none). (Default is hard fault.)")
        parser.add_argument("-s", "--step-int", dest="step_into_interrupt", default=None, action="store_true", help="Allow single stepping to step into interrupts.")
        parser.add_argument("-f", "--frequency", dest="frequency", default=None, type=int, help="Set the SWD clock frequency in Hz.")
        parser.add_argument("-o", "--persist", dest="persist", default=None, action="store_true", help="Keep GDB server running even after remote has detached.")
        parser.add_argument("-bh", "--soft-bkpt-as-hard", dest="soft_bkpt_as_hard", default=False, action="store_true", help="Replace software breakpoints with hardware breakpoints (ignored).")
        group = parser.add_mutually_exclusive_group()
        group.add_argument("-ce", "--chip_erase", action="store_true", help="Use chip erase when programming.")
        group.add_argument("-se", "--sector_erase", action="store_true", help="Use sector erase when programming.")
        # -Currently "--unlock" does nothing since kinetis parts will automatically get unlocked
        parser.add_argument("-u", "--unlock", action="store_true", default=False, help="Unlock the device.")
        # reserved: "-a", "--address"
        # reserved: "-s", "--skip"
        parser.add_argument("-hp", "--hide_progress", action="store_true", default=None, help="Don't display programming progress.")
        parser.add_argument("-fp", "--fast_program", action="store_true", default=None, help="Use only the CRC of each page to determine if it already has the same data.")
        parser.add_argument("-S", "--semihosting", dest="enable_semihosting", action="store_true", default=None, help="Enable semihosting.")
        parser.add_argument("-G", "--gdb-syscall", dest="semihost_use_syscalls", action="store_true", default=None, help="Use GDB syscalls for semihosting file I/O.")
        parser.add_argument("-c", "--command", dest="commands", metavar="CMD", action='append', nargs='+', help="Run command (OpenOCD compatibility).")
        parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
        parser.add_argument("--elf", metavar="PATH", help="Optionally specify ELF file being debugged.")
        parser.add_argument("-O", "--option", metavar="OPTION", action="append", help="Set session option of form 'OPTION=VALUE'.")
        parser.add_argument("--no-deprecation-warning", action="store_true", help="Do not warn about pyocd-gdbserver being deprecated.")
        self.parser = parser
        return parser

    def get_chip_erase(self, args):
        # Determine programming mode
        chip_erase = "auto"
        if args.chip_erase:
            chip_erase = "chip"
        elif args.sector_erase:
            chip_erase = "sector"
        return chip_erase

    def get_vector_catch(self, args):
        vector_catch = args.vector_catch.lower()

        # Handle deprecated options.
        if args.break_on_reset:
            vector_catch += 'r'
        if args.no_break_at_hardfault:
            # Must handle all case specially since we can't just filter 'h'.
            if vector_catch == 'all' or 'a' in vector_catch:
                vector_catch = 'bmiscpr' # Does not include 'h'.
            else:
                vector_catch = vector_catch.replace('h', '')

        return vector_catch

    def get_gdb_server_settings(self, args):
        # Set gdb server settings
        return {
            'gdbserver_port' : self.args.port_number,
            'step_into_interrupt' : args.step_into_interrupt,
            'persist' : args.persist,
            'chip_erase': self.get_chip_erase(args),
            'hide_programming_progress' : args.hide_progress,
            'fast_program' : args.fast_program,
            'enable_semihosting' : args.enable_semihosting,
            'semihost_console_type' : args.semihost_console_type,
            'telnet_port' : args.telnet_port,
            'semihost_use_syscalls' : args.semihost_use_syscalls,
            'serve_local_only' : args.serve_local_only,
            'vector_catch' : self.get_vector_catch(args),
        }

    def setup_logging(self, args):
        format = "%(relativeCreated)07d:%(levelname)s:%(module)s:%(message)s"
        level = LEVELS.get(args.debug_level, logging.NOTSET)
        logging.basicConfig(level=level, format=format)

    def process_commands(self, commands):
        """! @brief Handle OpenOCD commands for compatibility."""
        if commands is None:
            return
        for cmd_list in commands:
            try:
                cmd_list = split_command_line(cmd_list)
                cmd = cmd_list[0]
                if cmd == 'gdb_port':
                    if len(cmd_list) < 2:
                        print("Missing port argument")
                    else:
                        self.args.port_number = int(cmd_list[1], base=0)
                elif cmd == 'telnet_port':
                    if len(cmd_list) < 2:
                        print("Missing port argument")
                    else:
                        self.gdb_server_settings['telnet_port'] = int(cmd_list[1], base=0)
                elif cmd == 'echo':
                    self.echo_msg = ' '.join(cmd_list[1:])
                else:
                    print("Unsupported command: %s" % ' '.join(cmd_list))
            except IndexError:
                pass

    def server_listening(self, server):
        if self.echo_msg is not None:
            print(self.echo_msg, file=sys.stderr)
            sys.stderr.flush()

    def disable_logging(self):
        logging.getLogger().setLevel(logging.FATAL)

    def list_boards(self):
        self.disable_logging()

        if not self.args.output_json:
            ConnectHelper.list_connected_probes()
        else:
            try:
                all_mbeds = ConnectHelper.get_sessions_for_all_connected_probes(blocking=False)
                status = 0
                error = ""
            except Exception as e:
                all_mbeds = []
                status = 1
                error = str(e)
                if not self.args.output_json:
                    raise

            boards = []
            obj = {
                'pyocd_version' : __version__,
                'version' : { 'major' : 1, 'minor' : 0 },
                'status' : status,
                'boards' : boards,
                }

            if status != 0:
                obj['error'] = error

            for mbed in all_mbeds:
                d = {
                    'unique_id' : mbed.probe.unique_id,
                    'info' : mbed.board.description,
                    'board_name' : mbed.board.name,
                    'target' : mbed.board.target_type,
                    'vendor_name' : mbed.probe.vendor_name,
                    'product_name' : mbed.probe.product_name,
                    }
                boards.append(d)

            print(json.dumps(obj, indent=4))

    def list_targets(self):
        self.disable_logging()

        if self.args.output_json:
            targets = []
            obj = {
                'pyocd_version' : __version__,
                'version' : { 'major' : 1, 'minor' : 0 },
                'status' : 0,
                'targets' : targets
                }

            for name in SUPPORTED_TARGETS:
                s = Session(None) # Create empty session
                t = target.TARGET[name](s)
                d = {
                    'name' : name,
                    'part_number' : t.part_number,
                    }
                if t._svd_location is not None:
                    svdPath = t._svd_location.filename
                    if os.path.exists(svdPath):
                        d['svd_path'] = svdPath
                targets.append(d)

            print(json.dumps(obj, indent=4))
        else:
            for t in SUPPORTED_TARGETS:
                print(t)

    def run(self, args=None):
        self.args = self.build_parser().parse_args(args)
        self.gdb_server_settings = self.get_gdb_server_settings(self.args)
        self.setup_logging(self.args)
        DAPAccess.set_args(self.args.daparg)

        if not self.args.no_deprecation_warning:
            LOG.warning("pyocd-gdbserver is deprecated; please use the new combined pyocd tool.")

        self.process_commands(self.args.commands)

        gdb = None
        gdbs = []
        if self.args.list_all == True:
            self.list_boards()
        elif self.args.list_targets == True:
            self.list_targets()
        else:
            try:
                # Build dict of session options.
                sessionOptions = convert_session_options(self.args.option)
                sessionOptions.update(self.gdb_server_settings)

                session = ConnectHelper.session_with_chosen_probe(
                    config_file=self.args.config,
                    no_config=self.args.no_config,
                    pack=self.args.pack,
                    unique_id=self.args.board_id,
                    target_override=self.args.target_override,
                    frequency=self.args.frequency,
                    **sessionOptions)
                if session is None:
                    print("No board selected")
                    return 1
                with session:
                    # Set ELF if provided.
                    if self.args.elf:
                        session.board.target.elf = self.args.elf
                    for core_number, core in session.board.target.cores.items():
                        if isinstance(session.board.target.cores[core_number], GenericMemAPTarget):
                            continue

                        gdb = GDBServer(session,
                            core=core_number,
                            server_listening_callback=self.server_listening)
                        gdbs.append(gdb)
                    gdb = gdbs[0]
                    while gdb.is_alive():
                        gdb.join(timeout=0.5)
            except KeyboardInterrupt:
                for gdb in gdbs:
                    gdb.stop()
            except Exception as e:
                LOG.error("uncaught exception: %s" % e, exc_info=Session.get_current().log_tracebacks)
                for gdb in gdbs:
                    gdb.stop()
                return 1

        # Successful exit.
        return 0

def main():
    sys.exit(GDBServerTool().run())

if __name__ == '__main__':
    main()
<commit_msg>pyocd-gdbserver: fix regression preventing the server from starting.<commit_after>#!/usr/bin/env python
# pyOCD debugger
# Copyright (c) 2006-2018 Arm Limited
# Copyright (c) 2020 Cypress Semiconductor Corporation
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import sys
import os
import logging
import argparse
import json
import pkg_resources

from .. import __version__
from .. import target
from ..core.session import Session
from ..core.helpers import ConnectHelper
from ..gdbserver import GDBServer
from ..utility.cmdline import (split_command_line, VECTOR_CATCH_CHAR_MAP, convert_vector_catch,
                                convert_session_options)
from ..probe.cmsis_dap_probe import CMSISDAPProbe
from ..probe.pydapaccess import DAPAccess
from ..core.session import Session
from ..coresight.generic_mem_ap import GenericMemAPTarget

LOG = logging.getLogger(__name__)

LEVELS = {
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL
}

SUPPORTED_TARGETS = list(sorted(target.TARGET.keys()))
DEBUG_LEVELS = list(LEVELS.keys())

class GDBServerTool(object):
    def __init__(self):
        self.args = None
        self.gdb_server_settings = None
        self.echo_msg = None

    def build_parser(self):
        # Build epilog with list of targets.
        epilog = "Available targets for use with --target option: " + ", ".join(SUPPORTED_TARGETS)

        # Keep args in snyc with flash_tool.py when possible
        parser = argparse.ArgumentParser(description='PyOCD GDB Server', epilog=epilog)
        parser.add_argument('--version', action='version', version=__version__)
        parser.add_argument('--config', metavar="PATH", default=None, help="Use a YAML config file.")
        parser.add_argument("--no-config", action="store_true", default=None, help="Do not use a configuration file.")
        parser.add_argument("--pack", metavar="PATH", help="Path to a CMSIS Device Family Pack")
        parser.add_argument("-p", "--port", dest="port_number", type=int, default=3333, help="Set the port number that GDB server will open (default 3333).")
        parser.add_argument("-sc", "--semihost-console", dest="semihost_console_type", default=None, choices=('telnet', 'stdx'), help="Console for semihosting.")
        parser.add_argument("-T", "--telnet-port", dest="telnet_port", type=int, default=4444, help="Specify the telnet port for semihosting (default 4444).")
        parser.add_argument("--allow-remote", dest="serve_local_only", default=True, action="store_false", help="Allow remote TCP/IP connections (default is no).")
        parser.add_argument("-b", "--board", dest="board_id", default=None, help="Connect to board by board ID. Use -l to list all connected boards. Only a unique part of the board ID needs to be provided.")
        parser.add_argument("-l", "--list", action="store_true", dest="list_all", default=False, help="List all connected boards.")
        parser.add_argument("--list-targets", action="store_true", dest="list_targets", default=False, help="List all available targets.")
        parser.add_argument("--json", action="store_true", dest="output_json", default=False, help="Output lists in JSON format. Only applies to --list and --list-targets.")
        parser.add_argument("-d", "--debug", dest="debug_level", choices=DEBUG_LEVELS, default='info', help="Set the level of system logging output. Supported choices are: " + ", ".join(DEBUG_LEVELS), metavar="LEVEL")
        parser.add_argument("-t", "--target", dest="target_override", default=None, help="Override target to debug.", metavar="TARGET")
        parser.add_argument("-n", "--nobreak", dest="no_break_at_hardfault", action="store_true", help="Disable halt at hardfault handler. (Deprecated)")
        parser.add_argument("-r", "--reset-break", dest="break_on_reset", action="store_true", help="Halt the target when reset. (Deprecated)")
        parser.add_argument("-C", "--vector-catch", default='h', help="Enable vector catch sources, one letter per enabled source in any order, or 'all' or 'none'. (h=hard fault, b=bus fault, m=mem fault, i=irq err, s=state err, c=check err, p=nocp, r=reset, a=all, n=none). (Default is hard fault.)")
        parser.add_argument("-s", "--step-int", dest="step_into_interrupt", default=None, action="store_true", help="Allow single stepping to step into interrupts.")
        parser.add_argument("-f", "--frequency", dest="frequency", default=None, type=int, help="Set the SWD clock frequency in Hz.")
        parser.add_argument("-o", "--persist", dest="persist", default=None, action="store_true", help="Keep GDB server running even after remote has detached.")
        parser.add_argument("-bh", "--soft-bkpt-as-hard", dest="soft_bkpt_as_hard", default=False, action="store_true", help="Replace software breakpoints with hardware breakpoints (ignored).")
        group = parser.add_mutually_exclusive_group()
        group.add_argument("-ce", "--chip_erase", action="store_true", help="Use chip erase when programming.")
        group.add_argument("-se", "--sector_erase", action="store_true", help="Use sector erase when programming.")
        # -Currently "--unlock" does nothing since kinetis parts will automatically get unlocked
        parser.add_argument("-u", "--unlock", action="store_true", default=False, help="Unlock the device.")
        # reserved: "-a", "--address"
        # reserved: "-s", "--skip"
        parser.add_argument("-hp", "--hide_progress", action="store_true", default=None, help="Don't display programming progress.")
        parser.add_argument("-fp", "--fast_program", action="store_true", default=None, help="Use only the CRC of each page to determine if it already has the same data.")
        parser.add_argument("-S", "--semihosting", dest="enable_semihosting", action="store_true", default=None, help="Enable semihosting.")
        parser.add_argument("-G", "--gdb-syscall", dest="semihost_use_syscalls", action="store_true", default=None, help="Use GDB syscalls for semihosting file I/O.")
        parser.add_argument("-c", "--command", dest="commands", metavar="CMD", action='append', nargs='+', help="Run command (OpenOCD compatibility).")
        parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
        parser.add_argument("--elf", metavar="PATH", help="Optionally specify ELF file being debugged.")
        parser.add_argument("-O", "--option", metavar="OPTION", action="append", help="Set session option of form 'OPTION=VALUE'.")
        parser.add_argument("--no-deprecation-warning", action="store_true", help="Do not warn about pyocd-gdbserver being deprecated.")
        self.parser = parser
        return parser

    def get_chip_erase(self, args):
        # Determine programming mode
        chip_erase = "auto"
        if args.chip_erase:
            chip_erase = "chip"
        elif args.sector_erase:
            chip_erase = "sector"
        return chip_erase

    def get_vector_catch(self, args):
        vector_catch = args.vector_catch.lower()

        # Handle deprecated options.
        if args.break_on_reset:
            vector_catch += 'r'
        if args.no_break_at_hardfault:
            # Must handle all case specially since we can't just filter 'h'.
            if vector_catch == 'all' or 'a' in vector_catch:
                vector_catch = 'bmiscpr' # Does not include 'h'.
            else:
                vector_catch = vector_catch.replace('h', '')

        return vector_catch

    def get_gdb_server_settings(self, args):
        # Set gdb server settings
        return {
            'gdbserver_port' : self.args.port_number,
            'step_into_interrupt' : args.step_into_interrupt,
            'persist' : args.persist,
            'chip_erase': self.get_chip_erase(args),
            'hide_programming_progress' : args.hide_progress,
            'fast_program' : args.fast_program,
            'enable_semihosting' : args.enable_semihosting,
            'semihost_console_type' : args.semihost_console_type,
            'telnet_port' : args.telnet_port,
            'semihost_use_syscalls' : args.semihost_use_syscalls,
            'serve_local_only' : args.serve_local_only,
            'vector_catch' : self.get_vector_catch(args),
        }

    def setup_logging(self, args):
        format = "%(relativeCreated)07d:%(levelname)s:%(module)s:%(message)s"
        level = LEVELS.get(args.debug_level, logging.NOTSET)
        logging.basicConfig(level=level, format=format)

    def process_commands(self, commands):
        """! @brief Handle OpenOCD commands for compatibility."""
        if commands is None:
            return
        for cmd_list in commands:
            try:
                cmd_list = split_command_line(cmd_list)
                cmd = cmd_list[0]
                if cmd == 'gdb_port':
                    if len(cmd_list) < 2:
                        print("Missing port argument")
                    else:
                        self.args.port_number = int(cmd_list[1], base=0)
                elif cmd == 'telnet_port':
                    if len(cmd_list) < 2:
                        print("Missing port argument")
                    else:
                        self.gdb_server_settings['telnet_port'] = int(cmd_list[1], base=0)
                elif cmd == 'echo':
                    self.echo_msg = ' '.join(cmd_list[1:])
                else:
                    print("Unsupported command: %s" % ' '.join(cmd_list))
            except IndexError:
                pass

    def server_listening(self, server):
        if self.echo_msg is not None:
            print(self.echo_msg, file=sys.stderr)
            sys.stderr.flush()

    def disable_logging(self):
        logging.getLogger().setLevel(logging.FATAL)

    def list_boards(self):
        self.disable_logging()

        if not self.args.output_json:
            ConnectHelper.list_connected_probes()
        else:
            try:
                all_mbeds = ConnectHelper.get_sessions_for_all_connected_probes(blocking=False)
                status = 0
                error = ""
            except Exception as e:
                all_mbeds = []
                status = 1
                error = str(e)
                if not self.args.output_json:
                    raise

            boards = []
            obj = {
                'pyocd_version' : __version__,
                'version' : { 'major' : 1, 'minor' : 0 },
                'status' : status,
                'boards' : boards,
                }

            if status != 0:
                obj['error'] = error

            for mbed in all_mbeds:
                d = {
                    'unique_id' : mbed.probe.unique_id,
                    'info' : mbed.board.description,
                    'board_name' : mbed.board.name,
                    'target' : mbed.board.target_type,
                    'vendor_name' : mbed.probe.vendor_name,
                    'product_name' : mbed.probe.product_name,
                    }
                boards.append(d)

            print(json.dumps(obj, indent=4))

    def list_targets(self):
        self.disable_logging()

        if self.args.output_json:
            targets = []
            obj = {
                'pyocd_version' : __version__,
                'version' : { 'major' : 1, 'minor' : 0 },
                'status' : 0,
                'targets' : targets
                }

            for name in SUPPORTED_TARGETS:
                s = Session(None) # Create empty session
                t = target.TARGET[name](s)
                d = {
                    'name' : name,
                    'part_number' : t.part_number,
                    }
                if t._svd_location is not None:
                    svdPath = t._svd_location.filename
                    if os.path.exists(svdPath):
                        d['svd_path'] = svdPath
                targets.append(d)

            print(json.dumps(obj, indent=4))
        else:
            for t in SUPPORTED_TARGETS:
                print(t)

    def run(self, args=None):
        self.args = self.build_parser().parse_args(args)
        self.gdb_server_settings = self.get_gdb_server_settings(self.args)
        self.setup_logging(self.args)
        DAPAccess.set_args(self.args.daparg)

        if not self.args.no_deprecation_warning:
            LOG.warning("pyocd-gdbserver is deprecated; please use the new combined pyocd tool.")

        self.process_commands(self.args.commands)

        gdb = None
        gdbs = []
        if self.args.list_all == True:
            self.list_boards()
        elif self.args.list_targets == True:
            self.list_targets()
        else:
            try:
                # Build dict of session options.
                sessionOptions = convert_session_options(self.args.option)
                sessionOptions.update(self.gdb_server_settings)

                session = ConnectHelper.session_with_chosen_probe(
                    config_file=self.args.config,
                    no_config=self.args.no_config,
                    pack=self.args.pack,
                    unique_id=self.args.board_id,
                    target_override=self.args.target_override,
                    frequency=self.args.frequency,
                    **sessionOptions)
                if session is None:
                    print("No board selected")
                    return 1
                with session:
                    # Set ELF if provided.
                    if self.args.elf:
                        session.board.target.elf = self.args.elf
                    for core_number, core in session.board.target.cores.items():
                        if isinstance(session.board.target.cores[core_number], GenericMemAPTarget):
                            continue

                        gdb = GDBServer(session,
                            core=core_number,
                            server_listening_callback=self.server_listening)
                        session.gdbservers[core_number] = gdb
                        gdbs.append(gdb)
                        gdb.start()
                    gdb = gdbs[0]
                    while gdb.is_alive():
                        gdb.join(timeout=0.5)
            except KeyboardInterrupt:
                for gdb in gdbs:
                    gdb.stop()
            except Exception as e:
                LOG.error("uncaught exception: %s" % e, exc_info=Session.get_current().log_tracebacks)
                for gdb in gdbs:
                    gdb.stop()
                return 1

        # Successful exit.
        return 0

def main():
    sys.exit(GDBServerTool().run())

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>from enum import Enum
from functools import lru_cache
from typing import (Callable, List, Mapping, Sequence, Type, Union, UnionMeta,
                    GenericMeta, MutableSequence, TypeVar, Any, FrozenSet,
                    MutableSet, Set, MutableMapping, Dict, AnyMeta)

from attr import NOTHING
from attr.validators import _InstanceOfValidator, _OptionalValidator

from .disambiguators import create_uniq_field_dis_func

try:
    from functools import singledispatch
except ImportError:
    # We use a backport for 3.3.
    from singledispatch import singledispatch

T = TypeVar('T')

class Converter(object):
    """Converts between attrs and Python primitives."""
    def __init__(self, dict_factory=dict):
        # Create a per-instance cache.
        self._get_dis_func = lru_cache()(self._get_dis_func)

        # Per-instance register of to-Python converters.
        dumps = singledispatch(self._dumps)
        dumps.register(Enum, self._dumps_enum)
        dumps.register(str, self._dumps_identity)
        dumps.register(bytes, self._dumps_identity)
        dumps.register(Sequence, self._dumps_seq)
        dumps.register(Mapping, self._dumps_mapping)

        self.dumps = dumps

        # Per-instance register of to-attrs converters.
        # Singledispatch dispatches based on the first argument, so we
        # store the function and switch the arguments in self.loads.
        loads = singledispatch(self._loads)
        loads.register(GenericMeta, self._loads_generic_meta)
        loads.register(UnionMeta, self._loads_union)
        loads.register(AnyMeta, self._loads_identity)

        self._loads = loads
        self._dict_factory = dict_factory

    def register_dumps_hook(self, cls: Type[T], func: Callable[[T], Any]):
        """Register a class-to-primitive converter function for a class.

        The converter function should take an instance of the class and return
        its Python equivalent.
        """
        self.dumps.register(cls, func)

    def register_loads_hook(self, cls: Type[T], func: Callable[[Any], T]):
        """Register a primitive-to-class converter function for a type.

        The converter function should take an instance of a Python primitive
        and return the instance of the class.
        """
        self._loads.register(cls, func)

    def loads(self, obj, cl: Type):
        """Convert unstructured Python data structures to structured data."""
        return self._loads(cl, obj)

    # Classes to Python primitives.

    def _dumps(self, obj):
        """Convert given attrs classes to their primitive equivalents."""
        return (self._dumps_attrs(obj)
                if getattr(obj.__class__, "__attrs_attrs__", None) is not None
                else obj)

    def _dumps_attrs(self, obj):
        """Our version of `attrs.asdict`, so we can call back to us."""
        attrs = obj.__class__.__attrs_attrs__
        rv = self._dict_factory()
        for a in attrs:
            v = getattr(obj, a.name)
            rv[a.name] = self.dumps(v)
        return rv

    def _dumps_enum(self, obj):
        """Convert an enum to its value."""
        return obj.value

    def _dumps_identity(self, obj):
        """Just pass it through."""
        return obj

    def _dumps_seq(self, seq):
        """Convert a sequence to primitive equivalents."""
        # We can reuse the sequence class, so tuples stay tuples.
        return seq.__class__(self.dumps(e) for e in seq)

    def _dumps_mapping(self, mapping: Mapping):
        """Convert a mapping of attr classes to primitive equivalents."""
        # We can reuse the mapping class, so dicts stay dicts and OrderedDicts
        # stay OrderedDicts.
        return mapping.__class__((self.dumps(k), self.dumps(v))
                                 for k, v in mapping.items())

    # Python primitives to classes.

    def _loads(self, cl, obj):
        """Things we can't recognize by isinstance."""
        if hasattr(cl, '__attrs_attrs__'):
            # This is an attrs class
            return self._loads_attrs(cl, obj)
        # We don't know what this is. Just try instantiating it.
        # This covers the basics: bools, ints, floats, strings, bytes, enums.
        return cl(obj)

    def _loads_attrs(self, cl, obj):
        """Handle actual attrs classes."""
        conv_obj = obj.copy()  # Dict of converted parameters.
        for a in cl.__attrs_attrs__:
            name = a.name
            # We detect the type by the validator.
            validator = a.validator
            converted = self._handle_attr_attribute(name, validator, obj)
            if converted is NOTHING:
                continue
            conv_obj[name] = converted

        return cl(**conv_obj)

    def _handle_attr_attribute(self, name, val, mapping):
        """Handle an individual validator attrs validator."""
        if val is None:
            # No validator.
            return mapping[name]
        elif isinstance(val, _OptionalValidator):
            # This is an Optional[something]
            if name not in mapping or mapping[name] is None:
                return NOTHING
            return self._handle_attr_attribute(name, val.validator, mapping)
        elif isinstance(val, _InstanceOfValidator):
            type_ = val.type
            return self._loads(type_, mapping.get(name))
        else:
            return mapping[name]

    def _loads_generic_meta(self, cl: Type[GenericMeta], obj):
        """Deal with converting a generic.

        From typing, this could be:
        * Sequence
        * MutableSequence
        * List
        * MutableSet
        * Set
        * FrozenSet
        * Mapping
        * MutableMapping
        * Dict
        """
        origin = cl.__origin__ or cl
        if origin is List or origin is Sequence or origin is MutableSequence:
            # Convert to a list. The object must be iterable.
            if not cl.__args__ or cl.__args__[0] is Any:
                return [e for e in obj]
            else:
                return [self._loads(cl.__args__[0], e) for e in obj]
        elif origin is Dict:
            # Convert to a dict. The object must support .items().
            if not cl.__args__ or cl.__args__ == (Any, Any):
                return dict((k, v) for k, v in obj.items())
            else:
                key_type, val_type = cl.__args__
                if key_type is Any:
                    # Skip converting the key.
                    dict((k, self._loads(val_type, v)) for k, v in obj.items())
                elif val_type is Any:
                    # Skip converting the value.
                    dict((self._loads(key_type, k), v) for k, v in obj.items())
                else:
                    return dict((self._loads(key_type, k),
                                 self._loads(val_type, v))
                                for k, v in obj.items())
        elif origin in (Mapping, MutableMapping):
            # Convert to whatever our dict_factory is.
            df = self._dict_factory
            if not cl.__args__:
                return df((k, v) for k, v in obj.items())
            else:
                key_type, val_type = cl.__args__
                return df((self._loads(key_type, k), self._loads(val_type, v))
                          for k, v in obj.items())
        elif origin is MutableSet or origin is Set:
            # Convert to a set.
            if not cl.__args__:
                return set(obj)
            else:
                return set(self._loads(cl.__args__[0], e) for e in obj)
        elif origin is FrozenSet:
            # Convert to a frozenset.
            if not cl.__args__:
                return frozenset(obj)
            else:
                return frozenset(self._loads(cl.__args__[0], e) for e in obj)
        else:
            raise ValueError("Unsupported generic type.")

    def _loads_union(self, union: Type[Union], obj: Union):
        """Deal with converting a union."""
        # Let's support only unions of attr classes for now.
        return self._loads(self._get_dis_func(union)(obj), obj)

    def _loads_identity(self, _, obj):
        """Return the object given, with no conversion. Used for 'Any'."""
        return obj

    def _get_dis_func(self, union: Type[Union]) -> Callable[..., Type]:
        """Fetch or try creating a disambiguation function for a union."""
        if not all(hasattr(e, '__attrs_attrs__')
                   for e in union.__union_params__):
            raise ValueError('Only unions of attr classes supported '
                             'currently.')
        return create_uniq_field_dis_func(*union.__union_params__)
<commit_msg>Save some lookups.<commit_after>from enum import Enum
from functools import lru_cache
from typing import (Callable, List, Mapping, Sequence, Type, Union, UnionMeta,
                    GenericMeta, MutableSequence, TypeVar, Any, FrozenSet,
                    MutableSet, Set, MutableMapping, Dict, AnyMeta)

from attr import NOTHING
from attr.validators import _InstanceOfValidator, _OptionalValidator

from .disambiguators import create_uniq_field_dis_func

try:
    from functools import singledispatch
except ImportError:
    # We use a backport for 3.3.
    from singledispatch import singledispatch

T = TypeVar('T')

class Converter(object):
    """Converts between attrs and Python primitives."""
    def __init__(self, dict_factory=dict):
        # Create a per-instance cache.
        self._get_dis_func = lru_cache()(self._get_dis_func)

        # Per-instance register of to-Python converters.
        dumps = singledispatch(self._dumps)
        dumps.register(Enum, self._dumps_enum)
        dumps.register(str, self._dumps_identity)
        dumps.register(bytes, self._dumps_identity)
        dumps.register(Sequence, self._dumps_seq)
        dumps.register(Mapping, self._dumps_mapping)

        self.dumps = dumps

        # Per-instance register of to-attrs converters.
        # Singledispatch dispatches based on the first argument, so we
        # store the function and switch the arguments in self.loads.
        loads = singledispatch(self._loads)
        loads.register(GenericMeta, self._loads_generic_meta)
        loads.register(UnionMeta, self._loads_union)
        loads.register(AnyMeta, self._loads_identity)

        self._loads = loads
        self._dict_factory = dict_factory

    def register_dumps_hook(self, cls: Type[T], func: Callable[[T], Any]):
        """Register a class-to-primitive converter function for a class.

        The converter function should take an instance of the class and return
        its Python equivalent.
        """
        self.dumps.register(cls, func)

    def register_loads_hook(self, cls: Type[T], func: Callable[[Any], T]):
        """Register a primitive-to-class converter function for a type.

        The converter function should take an instance of a Python primitive
        and return the instance of the class.
        """
        self._loads.register(cls, func)

    def loads(self, obj, cl: Type):
        """Convert unstructured Python data structures to structured data."""
        return self._loads(cl, obj)

    # Classes to Python primitives.

    def _dumps(self, obj):
        """Convert given attrs classes to their primitive equivalents."""
        return (self._dumps_attrs(obj)
                if getattr(obj.__class__, "__attrs_attrs__", None) is not None
                else obj)

    def _dumps_attrs(self, obj):
        """Our version of `attrs.asdict`, so we can call back to us."""
        attrs = obj.__class__.__attrs_attrs__
        rv = self._dict_factory()
        for a in attrs:
            v = getattr(obj, a.name)
            rv[a.name] = self.dumps(v)
        return rv

    def _dumps_enum(self, obj):
        """Convert an enum to its value."""
        return obj.value

    def _dumps_identity(self, obj):
        """Just pass it through."""
        return obj

    def _dumps_seq(self, seq):
        """Convert a sequence to primitive equivalents."""
        # We can reuse the sequence class, so tuples stay tuples.
        return seq.__class__(self.dumps(e) for e in seq)

    def _dumps_mapping(self, mapping: Mapping):
        """Convert a mapping of attr classes to primitive equivalents."""
        # We can reuse the mapping class, so dicts stay dicts and OrderedDicts
        # stay OrderedDicts.
        return mapping.__class__((self.dumps(k), self.dumps(v))
                                 for k, v in mapping.items())

    # Python primitives to classes.

    def _loads(self, cl, obj):
        """Things we can't recognize by isinstance."""
        if hasattr(cl, '__attrs_attrs__'):
            # This is an attrs class
            return self._loads_attrs(cl, obj)
        # We don't know what this is. Just try instantiating it.
        # This covers the basics: bools, ints, floats, strings, bytes, enums.
        return cl(obj)

    def _loads_attrs(self, cl, obj):
        """Handle actual attrs classes."""
        conv_obj = obj.copy()  # Dict of converted parameters.
        for a in cl.__attrs_attrs__:
            name = a.name
            # We detect the type by the validator.
            validator = a.validator
            converted = self._handle_attr_attribute(name, validator, obj)
            if converted is NOTHING:
                continue
            conv_obj[name] = converted

        return cl(**conv_obj)

    def _handle_attr_attribute(self, name, val, mapping):
        """Handle an individual validator attrs validator."""
        if val is None:
            # No validator.
            return mapping[name]
        elif isinstance(val, _OptionalValidator):
            # This is an Optional[something]
            if name not in mapping or mapping[name] is None:
                return NOTHING
            return self._handle_attr_attribute(name, val.validator, mapping)
        elif isinstance(val, _InstanceOfValidator):
            type_ = val.type
            return self._loads(type_, mapping.get(name))
        else:
            return mapping[name]

    def _loads_generic_meta(self, cl: Type[GenericMeta], obj):
        """Deal with converting a generic.

        From typing, this could be:
        * Sequence
        * MutableSequence
        * List
        * MutableSet
        * Set
        * FrozenSet
        * Mapping
        * MutableMapping
        * Dict
        """
        origin = cl.__origin__ or cl
        if origin is List or origin is Sequence or origin is MutableSequence:
            # Convert to a list. The object must be iterable.
            if not cl.__args__ or cl.__args__[0] is Any:
                return [e for e in obj]
            else:
                elem_type = cl.__args__[0]
                return [self._loads(elem_type, e) for e in obj]
        elif origin is Dict:
            # Convert to a dict. The object must support .items().
            if not cl.__args__ or cl.__args__ == (Any, Any):
                return dict((k, v) for k, v in obj.items())
            else:
                key_type, val_type = cl.__args__
                if key_type is Any:
                    # Skip converting the key.
                    dict((k, self._loads(val_type, v)) for k, v in obj.items())
                elif val_type is Any:
                    # Skip converting the value.
                    dict((self._loads(key_type, k), v) for k, v in obj.items())
                else:
                    return dict((self._loads(key_type, k),
                                 self._loads(val_type, v))
                                for k, v in obj.items())
        elif origin in (Mapping, MutableMapping):
            # Convert to whatever our dict_factory is.
            df = self._dict_factory
            if not cl.__args__:
                return df((k, v) for k, v in obj.items())
            else:
                key_type, val_type = cl.__args__
                return df((self._loads(key_type, k), self._loads(val_type, v))
                          for k, v in obj.items())
        elif origin is MutableSet or origin is Set:
            # Convert to a set.
            if not cl.__args__:
                return set(obj)
            else:
                return set(self._loads(cl.__args__[0], e) for e in obj)
        elif origin is FrozenSet:
            # Convert to a frozenset.
            if not cl.__args__:
                return frozenset(obj)
            else:
                return frozenset(self._loads(cl.__args__[0], e) for e in obj)
        else:
            raise ValueError("Unsupported generic type.")

    def _loads_union(self, union: Type[Union], obj: Union):
        """Deal with converting a union."""
        # Let's support only unions of attr classes for now.
        return self._loads(self._get_dis_func(union)(obj), obj)

    def _loads_identity(self, _, obj):
        """Return the object given, with no conversion. Used for 'Any'."""
        return obj

    def _get_dis_func(self, union: Type[Union]) -> Callable[..., Type]:
        """Fetch or try creating a disambiguation function for a union."""
        if not all(hasattr(e, '__attrs_attrs__')
                   for e in union.__union_params__):
            raise ValueError('Only unions of attr classes supported '
                             'currently.')
        return create_uniq_field_dis_func(*union.__union_params__)
<|endoftext|>


<commit_before>import asyncio
from itertools import chain
from threading import Lock
from typing import (
    AbstractSet, Any, Awaitable, Callable, Coroutine, Generator, Iterator, List,
    MutableSet, Optional, TypeVar, Union,
)
from weakref import ReferenceType, WeakSet, ref

from aio_pika.log import get_logger

log = get_logger(__name__)
T = TypeVar("T")

def iscoroutinepartial(fn: Callable[..., Any]) -> bool:
    """
    Function returns True if function is a partial instance of coroutine.
    See additional information here_.

    :param fn: Function
    :return: bool

    .. _here: https://goo.gl/C0S4sQ

    """

    while True:
        parent = fn

        fn = getattr(parent, "func", None)  # type: ignore

        if fn is None:
            break

    return asyncio.iscoroutinefunction(parent)

def _task_done(future: asyncio.Future) -> None:
    exc = future.exception()
    if exc is not None:
        raise exc

def create_task(
    func: Callable[..., Union[Coroutine[Any, Any, T], Awaitable[T]]],
    *args: Any,
    loop: Optional[asyncio.AbstractEventLoop] = None,
    **kwargs: Any
) -> Awaitable[T]:
    loop = loop or asyncio.get_event_loop()

    if iscoroutinepartial(func):
        task = loop.create_task(func(*args, **kwargs))      # type: ignore
        task.add_done_callback(_task_done)
        return task

    def run(future: asyncio.Future) -> Optional[asyncio.Future]:
        if future.done():
            return None

        try:
            future.set_result(func(*args, **kwargs))
        except Exception as e:
            future.set_exception(e)

        return future

    future = loop.create_future()
    future.add_done_callback(_task_done)
    loop.call_soon(run, future)
    return future

CallbackType = Callable[..., Union[T, Awaitable[T]]]
CallbackSetType = AbstractSet[CallbackType]

class StubAwaitable:
    __slots__ = ()

    def __await__(self) -> Generator[Any, Any, None]:
        yield

STUB_AWAITABLE = StubAwaitable()

class CallbackCollection(MutableSet):
    __slots__ = "__sender", "__callbacks", "__weak_callbacks", "__lock"

    def __init__(self, sender: Union[T, ReferenceType]):
        self.__sender: ReferenceType
        if isinstance(sender, ReferenceType):
            self.__sender = sender
        else:
            self.__sender = ref(sender)

        self.__callbacks: CallbackSetType = set()
        self.__weak_callbacks: MutableSet[CallbackType] = WeakSet()
        self.__lock: Lock = Lock()

    def add(self, callback: CallbackType, weak: bool = False) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection frozen")
        if not callable(callback):
            raise ValueError("Callback is not callable")

        with self.__lock:
            if weak:
                self.__weak_callbacks.add(callback)
            else:
                self.__callbacks.add(callback)      # type: ignore

    def discard(self, callback: CallbackType) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection frozen")

        with self.__lock:
            if callback in self.__callbacks:
                self.__callbacks.remove(callback)    # type: ignore
            elif callback in self.__weak_callbacks:
                self.__weak_callbacks.remove(callback)

    def clear(self) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection frozen")

        with self.__lock:
            self.__callbacks.clear()        # type: ignore
            self.__weak_callbacks.clear()

    @property
    def is_frozen(self) -> bool:
        return isinstance(self.__callbacks, frozenset)

    def freeze(self) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection already frozen")

        with self.__lock:
            self.__callbacks = frozenset(self.__callbacks)
            self.__weak_callbacks = WeakSet(self.__weak_callbacks)

    def unfreeze(self) -> None:
        if not self.is_frozen:
            raise RuntimeError("Collection is not frozen")

        with self.__lock:
            self.__callbacks = set(self.__callbacks)
            self.__weak_callbacks = WeakSet(self.__weak_callbacks)

    def __contains__(self, x: object) -> bool:
        return x in self.__callbacks or x in self.__weak_callbacks

    def __len__(self) -> int:
        return len(self.__callbacks) + len(self.__weak_callbacks)

    def __iter__(self) -> Iterator[CallbackType]:
        return iter(chain(self.__callbacks, self.__weak_callbacks))

    def __bool__(self) -> bool:
        return bool(self.__callbacks) or bool(self.__weak_callbacks)

    def __copy__(self) -> "CallbackCollection":
        instance = self.__class__(self.__sender)

        with self.__lock:
            for cb in self.__callbacks:
                instance.add(cb, weak=False)

            for cb in self.__weak_callbacks:
                instance.add(cb, weak=True)

        if self.is_frozen:
            instance.freeze()

        return instance

    def __call__(self, *args: Any, **kwargs: Any) -> Awaitable[Any]:
        futures: List[asyncio.Future] = []

        with self.__lock:
            sender = self.__sender()

            for cb in self:
                try:
                    result = cb(sender, *args, **kwargs)
                    if hasattr(result, "__await__"):
                        futures.append(asyncio.ensure_future(result))
                except Exception:
                    log.exception("Callback %r error", cb)

        if not futures:
            return STUB_AWAITABLE
        return asyncio.gather(*futures, return_exceptions=True)

    def __hash__(self) -> int:
        return id(self)

class OneShotCallback:
    __slots__ = ("loop", "finished", "__lock", "callback", "__task")

    def __init__(self, callback: Callable[..., Awaitable[T]]):
        self.callback: Callable[..., Awaitable[T]] = callback
        self.loop = asyncio.get_event_loop()
        self.finished: asyncio.Event = asyncio.Event()
        self.__lock: asyncio.Lock = asyncio.Lock()
        self.__task: asyncio.Future

    def __repr__(self) -> str:
        return f"<{self.__class__.__name__}: cb={self.callback!r}>"

    def wait(self) -> Awaitable[Any]:
        try:
            return self.finished.wait()
        except asyncio.CancelledError:
            self.__task.cancel()
            raise

    async def __task_inner(self, *args: Any, **kwargs: Any) -> None:
        async with self.__lock:
            if self.finished.is_set():
                return

            try:
                await self.callback(*args, **kwargs)
            finally:
                self.finished.set()
                del self.callback

    def __call__(self, *args: Any, **kwargs: Any) -> Awaitable[Any]:
        if self.finished.is_set():
            return STUB_AWAITABLE
        self.__task = self.loop.create_task(
            self.__task_inner(*args, **kwargs),
        )
        return self.__task

__all__ = (
    "CallbackCollection",
    "CallbackType",
    "CallbackSetType",
    "OneShotCallback",
    "create_task",
    "iscoroutinepartial",
)
<commit_msg>Fix linter error<commit_after>import asyncio
from itertools import chain
from threading import Lock
from typing import (
    AbstractSet, Any, Awaitable, Callable, Coroutine, Generator, Iterator, List,
    MutableSet, Optional, TypeVar, Union,
)
from weakref import ReferenceType, WeakSet, ref

from aio_pika.log import get_logger

log = get_logger(__name__)
T = TypeVar("T")

def iscoroutinepartial(fn: Callable[..., Any]) -> bool:
    """
    Function returns True if function is a partial instance of coroutine.
    See additional information here_.

    :param fn: Function
    :return: bool

    .. _here: https://goo.gl/C0S4sQ

    """

    while True:
        parent = fn

        fn = getattr(parent, "func", None)  # type: ignore

        if fn is None:
            break

    return asyncio.iscoroutinefunction(parent)

def _task_done(future: asyncio.Future) -> None:
    exc = future.exception()
    if exc is not None:
        raise exc

def create_task(
    func: Callable[..., Union[Coroutine[Any, Any, T], Awaitable[T]]],
    *args: Any,
    loop: Optional[asyncio.AbstractEventLoop] = None,
    **kwargs: Any
) -> Awaitable[T]:
    loop = loop or asyncio.get_event_loop()

    if iscoroutinepartial(func):
        task = loop.create_task(func(*args, **kwargs))      # type: ignore
        task.add_done_callback(_task_done)
        return task

    def run(future: asyncio.Future) -> Optional[asyncio.Future]:
        if future.done():
            return None

        try:
            future.set_result(func(*args, **kwargs))
        except Exception as e:
            future.set_exception(e)

        return future

    future = loop.create_future()
    future.add_done_callback(_task_done)
    loop.call_soon(run, future)
    return future

CallbackType = Callable[..., Union[T, Awaitable[T]]]
CallbackSetType = AbstractSet[CallbackType]

class StubAwaitable:
    __slots__ = ()

    def __await__(self) -> Generator[Any, Any, None]:
        yield

STUB_AWAITABLE = StubAwaitable()

class CallbackCollection(MutableSet):
    __slots__ = "__sender", "__callbacks", "__weak_callbacks", "__lock"

    def __init__(self, sender: Union[T, ReferenceType]):
        self.__sender: ReferenceType
        if isinstance(sender, ReferenceType):
            self.__sender = sender
        else:
            self.__sender = ref(sender)

        self.__callbacks: CallbackSetType = set()
        self.__weak_callbacks: MutableSet[CallbackType] = WeakSet()
        self.__lock: Lock = Lock()

    def add(self, callback: CallbackType, weak: bool = False) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection frozen")
        if not callable(callback):
            raise ValueError("Callback is not callable")

        with self.__lock:
            if weak:
                self.__weak_callbacks.add(callback)
            else:
                self.__callbacks.add(callback)      # type: ignore

    def discard(self, callback: CallbackType) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection frozen")

        with self.__lock:
            if callback in self.__callbacks:
                self.__callbacks.remove(callback)    # type: ignore
            elif callback in self.__weak_callbacks:
                self.__weak_callbacks.remove(callback)

    def clear(self) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection frozen")

        with self.__lock:
            self.__callbacks.clear()        # type: ignore
            self.__weak_callbacks.clear()

    @property
    def is_frozen(self) -> bool:
        return isinstance(self.__callbacks, frozenset)

    def freeze(self) -> None:
        if self.is_frozen:
            raise RuntimeError("Collection already frozen")

        with self.__lock:
            self.__callbacks = frozenset(self.__callbacks)
            self.__weak_callbacks = WeakSet(self.__weak_callbacks)

    def unfreeze(self) -> None:
        if not self.is_frozen:
            raise RuntimeError("Collection is not frozen")

        with self.__lock:
            self.__callbacks = set(self.__callbacks)
            self.__weak_callbacks = WeakSet(self.__weak_callbacks)

    def __contains__(self, x: object) -> bool:
        return x in self.__callbacks or x in self.__weak_callbacks

    def __len__(self) -> int:
        return len(self.__callbacks) + len(self.__weak_callbacks)

    def __iter__(self) -> Iterator[CallbackType]:
        return iter(chain(self.__callbacks, self.__weak_callbacks))

    def __bool__(self) -> bool:
        return bool(self.__callbacks) or bool(self.__weak_callbacks)

    def __copy__(self) -> "CallbackCollection":
        instance = self.__class__(self.__sender)

        with self.__lock:
            for cb in self.__callbacks:
                instance.add(cb, weak=False)

            for cb in self.__weak_callbacks:
                instance.add(cb, weak=True)

        if self.is_frozen:
            instance.freeze()

        return instance

    def __call__(self, *args: Any, **kwargs: Any) -> Awaitable[Any]:
        futures: List[asyncio.Future] = []

        with self.__lock:
            sender = self.__sender()

            for cb in self:
                try:
                    result = cb(sender, *args, **kwargs)
                    if hasattr(result, "__await__"):
                        futures.append(asyncio.ensure_future(result))
                except Exception:
                    log.exception("Callback %r error", cb)

        if not futures:
            return STUB_AWAITABLE
        return asyncio.gather(*futures, return_exceptions=True)

    def __hash__(self) -> int:
        return id(self)

class OneShotCallback:
    __slots__ = ("loop", "finished", "__lock", "callback", "__task")

    def __init__(self, callback: Callable[..., Awaitable[T]]):
        self.callback: Callable[..., Awaitable[T]] = callback
        self.loop = asyncio.get_event_loop()
        self.finished: asyncio.Event = asyncio.Event()
        self.__lock: asyncio.Lock = asyncio.Lock()
        self.__task: asyncio.Future

    def __repr__(self) -> str:
        return f"<{self.__class__.__name__}: cb={self.callback!r}>"

    def wait(self) -> Awaitable[Any]:
        try:
            return self.finished.wait()
        except asyncio.CancelledError:
            self.__task.cancel()
            raise

    async def __task_inner(self, *args: Any, **kwargs: Any) -> None:
        async with self.__lock:
            if self.finished.is_set():
                return

            try:
                await self.callback(*args, **kwargs)
            finally:
                self.finished.set()
                del self.callback

    def __call__(self, *args: Any, **kwargs: Any) -> Awaitable[Any]:
        if self.finished.is_set():
            return STUB_AWAITABLE
        self.__task = self.loop.create_task(
            self.__task_inner(*args, **kwargs),
        )
        return self.__task

__all__ = (
    "CallbackCollection",
    "CallbackType",
    "CallbackSetType",
    "OneShotCallback",
    "create_task",
    "iscoroutinepartial",
)
<|endoftext|>


<commit_before>import unittest
import numpy as np
from netCDF4 import Dataset

from desc.backend import put
from desc.vmec import VMECIO
from desc.basis import FourierZernikeBasis

class TestVMECIO(unittest.TestCase):
    """Tests VMECIO class"""

    def test_ptolemy_identity_fwd(self):
        """Tests forward implementation of Ptolemy's identity."""
        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        m_0 = np.array([0, 0, 1, 1, 1])
        n_0 = np.array([0, 1, -1, 0, 1])
        s = np.array([0, a0, a1, a3, 0])
        c = np.array([a0, 0, a2, 0, a3])

        # a0*sin(-z) + a1*sin(t+z) + a3*sin(t) = -a0*sin(z) + a1*sin(t)*cos(z) + a1*cos(t)*sin(z) + a3*sin(t)
        # a0 + a2*cos(t+z) + a3*cos(t-z) = a0 + (a2+a3)*cos(t)*cos(z) + (a3-a2)*sin(t)*sin(z)

        m_1_correct = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n_1_correct = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x_correct = np.array([[a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3]])

        m_1, n_1, x = VMECIO._ptolemy_identity_fwd(m_0, n_0, s, c)

        np.testing.assert_allclose(m_1, m_1_correct, atol=1e-8)
        np.testing.assert_allclose(n_1, n_1_correct, atol=1e-8)
        np.testing.assert_allclose(x, x_correct, atol=1e-8)

    def test_ptolemy_identity_rev(self):
        """Tests reverse implementation of Ptolemy's identity."""
        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        m_1 = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n_1 = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x = np.array([[a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3]])

        # -a0*sin(z) + a1*sin(t)*cos(z) + a1*cos(t)*sin(z) + a3*sin(t) = a0*sin(-z) + a1*sin(t+z) + a3*sin(t)
        # a0 + (a2+a3)*cos(t)*cos(z) + (a3-a2)*sin(t)*sin(z) = a0 + a2*cos(t+z) + a3*cos(t-z)

        m_0_correct = np.array([0, 0, 1, 1, 1])
        n_0_correct = np.array([0, 1, -1, 0, 1])
        s_correct = np.array([[0, a0, a1, a3, 0]])
        c_correct = np.array([[a0, 0, a2, 0, a3]])

        m_0, n_0, s, c = VMECIO._ptolemy_identity_rev(m_1, n_1, x)

        np.testing.assert_allclose(m_0, m_0_correct, atol=1e-8)
        np.testing.assert_allclose(n_0, n_0_correct, atol=1e-8)
        np.testing.assert_allclose(s, s_correct, atol=1e-8)
        np.testing.assert_allclose(c, c_correct, atol=1e-8)

    def test_fourier_to_zernike(self):
        """Tests conversion from radial-Fourier series to Fourier-Zernike polynomials."""
        M = 1
        N = 1

        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        surfs = 16
        rho = np.sqrt(np.linspace(0, 1, surfs))

        m = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x = np.array([a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3])

        # x * rho^|m|
        x_mn = np.power(np.atleast_2d(rho).T, np.atleast_2d(np.abs(m))) * np.atleast_2d(
            x
        )
        basis = FourierZernikeBasis(M=M, N=N)
        x_lmn = VMECIO._fourier_to_zernike(m, n, x_mn, basis)

        x_lmn_correct = np.zeros((basis.num_modes,))
        for k in range(basis.num_modes):
            idx = np.where(
                np.logical_and.reduce(
                    (
                        basis.modes[:, 0] == np.abs(m[k]),
                        basis.modes[:, 1] == m[k],
                        basis.modes[:, 2] == n[k],
                    )
                )
            )[0]
            x_lmn_correct = put(x_lmn_correct, idx, x[k])

        np.testing.assert_allclose(x_lmn, x_lmn_correct, atol=1e-8)

    def test_zernike_to_fourier(self):
        """Tests conversion from Fourier-Zernike polynomials to radial-Fourier series."""
        M = 1
        N = 1

        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        surfs = 16
        rho = np.sqrt(np.linspace(0, 1, surfs))

        m_correct = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n_correct = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x = np.array([a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3])

        # x * rho^|m|
        x_mn_correct = np.power(
            np.atleast_2d(rho).T, np.atleast_2d(np.abs(m_correct))
        ) * np.atleast_2d(x)
        basis = FourierZernikeBasis(M=M, N=N)

        x_lmn = np.zeros((basis.num_modes,))
        for k in range(basis.num_modes):
            idx = np.where(
                np.logical_and.reduce(
                    (
                        basis.modes[:, 0] == np.abs(m_correct[k]),
                        basis.modes[:, 1] == m_correct[k],
                        basis.modes[:, 2] == n_correct[k],
                    )
                )
            )[0]
            x_lmn = put(x_lmn, idx, x[k])

        m, n, x_mn = VMECIO._zernike_to_fourier(x_lmn, basis, rho)

        np.testing.assert_allclose(m, m_correct, atol=1e-8)
        np.testing.assert_allclose(n, n_correct, atol=1e-8)
        np.testing.assert_allclose(x_mn, x_mn_correct, atol=1e-8)

def test_load_then_save(TmpDir):
    """Tests if loading and then saving gives the original result."""

    input_path = "examples//VMEC//wout_SOLOVEV.nc"
    output_path = str(TmpDir.join("DESC_SOLOVEV.nc"))

    eq = VMECIO.load(input_path)
    VMECIO.save(eq, output_path)

    file1 = Dataset(input_path, mode="r")
    file2 = Dataset(output_path, mode="r")

    rmnc1 = file1.variables["rmnc"][:]
    rmnc2 = file2.variables["rmnc"][:]
    zmns1 = file1.variables["zmns"][:]
    zmns2 = file2.variables["zmns"][:]
    lmns1 = file1.variables["lmns"][:]
    lmns2 = file2.variables["lmns"][:]

    np.testing.assert_allclose(rmnc2, rmnc1, atol=1e-1)
    np.testing.assert_allclose(zmns2, zmns1, atol=1e-1)
    np.testing.assert_allclose(lmns2, lmns1, atol=1e-1)

    file1.close
    file2.close
<commit_msg>forgot to add test_vmec.py to the last commit<commit_after>import unittest
import numpy as np
from netCDF4 import Dataset

from desc.backend import put
from desc.vmec import VMECIO
from desc.basis import FourierZernikeBasis

class TestVMECIO(unittest.TestCase):
    """Tests VMECIO class"""

    def test_ptolemy_identity_fwd(self):
        """Tests forward implementation of Ptolemy's identity."""
        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        m_0 = np.array([0, 0, 1, 1, 1])
        n_0 = np.array([0, 1, -1, 0, 1])
        s = np.array([0, a0, a1, a3, 0])
        c = np.array([a0, 0, a2, 0, a3])

        # a0*sin(-z) + a1*sin(t+z) + a3*sin(t) = -a0*sin(z) + a1*sin(t)*cos(z) + a1*cos(t)*sin(z) + a3*sin(t)
        # a0 + a2*cos(t+z) + a3*cos(t-z) = a0 + (a2+a3)*cos(t)*cos(z) + (a3-a2)*sin(t)*sin(z)

        m_1_correct = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n_1_correct = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x_correct = np.array([[a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3]])

        m_1, n_1, x = VMECIO._ptolemy_identity_fwd(m_0, n_0, s, c)

        np.testing.assert_allclose(m_1, m_1_correct, atol=1e-8)
        np.testing.assert_allclose(n_1, n_1_correct, atol=1e-8)
        np.testing.assert_allclose(x, x_correct, atol=1e-8)

    def test_ptolemy_identity_rev(self):
        """Tests reverse implementation of Ptolemy's identity."""
        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        m_1 = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n_1 = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x = np.array([[a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3]])

        # -a0*sin(z) + a1*sin(t)*cos(z) + a1*cos(t)*sin(z) + a3*sin(t) = a0*sin(-z) + a1*sin(t+z) + a3*sin(t)
        # a0 + (a2+a3)*cos(t)*cos(z) + (a3-a2)*sin(t)*sin(z) = a0 + a2*cos(t+z) + a3*cos(t-z)

        m_0_correct = np.array([0, 0, 1, 1, 1])
        n_0_correct = np.array([0, 1, -1, 0, 1])
        s_correct = np.array([[0, a0, a1, a3, 0]])
        c_correct = np.array([[a0, 0, a2, 0, a3]])

        m_0, n_0, s, c = VMECIO._ptolemy_identity_rev(m_1, n_1, x)

        np.testing.assert_allclose(m_0, m_0_correct, atol=1e-8)
        np.testing.assert_allclose(n_0, n_0_correct, atol=1e-8)
        np.testing.assert_allclose(s, s_correct, atol=1e-8)
        np.testing.assert_allclose(c, c_correct, atol=1e-8)

    def test_fourier_to_zernike(self):
        """Tests conversion from radial-Fourier series to Fourier-Zernike polynomials."""
        M = 1
        N = 1

        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        surfs = 16
        rho = np.sqrt(np.linspace(0, 1, surfs))

        m = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x = np.array([a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3])

        # x * rho^|m|
        x_mn = np.power(np.atleast_2d(rho).T, np.atleast_2d(np.abs(m))) * np.atleast_2d(
            x
        )
        basis = FourierZernikeBasis(M=M, N=N)
        x_lmn = VMECIO._fourier_to_zernike(m, n, x_mn, basis)

        x_lmn_correct = np.zeros((basis.num_modes,))
        for k in range(basis.num_modes):
            idx = np.where((basis.modes == [np.abs(m[k]), m[k], n[k]]).all(axis=1))[0]
            x_lmn_correct[idx] = x[k]

        np.testing.assert_allclose(x_lmn, x_lmn_correct, atol=1e-8)

    def test_zernike_to_fourier(self):
        """Tests conversion from Fourier-Zernike polynomials to radial-Fourier series."""
        M = 1
        N = 1

        a0 = 3
        a1 = -1
        a2 = 1
        a3 = 2

        surfs = 16
        rho = np.sqrt(np.linspace(0, 1, surfs))

        m_correct = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1])
        n_correct = np.array([-1, 0, 1, -1, 0, 1, -1, 0, 1])
        x = np.array([a3 - a2, a3, a1, -a0, a0, 0, a1, 0, a2 + a3])

        # x * rho^|m|
        x_mn_correct = np.power(
            np.atleast_2d(rho).T, np.atleast_2d(np.abs(m_correct))
        ) * np.atleast_2d(x)
        basis = FourierZernikeBasis(M=M, N=N)

        x_lmn = np.zeros((basis.num_modes,))
        for k in range(basis.num_modes):
            idx = np.where(
                (basis.modes == [np.abs(m_correct[k]), m_correct[k], n_correct[k]]).all(
                    axis=1
                )
            )[0]
            x_lmn[idx] = x[k]

        m, n, x_mn = VMECIO._zernike_to_fourier(x_lmn, basis, rho)

        np.testing.assert_allclose(m, m_correct, atol=1e-8)
        np.testing.assert_allclose(n, n_correct, atol=1e-8)
        np.testing.assert_allclose(x_mn, x_mn_correct, atol=1e-8)

def test_load_then_save(TmpDir):
    """Tests if loading and then saving gives the original result."""

    input_path = "examples//VMEC//wout_SOLOVEV.nc"
    output_path = str(TmpDir.join("DESC_SOLOVEV.nc"))

    eq = VMECIO.load(input_path)
    VMECIO.save(eq, output_path)

    file1 = Dataset(input_path, mode="r")
    file2 = Dataset(output_path, mode="r")

    rmnc1 = file1.variables["rmnc"][:]
    rmnc2 = file2.variables["rmnc"][:]
    zmns1 = file1.variables["zmns"][:]
    zmns2 = file2.variables["zmns"][:]
    lmns1 = file1.variables["lmns"][:]
    lmns2 = file2.variables["lmns"][:]

    np.testing.assert_allclose(rmnc2, rmnc1, atol=1e-1)
    np.testing.assert_allclose(zmns2, zmns1, atol=1e-1)
    np.testing.assert_allclose(lmns2, lmns1, atol=1e-1)

    file1.close
    file2.close
<|endoftext|>


<commit_before>import os
import glob

def add_makefile_entry(subdir1):
    test_target = "";
    for filename in sorted(glob.glob("tests/" + subdir1 + "/*.py")):
        lShortName = os.path.basename(filename);
        if(not lShortName.lower().startswith("gen_all") and
           not lShortName.lower().startswith("gen_makefile") and
           not "prototyp" in lShortName.lower()):
            bn = subdir1 + "/" + lShortName;
            logfile = bn.replace("/" , "_");
            logname = logfile.replace(".py" , ".log");
            logfile = "logs/" + logname;
            reflogfile = "tests/references/" + logname;
            difffile = logfile + ".diff"
            # print("#PROCESSING FILE : " , filename, bn , logfile);

            print(bn , " : " , "\n\t", "$(PYTHON) " , filename , " > " , logfile, " 2>&1");
            print("\t", "$(PYTHON) scripts/num_diff.py " , reflogfile , logfile, " > " , difffile);
            print("\t", "tail -10 " ,  difffile, "\n");

            test_target = bn + " " + test_target;

    return test_target;

str1 = "artificial basic_checks bugs exog expsmooth HeartRateTimeSeries heroku hierarchical model_control perf svr transformations func neuralnet real-life  time_res perfs demos xgb";
subdirs = str1.split();

print("PYTHON=python3\n\n");

for subdir1 in sorted(subdirs):
    test_target = add_makefile_entry(subdir1)
    if(subdir1 == "bugs"):
        bugdirs = glob.glob("tests/bugs/*")
        bugdirs1 = [dir1.replace("tests/" , "") for dir1 in bugdirs]
        for dir1 in bugdirs1:
            test_target = test_target + add_makefile_entry(dir1)
    print("\n\n", subdir1, ": ", test_target, "\n" , "\n");

print("\n# ********************************************** \n");

print("all: " , str1 , "\n\t\n");

str2 = "demos basic_checks exog heroku hierarchical model_control svr transformations func real-life  time_res perfs xgb";

print("build-test : " , str2 , "\n\t\n");
<commit_msg>Updated Makefile to include cross validation tests<commit_after>import os
import glob

def add_makefile_entry(subdir1):
    test_target = "";
    for filename in sorted(glob.glob("tests/" + subdir1 + "/*.py")):
        lShortName = os.path.basename(filename);
        if(not lShortName.lower().startswith("gen_all") and
           not lShortName.lower().startswith("gen_makefile") and
           not "prototyp" in lShortName.lower()):
            bn = subdir1 + "/" + lShortName;
            logfile = bn.replace("/" , "_");
            logname = logfile.replace(".py" , ".log");
            logfile = "logs/" + logname;
            reflogfile = "tests/references/" + logname;
            difffile = logfile + ".diff"
            # print("#PROCESSING FILE : " , filename, bn , logfile);

            print(bn , " : " , "\n\t", "$(PYTHON) " , filename , " > " , logfile, " 2>&1");
            print("\t", "$(PYTHON) scripts/num_diff.py " , reflogfile , logfile, " > " , difffile);
            print("\t", "tail -10 " ,  difffile, "\n");

            test_target = bn + " " + test_target;

    return test_target;

str1 = "artificial basic_checks bugs cross_validation exog expsmooth func HeartRateTimeSeries heroku hierarchical model_control perf svr transformations  neuralnet real-life  time_res perfs demos xgb";
subdirs = str1.split();

print("PYTHON=python3\n\n");

for subdir1 in sorted(subdirs):
    test_target = add_makefile_entry(subdir1)
    if(subdir1 == "bugs"):
        bugdirs = glob.glob("tests/bugs/*")
        bugdirs1 = [dir1.replace("tests/" , "") for dir1 in bugdirs]
        for dir1 in bugdirs1:
            test_target = test_target + add_makefile_entry(dir1)
    print("\n\n", subdir1, ": ", test_target, "\n" , "\n");

print("\n# ********************************************** \n");

print("all: " , str1 , "\n\t\n");

str2 = "demos basic_checks cross_validation exog heroku hierarchical model_control perfs svr transformations func real-life  time_res xgb ";

print("build-test : " , str2 , "\n\t\n");
<|endoftext|>