I am working on the file "2. Python Advanced.ipynb".
The first task is:
Object variables
Now let us learn about the data part. The data part, i.e. fields, are nothing but ordinary variables that are bound to the namespaces of the classes and objects. This means that these names are valid within the context of these classes and objects only. That's why they are called name spaces.
There are two types of fields - class variables and object variables which are classified depending on whether the class or the object owns the variables respectively.
Class variables are shared - they can be accessed by all instances of that class. There is only one copy of the class variable and when any one object makes a change to a class variable, that change will be seen by all the other instances.
Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in any way to the field by the same name in a different instance.
An example will make this easy to understand.
Can you write Python code for it?

class Robot:
    ##   Represents a robot, with a name.

    # A class variable, counting the number of robots
    population = 0

    def __init__(self, name):
        ##   Initializes the data.
        self.name = name
        print("(Initializing {})".format(self.name))

        # When this person is created, the robot
        # adds to the population
        Robot.population += 1

    def die(self):
        ##   I am dying.
        print("{} is being destroyed!".format(self.name))

        Robot.population -= 1

        if Robot.population == 0:
            print("{} was the last one.".format(self.name))
        else:
            print("There are still {:d} robots working.".format(
                Robot.population))

    def say_hi(self):
        ##   Greeting by the robot. Yeah, they can do that.
        print("Greetings, my masters call me {}.".format(self.name))

    @classmethod
    def how_many(cls):
        ##   Prints the current population.
        print("We have {:d} robots.".format(cls.population))

droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()

droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()

print("\nRobots can do some work here.\n")

print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()

Robot.how_many()


I am working on the file "2. Python Advanced.ipynb".
The first task is:
Passed by reference
The list is stored at a memory locations and only a reference of this memory location is what the variable holds. So changes applied to one variable reflect in other variables as well.
Can you write Python code for it?

langs = ["Python", "Java", "C++", "C"]
languages = langs
langs.append("C#")
print(langs)
print(languages)


I am working on the file "2. Python Advanced.ipynb".
The first task is:
Searching
Searching is the process of finding a particular item in a collections of items. It is one of the most common problems that arise in computer programming. A search typically answers either True or False as to whether the item is present.
In Python, there is a very easy way to ask whether an item is in a list of items. We use the in operator.
Can you write Python code for it?

# Using in to check if number is present in the list.

print(15 in [3,5,2,4,1])
print('Work' in 'Python Advanced Workshop')


I am working on the file "2. Python Advanced.ipynb".
The first task is:
Since lists are considered to be sequentially ordered, they support a number of operations that can be applied to any Python sequence.
|Operation Name|Operator|Explanation|
|:-------------|:-------|:----------|
|Indexing|[ ]|Access an element of a sequence|
|Concatenation|+|Combine sequences together|
|Repetition|*|Concatenate a repeated number of times|
|Membership|in|Ask whether an item is in a sequence|
|Length|len|Ask the number of items in the sequence|
|Slicing|[ : ]|Extract a part of a sequence|
Can you write Python code for it?

myList = [1,2,3,4]

# Indexing
A = myList[2]
print(A)

# Repititoin
A = [A]*3
print(A)

# Concatenation
print(myList + A)

# Membership
print(1 in myList)

# Length
print(len(myList))

# Slicing [inclusive : exclusive]
print(myList[1:3])

# Leaving the exclusive parameter empty
print(myList[-3:])


I am working on the file "2. Python Advanced.ipynb".
The first task is:
Since the variable answer here is defined within each function seperately, you can reuse the same name of the variable, as the scope of the variables itself is different.
Note : Functions, however, can  access variables that are defined outside of its scope or in the larger scope, but can only read the value of the variable, not modify it. This is shown by the UnboundLocalError in the example below.
Can you write Python code for it?

egg_count = 0

def buy_eggs():
    egg_count += 12 # purchase a dozen eggs

# buy_eggs()


I am working on the file "2. Python Advanced.ipynb".
The first task is:
Sometimes it can be important to get the position of the searched value. In that case, we can use index method for lists and the find method for strings.
Can you write Python code for it?

# Using index to get position of the number if present in list.
# In case of lists, its important to remember that the index function will throw an error if the value isn't present in the list.

values = [3,5,2,4,1]
if 5 in values:
  print("Value present at",values.index(5))  ## 1
else:
  print("Value not present in list")

# Using find to get the index of the first occurrence of the word in a sentence.

sentence = "This be a string"
index = sentence.find("is")
if index == -1:
  print("There is no 'is' here!")
else:
  print("Found 'is' in the sentence at position "+str(index))

# Using index to find words in a list of words
sentence = "This be a string"
words = sentence.split(' ')
if 'is' in words:
  print("Found 'is' in the list at position "+str(words.index('is')))
else:
  print("There is no 'is' here!")


I am working on the file "2. Python Advanced.ipynb".
The first task is:
Sorting using additional key
For more complex custom sorting, sorted() takes an optional "key=" specifying a "key" function that transforms each element before comparison.
The key function takes in 1 value and returns 1 value, and the returned "proxy" value is used for the comparisons within the sort.
Can you write Python code for it?

# Using key in sorted

values = ['ccc', 'aaaa', 'd', 'bb']
print (sorted(values, key=len))            ## ['d', 'bb', 'ccc', 'aaaa']

# Remember case sensitivity :  All upper case characters come before lower case character in an ascending sequence.
sentence = "This is a test string from Andrew"
print(sorted(sentence.split(), key=str.lower))    ## ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

# Using reverse for ascending and descending
strs = ['aa', 'BB', 'zz', 'CC']
print (sorted(strs))                         ## ['BB', 'CC', 'aa', 'zz'] (case sensitive)
print (sorted(strs, reverse=True))          ## ['zz', 'aa', 'CC', 'BB']


I am working on the file "2. Python Advanced.ipynb".
The first task is:
The init
There are many method names which have special significance in Python classes. We will see the significance of the init method now.
The init method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. Notice the double underscores both at the beginning and at the end of the name.
Can you write Python code for it?

class Person:
    def __init__(self, name):
        self.name = name

    def say_hi(self):
        print('Hello, my name is', self.name)

p = Person('Shantanu')
p.say_hi()


I am working on the file "2. Python Advanced.ipynb".
The first task is:
The other type of error, known as a logic error, denotes a situation where the program executes but gives the wrong result. This can be due to an error in the underlying algorithm or an error in your translation of that algorithm. In some cases, logic errors lead to very bad situations such as trying to dividing by zero or trying to access an item in a list where the index of the item is outside the bounds of the list. In this case, the logic error leads to a runtime error that causes the program to terminate. These types of runtime errors are typically called exceptions.
When an exception occurs, we say that it has been raised. You can handle the exception that has been raised by using a try statement. For example, consider the following session that asks the user for an integer and then calls the square root function from the math library. If the user enters a value that is greater than or equal to 0, the print will show the square root. However, if the user enters a negative value, the square root function will report a ValueError exception.
Can you write Python code for it?

import math
anumber = int(input("Please enter an integer "))
# Give input as negative number and also see output from next code snippet
print(math.sqrt(anumber))


I am working on the file "2. Python Advanced.ipynb".
The first task is:
These extend to more than one line. But by using list comprehensions you can bring it down to just one line.
Can you write Python code for it?

# Using list comprehensions

squares = [x**2 for x in range(10)]
exponents = [2**i for i in range(13)]
evenSquares = [x for x in squares if x % 2 == 0]
print("Squares :", squares)           ## [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print("Exponents :", exponents)       ## [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
print("Even Squares :", evenSquares)  ## [0, 4, 16, 36, 64]


I am working on the file "2. Python Advanced.ipynb".
The first task is:
We can handle this exception by calling the print function from within a try block. A corresponding except block catches the exception and prints a message back to the user in the event that an exception occurs. For example:
Can you write Python code for it?

try:
   print(math.sqrt(anumber))
except:
   print("Bad Value for square root")
   print("Using absolute value instead")
   print(math.sqrt(abs(anumber)))


I am working on the file "2. Python Advanced.ipynb".
The first task is:
list.extend(list2): adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
Can you write Python code for it?

# list.extend example
MainChar = ['Ronald Weasley', 'Harry Potter', 'Hermione Granger']
SupChar = ['Neville Longbottom', 'Luna Lovegood']
MainChar.extend(SupChar)
print("Full list :", MainChar)  ## ['Ronald Weasley', 'Harry Potter', 'Hermione Granger', 'Neville Longbottom', 'Luna Lovegood']


I am working on the file "2. Python Advanced.ipynb".
The first task is:
list.index(element): searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use 'in' to check without a ValueError).
Can you write Python code for it?

# list.index example
names = ['Ronald Weasley', 'Harry Potter', 'Hermione Granger']
index = names.index('Harry Potter')
print("Index of Harry Potter in list :",index)  ## 1

# Throws a ValueError (Uncomment to see error.)
# index = names.index('Albus Dumbledore')


I am working on the file "2. Python Advanced.ipynb".
The first task is:
list.pop(index): removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).
Can you write Python code for it?

names = ['Ronald Weasley', 'Harry Potter', 'Hermione Granger']
index = names.pop(1)
print("Modified list :", names)  ## ['Ronald Weasley', 'Hermione Granger']


I am working on the file "2. Python Advanced.ipynb".
The first task is:
list.remove(element): searches for the first instance of the given element and removes it (throws ValueError if not present)
Can you write Python code for it?

names = ['Ronald Weasley', 'Harry Potter', 'Hermione Granger']
index = names.remove('Harry Potter')  ## ['Ronald Weasley', 'Hermione Granger']
print("Modified list :", names)


I am working on the file "2. Python Advanced.ipynb".
The first task is:
list.reverse(): reverses the list in place (does not return it).
Can you write Python code for it?

alphabets = ['a', 'b', 'c', 'd', 'e', 'f']
alphabets.reverse()
print("Reversed list :", alphabets)  ## ['f', 'e', 'd', 'c', 'b', 'a']


I am working on the file "2. Recommender system.ipynb".
The first task is:
1. Tokenization
Can you write Python code for it?

# Select morphemes
feature_pos = ('NNP', 'NNG', 'NNB', 'NP', 'VV', 'VA', 'MAG')

# wrapper
kkma = Kkma()

tags = []
kon_words = []
kon_words_tags = []
# tokenization
for i in cf_df['description'][0]:
    kon_word = kkma.pos(i)
    kon_words.append(kon_word)

# morphemes
des_token =[]
for i in range(0, len(kon_words)):
    tokens = []
    for j in range(0, len(kon_words[i])):
        if kon_words[i][j][1] in feature_pos:
            tokens.append(kon_words[i][j][0])
    des_token.append(tokens)

# transform requirements of doc2vec (words=[], tags=[])
kon_token_tags=[]
for i in np.arange(len(kon_words)):
    kon_token_tag = gensim.models.doc2vec.LabeledSentence(words = des_token[i], tags = [tb_df['id'][i]])
    kon_token_tags.append(kon_token_tag)

#token data save
output = open('des_token.pkl', 'wb')
pickle.dump(des_token, output)
output.close()

#token data load
pkl_file = open('des_token.pkl', 'rb')
des_token = pickle.load(pkl_file)
pkl_file.close()

# token_tags data save
output = open('kon_token_tags.pkl', 'wb')
pickle.dump(kon_token_tags, output)
output.close()

# token_tags data load
pkl_file = open('kon_token_tags.pkl', 'rb')
kon_token_tags = pickle.load(pkl_file)
pkl_file.close()


I am working on the file "2. Recommender system.ipynb".
The first task is:
2. Modeling
Can you write Python code for it?

# doc2vec modeling with tokens
cf_doc2vec_model = gensim.models.Doc2Vec(kon_token_tags, size = 200)

# doc2vec model save (based on konply tokenizer)
cf_doc2vec_model.save('cf_doc2vec_model.model')

# doc2vec model load (based on konply tokenizer)
cf_doc2vec_model = gensim.models.Doc2Vec.load('cf_doc2vec_model.model')

#Find the top-10 most similar words
cf_doc2vec_model.most_similar(['아빠'])

#Find the top-10 most similar words
cf_doc2vec_model.most_similar(['고양이'])


I am working on the file "2. Recommender system.ipynb".
The first task is:
Crowd Funding Recommender system
Idea

For new crowd funding projects, there are no such classificable factors with old projects (I suppose category is not a key factor)
Users are only interested in a project itself (Funding target money, duration, etc are non of their business)
Doc2vec is a quite logical method to classify documents
Classifying descriptions of projects by using Doc2vec -> Recommending 10 nearest projects with a project users clicked or supported

Outline

Tokenizing description
Run Doc2vec
Calculate distance of each project combinations
Recommend
Can you write Python code for it?


I am working on the file "2. Recommender system.ipynb".
The first task is:
looks like each word has relationship

3. Caculate distance

Use cosine distance (https://en.wikipedia.org/wiki/Cosine_similarity)
In this case, cosine distance seems like better option than euclidean distance, because some words would be over-weighted
Can you write Python code for it?

""" cosine distance """
def cos_dist_cal(doc_model):
    import scipy as sp
    # make vector bag
    doc_vecs = []
    dists = []
    id_list = []
    eu_dist = sp.spatial.distance.cosine
    for i in np.arange(len(kon_token_tags)):
        vec = doc_model.infer_vector(kon_token_tags[i][0])
        ids = kon_token_tags[i][1][0]
        id_list.append(ids)
        doc_vecs.append(vec)
    # caculate distance
    for a, b in itertools.combinations(np.arange(len(doc_vecs)), 2):
        dist = eu_dist(doc_vecs[a], doc_vecs[b])
        dists.append((id_list[a], id_list[b], dist))
    return dists

cos_token_df = pd.DataFrame(cos_dist_cal(cf_doc2vec_model))


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:

We can answer the challenge from panel one of the strip:
Can you write Python code for it?

def phrases(text, sep='/'): return {line.upper().strip() for line in text.split(sep)}

starwars = phrases('''The Phantom Menace / Attack of the Clones / Revenge of the Sith /
                      A New Hope / The Empire Strikes Back / Return of the Jedi''')

startrek = phrases('''The Wrath of Khan / The Search for Spock / The Voyage Home /
                      The Final Frontier / The Undiscovered Country / Generations /
                      First Contact / Insurrection / Nemesis''')

report(starwars, startrek)


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Defining the Problem
Let's be clear exactly what we're trying to achieve. We're looking for a Python regular expression which, when used with the re.search function, will match all of the winners but none of the losers.  We can define the function mistakes to return a set of misclassifications; if mistakes is empty  then the regular expression is verified correct:
Can you write Python code for it?

def mistakes(regex, winners, losers):
    "The set of mistakes made by this regex in classifying winners and losers."
    return ({"Should have matched: " + W
             for W in winners if not re.search(regex, W)} |
            {"Should not have matched: " + L
             for L in losers if re.search(regex, L)})

def verify(regex, winners, losers):
    assert not mistakes(regex, winners, losers)
    return True


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Glossary
Just to be clear, I define the terms I will be using:

winners: A set of strings; our solution is required to match each of them.
losers: A set of strings; our solution is not allowed to match any of them.
part: A small regular expression, a string, such as 'bu' or 'a.a'.
pool: A set of parts from which we will pick a subset to form the solution.
regex: A regular expression; a pattern used to match against a string.
solution: A regular expression that matches all winners but no losers.
whole: A part that matches a whole word (and nothing else): '^word$'

Regex Parts
Now we need to define what the regex_parts are.  Here's what I came up with:

For each winner, include a regex that matches the entire string exactly. I call this regex a whole.

Example: for 'word', include '^word$'
For each whole, generate subparts consisting of 1 to 4 consecutive characters.

Example: subparts('^it$') == {'^', 'i', 't', '$', '^i', 'it', 't$', '^it', 'it$', '^it$'}
For each subpart, generate all ways to replace any of the letters with a dot (the "match any" character).

Example: dotify('it') == {'it', 'i.', '.t', '..'}
Keep only the dotified subparts that do not match any of the losers.

Note that I only used a few of the regular expression mechanisms: '.', '^', and '$'.  I didn't try to use character classes ([a-z]), nor any of the repetition operators, nor other advanced mechanisms.  Why?  I thought that the advanced features usually take too many characters. For example, I don't allow the part '[rn]t', but I can achieve the same effect with the same number of characters by combining two parts: 'rt|nt'.  I could add more complicated mechanisms later, but for now, YAGNI. Here is the code:
Can you write Python code for it?

def regex_parts(winners, losers):
    "Return parts that match at least one winner, but no loser."
    wholes = {'^' + w + '$'  for w in winners}
    parts = {d for w in wholes for p in subparts(w) for d in dotify(p)}
    return wholes | {p for p in parts if not matches(p, losers)}

def subparts(word, N=4):
    "Return a set of subparts of word: consecutive characters up to length N (default 4)."
    return set(word[i:i+n+1] for i in range(len(word)) for n in range(N))

def dotify(part):
    "Return all ways to replace a subset of chars in part with '.'."
    choices = map(replacements, part)
    return {cat(chars) for chars in itertools.product(*choices)}

def replacements(c): return c if c in '^$' else c + '.'

cat = ''.join


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Let's  check the xkcd regex:
Can you write Python code for it?

xharag01 = "[gikuj]..n|a.[alt]|[pivo].l|i..o|[jocy]e|sh|di|oo"

mistakes(xkcd, winners, losers)


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Neat—our solution is one character shorter than Randall's.  We can verify that Randall's solution is also correct:
Can you write Python code for it?

verify('M | [TN]|B', starwars, startrek)


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Our regex is 15% shorter than Randall's—success!
Tests
Here's a test suite to give us more confidence in (and familiarity with) our functions:
Can you write Python code for it?

def tests():
    assert subparts('^it$') == {'^', 'i', 't', '$', '^i', 'it', 't$', '^it', 'it$', '^it$'}
    assert subparts('this') == {'t', 'h', 'i', 's', 'th', 'hi', 'is', 'thi', 'his', 'this'}
    subparts('banana') == {'a', 'an', 'ana', 'anan', 'b', 'ba', 'ban', 'bana',
                           'n', 'na', 'nan', 'nana'}

    assert dotify('it') == {'it', 'i.', '.t', '..'}
    assert dotify('^it$') == {'^it$', '^i.$', '^.t$', '^..$'}
    assert dotify('this') == {'this', 'thi.', 'th.s', 'th..', 't.is', 't.i.', 't..s', 't...',
                              '.his', '.hi.', '.h.s', '.h..', '..is', '..i.', '...s', '....'}
    assert regex_parts({'win'}, {'losers', 'bin', 'won'}) == {
        '^win$', '^win', '^wi.', 'wi.',  'wi', '^wi', 'win$', 'win', 'wi.$'}
    assert regex_parts({'win'}, {'bin', 'won', 'wine', 'wit'}) == {'^win$', 'win$'}
    regex_parts({'boy', 'coy'},
                {'ahoy', 'toy', 'book', 'cook', 'boycott', 'cowboy', 'cod', 'buy', 'oy',
                 'foil', 'coyote'}) == {'^boy$', '^coy$', 'c.y$', 'coy$'}

    assert matches('a|b|c', {'a', 'b', 'c', 'd', 'e'}) == {'a', 'b', 'c'}
    assert matches('a|b|c', {'any', 'bee', 'succeed', 'dee', 'eee!'}) == {
        'any', 'bee', 'succeed'}

    assert OR(['a', 'b', 'c']) == 'a|b|c'
    assert OR(['a']) == 'a'

    assert words('this is a test this is') == {'this', 'is', 'a', 'test'}

    assert findregex({"ahahah", "ciao"},  {"ahaha", "bye"}) == 'a.$'
    assert findregex({"this", "that", "the other"}, {"one", "two", "here", "there"}) == 'h..$'
    assert findregex({'boy', 'coy', 'toy', 'joy'}, {'ahoy', 'buy', 'oy', 'foil'}) == '^.oy'

    assert not mistakes('a|b|c', {'ahoy', 'boy', 'coy'}, {'joy', 'toy'})
    assert not mistakes('^a|^b|^c', {'ahoy', 'boy', 'coy'}, {'joy', 'toy', 'kickback'})
    assert mistakes('^.oy', {'ahoy', 'boy', 'coy'}, {'joy', 'ploy'}) == {
        "Should have matched: ahoy",
        "Should not have matched: joy"}
    return 'tests pass'

tests()


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Regex Golf with Arbitrary Lists
Let's move on to arbitrary lists. I define report, to call findregex, verify the solution, and print  the number of characters in the solution, the number of parts, the competitive ratio (the ratio between the lengths of a trivial solution and the actual solution),  and the number of winners   and losers.
Can you write Python code for it?

def report(winners, losers):
    "Find a regex to match A but not B, and vice-versa.  Print summary."
    solution = findregex(winners, losers)
    verify(solution, winners, losers)
    trivial  = '^(' + OR(winners) + ')$'
    print('Characters: {}, Parts: {}, Competitive ratio: {:.1f}, Winners: {}, Losers: {}'.format(
          len(solution), solution.count('|') + 1, len(trivial) / len(solution) , len(winners), len(losers)))
    return solution

report(winners, losers)


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Strategy for Finding a Regex
We need a strategy to find a regex that matches all the winners but none of the losers.  I came up with this approach:

Generate a pool of regex parts: small regexes of a few characters, such as "bu" or "r.e$" or  "j".
Consider only parts that match at least one winner, but no losers.
Our solution will be formed by "or"-ing together some of these parts (e.g. "bu|n.e|j").
This is a set cover problem: pick some of the parts so that they cover all the winners.
Set cover is an NP-hard problem, so I feel justified in using an approximation approach that finds a small but not necessarily smallest solution.
For many NP-hard problems a good approximation can be had with a greedy algorithm: Pick the "best" part first (the one that covers the most winners with the fewest characters), and repeat, choosing the "best" each time until there are no more winners to cover.
To guarantee that we will find a solution, make sure that each winner has at least one part that matches it.

There are three ways this strategy can fail to find the shortest possible regex:

The shortest regex might not be a disjunction. Our strategy can only find disjunctions (of the form "a|b|c|...").
The shortest regex might be a disjunction formed with different parts. For example,  "[rn]t"  is not in our pool of parts.
The greedy algorithm isn't guaranteed to find the shortest solution.  We might have all the right parts, but pick the wrong ones.

The algorithm is below.  Our pool of parts is a set of strings created with regex_parts(winners, losers). We accumulate parts into the list solution, which starts empty. On each iteration choose the best part: the one with a maximum score.  (I decided by default to score 4 points for each winner matched, minus one point for each character in the part.)  We then add the best part to solution, and remove from winners all the strings that are matched by best.  Finally, we update the pool, keeping only those parts that still match one or more of the remaining winners. When there...
Can you write Python code for it?

def findregex(winners, losers, k=4):
    "Find a regex that matches all winners but no losers (sets of strings)."
    # Make a pool of regex parts, then pick from them to cover winners.
    # On each iteration, add the 'best' part to 'solution',
    # remove winners covered by best, and keep in 'pool' only parts
    # that still match some winner.
    pool = regex_parts(winners, losers)
    solution = []
    def score(part): return k * len(matches(part, winners)) - len(part)
    while winners:
        best = max(pool, key=score)
        solution.append(best)
        winners = winners - matches(best, winners)
        pool = {r for r in pool if matches(r, winners)}
    return OR(solution)

def matches(regex, strings):
    "Return a set of all the strings that are matched by regex."
    return {s for s in strings if re.search(regex, s)}

OR = '|'.join # Join a sequence of strings with '|' between them


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
This is interesting because 'a.$|e.$|a.o' is an example of something that could be re-written in a shorter form if we allowed more complex parts. The following would save one character:
Can you write Python code for it?


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
Update (Nov 2015): There are two new movies in the works!

Let's add them:
Can you write Python code for it?

starwars.add('THE FORCE AWAKENS')
startrek.add('BEYOND')

findregex(starwars, startrek)


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
We can see that there are multiple names that are both winners and losers:
Can you write Python code for it?


I am working on the file "2. Semester/ISJ - Scripting Languages/1. Project/xharag01.ipynb".
The first task is:
What To Do Next?
I see two options:

Stop here and declare victory! Yay!
Try to make the program faster and capable of finding shorter regexes.

My first inclination was "stop here", and that's what this notebook will shortly do. But several correspondents offered very interesting suggestions, so I returned to the problem in a second notebook.
I was asked whether Randall was wrong to come up with "only" a 10-character Star Wars regex, whereas I showed there is a 9-character version. I would say that, given his role as a cartoonist, author, public speaker, educator, and entertainer, he has chosen ... wisely. He wrote a program that was good enough to allow him to make a great webcomic. A 9-character regex would not improve the comic. Randall stated that he used a genetic algorithm to find his regexes, and it has been said that genetic algorithms are often the second (or was it the third?) best method to solve any problem, and that's all he needed. But if you consider that in addition to all those roles, Randall is also still a practicing computer scientist, you could say
he chose ... poorly. Genetic algorithms are good when you want to combine the structure of two solutions to yield a better solution, so they would work well if the best regexes had a complicated tree structure. But they don't! The best solutions are disjunctions of small parts. So the genetic algorithm is trying to combine the first half of one disjunction with the second half of another—but that isn't useful, because the components of a disjunction are unordered; imposing an ordering on them doesn't help.
Summary
That was fun! I hope this page gives you an idea of how to think about problems like this. Let's review what we did:

Found an interesting problem (in a comic strip) and realized that it would not be hard to program a solution.
Wrote the function mistakes to prove that we really understand exactly what the problem is.
Came up with an approach: create lots of regex parts, and "or" together a subset of them.
Realized that this...
Can you write Python code for it?


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:

Can you write Python code for it?

topicCoupling = topics.topic_coupling(model, threshold=0.2)

print '%i nodes and %i edges' % (topicCoupling.order(), topicCoupling.size())

to_graphml(topicCoupling, '/Users/erickpeirson/Desktop/lda_topicCoupling.graphml')


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
Combining DfR and WoS data
We can combine our datasets using the merge() function. First, we load our WoS data in a separate Corpus:
Can you write Python code for it?

from tethne.readers import wos
wos_corpus = wos.read('/Users/erickpeirson/Dropbox/HSS ThatCamp Workshop/sample_data/wos')


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
FeatureSets
Our wordcount data are represented by a FeatureSet. A FeatureSet is a description of how certain sets of elements are distributed across a Corpus. This is kind of like an inversion of an index. For example, we might be interested in which words (elements) are found in which Papers. We can think of authors as a FeatureSet, too.
All of the available FeatureSets are available in the features attribute (a dictionary) of our Corpus. We can see the available FeatureSets by inspecting its:
Can you write Python code for it?


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
If we add the argument mode='documentCounts', we get the number of documents in which 'evolutionary' occurs.
Can you write Python code for it?

plt.figure(figsize=(10, 5))

plt.bar(*corpus.feature_distribution('wordcounts', 'evolutionary', mode='documentCounts'))    # <-- The action.

plt.ylabel('Documents containing ``evolutionary``')
plt.xlabel('Publication Date')
plt.show()


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
Next we'll fit the model. We need to tell MALLET how many topics to fit (the hyperparameter Z), and how many iterations (max_iter) to perform. This step may take a little while, depending on the size of your corpus.
Can you write Python code for it?


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
Note that we can look how documents themselves are distributed using the distribution() method.
Can you write Python code for it?

plt.figure(figsize=(10, 5))

plt.bar(*corpus.distribution())    # <-- The action.

plt.ylabel('Number of Documents')
plt.xlabel('Publication Date')
plt.show()


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
Once again, read() accepts a string containing a path to either a single DfR dataset, or a directory containing several. Here, "DfR dataset" refers to the folder containing the file "citations.xml", and the contents of that folder.
This will take considerably more time than loading a WoS dataset. The reason is that Tethne automatically detects and parses all of the wordcount data.
Can you write Python code for it?

dfr_corpus = dfr.read('/Users/erickpeirson/Dropbox/HSS ThatCamp Workshop/sample_data/DfR')


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
The LDA topic model
Tethne provides a class called LDAModel. You should be able to import it directly from the tethne package:
Can you write Python code for it?


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
The terms function generates a network of words connected on the basis of shared affinity with a topic. If two words i and j are both associated with a topic z with $\Phi(i|z) >= 0.01$ and $\Phi(j|z) >= 0.01$, then an edge is drawn between them.
Can you write Python code for it?

termGraph = topics.terms(model, threshold=0.01)

termGraph.order(), termGraph.size()

termGraph.name = ''

from tethne.writers.graph import to_graphml
to_graphml(termGraph, '/Users/erickpeirson/Desktop/topic_terms.graphml')


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
We apply the stoplist using the transform() method. FeatureSets are not modified in place; instead, a new FeatureSet is generated that reflects the specified changes. We'll call the new FeatureSet 'wordcounts_filtered'.
Can you write Python code for it?


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
We can use the feature_distribution() method of our Corpus to look at the distribution of words over time. In the example below I used MatPlotLib to visualize the distribution.
Can you write Python code for it?

plt.figure(figsize=(10, 5))

plt.bar(*corpus.feature_distribution('wordcounts', 'evolutionary'))    # <-- The action.

plt.ylabel('Frequency of the word ``evolutionary`` in this Corpus')
plt.xlabel('Publication Date')
plt.show()


I am working on the file "2. Working with data from JSTOR Data-for-Research.ipynb".
The first task is:
We then create a new Corpus by passing both Corpus objects to merge(). If there is conflicting information in the two corpora, the first Corpus gets priority.
Can you write Python code for it?


I am working on the file "2. Бостон.ipynb".
The first task is:

Задача 1.

        Обучите LinearRegression из пакета sklearn.linear_model на обучающей выборке (X_train, y_train) и измерьте качество на X_test.

        P.s. Ошибка должна быть в районе 20.

Can you write Python code for it?

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score

clf = LinearRegression()
clf.fit(X_train, y_train);

print('Вышла средняя ошибка, равная %5.4f' % \
            (-np.mean(cross_val_score(clf, X_test, y_test, cv=5, scoring='neg_mean_squared_error'))))


I am working on the file "2. Бостон.ipynb".
The first task is:

Задача 2. (с подвохом)

        Обучите SGDRegressor из пакета sklearn.linear_model на обучающей выборке (X_train, y_train) и измерьте качество на X_test.

Can you write Python code for it?

from sklearn.linear_model import SGDRegressor
from sklearn.preprocessing import StandardScaler

ss = StandardScaler()
X_scaled = ss.fit_transform(X_train)
y_scaled = ss.fit_transform(y_train)

sgd = SGDRegressor()
sgd.fit(X_scaled, y_scaled);

print('Вышла средняя ошибка, равная %5.4f' % \
            (-np.mean(cross_val_score(sgd, X_scaled, y_scaled, cv=5, scoring='neg_mean_squared_error'))))


I am working on the file "2. Бостон.ipynb".
The first task is:

Задача 3.

        Попробуйте все остальные классы:

Ridge

Lasso

ElasticNet

        В них, как вам уже известно, используются параметры регуляризации alpha. Настройте его как с помощью GridSearchCV, так и с помощью готовых -CV классов (RidgeCV, LassoCV и т.д.).

        Найдите уже, в конце-концов, самую точную линейную модель!

Can you write Python code for it?

from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import RidgeCV

############Ridge
params = {
    'alpha': [10**x for x in range(-2,3)]
}

from sklearn.linear_model import Ridge

gsR = RidgeCV() #GridSearchCV(Ridge(), param_grid=params)
gsR.fit(X_train, y_train);

print('Вышла средняя ошибка, равная %5.4f' % \
            (-np.mean(cross_val_score(gsR, X_test, y_test, cv=5, scoring='neg_mean_squared_error'))))

############Lasso
from sklearn.linear_model import Lasso
from sklearn.linear_model import LassoCV

gsL = GridSearchCV(Lasso(), param_grid=params) #LassoCV() - медленнее
gsL.fit(X_train, y_train);

print('Вышла средняя ошибка, равная %5.4f' % \
            (-np.mean(cross_val_score(gsL, X_test, y_test, cv=5, scoring='neg_mean_squared_error'))))

from sklearn.linear_model import ElasticNet
from sklearn.linear_model import ElasticNetCV

gsE = GridSearchCV(ElasticNet(), param_grid=params) #ElasticNetCV() - просто заменить, не слишком точен
gsE.fit(X_train, y_train);

print('Вышла средняя ошибка, равная %5.4f' % \
            (-np.mean(cross_val_score(gsE, X_test, y_test, cv=5, scoring='neg_mean_squared_error'))))


I am working on the file "2. Бостон.ipynb".
The first task is:
Домашка!
Разделим данные на условно обучающую и отложенную выборки:
Can you write Python code for it?

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED)

X_train.shape, y_train.shape, X_test.shape, y_test.shape


I am working on the file "2. Бостон.ipynb".
The first task is:
Загрузим данные
Can you write Python code for it?

from sklearn.datasets import load_boston

bunch = load_boston()

print(bunch.DESCR)

X, y = pd.DataFrame(data=bunch.data, columns=bunch.feature_names.astype(str)), bunch.target

X.head()


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:

Can you write Python code for it?

def international_collaboration(subset):
    node_counts = Counter()
    edge_counts = Counter()

    for paper in subset:
        if not hasattr(paper, 'authorAddress'):
            continue

        addresses = getattr(paper, 'authorAddress', [])
        if not type(addresses) is list:
            addresses = [addresses]

        countries = [extract_country(address) for address in addresses]
        # Combinations is pretty cool. It will give us all of the
        #  possible combinations of countries in this paper.
        for u, v in combinations(countries, 2):
            edge_key = tuple(sorted([u, v]))
            edge_counts[edge_key] += 1.

        for u in set(countries):
            node_counts[u] += 1.

    graph = nx.Graph()
    for u, count in node_counts.items():
        graph.add_node(u, weight=count)

    for (u, v), count in edge_counts.items():
        if count > 1.:
            graph.add_edge(u, v, weight=count)

    return graph

years = []
graphs = []
for year, subset in metadata.slice(window_size=3):
    graph = international_collaboration(subset)
    graphs.append(graph)
    years.append(year)
    print year, graph.order(), graph.size(),


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
2.0. Co-author Graphs
Social network analysis is a popular method in the field of historical network research. By far the most accessible source of information about social networks in science are bibliographic metadata: we can examine relationships among published authors by modeling coauthorship as a graph. Of course, using bibliographic metadata alone has some drawbacks; for one, it renders many participants in scientific production invisible by focusing only on those who "merit" authorship -- laboratory technicians and other support staff, for example, aren't usually included as authors. Nevertheless, the sheer volume of available data makes bibliographic metadata an attractive place to start.
In this notebook, we'll use metadata collected from the Web of Science database to build a coauthor graph. For background, reading, and resources (including instructions on how to download WoS metadata in the correct format), please see this page.
Loading data
I have provided some sample WoS metadata in the data directory. Tethne (the Python package that we will use to parse these metadata) can parse multiple files at once; we just pass the path to the folder that contains our metadata files.
This set of metadata is from several years of the journal Plant Physiology -- so this is an artificially myopic sample of texts, but it works just fine for demonstration purposes.
Can you write Python code for it?

from tethne.readers import wos

metadata = wos.read('../data/Baldwin/PlantPhysiology',
                    streaming=True, index_fields=['date'], index_features=['authors'])


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
Change over time
We can use Tethne to look at how graphs evolve. To do this, we use the slice() method. slice() is an iterator, which means that you can iterate over it (e.g. use it in a for-loop). It yields subsets of the metadata, and we can use those build a graph for each time period.
By default, it returns a subset for each year.
Can you write Python code for it?

graphs = []
years = []
for year, subset in metadata.slice(feature_name='authors'):
    graph = coauthors(subset)
    graphs.append(graph)
    years.append(year)
    print '\r', year,

for year, graph in zip(years, graphs):
    print year, graph.order(), graph.size(), nx.number_connected_components(graph)

nx.write_graphml(graphs[0], 'coauthors_1999.graphml')


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
Closeness Centrality
Now let's try closeness centrality. Recall that:
$
C(x) = \frac{1}{\sum_y d(y, x)}
$
Where $d(y, x)$ is the distance between the nodes $y$ and $x$. $d(y, x)$ can be any function that calculates a distance value for two nodes. $y$ is all of the other nodes in the network (i.e. that are not $x$) reachable from $x$.
By default, NetworkX uses shortest path length as the distance parameter, and normalizes closeness based on the size of the connected component (i.e. the number of possible paths).
Can you write Python code for it?

closeness_data = pd.DataFrame(columns=['Surname', 'Forename', 'Closeness'])

i = 0
for (surname, forename), d in nx.closeness_centrality(graph).items():
    closeness_data.loc[i] = [surname, forename, d]
    i += 1

plt.hist(closeness_data.Closeness)
plt.ylabel('Number of nodes')
plt.xlabel('Closeness Centrality')
plt.show()

# And we can get the nodes with the highest closeness...
sort_indices = np.argsort(closeness_data.Closeness)[::-1]

# Here are the nodes with the highest degree.
for i in sort_indices[:5]:    # (just the top 5)
    print closeness_data.loc[i]
    print '-' * 25


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
Has collaboration between Netherlands and specific neighbors changed over time?
Can you write Python code for it?

collaboration_england = netherlands_data[netherlands_data.Neighbor == 'England']
plt.scatter(collaboration_england.Year, collaboration_england.Collaboration)
plt.show()

collaboration_usa = netherlands_data[netherlands_data.Neighbor == 'USA']
plt.scatter(collaboration_usa.Year, collaboration_usa.Collaboration)
plt.show()


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
How about with itself? I.e. is Netherlands becoming more or less externally collaborative over time?
Can you write Python code for it?

collaboration_self = netherlands_data[netherlands_data.Neighbor == 'Netherlands']
plt.scatter(collaboration_self.Year, collaboration_self.Collaboration)
plt.show()


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
I wonder if that upward trend has anything to do with the overall number of authors or number of papers in each time period.
Can you write Python code for it?

authorship_data = pd.DataFrame(columns=['Year', 'AverageNumAuthors', 'NumPapers'])
i = 0
for year, subset in metadata.slice(window_size=3):
    avg_no_authors = np.mean([len(paper.authors) for paper in subset])
    authorship_data.loc[i] = [year, avg_no_authors, len(subset)]
    i += 1

plt.scatter(authorship_data.Year, authorship_data.AverageNumAuthors, c='green')
plt.legend(loc=4)
plt.ylabel('Average Number of Authors (green)')
ax = plt.gca()
ax2 = plt.twinx()

ax2.scatter(authorship_data.Year, authorship_data.NumPapers)
plt.ylabel('Number of Papers (blue)')

plt.xlabel('Year')
plt.legend(loc='best')
plt.show()


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
International collaboration
We can use the information in the authorAddress attribute of each record to look at collaboration between scientists in different countries.
Note that the address is a bit "messy" -- it was written for human use, not for computers.
Can you write Python code for it?

metadata[5].authorAddress

metadata[205].authorAddress

len(metadata)


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
Let's make a graph by hand.
Can you write Python code for it?

from collections import Counter
from itertools import combinations

node_counts = Counter()
edge_counts = Counter()

for paper in metadata:
    if not hasattr(paper, 'authorAddress'):
        continue

    addresses = getattr(paper, 'authorAddress', [])
    if not type(addresses) is list:
        addresses = [addresses]

    countries = [extract_country(address) for address in addresses]
    # Combinations is pretty cool. It will give us all of the
    #  possible combinations of countries in this paper.
    for u, v in combinations(countries, 2):
        edge_key = tuple(sorted([u, v]))
        edge_counts[edge_key] += 1.

    for u in set(countries):
        node_counts[u] += 1.

international = nx.Graph()
for u, count in node_counts.items():
    international.add_node(u, weight=count)

for (u, v), count in edge_counts.items():
    if count > 1. and u != v:
        international.add_edge(u, v, weight=count)

nx.adjacency_matrix(international).todense()

plt.imshow(nx.adjacency_matrix(international).todense(),
           interpolation='none')

print international.order(), \
      international.size(), \
       nx.number_connected_components(international)

nx.write_graphml(international, 'international.graphml')


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
So we define a procedure to extract the country from each address. Note that the country seems to come at the end, so...
* We split on commas and take the last element;
* We have to strip the '.' at the end;
* Note that USA addresses have state zip and country in the last position;
Can you write Python code for it?

def extract_country(address):
    country = address.split(',')[-1].strip().replace('.', '')
    if country.endswith('USA'):
        return u'USA'
    return country

print metadata[205].authorAddress[0]
print extract_country(metadata[205].authorAddress[0])


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
The resulting graph is pretty sparse.

Sometimes it's helpful to use a sliding time-window: select subsets of several years, that overlap. This "smooths" things out. We'll try 3 years, since that's a pretty typical funding cycle.
Can you write Python code for it?

graphs = []
years = []
for year, subset in metadata.slice(window_size=3, feature_name='authors'):
    graph = coauthors(subset)
    graphs.append(graph)
    years.append(year)
    print '\r', year,

for year, graph in zip(years, graphs):
    print year, graph.order(), graph.size(), nx.number_connected_components(graph)

nx.write_graphml(graphs[0], 'coauthors_1999-2001.graphml')


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
The resulting graph looks something like this:

There is a large connected component in the upper left, and then a whole host of smaller connected components.
Central authors
Depending on our research question, we may be interested in identifying the most "important" or "central" nodes in the coauthor graph. Networkx has a whole bunch of algorithms that you can use to analyze your graph. Please take a look through the list; there are quite a few useful functions in there!
Degree Centrality
Let's evaluate the degree-centrality of the nodes in our network.
Can you write Python code for it?

degree_data = pd.DataFrame(columns=['Surname', 'Forename', 'Degree'])

i = 0
for (surname, forename), d in nx.degree(graph).items():
    degree_data.loc[i] = [surname, forename, d]
    i += 1

plt.hist(degree_data.Degree, bins=np.arange(0, 70, 1))
plt.ylabel('Number of nodes')
plt.xlabel('Degree')
plt.show()

# np.argsort() gives us row indices in ascending order of
#  value. [::-1] reverses the array, so that the values are
#  descending.
sort_indices = np.argsort(degree_data.Degree)[::-1]

# Here are the nodes with the highest degree.
for i in sort_indices[:5]:    # (just the top 5)
    print degree_data.loc[i]
    print '-' * 25


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
This looks a bit better:

Following a specific node.
Can you write Python code for it?

focal_author = ('FERNIE', 'ALISDAIR R')
fernie_data = pd.DataFrame(columns=['Year', 'Degree'])
i = 0
for year, graph in zip(years, graphs):
    degree = nx.degree(graph)
    # If the focal author is not in the graph for this time-period, then
    #  we will assign a closeness of 0.0.
    focal_degree = degree.get(focal_author, 0.0)
    fernie_data.loc[i] = [year, focal_degree]
    i += 1

plt.scatter(fernie_data.Year, fernie_data.Degree)
plt.ylabel('Degree Centrality')
plt.xlabel('Year')

fernie_closeness = pd.DataFrame(columns=['Year', 'Closeness'])
i = 0
for year, subset in metadata.slice(window_size=3):
    graph = coauthors(subset, min_weight=2.)

    if focal_author in graph.nodes():
        focal_closeness = nx.algorithms.closeness_centrality(graph, u=focal_author)
    else:
        focal_closeness = 0.0

    fernie_closeness.loc[i] = [year, focal_closeness]
    print '\r', year,

    i += 1

plt.scatter(fernie_closeness.Year, fernie_closeness.Closeness)
plt.ylabel('Closeness Centrality')
plt.xlabel('Year')
plt.show()


I am working on the file "2.0. Co-author networks/2.0. Co-Author Graphs.ipynb".
The first task is:
With whom does the Netherlands collaborate?
Can you write Python code for it?

netherlands_data = pd.DataFrame(columns=['Year', 'Neighbor', 'Collaboration'])

i = 0
for year, graph in zip(years, graphs):
    if 'Netherlands' not in graph.nodes():
        continue

    counts = Counter()
    for neighbor in graph.neighbors('Netherlands'):
        counts[neighbor] += graph['Netherlands'][neighbor]['weight']

    N_all = sum(counts.values())
    for neighbor, count in counts.items():
        netherlands_data.loc[i] = [year, neighbor, count/N_all]
        i += 1

    print '\r', year,

grouped = netherlands_data.groupby('Neighbor')

collaboration_means = grouped.Collaboration.mean()
collaboration_std = grouped.Collaboration.std()

grouped.Collaboration.describe()

positions = np.arange(len(collaboration_means))
plt.bar(positions, collaboration_means.values, yerr=collaboration_std, alpha=0.5)
plt.xticks(positions + 0.4, collaboration_means.keys(), rotation=90)
plt.ylabel('Proportion of collaborations (with standard error)')
plt.show()


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
Adding Spots
Let's add one spot to each of our stars in the binary.
NOTE: the parameter name was changed from "colon" to "long" in 2.0.2.  For all further releases in 2.0.X, the "colon" parameter still exists but is read-only.  Starting with 2.1.0, the "colon" parameter will no longer exist.
A spot is a feature, and needs to be attached directly to a component upon creation.  Providing a tag for 'feature' is entirely optional - if one is not provided it will be created automatically.
Can you write Python code for it?


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
As a shortcut, we can also call add_spot directly.
Can you write Python code for it?


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

%matplotlib inline

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
At a later time, the spot is still technically at the same coordinates, but longitude of 0 no longer corresponds to pointing to the companion star.  The coordinate system has rotated along with the asyncronous rotation of the star.
Can you write Python code for it?

axs, artists = b.plot(time=0.25, facecolor='teffs', facecmap='YlOrRd', edgecolor=None, y='zs')

axs, artists = b.plot(time=0.5, facecolor='teffs', facecmap='YlOrRd', edgecolor=None, y='zs')

axs, artists = b.plot(time=0.75, facecolor='teffs', facecmap='YlOrRd', edgecolor=None, y='zs')


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
At time=t0=0, we can see that the spot is where defined: 45 degrees south of the north pole and 90 degree longitude (where longitude of 0 is defined as pointing towards the companion star at t0).
Can you write Python code for it?

print "t0 = {}".format(b.get_value('t0', context='system'))

axs, artists = b.plot(time=0, facecolor='teffs', facecmap='YlOrRd', edgecolor=None, y='zs')


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
Spot Corotation
NOTE: spots failed to corotate correctly before version 2.0.2.
The positions (colat, long) of a spot are defined at t0 (note: t0@system, not necessarily t0_perpass or t0_supconj).  If the stars are not synchronous, then the spots will corotate with the star.  To illustrate this, let's set the syncpar > 1 and plot the mesh at three different phases from above.
Can you write Python code for it?

b.set_value('syncpar@primary', 1.5)

b.add_dataset('mesh', times=[0,0.25,0.5,0.75,1.0])

b.run_compute(irrad_method='none')


I am working on the file "2.0/examples/binary_spots.ipynb".
The first task is:
To see the spot, let's compute and plot the protomesh.
Can you write Python code for it?

b.run_compute(protomesh=True)

axs, artists = b.plot(component='primary', facecolor='teffs', facecmap='YlOrRd', edgecolor=None)


I am working on the file "2.0/examples/detached_rotstar.ipynb".
The first task is:
Adding Datasets
Now we'll create an empty mesh dataset at quarter-phase so we can compare the difference between using roche and rotstar for deformation potentials:
Can you write Python code for it?


I am working on the file "2.0/examples/detached_rotstar.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()


I am working on the file "2.0/examples/detached_rotstar.ipynb".
The first task is:
Plotting
Can you write Python code for it?

axs, artists = b['rochemodel'].plot()

axs, artists = b['rotstarmodel'].plot()


I am working on the file "2.0/examples/detached_rotstar.ipynb".
The first task is:
Running Compute
Let's set the radius of the primary component to be large enough to start to show some distortion when using the roche potentials.
Can you write Python code for it?


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
And also plot the residuals of both the primary and secondary RVs (notice the scale on the y-axis)
Can you write Python code for it?

artist, = plt.plot(b.get_value('rvs@rvdyn@primary@phoebe2marchingmodel') - b.get_value('rvs@rvdyn@primary@phoebe1model'), color='b', ls=':')
artist, = plt.plot(b.get_value('rvs@rvdyn@secondary@phoebe2marchingmodel') - b.get_value('rvs@rvdyn@secondary@phoebe1model'), color='b', ls='-.')
artist = plt.axhline(0.0, linestyle='dashed', color='k')
ylim = plt.ylim(-1e-12, 1e-12)


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
And set the two RV datasets to use the correct methods (for both compute options)
Can you write Python code for it?

b.set_value_all('rv_method', dataset='rvdyn', value='dynamical')

b.set_value_all('rv_method', dataset='rvnum', value='flux-weighted')


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

%matplotlib inline

import phoebe
from phoebe import u
import numpy as np
import matplotlib.pyplot as plt

phoebe.devel_on()  # needed to use WD-style meshing, which isn't fully supported yet

logger = phoebe.logger()

b = phoebe.default_binary()
b['q'] = 0.7


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
Dynamical RVs
Since the dynamical RVs don't depend on the mesh, there should be no difference between the 'phoebe2marching' and 'phoebe2wd' synthetic models.  Here we'll just choose one to plot.
Can you write Python code for it?

axs, artists = b['rvdyn@phoebe1model'].plot(color='r')


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
Let's add compute options for phoebe using both the new (marching) method for creating meshes as well as the WD method which imitates the format of the mesh used within legacy.
Can you write Python code for it?

b.add_compute(compute='phoebe2marching', irrad_method='none', mesh_method='marching')

b.add_compute(compute='phoebe2wd', irrad_method='none', mesh_method='wd', eclipse_method='graham')


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
Let's also disable other special effect such as heating, gravity, and light-time effects.
Can you write Python code for it?

b.set_value_all('ld_func', 'logarithmic')
b.set_value_all('ld_coeffs', [0.,0.])

b.set_value_all('irrad_method', 'none') # phoebe
b.set_value('refl_num', 0) # legacy

b.set_value_all('rv_grav', False)

b.set_value_all('ltte', False)


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
Let's use the external atmospheres available for both phoebe1 and phoebe2
Can you write Python code for it?


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
Now let's plot the residuals between these two models
Can you write Python code for it?

artist, = plt.plot(b.get_value('fluxes@lc01@phoebe2marchingmodel') - b.get_value('fluxes@lc01@phoebe1model'), 'g-')
artist = plt.axhline(0.0, linestyle='dashed', color='k')


I am working on the file "2.0/examples/legacy.ipynb".
The first task is:
Plotting
Light Curve
Can you write Python code for it?

axs, artists = b['lc01@phoebe2marchingmodel'].plot(color='g')
axs, artists = b['lc01@phoebe1model'].plot(color='r')
leg = plt.legend(loc=4)


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

%matplotlib inline

import phoebe
from phoebe import u
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary(contact_binary=True)


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Finally, let's compute our models
Can you write Python code for it?

b.run_compute(compute='phoebe2', model='phoebe2model', irrad_method='none')

b.run_compute(compute='phoebe1', model='phoebe1model')


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Let's add compute options for phoebe using the new (marching) method for creating meshes.
Can you write Python code for it?


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Let's also disable other special effect such as heating, gravity, and light-time effects.
Can you write Python code for it?

b.set_value_all('ld_func', 'logarithmic')
b.set_value_all('ld_coeffs', [0.0, 0.0])

b.set_value_all('refl_num',0)
b.set_value_all('rv_grav', False)
b.set_value_all('ltte', False)


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Let's use the external atmospheres available for both phoebe1 and phoebe2
Can you write Python code for it?


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Now we add compute options for the 'legacy' backend.
Can you write Python code for it?


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Plotting
Light Curve
Can you write Python code for it?

axs, artists = b['lc01@phoebe2model'].plot(color='g')
axs, artists = b['lc01@phoebe1model'].plot(color='r')
leg = plt.legend(loc=4)


I am working on the file "2.0/examples/legacy_contact_binary.ipynb".
The first task is:
Set value of gridsize for the trapezoidal (WD) mesh.
Can you write Python code for it?


I am working on the file "2.0/examples/legacy_spots.ipynb".
The first task is:
Adding Spots and Compute Options
Can you write Python code for it?

b.add_spot(component='primary', relteff=0.8, radius=20, colat=45, colon=90, feature='spot01')

b.add_dataset('lc', times=np.linspace(0,1,101))

b.add_compute('phoebe', irrad_method='none', compute='phoebe2')

b.add_compute('legacy', refl_num=0, compute='phoebe1')


I am working on the file "2.0/examples/legacy_spots.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

%matplotlib inline

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()


I am working on the file "2.0/examples/legacy_spots.ipynb".
The first task is:
Let's use the external atmospheres available for both phoebe1 and phoebe2
Can you write Python code for it?

b.set_value_all('atm', 'extern_planckint')

b.set_value_all('ld_func', 'logarithmic')
b.set_value_all('ld_coeffs', [0.0, 0.0])

b.run_compute('phoebe2', model='phoebe2model')

b.run_compute('phoebe1', model='phoebe1model')


I am working on the file "2.0/examples/legacy_spots.ipynb".
The first task is:
Plotting
Can you write Python code for it?

axs, artists = b.plot()
legend = plt.legend()
ylims = plt.ylim(1.94, 2.02)


I am working on the file "2.0/examples/mesh_wd.ipynb".
The first task is:
And see which elements are visible at the current time.  This defaults to use the 'RdYlGn' colormap which will make visible elements green, partially hidden elements yellow, and hidden elements red.  Note that the observer is in the positive z-direction.
Can you write Python code for it?

axs, artists = b['secondary@mesh01@model'].plot(time=0.0, x='xs', y='zs', edgecolor='None', facecolor='visibilities')


I am working on the file "2.0/examples/mesh_wd.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()


I am working on the file "2.0/examples/mesh_wd.ipynb".
The first task is:
Now let's zoom in so we can see the layout of the triangles.  Note that Wilson-Devinney uses trapezoids, but since PHOEBE uses triangles, we take each of the trapezoids and split it into two triangles.
Can you write Python code for it?

axs, artists = b['primary@mesh01@model'].plot(time=0.0, edgecolor='b', facecolor='0.9', xlim=(-0.2,0.2), ylim=(-0.2,0.2))


I am working on the file "2.0/examples/mesh_wd.ipynb".
The first task is:
Plotting
Can you write Python code for it?

axs, artists = b['mesh01@model'].plot(time=0.5)


I am working on the file "2.0/examples/mesh_wd.ipynb".
The first task is:
Running Compute
Can you write Python code for it?


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
As you can see, the overcontact system has an additional "component" with method "envelope" and component label "contact_envelope".
Next let's look at the parameters in this envelope component
Can you write Python code for it?

print b_cb.filter(component='contact_envelope', kind='envelope', context='component')

b_cb['pot@contact_envelope'] = 3.5

b_cb['pot@contact_envelope']


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
But dynamical quantities are still attached for each star component - regardless of whether they're in a detached or overcontact system
Can you write Python code for it?

print b_cb['orb01@model'].components

print b_detached['orb01@model'].components


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
Here we'll initialize a default binary, but ask for it to be created as an overcontact
Can you write Python code for it?


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
Hierarchy
Let's first look at the hierarchy of the default detached binary, and then compare that to the hierarchy of the overcontact system
Can you write Python code for it?

print b_detached.hierarchy

print b_cb.hierarchy


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
Light Curves
Can you write Python code for it?

axs, artists = b_cb['lc01@model'].plot()

axs, artists = b_detached['lc01@model'].plot()


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
RVs
Can you write Python code for it?

axs, artists = b_cb['rv01@model'].plot()

axs, artists = b_detached['rv01@model'].plot()


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
Running Compute
Can you write Python code for it?


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
Synthetics
The synthetic meshes for our overcontact system are attached to the envelope component, whereas the detached system are attached to the two star components
Can you write Python code for it?

print b_cb['mesh01@model'].components

print b_detached['mesh01@model'].components


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
The individual stars are still there, but since the surface is being defined by the contact envelope, most of the parameters are no longer relevant.
Can you write Python code for it?

print b_cb.filter(component='primary', kind='star', context='component')


I am working on the file "2.0/examples/minimal_contact_binary.ipynb".
The first task is:
We'll compare this to the default detached binary
Can you write Python code for it?


I am working on the file "2.0/examples/minimal_synthetic.ipynb".
The first task is:
Adding Datasets
Now we'll create an empty lc dataset:
Can you write Python code for it?


I am working on the file "2.0/examples/minimal_synthetic.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?


I am working on the file "2.0/examples/minimal_synthetic.ipynb".
The first task is:
Running Compute
Now we'll compute synthetics at the times provided using the default options
Can you write Python code for it?


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Adding Datasets
Now we'll add radial velocity and mesh datasets.  We'll add two identical datasets for RVs so that we can have one computed dynamically and the other computed numerically (these options will need to be set later).
Can you write Python code for it?

b.add_dataset('rv', times=np.linspace(0,2,201), dataset='dynamicalrvs')
# TODO: can't set rv_method here because compute options don't exist yet... and that's kind of annoying

b.add_dataset('rv', times=np.linspace(0,2,201), dataset='numericalrvs')


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
But the numerical method integrates over the visible surface elements, giving us what we'd observe if deriving RVs from observed spectra of the binary.  Here we do see the Rossiter McLaughlin effect.  You'll also notice that RVs are not available for the secondary star when its completely occulted (they're nans in the array).
Can you write Python code for it?

axs, artists = b['numericalrvs@model'].plot(component='primary', color='b')
axs, artists = b['numericalrvs@model'].plot(component='secondary', color='r')


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Here you can see that the secondary star is blocking part of the "red" RVs of the primary star.
This is essentially the same as plotting the negative z-component of the velocities (for convention - our system is in a right handed system with +z towards the viewer, but RV convention has negative RVs for blue shifts).
We could also plot the RV per triangle by plotting 'vzs'.  Note that this is actually defaulting to an inverted colormap to show you the same colorscheme ('RdBu_r' vs 'RdBu').
Can you write Python code for it?

fig = plt.figure(figsize=(12,12))
axs, artists = b['mesh01@model'].plot(time=0.09, facecolor='vzs', edgecolor=None)


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Let's check to make sure that rv_method is set as we'd expect.
Can you write Python code for it?

print b['rv_method']

b.run_compute(irrad_method='none')


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Let's make a significant mass ratio and radius ratio...
Can you write Python code for it?

b['q'] = 0.7

b['rpole@primary'] = 1.0

b['rpole@secondary'] = 0.5

b['teff@secondary@component'] = 5000


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Plotting
Now let's plot the radial velocities.
First we'll plot the dynamical RVs.  Note that dynamical RVs show the true radial velocity of the center of mass of each star, and so we do not see the Rossiter McLaughlin effect.
Can you write Python code for it?

axs, artists = b['dynamicalrvs@model'].plot(component='primary', color='b')
axs, artists = b['dynamicalrvs@model'].plot(component='secondary', color='r')


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Running Compute
Now let's set the rv_method so that one dataset uses the dynamical method and the other uses the flux-weighted (numerical) method.  Note that here we have to use set_value_all or loop over the components, as technically there are parameters for each component-dataset pair.
Can you write Python code for it?


I am working on the file "2.0/examples/rossiter_mclaughlin.ipynb".
The first task is:
Storing the mesh at every timestep is overkill, and will be both computationally and memory intensive.  So let's just sample at the times we care about.
Can you write Python code for it?

times = b.get_value('times@primary@numericalrvs@dataset')
times = times[times<0.1]
print times

b.add_dataset('mesh', dataset='mesh01', times=times)


I am working on the file "2.0/examples/single_spots.ipynb".
The first task is:
Adding Spots
Let's add one spot to our star.  Since there is only one star, the spot will automatically attach without needing to provide component (as is needed in the binary with spots example
Can you write Python code for it?

b.add_spot(radius=30, colat=80, long=0, relteff=0.9)


I am working on the file "2.0/examples/single_spots.ipynb".
The first task is:
And if we change the inclination to 0, we'll be looking at the north pole of the star.  This clearly illustrates the right-handed rotation of the star.  At time=t0=5 the spot will now be pointing in the negative y-direction.
Can you write Python code for it?

b.set_value('incl', 0)

b.run_compute(distortion_method='rotstar', irrad_method='none')

b.animate(x='xs', y='ys', facecolor='teffs')


I am working on the file "2.0/examples/single_spots.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Can you write Python code for it?

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_star()


I am working on the file "2.0/examples/single_spots.ipynb".
The first task is:
The 'colat' parameter defines the latitude on the star measured from its North Pole.  The 'long' parameter measures the longitude of the spot - with longitude = 0 being defined as pointing towards the observer at t0.
NOTE: prior to version 2.0.2, the definition for the location of a spot on a single star was different and spots did not corotate correctly.  If using spots, please make sure to be using 2.0.2 or later.
Can you write Python code for it?

times = np.linspace(0, 10, 11)
b.set_value('period', 10)
b.add_dataset('mesh', times=times)

b.run_compute(distortion_method='rotstar', irrad_method='none')

b.animate(x='xs', y='ys', facecolor='teffs')


I am working on the file "2.0/examples/sun.ipynb".
The first task is:
Comparing to Expected Values
Can you write Python code for it?

axs, artists = b['protomesh'].plot(facecolor='teffs')

axs, artists = b['pbmesh'].plot(facecolor='teffs')

print "teff: {} ({})".format(b.get_value('teffs', dataset='pbmesh').mean(),
                             b.get_value('teff', context='component'))

print "rpole: {} ({})".format(b.get_value('rpole', dataset='pbmesh'),
                              b.get_value('rpole', context='component'))


I am working on the file "2.0/examples/sun.ipynb".
The first task is:
Let's set all the values of the sun based on the nominal solar values provided in the units package.
Can you write Python code for it?

b.set_value('teff', 1.0*u.solTeff)
b.set_value('rpole', 1.0*u.solRad)
b.set_value('mass', 1.0*u.solMass)
b.set_value('period', 24.47*u.d)


I am working on the file "2.0/examples/sun.ipynb".
The first task is:
Now we run our model and store the mesh so that we can plot the temperature distributions and test the size of the sun verse known values.
Can you write Python code for it?

b.run_compute(protomesh=True, pbmesh=True, irrad_method='none', distortion_method='rotstar')


I am working on the file "2.0/examples/sun.ipynb".
The first task is:
Running Compute
Let's add a light curve so that we can compute the flux at a single time and compare it to the expected value.  We'll set the passband luminosity to be the nominal value for the sun.
Can you write Python code for it?


I am working on the file "2.0/examples/sun.ipynb".
The first task is:
Setting Parameters
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
'abs_intensities' are the projected absolute intensities (towards the observer) per-element.
intensities
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
'abs_normal_intensities' are the absolute normal intensities per-element.
normal_intensities
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
'intensities' are the projected relative intensities (towards the observer) per-element.
boost_factors
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new Bundle.  See Building a System for more details.
Can you write Python code for it?

%matplotlib inline

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
Compute Options
Let's look at the compute options (for the default PHOEBE 2 backend) that relate to computing fluxes and the LC dataset.
Other compute options are covered elsewhere:
* parameters related to dynamics are explained in the section on the orb dataset
* parameters related to meshing, eclipse detection, and subdivision are explained in the section on the mesh dataset
Can you write Python code for it?

ps_compute = phoebe.compute.phoebe()
print ps_compute


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
Dataset Parameters
Let's create the ParameterSets which would be added to the Bundle when calling add_dataset. Later we'll call add_dataset, which will create and attach both these ParameterSets for us.
Can you write Python code for it?

ps, constraints = phoebe.dataset.lc()
print ps

ps_dep = phoebe.dataset.lc_dep()
print ps_dep


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
For more details on boosting, see the Beaming and Boosting example script
atm
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
Mesh Fields
If a mesh dataset exists at any of the same times as the time array in the lc dataset, OR if store_mesh is set to True in the compute options, then flux-related surface element quantities will be available in the model as well.
Let's add a single mesh at the first time of the light-curve and re-call run_compute
Can you write Python code for it?

b.add_dataset('mesh', times=[0], dataset='mesh01')

b.run_compute()

print b['model'].datasets


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
Now let's look at each of the available fields.
pblum
For more details, see the tutorial on Passband Luminosities
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
Plotting
By default, LC datasets plot as flux vs time.
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
Since these are the only two columns available in the synthetic model, the only other option is to plot in phase instead of time.
Can you write Python code for it?

axs, artists = b['lc@model'].plot(x='phases')


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
boosting_method
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
intens_weighting
See the Intensity Weighting tutorial
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
irrad_method
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
ld_func
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
passband
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
pblum
See the Passband Luminosity tutorial
Can you write Python code for it?


I am working on the file "2.0/tutorials/LC.ipynb".
The first task is:
times
Can you write Python code for it?


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
Alternatively, if you provide simple 1-D fields to plot, a 2D x-y plot will be created using the values from each element (always for a single time - if meshes exist for multiple times in the model, you must provide a single time either in the twig or as an argument to plot).
Can you write Python code for it?

axs, artists = b['mesh@model'].plot(time=0.0, x='mus', y='teffs')


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new Bundle.  See Building a System for more details.
Can you write Python code for it?


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
Dataset Parameters
Let's create the ParameterSet which would be added to the Bundle when calling add_dataset. Later we'll call add_dataset, which will create and attach this ParameterSet for us.
Can you write Python code for it?

ps, constraints = phoebe.dataset.mesh()
print ps


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
Per-Element Parameters
Can you write Python code for it?

print b['vertices@primary@mesh01@model']

print b['xs@primary@mesh01@model']

print b['rs@primary@mesh01@model']

print b['r_projs@primary@mesh01@model']

print b['cosbetas@primary@mesh01@model']

print b['normals@primary@mesh01@model']

print b['nxs@primary@mesh01@model']

print b['mus@primary@mesh01@model']

print b['vxs@primary@mesh01@model']

print b['areas@primary@mesh01@model']

print b['loggs@primary@mesh01@model']

print b['teffs@primary@mesh01@model']

print b['visibilities@primary@mesh01@model']


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
Per-Time Parameters
Can you write Python code for it?

print b['volume@primary@mesh01@model']

print b['rpole@primary@mesh01@model']

print b['pot@primary@mesh01@model']


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
The 'mesh_method' parameter determines how each component in the system is discretized into its mesh, but currently only has one option:
 * marching (default): this is the new method introduced in PHOEBE 2.  The star is discretized into triangles, with the attempt to make them each of equal-area and nearly equilateral.  Although not as fast as 'wd', this method is more robust and will always form a closed surface (when possible).
ntriangles
The 'ntriangles' parameter is only relevenat if mesh_method=='marching' (so will not be available unless that is the case).
Can you write Python code for it?


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
The exception to needing to provide a time is for the per-time parameters mentioned above.  For these, time can be the x-array (not very exciting in this case with only a single time).
For more examples see the following:
- Passband Luminosity Tutorial
Can you write Python code for it?

axs, artists = b['mesh@model'].plot(x='times', y='rpole', marker='s')


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
gridsize
The 'gridsize' parameter is only relevant if mesh_method=='wd' (so will not be available unless that is the case).
Can you write Python code for it?


I am working on the file "2.0/tutorials/MESH.ipynb".
The first task is:
times
Can you write Python code for it?


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
3d axes are not yet supported for orbits, but hopefully will be soon.
Once they are supported, they will default to x, y, and z positions plotted on their respective axes.
Can you write Python code for it?

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

axs, artists = b['orb@model'].plot(xlim=(-4,4), ylim=(-4,4), zlim=(-4,4))


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new Bundle.  See Building a System for more details.
Can you write Python code for it?


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
As always, you have access to any of the arrays for either axes, so if you want to plot 'vxs' vs 'times'
Can you write Python code for it?

axs, artists = b['orb@model'].plot(x='times', y='vxs')


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
Dataset Parameters
Let's create the ParameterSet which would be added to the Bundle when calling add_dataset. Later we'll call add_dataset, which will create and attach this ParameterSet for us.
Can you write Python code for it?

ps, constraints = phoebe.dataset.orb()
print ps


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
Plotting
By default, orb datasets plot as 'ys' vx 'xs' (plane of sky).  Notice the y-scale here with inclination set to 90.
Can you write Python code for it?


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
The 'dynamics_method' parameter controls how stars and components are placed in the coordinate system as a function of time and has several choices:
 * keplerian (default): Use Kepler's laws to determine positions.  If the system has more than two components, then each orbit is treated independently and nested (ie there are no dynamical/tidal effects - the inner orbit is treated as a single point mass in the outer orbit).
 * more coming soon
ltte
Can you write Python code for it?


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
dynamics_method
Can you write Python code for it?


I am working on the file "2.0/tutorials/ORB.ipynb".
The first task is:
times
Can you write Python code for it?


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
Any of these columns are then available to use as edge or facecolors when plotting the mesh (see the section on the MESH dataset), but since the mesh elements are stored with the 'mesh01' dataset tag, and the rv (including flux-related) quantities are stored with the 'rv01' dataset tag, it is important not to provide the 'mesh01' dataset tag before plotting.
Can you write Python code for it?

axs, artists = b['mesh@model'].plot(facecolor='rvs', edgecolor=None)
# NOT:
# axs, artists = b['mesh01@model'].plot(facecolor='rvs', edgecolor=None)


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new Bundle.  See Building a System for more details.
Can you write Python code for it?


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
Dataset Parameters
Let's create the ParameterSets which would be added to the Bundle when calling add_dataset. Later we'll call add_dataset, which will create and attach both these ParameterSets for us.
Can you write Python code for it?

ps, constraints = phoebe.dataset.rv()
print ps

ps_dep = phoebe.dataset.rv_dep()
print ps_dep


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
In system hierarchies where there may be multiple periods, it is also possible to determine whose period to use for phasing.
Can you write Python code for it?

b['period'].components

axs, artists = b['rv@model'].plot(x='phases:binary')


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
Mesh Fields
If a mesh dataset exists at any of the same times as the time array in the rv dataset, or if pbmesh is set to True in the compute options, then radial velocities for each surface element will be available in the model as well (only if mesh_method=='flux_weighted').
Since the radial velocities are flux-weighted, the flux-related quantities are also included.  For a description of these, see the section on the lc dataset.
Let's add a single mesh at the first time of the rv dataset and re-call run_compute
Can you write Python code for it?

b.add_dataset('mesh', times=[0], dataset='mesh01')

b.run_compute(irrad_method='none')

print b['model'].datasets


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
Plotting
By default, RV datasets plot as 'rvs' vs 'times'.
Can you write Python code for it?


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
See the Gravitational Redshift Example Script for more details on the influence this parameter has on radial velocities.
Synthetics
Can you write Python code for it?

b.add_dataset('rv', times=np.linspace(0,1,101), dataset='rv01')

b.run_compute(irrad_method='none')

b['rv@model'].twigs

print b['times@primary@rv@model']

print b['rvs@primary@rv@model']


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
These new columns are stored with the rv's dataset tag, but with the mesh model-kind.
Can you write Python code for it?


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
rv_method
Can you write Python code for it?


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
rvs
Can you write Python code for it?


I am working on the file "2.0/tutorials/RV.ipynb".
The first task is:
sigmas
Can you write Python code for it?


I am working on the file "2.0/tutorials/alternate_backends.ipynb".
The first task is:
And we'll attach some dummy datasets.  See Datasets for more details.
Can you write Python code for it?

b.add_dataset('orb', times=np.linspace(0,10,1000), dataset='orb01', component=['primary', 'secondary'])

b.add_dataset('lc', times=np.linspace(0,10,1000), dataset='lc01')


I am working on the file "2.0/tutorials/alternate_backends.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new Bundle.  See Building a System for more details.
Can you write Python code for it?

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()


I am working on the file "2.0/tutorials/alternate_backends.ipynb".
The first task is:
Running Multiple Backends Simultaneously
Running multiple backends simultaneously is just as simple as running the PHOEBE backend with multiple sets of compute options (see Compute).
We just need to make sure that each dataset is only enabled for one (or none) of the backends that we want to use, and then send a list of the compute tags to run_compute.  Here we'll use the PHOEBE backend to compute orbits and the legacy backend to compute light curves.
Can you write Python code for it?

b.set_value_all('enabled@lc01@phoebebackend', False)
#b.set_value_all('enabled@orb01@legacybackend', False)  # don't need this since legacy NEVER computes orbits
print b['enabled']

b.run_compute(['phoebebackend', 'legacybackend'], model='mixedresults')


I am working on the file "2.0/tutorials/alternate_backends.ipynb".
The first task is:
Using Alternate Backends
Adding Compute Options
Adding a set of compute options for an alternate backend is just as easy as for the PHOEBE backend.  Simply provide the function or name of the function in phoebe.parameters.compute that points to the parameters for that backend.
Here we'll add the default PHOEBE backend as well as the PHOEBE 1.0 (legacy) backend.  Note that in order to use an alternate backend, that backend must be installed on your machine.
Can you write Python code for it?

b.add_compute('phoebe', compute='phoebebackend')

print b['phoebebackend']


I am working on the file "2.0/tutorials/apsidal_motion.ipynb".
The first task is:
Influence on Light Curves (fluxes)
Now looking at the light curve, we can see that this is resulting in the eclipses moving in phase-space.
Can you write Python code for it?

axs, artists = b['lc01@model'].plot(time=[0,1])
axs, artists = b['lc02@model'].plot(time=[4,5])

axs, artists = b['lc01@model'].plot(x='phases')
axs, artists = b['lc02@model'].plot(x='phases')


I am working on the file "2.0/tutorials/apsidal_motion.ipynb".
The first task is:
Influence on Oribits (positions)
Let's plot the orbit from above and highlight the positions of each star at each cycle (times that are multiples of the period).
Can you write Python code for it?

axs, artists = b['orb01@model'].plot(y='zs', time=[0,1,2,3,4,5])


I am working on the file "2.0/tutorials/apsidal_motion.ipynb".
The first task is:
Let's set a very noticeable rate of precession.
Can you write Python code for it?


I am working on the file "2.0/tutorials/atm_passbands.ipynb".
The first task is:
A 'passband' parameter exists for each passband-dependent-dataset (i.e. not meshes or orbits, but light curves and radial velocities).  This parameter dictates which passband should be used for the computation of all intensities.
Can you write Python code for it?


I am working on the file "2.0/tutorials/atm_passbands.ipynb".
The first task is:
Lastly, to manually download and install one of these online passbands, you can do so explicitly via phoebe.download_passband.
Note that this isn't necessary unless you want to explicitly download passbands before needed by run_compute (perhaps if you're expecting to have unreliable network connection in the future and want to ensure you have all needed passbands).
Can you write Python code for it?


I am working on the file "2.0/tutorials/atm_passbands.ipynb".
The first task is:
Relevant Parameters
An 'atm' parameter exists for each of the components in the system (for each set of compute options) and defines which atmosphere table should be used.
By default, these are set to 'ck2004' (Castelli-Kurucz) but can be set to 'blackbody' as well as 'extern_atmx' and 'extern_planckint' (which are included primarily for direct comparison with PHOEBE legacy).
Can you write Python code for it?

b['atm']

b['atm@primary']

b['atm@primary'].description

b['atm@primary'].choices


I am working on the file "2.0/tutorials/atm_passbands.ipynb".
The first task is:
The available choices will include both locally installed passbands as well as passbands currently available from the online PHOEBE repository.  If you choose an online-passband, it will be downloaded and installed locally as soon as required by b.run_compute.
Can you write Python code for it?


I am working on the file "2.0/tutorials/atm_passbands.ipynb".
The first task is:
These installed passbands can be in any of a number of directories, which can be accessed via phoebe.list_passband_directories().
The first entry is the global location - this is where passbands can be stored by a server-admin to be available to all PHOEBE-users on that machine.
The second entry is the local location - this is where individual users can store passbands and where PHOEBE will download and install passbands (by default).
Can you write Python code for it?


I am working on the file "2.0/tutorials/atm_passbands.ipynb".
The first task is:
To see your current locally-installed passbands, call phoebe.list_installed_passbands().
Can you write Python code for it?


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
And we'll attach some dummy datasets.  See Datasets for more details.
Can you write Python code for it?

b.add_dataset(phoebe.dataset.orb, times=np.linspace(0,40,1000), dataset='orb01', component=['primary', 'secondary'])

times, fluxes, sigmas = np.loadtxt('test.lc.in', unpack=True)
b.add_dataset(phoebe.dataset.lc, times=times, fluxes=fluxes, sigmas=sigmas, dataset='lc01')


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
For Keplerian dynamics, there is an additional option whether to return the Euler angles as well.  Since we'll later be using these to place the mesh in orbit, we'll set this to True.
Can you write Python code for it?

ts, xs, ys, zs, vxs, vys, vzs, ethetas, elongans, eincls =\
    phoebe.dynamics.keplerian.dynamics_from_bundle(b, np.linspace(0, 6, 500), compute='default_options', return_euler=True)

plt.cla()
artist, = plt.plot(xs[0], zs[0], 'b-') # primary
artist, = plt.plot(xs[1], zs[1], 'r-') # secondary


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
Meshing
Can you write Python code for it?

F = 1.0
Phi = 9.5
masses = [1.0, 0.8]  # solar Masses
sma = 8.0   # solar radii
ecc = 0.0
freq_rot = 2*np.pi/1  # radians/day
abun = 0.0
alb_ref = 0.0
teff = 6000  # K
gravb_bol = 0.2
gravb_law = 'zeipel'

primarymesh = phoebe.backend.universe.Star(F, Phi, masses, sma, ecc,
                                           freq_rot, teff, gravb_bol,
                                           abun, alb_ref,
                                           delta=0.1, maxpoints=1e5,
                                           ind_self=0,
                                           ind_sibling=1,
                                           comp_no=1)

secondarymesh = primarymesh.copy()

system = phoebe.backend.universe.System({'primary': primarymesh, 'secondary': secondarymesh})


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
Meshing
Similarly, we can create a backend "system" directly from the Bundle
Can you write Python code for it?

system = phoebe.backend.universe.System.from_bundle(b, compute='default_options')


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
Note that the column names are not identical to those exposed in the frontend (since tags do not exist in the backend).  To access the list of available keys in the mesh, we can directly inspect the mesh.
Can you write Python code for it?


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
The first returned item is an array of times - not surprisingly these match the times we just sent.
The rest of the returned items (xs, ys, etc) are each a list of arrays.  The list contains an entry for each component in the system.  So in this case, since we have a binary, each is a list of two arrays.
Now we can plot any of this information by pulling the correct array from these lists.
Can you write Python code for it?

artist, = plt.plot(xs[0], zs[0], 'b-') # primary
artist, = plt.plot(xs[1], zs[1], 'r-') # secondary


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
The meshes are now placed in orbit at the current time.  These meshes are accessible via:
Can you write Python code for it?


I am working on the file "2.0/tutorials/backend.ipynb".
The first task is:
These standards can then be accessed:
Can you write Python code for it?