<commit_before>"""Wrapper for the Python StanfordNLP package ``stanfordnlp``.
About: https://github.com/stanfordnlp/stanfordnlp.
"""

import os
from typing import Dict, Optional

import stanfordnlp  # type: ignore
from cltkv1.utils import UnknownLanguageError, file_exists, suppress_stdout

class StanfordNLPWrapper:
    """CLTK's wrapper for the StanfordNLP project."""

    def __init__(self, language: str, treebank: Optional[str] = None) -> None:
        """Constructor for ``stanford`` wrapper class.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> isinstance(stanford_wrapper, StanfordNLPWrapper)
        True
        >>> stanford_wrapper.language
        'greek'
        >>> stanford_wrapper.treebank
        'grc_proiel'

        >>> stanford_wrapper_perseus = StanfordNLPWrapper(language='greek', treebank='grc_perseus')
        >>> isinstance(stanford_wrapper_perseus, StanfordNLPWrapper)
        True
        >>> stanford_wrapper_perseus.language
        'greek'
        >>> stanford_wrapper_perseus.treebank
        'grc_perseus'

        >>> xen_anab = "Δαρείου καὶ Παρυσάτιδος γίγνονται παῖδες δύο, πρεσβύτερος μὲν Ἀρταξέρξης, νεώτερος δὲ Κῦρος: ἐπεὶ δὲ ἠσθένει Δαρεῖος καὶ ὑπώπτευε τελευτὴν τοῦ βίου, ἐβούλετο τὼ παῖδε ἀμφοτέρω παρεῖναι."
        >>> xen_anab_nlp = stanford_wrapper.parse(xen_anab)

        >>> stanford_nlp_obj_bad = StanfordNLPWrapper(language='BADLANG')
        Traceback (most recent call last):
          ...
        cltkv1.utils.exceptions.UnknownLanguageError: Language 'BADLANG' either not in scope for CLTK or not supported by StanfordNLP.
        """
        self.language = language
        self.treebank = treebank

        # Setup language
        self.map_langs_cltk_stanford = dict(
            greek="Ancient_Greek",
            latin="Latin",
            old_church_slavonic="Old_Church_Slavonic",
            old_french="Old_French",
        )

        self.wrapper_available = self.is_wrapper_available()  # type: bool
        if not self.wrapper_available:
            raise UnknownLanguageError(
                "Language '{}' either not in scope for CLTK or not supported by StanfordNLP.".format(
                    self.language
                )
            )
        self.stanford_code = self._get_stanford_code()

        # Setup optional treebank if specified
        self.map_code_treebanks = dict(
            grc=["grc_proiel", "grc_perseus"], la=["la_perseus", "la_proiel"]
        )
        # if not specified, will use the default treebank of stanfordnlp
        if self.treebank:
            valid_treebank = self._is_valid_treebank()
            if not valid_treebank:
                raise UnknownLanguageError(
                    "Invalid treebank '{0}' for language '{1}'.".format(
                        self.treebank, self.language
                    )
                )
        else:
            self.treebank = self._get_default_treebank()

        # check if model present
        # this fp is just to confirm that some model has already been downloaded. This is a weak check for the models actually being downloaded and valid
        self.model_path = os.path.expanduser(
            "~/stanfordnlp_resources/{0}_models/{0}_tokenizer.pt".format(self.treebank)
        )
        if not self._is_model_present():
            # download model if necessary
            self._download_model()

        # instantiate actual stanfordnlp class
        # Note: `suppress_stdout` is used to prevent `stanfordnlp`
        # from printing a long log of its parameters to screen.
        # Though we should capture these, within `_load_pipeline()`,
        # for the log file.
        with suppress_stdout():
            self.nlp = self._load_pipeline()

    def parse(self, text: str):
        """Run all ``stanfordnlp`` parsing on input text.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> xen_anab = "Δαρείου καὶ Παρυσάτιδος γίγνονται παῖδες δύο, πρεσβύτερος μὲν Ἀρταξέρξης, νεώτερος δὲ Κῦρος: ἐπεὶ δὲ ἠσθένει Δαρεῖος καὶ ὑπώπτευε τελευτὴν τοῦ βίου, ἐβούλετο τὼ παῖδε ἀμφοτέρω παρεῖναι."
        >>> xen_anab_nlp = stanford_wrapper.parse(xen_anab)
        >>> isinstance(xen_anab_nlp, stanfordnlp.pipeline.doc.Document)
        True

        >>> nlp_xen_anab_first_sent = xen_anab_nlp.sentences[0]
        >>> nlp_xen_anab_first_sent.tokens[0].index
        '1'
        >>> nlp_xen_anab_first_sent.tokens[0].text
        'Δαρείου'
        >>> first_word = nlp_xen_anab_first_sent.tokens[0].words[0]
        >>> first_word.dependency_relation  # was 'iobj' did the model change?
        'nmod'
        >>> first_word.feats
        'Case=Gen|Gender=Masc|Number=Sing'
        >>> first_word.governor
        4
        >>> first_word.index
        '1'
        >>> first_word.lemma
        'Δαρεῖος'
        >>> first_word.pos
        'Ne'
        >>> first_word.text
        'Δαρείου'
        >>> first_word.upos
        'PROPN'
        >>> first_word.xpos
        'Ne'
        """
        parsed_text = self.nlp(text)
        return parsed_text

    def _load_pipeline(self):
        """Instantiate `stanfordnlp.Pipeline()`.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> with suppress_stdout():    nlp_obj = stanford_wrapper._load_pipeline()
        >>> isinstance(nlp_obj, stanfordnlp.pipeline.core.Pipeline)
        True
        """
        models_dir = os.path.expanduser("~/stanfordnlp_resources/")
        nlp = stanfordnlp.Pipeline(
            processors="tokenize,mwt,pos,lemma,depparse",  # these are the default processors
            lang=self.stanford_code,
            models_dir=models_dir,
            treebank=self.treebank,
            use_gpu=True,  # default, won't fail if GPU not present
        )
        return nlp

    def _is_model_present(self) -> bool:
        """Checks if the model is already downloaded.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper._is_model_present()
        True
        """
        if file_exists(self.model_path):
            return True
        else:
            return False

    def _download_model(self):
        """Interface with the `stanfordnlp` model downloader.

        TODO: Figure out why doctests here hang. Presumably because waiting for user input, but prompt shouldn't arise if models already present.

        # >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        # >>> stanford_wrapper._download_model()
        # True
        """
        # prompt user to DL the stanford models
        print("")
        print("")
        print("Α" * 80)
        print("")
        print(
            "CLTK message: The part of the CLTK that you are using depends upon the Stanford NLP library (`stanfordnlp`). What follows are several question prompts coming from it. (More at: <https://github.com/stanfordnlp/stanfordnlp>.) Answer with defaults."
        )
        print("")
        print("Ω" * 80)
        print("")
        print("")
        stanfordnlp.download(self.treebank)
        # if file model still not available after attempted DL, then raise error
        if not file_exists(self.model_path):
            raise FileNotFoundError(
                "Missing required models for `stanfordnlp` at `{0}`.".format(
                    self.model_path
                )
            )
        pass

    def _get_default_treebank(self) -> str:
        """Return name of a language's default treebank if none
        supplied.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper._get_default_treebank()
        'grc_proiel'
        """
        stanford_default_treebanks = (
            stanfordnlp.utils.resources.default_treebanks
        )  # type: Dict[str, str]
        return stanford_default_treebanks[self.stanford_code]

    def _is_valid_treebank(self) -> bool:
        """Check whether for chosen language, optional
        treebank value is valid.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek', treebank='grc_proiel')
        >>> stanford_wrapper._is_valid_treebank()
        True
        """
        possible_treebanks = self.map_code_treebanks[self.stanford_code]
        if self.treebank in possible_treebanks:
            return True
        else:
            return False

    def is_wrapper_available(self) -> bool:
        """Maps CLTK's internal language term (e.g., ``latin``) to
        that used by ``stanfordnlp`` (``la``); confirms that this is
        a language the CLTK supports (i.e., is it pre-modern or not).

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper.is_wrapper_available()
        True
        """
        if self.language in self.map_langs_cltk_stanford:
            return True
        else:
            return False

    def _get_stanford_code(self) -> str:
        """Using known-supported language, use the CLTK's
        internal code to look up the code used by StanfordNLP.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper._get_stanford_code()
        'grc'
        """
        try:
            stanford_lang_name = self.map_langs_cltk_stanford[self.language]
        except KeyError:
            raise KeyError
        stanford_lang_code = (
            stanfordnlp.models.common.constant.lang2lcode
        )  # type: Dict[str, str]
        try:
            return stanford_lang_code[stanford_lang_name]
        except KeyError:
            raise KeyError

if __name__ == "__main__":

    stanford_nlp_obj = StanfordNLPWrapper(language="latin")
    print(stanford_nlp_obj.language == "latin")

    stanford_nlp_obj = StanfordNLPWrapper(language="greek", treebank="grc_perseus")
    print(stanford_nlp_obj.language == "greek")
    print(stanford_nlp_obj.treebank == "grc_perseus")
    print(stanford_nlp_obj.wrapper_available == True)

    stanford_nlp_obj = StanfordNLPWrapper(language="greek")
    print(stanford_nlp_obj.language == "greek")
    print(stanford_nlp_obj.treebank == "grc_proiel")
    print(stanford_nlp_obj.wrapper_available == True)
    fp_model = stanford_nlp_obj.model_path
    print(os.path.split(fp_model)[1] == "grc_proiel_tokenizer.pt")
    print(isinstance(stanford_nlp_obj.nlp, stanfordnlp.pipeline.core.Pipeline))

    xen_anab = "Δαρείου καὶ Παρυσάτιδος γίγνονται παῖδες δύο, πρεσβύτερος μὲν Ἀρταξέρξης, νεώτερος δὲ Κῦρος: ἐπεὶ δὲ ἠσθένει Δαρεῖος καὶ ὑπώπτευε τελευτὴν τοῦ βίου, ἐβούλετο τὼ παῖδε ἀμφοτέρω παρεῖναι."
    xen_anab_nlp = stanford_nlp_obj.parse(xen_anab)

    nlp_xen_anab_first_sent = xen_anab_nlp.sentences[0]
    # print(dir(nlp_xen_anab_first_sent))  # build_dependencies', 'dependencies', 'print_dependencies', 'print_tokens', 'print_words', 'tokens', 'words'
    print(nlp_xen_anab_first_sent.tokens[0].index == "1")
    print(nlp_xen_anab_first_sent.tokens[0].text == "Δαρείου")
    first_word = nlp_xen_anab_first_sent.tokens[0].words[
        0
    ]  # 'dependency_relation', 'feats', 'governor', 'index', 'lemma', 'parent_token', 'pos', 'text', 'upos', 'xpos'
    print(first_word.dependency_relation == "iobj")
    print(first_word.feats == "Case=Gen|Gender=Masc|Number=Sing")
    print(first_word.governor == 4)
    print(first_word.index == "1")
    print(first_word.lemma == "Δαρεῖος")
    print(first_word.pos == "Ne")
    print(first_word.text == "Δαρείου")
    print(first_word.upos == "PROPN")
    print(first_word.xpos == "Ne")
    # print(first_word.parent_token)  # <Token index=1;words=[<Word index=1;text=Δαρείου;lemma=Δαρεῖος;upos=PROPN;xpos=Ne;feats=Case=Gen|Gender=Masc|Number=Sing;governor=4;dependency_relation=iobj>]>

    try:
        stanford_nlp_obj_bad = StanfordNLPWrapper(language="FAKELANG")
    except UnknownLanguageError as err:
        print(isinstance(err, UnknownLanguageError))
<commit_msg>update stanford wrapper test<commit_after>"""Wrapper for the Python StanfordNLP package ``stanfordnlp``.
About: https://github.com/stanfordnlp/stanfordnlp.
"""

import os
from typing import Dict, Optional

import stanfordnlp  # type: ignore
from cltkv1.utils import UnknownLanguageError, file_exists, suppress_stdout

class StanfordNLPWrapper:
    """CLTK's wrapper for the StanfordNLP project."""

    def __init__(self, language: str, treebank: Optional[str] = None) -> None:
        """Constructor for ``stanford`` wrapper class.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> isinstance(stanford_wrapper, StanfordNLPWrapper)
        True
        >>> stanford_wrapper.language
        'greek'
        >>> stanford_wrapper.treebank
        'grc_proiel'

        >>> stanford_wrapper_perseus = StanfordNLPWrapper(language='greek', treebank='grc_perseus')
        >>> isinstance(stanford_wrapper_perseus, StanfordNLPWrapper)
        True
        >>> stanford_wrapper_perseus.language
        'greek'
        >>> stanford_wrapper_perseus.treebank
        'grc_perseus'

        >>> xen_anab = "Δαρείου καὶ Παρυσάτιδος γίγνονται παῖδες δύο, πρεσβύτερος μὲν Ἀρταξέρξης, νεώτερος δὲ Κῦρος: ἐπεὶ δὲ ἠσθένει Δαρεῖος καὶ ὑπώπτευε τελευτὴν τοῦ βίου, ἐβούλετο τὼ παῖδε ἀμφοτέρω παρεῖναι."
        >>> xen_anab_nlp = stanford_wrapper.parse(xen_anab)

        >>> stanford_nlp_obj_bad = StanfordNLPWrapper(language='BADLANG')
        Traceback (most recent call last):
          ...
        cltkv1.utils.exceptions.UnknownLanguageError: Language 'BADLANG' either not in scope for CLTK or not supported by StanfordNLP.
        """
        self.language = language
        self.treebank = treebank

        # Setup language
        self.map_langs_cltk_stanford = dict(
            greek="Ancient_Greek",
            latin="Latin",
            old_church_slavonic="Old_Church_Slavonic",
            old_french="Old_French",
        )

        self.wrapper_available = self.is_wrapper_available()  # type: bool
        if not self.wrapper_available:
            raise UnknownLanguageError(
                "Language '{}' either not in scope for CLTK or not supported by StanfordNLP.".format(
                    self.language
                )
            )
        self.stanford_code = self._get_stanford_code()

        # Setup optional treebank if specified
        self.map_code_treebanks = dict(
            grc=["grc_proiel", "grc_perseus"], la=["la_perseus", "la_proiel"]
        )
        # if not specified, will use the default treebank of stanfordnlp
        if self.treebank:
            valid_treebank = self._is_valid_treebank()
            if not valid_treebank:
                raise UnknownLanguageError(
                    "Invalid treebank '{0}' for language '{1}'.".format(
                        self.treebank, self.language
                    )
                )
        else:
            self.treebank = self._get_default_treebank()

        # check if model present
        # this fp is just to confirm that some model has already been downloaded. This is a weak check for the models actually being downloaded and valid
        self.model_path = os.path.expanduser(
            "~/stanfordnlp_resources/{0}_models/{0}_tokenizer.pt".format(self.treebank)
        )
        if not self._is_model_present():
            # download model if necessary
            self._download_model()

        # instantiate actual stanfordnlp class
        # Note: `suppress_stdout` is used to prevent `stanfordnlp`
        # from printing a long log of its parameters to screen.
        # Though we should capture these, within `_load_pipeline()`,
        # for the log file.
        with suppress_stdout():
            self.nlp = self._load_pipeline()

    def parse(self, text: str):
        """Run all ``stanfordnlp`` parsing on input text.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> xen_anab = "Δαρείου καὶ Παρυσάτιδος γίγνονται παῖδες δύο, πρεσβύτερος μὲν Ἀρταξέρξης, νεώτερος δὲ Κῦρος: ἐπεὶ δὲ ἠσθένει Δαρεῖος καὶ ὑπώπτευε τελευτὴν τοῦ βίου, ἐβούλετο τὼ παῖδε ἀμφοτέρω παρεῖναι."
        >>> xen_anab_nlp = stanford_wrapper.parse(xen_anab)
        >>> isinstance(xen_anab_nlp, stanfordnlp.pipeline.doc.Document)
        True

        >>> nlp_xen_anab_first_sent = xen_anab_nlp.sentences[0]
        >>> nlp_xen_anab_first_sent.tokens[0].index
        '1'
        >>> nlp_xen_anab_first_sent.tokens[0].text
        'Δαρείου'
        >>> first_word = nlp_xen_anab_first_sent.tokens[0].words[0]
        >>> first_word.dependency_relation  # was 'iobj' did the model change?
        'nmod'
        >>> first_word.feats
        'Case=Gen|Gender=Masc|Number=Sing'
        >>> first_word.governor  # was 4 on local, due to model update?
        5
        >>> first_word.index
        '1'
        >>> first_word.lemma
        'Δαρεῖος'
        >>> first_word.pos
        'Ne'
        >>> first_word.text
        'Δαρείου'
        >>> first_word.upos
        'PROPN'
        >>> first_word.xpos
        'Ne'
        """
        parsed_text = self.nlp(text)
        return parsed_text

    def _load_pipeline(self):
        """Instantiate `stanfordnlp.Pipeline()`.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> with suppress_stdout():    nlp_obj = stanford_wrapper._load_pipeline()
        >>> isinstance(nlp_obj, stanfordnlp.pipeline.core.Pipeline)
        True
        """
        models_dir = os.path.expanduser("~/stanfordnlp_resources/")
        nlp = stanfordnlp.Pipeline(
            processors="tokenize,mwt,pos,lemma,depparse",  # these are the default processors
            lang=self.stanford_code,
            models_dir=models_dir,
            treebank=self.treebank,
            use_gpu=True,  # default, won't fail if GPU not present
        )
        return nlp

    def _is_model_present(self) -> bool:
        """Checks if the model is already downloaded.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper._is_model_present()
        True
        """
        if file_exists(self.model_path):
            return True
        else:
            return False

    def _download_model(self):
        """Interface with the `stanfordnlp` model downloader.

        TODO: Figure out why doctests here hang. Presumably because waiting for user input, but prompt shouldn't arise if models already present.

        # >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        # >>> stanford_wrapper._download_model()
        # True
        """
        # prompt user to DL the stanford models
        print("")
        print("")
        print("Α" * 80)
        print("")
        print(
            "CLTK message: The part of the CLTK that you are using depends upon the Stanford NLP library (`stanfordnlp`). What follows are several question prompts coming from it. (More at: <https://github.com/stanfordnlp/stanfordnlp>.) Answer with defaults."
        )
        print("")
        print("Ω" * 80)
        print("")
        print("")
        stanfordnlp.download(self.treebank)
        # if file model still not available after attempted DL, then raise error
        if not file_exists(self.model_path):
            raise FileNotFoundError(
                "Missing required models for `stanfordnlp` at `{0}`.".format(
                    self.model_path
                )
            )
        pass

    def _get_default_treebank(self) -> str:
        """Return name of a language's default treebank if none
        supplied.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper._get_default_treebank()
        'grc_proiel'
        """
        stanford_default_treebanks = (
            stanfordnlp.utils.resources.default_treebanks
        )  # type: Dict[str, str]
        return stanford_default_treebanks[self.stanford_code]

    def _is_valid_treebank(self) -> bool:
        """Check whether for chosen language, optional
        treebank value is valid.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek', treebank='grc_proiel')
        >>> stanford_wrapper._is_valid_treebank()
        True
        """
        possible_treebanks = self.map_code_treebanks[self.stanford_code]
        if self.treebank in possible_treebanks:
            return True
        else:
            return False

    def is_wrapper_available(self) -> bool:
        """Maps CLTK's internal language term (e.g., ``latin``) to
        that used by ``stanfordnlp`` (``la``); confirms that this is
        a language the CLTK supports (i.e., is it pre-modern or not).

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper.is_wrapper_available()
        True
        """
        if self.language in self.map_langs_cltk_stanford:
            return True
        else:
            return False

    def _get_stanford_code(self) -> str:
        """Using known-supported language, use the CLTK's
        internal code to look up the code used by StanfordNLP.

        >>> stanford_wrapper = StanfordNLPWrapper(language='greek')
        >>> stanford_wrapper._get_stanford_code()
        'grc'
        """
        try:
            stanford_lang_name = self.map_langs_cltk_stanford[self.language]
        except KeyError:
            raise KeyError
        stanford_lang_code = (
            stanfordnlp.models.common.constant.lang2lcode
        )  # type: Dict[str, str]
        try:
            return stanford_lang_code[stanford_lang_name]
        except KeyError:
            raise KeyError

if __name__ == "__main__":

    stanford_nlp_obj = StanfordNLPWrapper(language="latin")
    print(stanford_nlp_obj.language == "latin")

    stanford_nlp_obj = StanfordNLPWrapper(language="greek", treebank="grc_perseus")
    print(stanford_nlp_obj.language == "greek")
    print(stanford_nlp_obj.treebank == "grc_perseus")
    print(stanford_nlp_obj.wrapper_available == True)

    stanford_nlp_obj = StanfordNLPWrapper(language="greek")
    print(stanford_nlp_obj.language == "greek")
    print(stanford_nlp_obj.treebank == "grc_proiel")
    print(stanford_nlp_obj.wrapper_available == True)
    fp_model = stanford_nlp_obj.model_path
    print(os.path.split(fp_model)[1] == "grc_proiel_tokenizer.pt")
    print(isinstance(stanford_nlp_obj.nlp, stanfordnlp.pipeline.core.Pipeline))

    xen_anab = "Δαρείου καὶ Παρυσάτιδος γίγνονται παῖδες δύο, πρεσβύτερος μὲν Ἀρταξέρξης, νεώτερος δὲ Κῦρος: ἐπεὶ δὲ ἠσθένει Δαρεῖος καὶ ὑπώπτευε τελευτὴν τοῦ βίου, ἐβούλετο τὼ παῖδε ἀμφοτέρω παρεῖναι."
    xen_anab_nlp = stanford_nlp_obj.parse(xen_anab)

    nlp_xen_anab_first_sent = xen_anab_nlp.sentences[0]
    # print(dir(nlp_xen_anab_first_sent))  # build_dependencies', 'dependencies', 'print_dependencies', 'print_tokens', 'print_words', 'tokens', 'words'
    print(nlp_xen_anab_first_sent.tokens[0].index == "1")
    print(nlp_xen_anab_first_sent.tokens[0].text == "Δαρείου")
    first_word = nlp_xen_anab_first_sent.tokens[0].words[
        0
    ]  # 'dependency_relation', 'feats', 'governor', 'index', 'lemma', 'parent_token', 'pos', 'text', 'upos', 'xpos'
    print(first_word.dependency_relation == "iobj")
    print(first_word.feats == "Case=Gen|Gender=Masc|Number=Sing")
    print(first_word.governor == 4)
    print(first_word.index == "1")
    print(first_word.lemma == "Δαρεῖος")
    print(first_word.pos == "Ne")
    print(first_word.text == "Δαρείου")
    print(first_word.upos == "PROPN")
    print(first_word.xpos == "Ne")
    # print(first_word.parent_token)  # <Token index=1;words=[<Word index=1;text=Δαρείου;lemma=Δαρεῖος;upos=PROPN;xpos=Ne;feats=Case=Gen|Gender=Masc|Number=Sing;governor=4;dependency_relation=iobj>]>

    try:
        stanford_nlp_obj_bad = StanfordNLPWrapper(language="FAKELANG")
    except UnknownLanguageError as err:
        print(isinstance(err, UnknownLanguageError))
<|endoftext|>


<commit_before>import matplotlib
# Force GTK3 backend. By default GTK2 gets loaded and conflicts with
# graph-tool
matplotlib.use('GTK3Agg')
#global plt
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

from plotting_abstract import PlottingBase

plot_figure_for_paper = False

class Plotting(PlottingBase):

    def __init__(self):
        self._ax = None
        return

    def figure(self):
        fig = plt.figure()
        self._ax = fig.gca()

    @property
    def ax(self):
        if self._ax is None:
            fig = plt.figure()
            self._ax = fig.gca()
        return self._ax

    def show(self):
        plt.show()

    def plot_abs_states(self, AA, s):
        color_map = {
                    'init': 'g',
                    'final': 'r',
                    'regular': 'k'
                    }
        for atype, abs_states in s.iteritems():
            for abs_state in abs_states:
                #r = AA.plant_abs.rect(abs_state.plant_state)
                #if c.dim != 2:
                #    raise StandardError('dim should be 2 for plotting 2D!')
                r = AA.plant_abs.get_ival_cons_abs_state(abs_state.plant_state).rect()
                self.plot_rect(r, color_map[atype])

    def plot_rect(self, r, edgecolor='k'):
        self.ax.add_patch(
            patches.Rectangle(r[0], *r[1], fill=False, edgecolor=edgecolor)
            #patches.Rectangle((0.1, 0.1), 0.5, 0.5, fill=False)
        )

    def plot_pwa_traces(self, txs):
        N = np.array([None])
        x = []
        y = []
        for tx in txs:
            #t = np.array(tx[0])
            xy = np.vstack(tx[1])
            x.append(xy[:, 0])
            x.append(N)
            y.append(xy[:, 1])
            y.append(N)
            #self.ax.plot(x[:, 0], x[:, 1], '-*')
        self.ax.plot(np.hstack(x), np.hstack(y), '-*')

    def parse_plot_cmd(self, plot_cmd, trace_obj):
        if len(plot_cmd) != 4:
            print 'plot command NOT of length 4: {}'.format(plot_cmd)
            return None
        x_axis_str, x_idx, y_axis_str, y_idx = plot_cmd
        try:
            x_axis = getattr(trace_obj, x_axis_str)
            y_axis = getattr(trace_obj, y_axis_str)
        except AttributeError, e:
            print 'unexpected plot command received: {}'.format(plot_cmd)
            print e
            return None
        x_idx = int(x_idx)
        y_idx = int(y_idx)
        try:
            if x_axis_str != 't':
                x = x_axis[x_idx, :]
        except:
            print 'unexpected indices for the first var: {}'.format(plot_cmd)
            return None
        try:
            if y_axis_str != 't':
                y = y_axis[y_idx, :]
        except:
            print 'unexpected indices for the second var: {}'.format(plot_cmd)
            return None

        return x, y

    def get_plot_cmd_from_stdin(self):
        plot_cmd_format = \
            '''########### plot command format ########## \n
               [t,x,s,u,ci,pi][0-n][t,x,s,u,ci,pi][0-n]   \n
               e.g. (a) phase plot,    x[1] vs x[0]: x0x1 \n
                    (b) state vs time, t    vs x[0]: t0x0 \n
               ########################################## \n'''
        print plot_cmd_format
        corrected_plot_cmd = raw_input('please type the correct command:')
        return corrected_plot_cmd

    def plot_trace_list_slow(self, trace_list):
        '''
        @type plt: matplotlib.pyplot
        '''

        # plot for each plant state
        # NUM_PLOTS = num_dims.x+1

        # plot all continuous plant states against time
        NUM_PLOTS = trace_list[0].x_array.shape[1]
    #     AX_list = []
        #plt.figure()
        #AX0X1 = plt.gca()

    #     for i in range(NUM_PLOTS):
    #         plt.figure()
    #         ax = plt.gca()
    #         AX_list.append(ax)
    #         plt.title('x{}'.format(i))

    #     for trace in trace_list:
    #         x_array = trace.x_array
    #         t_array = trace.t_array

    #         # plt_x0 = AX0.plot(t_array, x_array[:, 10], label='x10')
    #         for i in range(NUM_PLOTS):
    #             AX_list[i].plot(t_array, x_array[:, i])

    #         #plt_x0x1 = AX0X1.plot(x_array[:, 0], x_array[:, 1], label='x0x1')

    #         # plt.legend([plt_x0, plt_x1, plt_s0, plt_s1], ['x0', 'x1', 's0', 's1'])

    #     # plt.plot(t_array, ref_signal, drawstyle='steps-post')
    #     # plt.autoscale()
    #     plt.show()

        if plot_figure_for_paper:
            # count num of plotted sims
            ctr_total = 0
            import plothelper as ph
            line_list = []
            for i in range(NUM_PLOTS):
                plt.figure()
                ax = plt.gca()
                plt.title('x{}'.format(i))
                for trace in trace_list:
                    x_array = trace.x_array
                    t_array = trace.t_array
                    if not (x_array[0, i] <= 70.0 and x_array[0, i] >= 69.9):
                        # select to plot with probability 20%
                        if np.random.random() >= 0.05:
                            continue
                    line, = ax.plot(t_array, x_array[:, i])
                    line_list.append(line)
                    ctr_total += 1
                print('plotted {} sims'.format(ctr_total))
                ph.figure_for_paper(ax, line_list)
        else:
    #         for i in range(NUM_PLOTS):
    #             plt.figure()
    #             ax = plt.gca()
    #             plt.title('x{}'.format(i))
    #             for trace in trace_list:
    #                 x_array = trace.x_array
    #                 t_array = trace.t_array
    #                 ax.plot(t_array, x_array[:, i])
    #             #plt.show()

            plt.figure()
            ax = plt.gca()
            plt.title('x0-x1')
            for trace in trace_list:
                x_array = trace.x_array
                ax.plot(x_array[:, 0], x_array[:, 1])
            #plt.show()

    def plot_trace_list(self, trace_list, xvsy=[]):
        """plot all state vars """
        if not xvsy:
            nd = trace_list[0].x_array.shape[1]
            xvsy = [('t', 'x{}'.format(i)) for i in range(nd)]
        print xvsy
        self.plot_trace_list_xvsy(trace_list, xvsy)

    def plot_trace_list_xvsy(self, trace_list, x_vs_y):
        """plot_trace_list

        Parameters
        ----------
        trace_list :
        x_vs_y : [(x,y)]: x Vs y
               e.g. [(t, x0), (t, x0),(t, x1),(x0, x1)]
        """

        # Would be good to handle errors in the x Vs y string
        #sanity_check_xVsy()

        def prep_t_trace(trace_list):
            N = np.array([None])
            t_ = []
            for trace in trace_list:
                t_.append(trace.t_array)
                t_.append(N)
            return t_

        def prep_x_trace(trace_list, idx):
            N = np.array([None])
            x_ = []
            for trace in trace_list:
                x_.append(trace.x_array[:, idx])
                x_.append(N)
            return x_

        # ordinate, abcissa
        for a, o in x_vs_y:
            ax = plt.figure().gca()
            a_str = a[0]
            o_str = o[0]

            a_idx = int(a[1:]) if a[1:] else 0
            o_idx = int(o[1:]) if o[1:] else 0

            title = '{} - {}'.format(a, o)

            ax.set_title(title)

            # collect t_array
            x = (prep_t_trace(trace_list) if a_str == 't'
                 else prep_x_trace(trace_list, a_idx))
            # collect t_array
            y = (prep_t_trace(trace_list) if o_str == 't'
                 else prep_x_trace(trace_list, o_idx))

            ax.plot(np.hstack(x), np.hstack(y))

#         self.ax.set_title('x0-x1')
#         x = []
#         y = []
#         N = np.array([None])
#         for trace in trace_list:
#             x_array = trace.x_array
#             x.append(x_array[:, 0])
#             x.append(N)
#             y.append(x_array[:, 1])
#             y.append(N)
#             #ax.plot(x_array[:, 0], x_array[:, 1])
#         self.ax.plot(np.hstack(x), np.hstack(y))
#         #plt.show()

    def plot_trace_list_sat(self, trace_list):
        '''
        @type plt: matplotlib.pyplot
        '''
        NUM_PLOTS = 1
        for i in range(NUM_PLOTS):
            plt.figure()
            #ax = plt.gca(projection='3d')
            ax = plt.gca()
            #for trace in trace_list:
            #    x_array = trace.x_array
            #    t_array = trace.t_array
            #    ax.plot(t_array, x_array[:, i])
            plt.title('x-y'.format(i))
            for trace in trace_list:
                x_array = trace.x_array
                #ax.plot(x_array[:, 0], x_array[:, 1], x_array[:, 2])
                ax.plot(x_array[:, 0], x_array[:, 1])
            plt.figure()
            ax = plt.gca()
            plt.title('y-z'.format(i))
            for trace in trace_list:
                x_array = trace.x_array
                #ax.plot(x_array[:, 0], x_array[:, 1], x_array[:, 2])
                ax.plot(x_array[:, 1], x_array[:, 2])
            plt.figure()
            ax = plt.gca()
            plt.title('x-z'.format(i))
            for trace in trace_list:
                x_array = trace.x_array
                #ax.plot(x_array[:, 0], x_array[:, 1], x_array[:, 2])
                ax.plot(x_array[:, 0], x_array[:, 2])
            plt.show()
<commit_msg>added a slow plotting function which can generate different color traces<commit_after>import matplotlib
# Force GTK3 backend. By default GTK2 gets loaded and conflicts with
# graph-tool
matplotlib.use('GTK3Agg')
#global plt
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cm as cm
import numpy as np

from plotting_abstract import PlottingBase

plot_figure_for_paper = False

class Plotting(PlottingBase):

    def __init__(self):
        self._ax = None
        return

    def figure(self):
        fig = plt.figure()
        self._ax = fig.gca()

    @property
    def ax(self):
        if self._ax is None:
            fig = plt.figure()
            self._ax = fig.gca()
        return self._ax

    def show(self):
        plt.show()

    def plot_abs_states(self, AA, s):
        color_map = {
                    'init': 'g',
                    'final': 'r',
                    'regular': 'k'
                    }
        for atype, abs_states in s.iteritems():
            for abs_state in abs_states:
                #r = AA.plant_abs.rect(abs_state.plant_state)
                #if c.dim != 2:
                #    raise StandardError('dim should be 2 for plotting 2D!')
                r = AA.plant_abs.get_ival_cons_abs_state(abs_state.plant_state).rect()
                self.plot_rect(r, color_map[atype])

    def plot_rect(self, r, edgecolor='k'):
        self.ax.add_patch(
            patches.Rectangle(r[0], *r[1], fill=False, edgecolor=edgecolor)
            #patches.Rectangle((0.1, 0.1), 0.5, 0.5, fill=False)
        )

    def plot_pwa_traces(self, txs):
        N = np.array([None])
        x = []
        y = []
        for tx in txs:
            #t = np.array(tx[0])
            xy = np.vstack(tx[1])
            x.append(xy[:, 0])
            x.append(N)
            y.append(xy[:, 1])
            y.append(N)
            #self.ax.plot(x[:, 0], x[:, 1], '-*')
        self.ax.plot(np.hstack(x), np.hstack(y), '-*')

    def parse_plot_cmd(self, plot_cmd, trace_obj):
        if len(plot_cmd) != 4:
            print 'plot command NOT of length 4: {}'.format(plot_cmd)
            return None
        x_axis_str, x_idx, y_axis_str, y_idx = plot_cmd
        try:
            x_axis = getattr(trace_obj, x_axis_str)
            y_axis = getattr(trace_obj, y_axis_str)
        except AttributeError, e:
            print 'unexpected plot command received: {}'.format(plot_cmd)
            print e
            return None
        x_idx = int(x_idx)
        y_idx = int(y_idx)
        try:
            if x_axis_str != 't':
                x = x_axis[x_idx, :]
        except:
            print 'unexpected indices for the first var: {}'.format(plot_cmd)
            return None
        try:
            if y_axis_str != 't':
                y = y_axis[y_idx, :]
        except:
            print 'unexpected indices for the second var: {}'.format(plot_cmd)
            return None

        return x, y

    def get_plot_cmd_from_stdin(self):
        plot_cmd_format = \
            '''########### plot command format ########## \n
               [t,x,s,u,ci,pi][0-n][t,x,s,u,ci,pi][0-n]   \n
               e.g. (a) phase plot,    x[1] vs x[0]: x0x1 \n
                    (b) state vs time, t    vs x[0]: t0x0 \n
               ########################################## \n'''
        print plot_cmd_format
        corrected_plot_cmd = raw_input('please type the correct command:')
        return corrected_plot_cmd

    def plot_trace_list_slow(self, trace_list):
        '''
        @type plt: matplotlib.pyplot
        '''

        # plot for each plant state
        # NUM_PLOTS = num_dims.x+1

        # plot all continuous plant states against time
        NUM_PLOTS = trace_list[0].x_array.shape[1]
    #     AX_list = []
        #plt.figure()
        #AX0X1 = plt.gca()

    #     for i in range(NUM_PLOTS):
    #         plt.figure()
    #         ax = plt.gca()
    #         AX_list.append(ax)
    #         plt.title('x{}'.format(i))

    #     for trace in trace_list:
    #         x_array = trace.x_array
    #         t_array = trace.t_array

    #         # plt_x0 = AX0.plot(t_array, x_array[:, 10], label='x10')
    #         for i in range(NUM_PLOTS):
    #             AX_list[i].plot(t_array, x_array[:, i])

    #         #plt_x0x1 = AX0X1.plot(x_array[:, 0], x_array[:, 1], label='x0x1')

    #         # plt.legend([plt_x0, plt_x1, plt_s0, plt_s1], ['x0', 'x1', 's0', 's1'])

    #     # plt.plot(t_array, ref_signal, drawstyle='steps-post')
    #     # plt.autoscale()
    #     plt.show()

        if plot_figure_for_paper:
            # count num of plotted sims
            ctr_total = 0
            import plothelper as ph
            line_list = []
            for i in range(NUM_PLOTS):
                plt.figure()
                ax = plt.gca()
                plt.title('x{}'.format(i))
                for trace in trace_list:
                    x_array = trace.x_array
                    t_array = trace.t_array
                    if not (x_array[0, i] <= 70.0 and x_array[0, i] >= 69.9):
                        # select to plot with probability 20%
                        if np.random.random() >= 0.05:
                            continue
                    line, = ax.plot(t_array, x_array[:, i])
                    line_list.append(line)
                    ctr_total += 1
                print('plotted {} sims'.format(ctr_total))
                ph.figure_for_paper(ax, line_list)
        else:
    #         for i in range(NUM_PLOTS):
    #             plt.figure()
    #             ax = plt.gca()
    #             plt.title('x{}'.format(i))
    #             for trace in trace_list:
    #                 x_array = trace.x_array
    #                 t_array = trace.t_array
    #                 ax.plot(t_array, x_array[:, i])
    #             #plt.show()

            plt.figure()
            ax = plt.gca()
            plt.title('x0-x1')
            for trace in trace_list:
                x_array = trace.x_array
                ax.plot(x_array[:, 0], x_array[:, 1])
            #plt.show()

    def plot_trace_list(self, trace_list, xvsy=[]):
        """plot all state vars """
        if not xvsy:
            nd = trace_list[0].x_array.shape[1]
            xvsy = [('t', 'x{}'.format(i)) for i in range(nd)]
        print xvsy
        self.plot_trace_list_xvsy_dc(trace_list, xvsy)

    def plot_trace_list_xvsy_sc(self, trace_list, x_vs_y):
        """plot_trace_list: Same Color but faster?

        Parameters
        ----------
        trace_list :
        x_vs_y : [(x,y)]: x Vs y
               e.g. [(t, x0), (t, x0),(t, x1),(x0, x1)]
        """

        # Would be good to handle errors in the x Vs y string
        #sanity_check_xVsy()
        print 'plotting...'

        def prep_t_trace(trace_list):
            N = np.array([None])
            t_ = []
            for trace in trace_list:
                t_.append(trace.t_array)
                t_.append(N)
            return t_

        def prep_x_trace(trace_list, idx):
            N = np.array([None])
            x_ = []
            for trace in trace_list:
                x_.append(trace.x_array[:, idx])
                x_.append(N)
            return x_

        # ordinate, abcissa
        for a, o in x_vs_y:
            ax = plt.figure().gca()
            a_str = a[0]
            o_str = o[0]

            a_idx = int(a[1:]) if a[1:] else 0
            o_idx = int(o[1:]) if o[1:] else 0

            title = '{} - {}'.format(a, o)

            ax.set_title(title)

            # collect t_array
            x = (prep_t_trace(trace_list) if a_str == 't'
                 else prep_x_trace(trace_list, a_idx))
            # collect t_array
            y = (prep_t_trace(trace_list) if o_str == 't'
                 else prep_x_trace(trace_list, o_idx))

            ax.plot(np.hstack(x), np.hstack(y))

    def plot_trace_list_xvsy_dc(self, trace_list, x_vs_y):
        """plot_trace_list: Different Color but slower?

        Parameters
        ----------
        trace_list :
        x_vs_y : [(x,y)]: x Vs y
               e.g. [(t, x0), (t, x0),(t, x1),(x0, x1)]
        """

        # Would be good to handle errors in the x Vs y string
        #sanity_check_xVsy()

        print 'plotting...'

        def prep_t_trace(trace_list):
            return [trace.t_array for trace in trace_list]

        def prep_x_trace(trace_list, idx):
            return [trace.x_array[:, idx] for trace in trace_list]

        # ordinate, abcissa
        for a, o in x_vs_y:
            ax = plt.figure().gca()
            a_str = a[0]
            o_str = o[0]

            a_idx = int(a[1:]) if a[1:] else 0
            o_idx = int(o[1:]) if o[1:] else 0

            title = '{} - {}'.format(a, o)

            ax.set_title(title)

            # collect t_array
            x = (prep_t_trace(trace_list) if a_str == 't'
                 else prep_x_trace(trace_list, a_idx))
            # collect t_array
            y = (prep_t_trace(trace_list) if o_str == 't'
                 else prep_x_trace(trace_list, o_idx))

            for i, j in zip(x, y):
                ax.plot(i, j)

    def plot_trace_list_sat(self, trace_list):
        '''
        @type plt: matplotlib.pyplot
        '''
        NUM_PLOTS = 1
        for i in range(NUM_PLOTS):
            plt.figure()
            #ax = plt.gca(projection='3d')
            ax = plt.gca()
            #for trace in trace_list:
            #    x_array = trace.x_array
            #    t_array = trace.t_array
            #    ax.plot(t_array, x_array[:, i])
            plt.title('x-y'.format(i))
            for trace in trace_list:
                x_array = trace.x_array
                #ax.plot(x_array[:, 0], x_array[:, 1], x_array[:, 2])
                ax.plot(x_array[:, 0], x_array[:, 1])
            plt.figure()
            ax = plt.gca()
            plt.title('y-z'.format(i))
            for trace in trace_list:
                x_array = trace.x_array
                #ax.plot(x_array[:, 0], x_array[:, 1], x_array[:, 2])
                ax.plot(x_array[:, 1], x_array[:, 2])
            plt.figure()
            ax = plt.gca()
            plt.title('x-z'.format(i))
            for trace in trace_list:
                x_array = trace.x_array
                #ax.plot(x_array[:, 0], x_array[:, 1], x_array[:, 2])
                ax.plot(x_array[:, 0], x_array[:, 2])
            plt.show()
<|endoftext|>


<commit_before># Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.
import configparser
import io

from UM.Signal import Signal, signalemitter
from UM.PluginObject import PluginObject
from UM.Logger import Logger
from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
from UM.Settings.DefinitionContainer import DefinitionContainer #For getting all definitions in this stack.

import UM.Settings.ContainerRegistry

from . import ContainerInterface
from . import SettingFunction

class IncorrectVersionError(Exception):
    pass

class InvalidContainerStackError(Exception):
    pass

MimeTypeDatabase.addMimeType(
    MimeType(
        name = "application/x-uranium-containerstack",
        comment = "Uranium Container Stack",
        suffixes = [ "stack.cfg" ]
    )
)

##  A stack of setting containers to handle setting value retrieval.
@signalemitter
class ContainerStack(ContainerInterface.ContainerInterface, PluginObject):
    Version = 2

    ##  Constructor
    #
    #   \param stack_id \type{string} A unique, machine readable/writable ID.
    def __init__(self, stack_id, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._id = str(stack_id)
        self._name = stack_id
        self._metadata = {}
        self._containers = []
        self._next_stack = None
        self._read_only = False
        self._dirty = True

    ##  \copydoc ContainerInterface::getId
    #
    #   Reimplemented from ContainerInterface
    def getId(self):
        return self._id

    ##  \copydoc ContainerInterface::getName
    #
    #   Reimplemented from ContainerInterface
    def getName(self):
        return str(self._name)

    ##  Emitted whenever the name of this stack changes.
    nameChanged = Signal()

    containersChanged = Signal()

    ##  Set the name of this stack.
    #
    #   \param name \type{string} The new name of the stack.
    def setName(self, name):
        if name != self._name:
            self._name = name
            self.nameChanged.emit()

    ##  \copydoc ContainerInterface::isReadOnly
    #
    #   Reimplemented from ContainerInterface
    def isReadOnly(self):
        return self._read_only

    def setReadOnly(self, read_only):
        self._read_only = read_only

    ##  \copydoc ContainerInterface::getMetaData
    #
    #   Reimplemented from ContainerInterface
    def getMetaData(self):
        return self._metadata

    ##  \copydoc ContainerInterface::getMetaDataEntry
    #
    #   Reimplemented from ContainerInterface
    def getMetaDataEntry(self, entry, default = None):
        value = self._metadata.get(entry, None)

        if value is None:
            for container in self._containers:
                value = container.getMetaDataEntry(entry, None)
                if value is not None:
                    break

        if value is None:
            return default
        else:
            return value

    def addMetaDataEntry(self, key, value):
        if key not in self._metadata:
            self._dirty = True
            self._metadata[key] = value
        else:
            Logger.log("w", "Meta data with key %s was already added.", key)

    def setMetaDataEntry(self, key, value):
        if key in self._metadata:
            self._dirty = True
            self._metadata[key] = value
        else:
            Logger.log("w", "Meta data with key %s was not found. Unable to change.", key)

    def isDirty(self):
        return self._dirty

    def setDirty(self, dirty):
        self._dirty = dirty

    ##  \copydoc ContainerInterface::getProperty
    #
    #   Reimplemented from ContainerInterface.
    #
    #   getProperty will start at the top of the stack and try to get the property
    #   specified. If that container returns no value, the next container on the
    #   stack will be tried and so on until the bottom of the stack is reached.
    #   If a next stack is defined for this stack it will then try to get the
    #   value from that stack. If no next stack is defined, None will be returned.
    #
    #   Note that if the property value is a function, this method will return the
    #   result of evaluating that property with the current stack. If you need the
    #   actual function, use getRawProperty()
    def getProperty(self, key, property_name):
        value = self.getRawProperty(key, property_name)
        if isinstance(value, SettingFunction.SettingFunction):
            return value(self)

        return value

    ##  Retrieve a property of a setting by key and property name.
    #
    #   This method does the same as getProperty() except it does not perform any
    #   special handling of the result, instead the raw stored value is returned.
    def getRawProperty(self, key, property_name):
        for container in self._containers:
            value = container.getProperty(key, property_name)
            if value is not None:
                return value

        if self._next_stack:
            return self._next_stack.getRawProperty(key, property_name)
        else:
            return None

    ##  \copydoc ContainerInterface::hasProperty
    #
    #   Reimplemented from ContainerInterface.
    #
    #   hasProperty will check if any of the containers in the stack has the
    #   specified property. If it does, it stops and returns True. If it gets to
    #   the end of the stack, it returns False.
    def hasProperty(self, key, property_name):
        for container in self._containers:
            if container.hasProperty(key, property_name):
                return True

        if self._next_stack:
            return self._next_stack.hasProperty(key, property_name)
        return False

    propertyChanged = Signal()

    ##  \copydoc ContainerInterface::serialize
    #
    #   Reimplemented from ContainerInterface
    #
    #   TODO: Expand documentation here, include the fact that this should _not_ include all containers
    def serialize(self):
        parser = configparser.ConfigParser(interpolation = None, empty_lines_in_values = False)

        parser["general"] = {}
        parser["general"]["version"] = str(self.Version)
        parser["general"]["name"] = str(self._name)
        parser["general"]["id"] = str(self._id)

        parser["metadata"] = {}
        for key, value in self._metadata.items():
            parser["metadata"][key] = str(value)

        container_id_string = ""
        for container in self._containers:
            container_id_string += str(container.getId()) + ","

        parser["general"]["containers"] = container_id_string

        stream = io.StringIO()
        parser.write(stream)
        return stream.getvalue()

    ##  \copydoc ContainerInterface::deserialize
    #
    #   Reimplemented from ContainerInterface
    #
    #   TODO: Expand documentation here, include the fact that this should _not_ include all containers
    def deserialize(self, serialized):
        parser = configparser.ConfigParser(interpolation=None, empty_lines_in_values=False)
        parser.read_string(serialized)

        if not "general" in parser or not "version" in parser["general"] or not "name" in parser["general"] or not "id" in parser["general"]:
            raise InvalidContainerStackError("Missing required section 'general' or 'version' property")

        if parser["general"].getint("version") != self.Version:
            raise IncorrectVersionError

        self._name = parser["general"].get("name")
        self._id = parser["general"].get("id")

        if "metadata" in parser:
            self._metadata = dict(parser["metadata"])

        # The containers are saved in a single coma separated list.
        container_id_list = parser["general"].get("containers", "").split(",")
        for container_id in container_id_list:
            if container_id != "":
                containers = UM.Settings.ContainerRegistry.getInstance().findContainers(id = container_id)
                if containers:
                    containers[0].propertyChanged.connect(self.propertyChanged)
                    self._containers.append(containers[0])
                else:
                    raise Exception("When trying to deserialize, we recieved an unknown ID (%s) for container" % container_id)

        ## TODO; Deserialize the containers.

    ##  Get all keys known to this container stack.
    #
    #   In combination with getProperty(), you can obtain the current property
    #   values of all settings.
    #
    #   \return A set of all setting keys in this container stack.
    def getAllKeys(self):
        keys = set()
        definition_containers = [container for container in self.getContainers() if container.__class__ == DefinitionContainer] #To get all keys, get all definitions from all definition containers.
        for definition_container in definition_containers:
            keys |= definition_container.getAllKeys()
        if self._next_stack:
            keys |= self._next_stack.getAllKeys()
        return keys

    ##  Get a list of all containers in this stack.
    #
    #   Note that it returns a shallow copy of the container list, as it's only allowed to change the order or entries
    #   in this list by the proper functions.
    #   \return \type{list} A list of all containers in this stack.
    def getContainers(self):
        return self._containers[:]

    def getContainerIndex(self, container):
        return self._containers.index(container)

    ##  Get a container by index.
    #
    #   \param index \type{int} The index of the container to get.
    #
    #   \return The container at the specified index.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def getContainer(self, index):
        if index < 0:
            raise IndexError
        return self._containers[index]

    ##  Get the container at the top of the stack.
    #
    #   This is a convenience method that will always return the top of the stack.
    #
    #   \return The container at the top of the stack, or None if no containers have been added.
    def getTop(self):
        if self._containers:
            return self._containers[0]

        return None

    ##  Get the container at the bottom of the stack.
    #
    #   This is a convenience method that will always return the bottom of the stack.
    #
    #   \return The container at the bottom of the stack, or None if no containers have been added.
    def getBottom(self):
        if self._containers:
            return self._containers[-1]

        return None

    ##  Get the SettingDefinition object for a specified key
    def getSettingDefinition(self, key):
        for container in self._containers:
            if not isinstance(container, DefinitionContainer):
                continue

            settings = container.findDefinitions(key = key)
            if settings:
                return settings[0]

        if self._next_stack:
            return self._next_stack.getSettingDefinition(key)
        else:
            return None

    ##  Find a container matching certain criteria.
    #
    #   \param filter \type{dict} A dictionary containing key and value pairs
    #   that need to match the container. Note that the value of "*" can be used
    #   as a wild card. This will ensure that any container that has the
    #   specified key in the meta data is found.
    #   \param container_type \type{class} An optional type of container to
    #   filter on.
    #   \return The first container that matches the filter criteria or None if not found.
    def findContainer(self, filter, container_type = None):
        for container in self._containers:
            meta_data = container.getMetaData()
            match = True
            for key in filter:
                try:
                    if (meta_data[key] == filter[key] or filter[key] == "*") and (container.__class__ == container_type or container_type == None):
                        continue
                    else:
                        match = False
                        break
                except KeyError:
                    match = False
                    break

            if match:
                return container

        return None

    ##  Add a container to the top of the stack.
    #
    #   \param container The container to add to the stack.
    def addContainer(self, container):
        if container is not self:
            container.propertyChanged.connect(self.propertyChanged)
            self._containers.insert(0, container)
            self.containersChanged.emit(container)
        else:
            raise Exception("Unable to add stack to itself.")

    ##  Replace a container in the stack.
    #
    #   \param index \type{int} The index of the container to replace.
    #   \param container The container to replace the existing entry with.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    #   \exception Exception when trying to replace container ContainerStack.
    def replaceContainer(self, index, container):
        if index < 0:
            raise IndexError
        if container is self:
            raise Exception("Unable to replace container with ContainerStack (self) ")

        self._containers[index].propertyChanged.disconnect(self.propertyChanged)
        container.propertyChanged.connect(self.propertyChanged)
        self._containers[index] = container
        self.containersChanged.emit(container)

    ##  Remove a container from the stack.
    #
    #   \param index \type{int} The index of the container to remove.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def removeContainer(self, index = 0):
        if index < 0:
            raise IndexError
        try:
            container = self._containers[index]
            container.propertyChanged.disconnect(self.propertyChanged)
            del self._containers[index]
            self.containersChanged.emit(container)
        except TypeError:
            raise IndexError("Can't delete container with index %s" % index)

    ##  Get the next stack
    #
    #   The next stack is the stack that is searched for a setting value if the
    #   bottom of the stack is reached when searching for a value.
    #
    #   \return \type{ContainerStack} The next stack or None if not set.
    def getNextStack(self):
        return self._next_stack

    ##  Set the next stack
    #
    #   \param stack \type{ContainerStack} The next stack to set. Can be None.
    #   Raises Exception when trying to set itself as next stack (to prevent infinite loops)
    #   \sa getNextStack
    def setNextStack(self, stack):
        if self is stack:
            raise Exception("Next stack can not be itself")
        self._next_stack = stack
<commit_msg>Added remove metadata function<commit_after># Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.
import configparser
import io

from UM.Signal import Signal, signalemitter
from UM.PluginObject import PluginObject
from UM.Logger import Logger
from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
from UM.Settings.DefinitionContainer import DefinitionContainer #For getting all definitions in this stack.

import UM.Settings.ContainerRegistry

from . import ContainerInterface
from . import SettingFunction

class IncorrectVersionError(Exception):
    pass

class InvalidContainerStackError(Exception):
    pass

MimeTypeDatabase.addMimeType(
    MimeType(
        name = "application/x-uranium-containerstack",
        comment = "Uranium Container Stack",
        suffixes = [ "stack.cfg" ]
    )
)

##  A stack of setting containers to handle setting value retrieval.
@signalemitter
class ContainerStack(ContainerInterface.ContainerInterface, PluginObject):
    Version = 2

    ##  Constructor
    #
    #   \param stack_id \type{string} A unique, machine readable/writable ID.
    def __init__(self, stack_id, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._id = str(stack_id)
        self._name = stack_id
        self._metadata = {}
        self._containers = []
        self._next_stack = None
        self._read_only = False
        self._dirty = True

    ##  \copydoc ContainerInterface::getId
    #
    #   Reimplemented from ContainerInterface
    def getId(self):
        return self._id

    ##  \copydoc ContainerInterface::getName
    #
    #   Reimplemented from ContainerInterface
    def getName(self):
        return str(self._name)

    ##  Emitted whenever the name of this stack changes.
    nameChanged = Signal()

    containersChanged = Signal()

    ##  Set the name of this stack.
    #
    #   \param name \type{string} The new name of the stack.
    def setName(self, name):
        if name != self._name:
            self._name = name
            self.nameChanged.emit()

    ##  \copydoc ContainerInterface::isReadOnly
    #
    #   Reimplemented from ContainerInterface
    def isReadOnly(self):
        return self._read_only

    def setReadOnly(self, read_only):
        self._read_only = read_only

    ##  \copydoc ContainerInterface::getMetaData
    #
    #   Reimplemented from ContainerInterface
    def getMetaData(self):
        return self._metadata

    ##  \copydoc ContainerInterface::getMetaDataEntry
    #
    #   Reimplemented from ContainerInterface
    def getMetaDataEntry(self, entry, default = None):
        value = self._metadata.get(entry, None)

        if value is None:
            for container in self._containers:
                value = container.getMetaDataEntry(entry, None)
                if value is not None:
                    break

        if value is None:
            return default
        else:
            return value

    def addMetaDataEntry(self, key, value):
        if key not in self._metadata:
            self._dirty = True
            self._metadata[key] = value
        else:
            Logger.log("w", "Meta data with key %s was already added.", key)

    def setMetaDataEntry(self, key, value):
        if key in self._metadata:
            self._dirty = True
            self._metadata[key] = value
        else:
            Logger.log("w", "Meta data with key %s was not found. Unable to change.", key)

    def removeMetaDataEntry(self, key):
        if key in self._metadata:
            del self._metadata[key]

    def isDirty(self):
        return self._dirty

    def setDirty(self, dirty):
        self._dirty = dirty

    ##  \copydoc ContainerInterface::getProperty
    #
    #   Reimplemented from ContainerInterface.
    #
    #   getProperty will start at the top of the stack and try to get the property
    #   specified. If that container returns no value, the next container on the
    #   stack will be tried and so on until the bottom of the stack is reached.
    #   If a next stack is defined for this stack it will then try to get the
    #   value from that stack. If no next stack is defined, None will be returned.
    #
    #   Note that if the property value is a function, this method will return the
    #   result of evaluating that property with the current stack. If you need the
    #   actual function, use getRawProperty()
    def getProperty(self, key, property_name):
        value = self.getRawProperty(key, property_name)
        if isinstance(value, SettingFunction.SettingFunction):
            return value(self)

        return value

    ##  Retrieve a property of a setting by key and property name.
    #
    #   This method does the same as getProperty() except it does not perform any
    #   special handling of the result, instead the raw stored value is returned.
    def getRawProperty(self, key, property_name):
        for container in self._containers:
            value = container.getProperty(key, property_name)
            if value is not None:
                return value

        if self._next_stack:
            return self._next_stack.getRawProperty(key, property_name)
        else:
            return None

    ##  \copydoc ContainerInterface::hasProperty
    #
    #   Reimplemented from ContainerInterface.
    #
    #   hasProperty will check if any of the containers in the stack has the
    #   specified property. If it does, it stops and returns True. If it gets to
    #   the end of the stack, it returns False.
    def hasProperty(self, key, property_name):
        for container in self._containers:
            if container.hasProperty(key, property_name):
                return True

        if self._next_stack:
            return self._next_stack.hasProperty(key, property_name)
        return False

    propertyChanged = Signal()

    ##  \copydoc ContainerInterface::serialize
    #
    #   Reimplemented from ContainerInterface
    #
    #   TODO: Expand documentation here, include the fact that this should _not_ include all containers
    def serialize(self):
        parser = configparser.ConfigParser(interpolation = None, empty_lines_in_values = False)

        parser["general"] = {}
        parser["general"]["version"] = str(self.Version)
        parser["general"]["name"] = str(self._name)
        parser["general"]["id"] = str(self._id)

        parser["metadata"] = {}
        for key, value in self._metadata.items():
            parser["metadata"][key] = str(value)

        container_id_string = ""
        for container in self._containers:
            container_id_string += str(container.getId()) + ","

        parser["general"]["containers"] = container_id_string

        stream = io.StringIO()
        parser.write(stream)
        return stream.getvalue()

    ##  \copydoc ContainerInterface::deserialize
    #
    #   Reimplemented from ContainerInterface
    #
    #   TODO: Expand documentation here, include the fact that this should _not_ include all containers
    def deserialize(self, serialized):
        parser = configparser.ConfigParser(interpolation=None, empty_lines_in_values=False)
        parser.read_string(serialized)

        if not "general" in parser or not "version" in parser["general"] or not "name" in parser["general"] or not "id" in parser["general"]:
            raise InvalidContainerStackError("Missing required section 'general' or 'version' property")

        if parser["general"].getint("version") != self.Version:
            raise IncorrectVersionError

        self._name = parser["general"].get("name")
        self._id = parser["general"].get("id")

        if "metadata" in parser:
            self._metadata = dict(parser["metadata"])

        # The containers are saved in a single coma separated list.
        container_id_list = parser["general"].get("containers", "").split(",")
        for container_id in container_id_list:
            if container_id != "":
                containers = UM.Settings.ContainerRegistry.getInstance().findContainers(id = container_id)
                if containers:
                    containers[0].propertyChanged.connect(self.propertyChanged)
                    self._containers.append(containers[0])
                else:
                    raise Exception("When trying to deserialize, we recieved an unknown ID (%s) for container" % container_id)

        ## TODO; Deserialize the containers.

    ##  Get all keys known to this container stack.
    #
    #   In combination with getProperty(), you can obtain the current property
    #   values of all settings.
    #
    #   \return A set of all setting keys in this container stack.
    def getAllKeys(self):
        keys = set()
        definition_containers = [container for container in self.getContainers() if container.__class__ == DefinitionContainer] #To get all keys, get all definitions from all definition containers.
        for definition_container in definition_containers:
            keys |= definition_container.getAllKeys()
        if self._next_stack:
            keys |= self._next_stack.getAllKeys()
        return keys

    ##  Get a list of all containers in this stack.
    #
    #   Note that it returns a shallow copy of the container list, as it's only allowed to change the order or entries
    #   in this list by the proper functions.
    #   \return \type{list} A list of all containers in this stack.
    def getContainers(self):
        return self._containers[:]

    def getContainerIndex(self, container):
        return self._containers.index(container)

    ##  Get a container by index.
    #
    #   \param index \type{int} The index of the container to get.
    #
    #   \return The container at the specified index.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def getContainer(self, index):
        if index < 0:
            raise IndexError
        return self._containers[index]

    ##  Get the container at the top of the stack.
    #
    #   This is a convenience method that will always return the top of the stack.
    #
    #   \return The container at the top of the stack, or None if no containers have been added.
    def getTop(self):
        if self._containers:
            return self._containers[0]

        return None

    ##  Get the container at the bottom of the stack.
    #
    #   This is a convenience method that will always return the bottom of the stack.
    #
    #   \return The container at the bottom of the stack, or None if no containers have been added.
    def getBottom(self):
        if self._containers:
            return self._containers[-1]

        return None

    ##  Get the SettingDefinition object for a specified key
    def getSettingDefinition(self, key):
        for container in self._containers:
            if not isinstance(container, DefinitionContainer):
                continue

            settings = container.findDefinitions(key = key)
            if settings:
                return settings[0]

        if self._next_stack:
            return self._next_stack.getSettingDefinition(key)
        else:
            return None

    ##  Find a container matching certain criteria.
    #
    #   \param filter \type{dict} A dictionary containing key and value pairs
    #   that need to match the container. Note that the value of "*" can be used
    #   as a wild card. This will ensure that any container that has the
    #   specified key in the meta data is found.
    #   \param container_type \type{class} An optional type of container to
    #   filter on.
    #   \return The first container that matches the filter criteria or None if not found.
    def findContainer(self, filter, container_type = None):
        for container in self._containers:
            meta_data = container.getMetaData()
            match = True
            for key in filter:
                try:
                    if (meta_data[key] == filter[key] or filter[key] == "*") and (container.__class__ == container_type or container_type == None):
                        continue
                    else:
                        match = False
                        break
                except KeyError:
                    match = False
                    break

            if match:
                return container

        return None

    ##  Add a container to the top of the stack.
    #
    #   \param container The container to add to the stack.
    def addContainer(self, container):
        if container is not self:
            container.propertyChanged.connect(self.propertyChanged)
            self._containers.insert(0, container)
            self.containersChanged.emit(container)
        else:
            raise Exception("Unable to add stack to itself.")

    ##  Replace a container in the stack.
    #
    #   \param index \type{int} The index of the container to replace.
    #   \param container The container to replace the existing entry with.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    #   \exception Exception when trying to replace container ContainerStack.
    def replaceContainer(self, index, container):
        if index < 0:
            raise IndexError
        if container is self:
            raise Exception("Unable to replace container with ContainerStack (self) ")

        self._containers[index].propertyChanged.disconnect(self.propertyChanged)
        container.propertyChanged.connect(self.propertyChanged)
        self._containers[index] = container
        self.containersChanged.emit(container)

    ##  Remove a container from the stack.
    #
    #   \param index \type{int} The index of the container to remove.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def removeContainer(self, index = 0):
        if index < 0:
            raise IndexError
        try:
            container = self._containers[index]
            container.propertyChanged.disconnect(self.propertyChanged)
            del self._containers[index]
            self.containersChanged.emit(container)
        except TypeError:
            raise IndexError("Can't delete container with index %s" % index)

    ##  Get the next stack
    #
    #   The next stack is the stack that is searched for a setting value if the
    #   bottom of the stack is reached when searching for a value.
    #
    #   \return \type{ContainerStack} The next stack or None if not set.
    def getNextStack(self):
        return self._next_stack

    ##  Set the next stack
    #
    #   \param stack \type{ContainerStack} The next stack to set. Can be None.
    #   Raises Exception when trying to set itself as next stack (to prevent infinite loops)
    #   \sa getNextStack
    def setNextStack(self, stack):
        if self is stack:
            raise Exception("Next stack can not be itself")
        self._next_stack = stack
<|endoftext|>


<commit_before># Copyright (C) 2007-2010 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""
The Telnet protocol.
"""
from Exscript.util.tty            import get_terminal_size
from Exscript.protocols           import telnetlib
from Exscript.protocols.Protocol  import Protocol
from Exscript.protocols.Exception import ProtocolException, \
                                         TimeoutException, \
                                         DriverReplacedException, \
                                         ExpectCancelledException

class Telnet(Protocol):
    """
    The Telnet protocol adapter.
    """

    def __init__(self, **kwargs):
        Protocol.__init__(self, **kwargs)
        self.tn = None

    def _telnetlib_received(self, data):
        self._receive_cb(data)
        self.buffer.append(data)

    def _connect_hook(self, hostname, port):
        assert self.tn is None
        rows, cols = get_terminal_size()
        self.tn = telnetlib.Telnet(hostname,
                                   port or 23,
                                   termsize         = (rows, cols),
                                   termtype         = self.termtype,
                                   stderr           = self.stderr,
                                   receive_callback = self._telnetlib_received)
        if self.debug >= 5:
            self.tn.set_debuglevel(1)
        if self.tn is None:
            return False
        return True

    def send(self, data):
        self._dbg(4, 'Sending %s' % repr(data))
        try:
            self.tn.write(data)
        except Exception:
            self._dbg(1, 'Error while writing to connection')
            raise

    def _domatch(self, prompt, flush):
        if flush:
            func = self.tn.expect
        else:
            func = self.tn.waitfor

        # Wait for a prompt.
        clean = self.get_driver().clean_response_for_re_match
        self.response = None
        try:
            result, match, self.response = func(prompt, self.timeout, cleanup = clean)
        except Exception:
            self._dbg(1, 'Error while waiting for ' + repr(prompt))
            raise

        if match:
            self._dbg(2, "Got a prompt, match was %s" % repr(match.group()))
            self.buffer.pop(len(self.response))

        self._dbg(5, "Response was %s" % repr(self.response))

        if result == -1:
            error = 'Error while waiting for response from device'
            raise TimeoutException(error)
        if result == -2:
            if self.driver_replaced:
                self.driver_replaced = False
                raise DriverReplacedException()
            else:
                raise ExpectCancelledException()
        if self.response is None:
            raise ProtocolException('whoops - response is None')

        return result, match

    def cancel_expect(self):
        self.tn.cancel_expect = True

    def _set_terminal_size(self, rows, cols):
        self.tn.set_window_size(rows, cols)

    def interact(self, key_handlers = None, handle_window_size = True):
        return self._open_shell(self.tn.sock, key_handlers, handle_window_size)

    def close(self, force = False):
        if self.tn is None:
            return
        if not force:
            try:
                self.response = self.tn.read_all()
            except Exception:
                pass
        self.tn.close()
        self.tn = None
        self.buffer.clear()
<commit_msg>Exscript: Fixed bug: Do not remove newlines from recieved data.<commit_after># Copyright (C) 2007-2010 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""
The Telnet protocol.
"""
from Exscript.util.tty            import get_terminal_size
from Exscript.protocols           import telnetlib
from Exscript.protocols.Protocol  import Protocol
from Exscript.protocols.Exception import ProtocolException, \
                                         TimeoutException, \
                                         DriverReplacedException, \
                                         ExpectCancelledException

class Telnet(Protocol):
    """
    The Telnet protocol adapter.
    """

    def __init__(self, **kwargs):
        Protocol.__init__(self, **kwargs)
        self.tn = None

    def _telnetlib_received(self, data):
        self._receive_cb(data, False)
        self.buffer.append(data)

    def _connect_hook(self, hostname, port):
        assert self.tn is None
        rows, cols = get_terminal_size()
        self.tn = telnetlib.Telnet(hostname,
                                   port or 23,
                                   termsize         = (rows, cols),
                                   termtype         = self.termtype,
                                   stderr           = self.stderr,
                                   receive_callback = self._telnetlib_received)
        if self.debug >= 5:
            self.tn.set_debuglevel(1)
        if self.tn is None:
            return False
        return True

    def send(self, data):
        self._dbg(4, 'Sending %s' % repr(data))
        try:
            self.tn.write(data)
        except Exception:
            self._dbg(1, 'Error while writing to connection')
            raise

    def _domatch(self, prompt, flush):
        if flush:
            func = self.tn.expect
        else:
            func = self.tn.waitfor

        # Wait for a prompt.
        clean = self.get_driver().clean_response_for_re_match
        self.response = None
        try:
            result, match, self.response = func(prompt, self.timeout, cleanup = clean)
        except Exception:
            self._dbg(1, 'Error while waiting for ' + repr(prompt))
            raise

        if match:
            self._dbg(2, "Got a prompt, match was %s" % repr(match.group()))
            self.buffer.pop(len(self.response))

        self._dbg(5, "Response was %s" % repr(self.response))

        if result == -1:
            error = 'Error while waiting for response from device'
            raise TimeoutException(error)
        if result == -2:
            if self.driver_replaced:
                self.driver_replaced = False
                raise DriverReplacedException()
            else:
                raise ExpectCancelledException()
        if self.response is None:
            raise ProtocolException('whoops - response is None')

        return result, match

    def cancel_expect(self):
        self.tn.cancel_expect = True

    def _set_terminal_size(self, rows, cols):
        self.tn.set_window_size(rows, cols)

    def interact(self, key_handlers = None, handle_window_size = True):
        return self._open_shell(self.tn.sock, key_handlers, handle_window_size)

    def close(self, force = False):
        if self.tn is None:
            return
        if not force:
            try:
                self.response = self.tn.read_all()
            except Exception:
                pass
        self.tn.close()
        self.tn = None
        self.buffer.clear()
<|endoftext|>


<commit_before># %matplotlib inline
from numpy import *
import matplotlib.pyplot as plt

dt = 0.125
time = arange(0,T+dt,dt)
time_for_refractory_period=0
Vs = zeros(len(time))       # potential (V) trace over time
Rm = 1                      # Rawr! Resistance! (kOhm)
Cm = 10                     # capacitance (uF)
tau_m = Rm*Cm               # time constant (msec)
tau_refractory = 4          # refractory period (msec)
Vth = 1
add_pointy_thing = 0.5      # spike (V)
I = 1.5                     # input current (A)
for i, t in enumerate(time):
    if t > time_for_refractory_period:
        Vs[i] = Vs[i-1] + (-Vs[i-1] + I*Rm) / tau_m * dt
        if Vs[i] >= Vth:
            Vs[i] += add_pointy_thing
            time_for_refractory_period = t + tau_refractory

# Run and plot membrane potential trace
plt.plot(time, Vs); plt.title('Leaky Integrate-and-Fire Example');plt.ylabel('Membrane Potential (V)'); plt.xlabel('Time (msec)'); plt.ylim([0, 2]);
plt.show()
plt.close()
<commit_msg>Added T back<commit_after># %matplotlib inline
from numpy import *
import matplotlib.pyplot as plt

T = 100
dt = 0.125
time = arange(0,T+dt,dt)
time_for_refractory_period=0
Vs = zeros(len(time))       # potential (V) trace over time
Rm = 1                      # Rawr! Resistance! (kOhm)
Cm = 10                     # capacitance (uF)
tau_m = Rm*Cm               # time constant (msec)
tau_refractory = 4          # refractory period (msec)
Vth = 1
add_pointy_thing = 0.5      # spike (V)
I = 1.5                     # input current (A)
for i, t in enumerate(time):
    if t > time_for_refractory_period:
        Vs[i] = Vs[i-1] + (-Vs[i-1] + I*Rm) / tau_m * dt
        if Vs[i] >= Vth:
            Vs[i] += add_pointy_thing
            time_for_refractory_period = t + tau_refractory

# Run and plot membrane potential trace
plt.plot(time, Vs); plt.title('Leaky Integrate-and-Fire Example');plt.ylabel('Membrane Potential (V)'); plt.xlabel('Time (msec)'); plt.ylim([0, 2]);
plt.show()
plt.close()
<|endoftext|>


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

# ==================================================
# open
# --------------------------------------------------

from __future__ import print_function
from __future__ import absolute_import
import os
import tct
import sys

params = tct.readjson(sys.argv[1])
binabspath = sys.argv[2]
facts = tct.readjson(params['factsfile'])
milestones = tct.readjson(params['milestonesfile'])
resultfile = params['resultfile']
result = tct.readjson(resultfile)
loglist = result['loglist'] = result.get('loglist', [])
toolname = params['toolname']
toolname_pure = params['toolname_pure']
workdir = params['workdir']
exitcode = CONTINUE = 0

# ==================================================
# Make a copy of milestones for later inspection?
# --------------------------------------------------

if 0 or milestones.get('debug_always_make_milestones_snapshot'):
    tct.make_snapshot_of_milestones(params['milestonesfile'], sys.argv[1])

# ==================================================
# Get and check required milestone(s)
# --------------------------------------------------

def milestones_get(name, default=None):
    result = milestones.get(name, default)
    loglist.append((name, result))
    return result

def facts_get(name, default=None):
    result = facts.get(name, default)
    loglist.append((name, result))
    return result

def params_get(name, default=None):
    result = params.get(name, default)
    loglist.append((name, result))
    return result

# ==================================================
# define
# --------------------------------------------------

xeq_name_cnt = 0
copied_latex_resources = []

# ==================================================
# Check params
# --------------------------------------------------

if exitcode == CONTINUE:
    loglist.append('CHECK PARAMS')

    configset = milestones_get('configset')

    if not configset:
        exitcode = 22

if exitcode == CONTINUE:
    loglist.append('PARAMS are ok')
else:
    loglist.append('PROBLEMS with params')

if CONTINUE != 0:
    loglist.append({'CONTINUE': CONTINUE})
    loglist.append('NOTHING to do')

# ==================================================
# work
# --------------------------------------------------

if exitcode == CONTINUE:
    make_latex = params_get('make_latex')
    if not make_latex:
        CONTINUE == -2

if exitcode == CONTINUE:
    build_latex = milestones_get('build_latex')
    build_latex_folder = milestones_get('build_latex_folder')
    latex_contrib_typo3_folder = tct.deepget(facts, 'tctconfig', configset, 'latex_contrib_typo3_folder')
    loglist.append(('latex_contrib_typo3_folder', latex_contrib_typo3_folder))
    if not (build_latex and build_latex_folder and latex_contrib_typo3_folder):
        CONTINUE = -1

    if CONTINUE != 0:
        loglist.append('not enough info to start')

if exitcode == CONTINUE:
    if not os.path.isdir(latex_contrib_typo3_folder):
        loglist.append(('is not a directory', latex_contrib_typo3_folder))
        exitcode = 22

if exitcode == CONTINUE:

    import shutil

    for thing in os.listdir(latex_contrib_typo3_folder):
        if thing in ['.', '..', 'Makefile']:
            continue
        srcpath = os.path.join(latex_contrib_typo3_folder, thing)
        destpath = os.path.join(build_latex_folder, thing)
        if os.path.isdir(srcpath):
            shutil.copytree(srcpath, destpath)
        else:
            shutil.copy(srcpath, destpath)
        copied_latex_resources.append(thing)

# ==================================================
# Set MILESTONE
# --------------------------------------------------

if copied_latex_resources:
    result['MILESTONES'].append({
        'copied_latex_resources': copied_latex_resources,
    })

# ==================================================
# save result
# --------------------------------------------------

tct.writejson(result, resultfile)

# ==================================================
# Return with proper exitcode
# --------------------------------------------------

sys.exit(exitcode)
<commit_msg>Improve code run_45-Copy-latex-typo3-stuff.py<commit_after>#!/usr/bin/env python
# coding: utf-8

# ==================================================
# open
# --------------------------------------------------

from __future__ import print_function
from __future__ import absolute_import

import os
import shutil
import sys
import tct

params = tct.readjson(sys.argv[1])
binabspath = sys.argv[2]
facts = tct.readjson(params['factsfile'])
milestones = tct.readjson(params['milestonesfile'])
resultfile = params['resultfile']
result = tct.readjson(resultfile)
loglist = result['loglist'] = result.get('loglist', [])
toolname = params['toolname']
toolname_pure = params['toolname_pure']
workdir = params['workdir']
exitcode = CONTINUE = 0

# ==================================================
# Make a copy of milestones for later inspection?
# --------------------------------------------------

if 0 or milestones.get('debug_always_make_milestones_snapshot'):
    tct.make_snapshot_of_milestones(params['milestonesfile'], sys.argv[1])

# ==================================================
# Helper functions
# --------------------------------------------------

def lookup(D, *keys, **kwdargs):
    result = tct.deepget(D, *keys, **kwdargs)
    loglist.append((keys, result))
    return result

# ==================================================
# define
# --------------------------------------------------

copied_latex_resources = []
xeq_name_cnt = 0

# ==================================================
# Check params
# --------------------------------------------------

if exitcode == CONTINUE:
    loglist.append('CHECK PARAMS')

    make_latex = lookup(milestones, 'make_latex', default=None)
    if not make_latex:
        CONTINUE == -2

if exitcode == CONTINUE:
    build_latex = lookup(milestones, 'build_latex', default=None)
    build_latex_folder = lookup(milestones, 'build_latex_folder', default=None)
    latex_contrib_typo3_folder = tct.deepget(facts, 'tctconfig', 'configset',
                                             'latex_contrib_typo3_folder', default=None)
    if not (1
            and build_latex
            and build_latex_folder
            and latex_contrib_typo3_folder):
        CONTINUE = -2

if exitcode == CONTINUE:
    loglist.append('PARAMS are ok')
else:
    loglist.append('Bad PARAMS or nothing to do')

# ==================================================
# work
# --------------------------------------------------

if exitcode == CONTINUE:
    if not os.path.isdir(latex_contrib_typo3_folder):
        loglist.append(('is not a directory', latex_contrib_typo3_folder))
        CONTINUE = -2

if exitcode == CONTINUE:

    for thing in os.listdir(latex_contrib_typo3_folder):
        if thing in ['.', '..', 'Makefile']:
            continue
        srcpath = os.path.join(latex_contrib_typo3_folder, thing)
        destpath = os.path.join(build_latex_folder, thing)
        if os.path.isdir(srcpath):
            shutil.copytree(srcpath, destpath)
        else:
            shutil.copy(srcpath, destpath)
        copied_latex_resources.append(thing)

# ==================================================
# Set MILESTONE
# --------------------------------------------------

if copied_latex_resources:
    result['MILESTONES'].append({'copied_latex_resources':
                                 copied_latex_resources})

# ==================================================
# save result
# --------------------------------------------------

tct.writejson(result, resultfile)

# ==================================================
# Return with proper exitcode
# --------------------------------------------------

sys.exit(exitcode)
<|endoftext|>