I am working on the file "Examples/12_tacticities/polycarboxybetaine/scripts/rnd_walk.pilot_tutorial.ipynb".
The first task is:
The figure confirms that the optimization can change the initial distribution of monomer orientations. However, the chain obtained with random_walk_tacticity() has more recemo- than meso- diads (as the example was based on creating a syndiotactic polymer), as compared to the chain from the original random_walk() (10%-30%).

A setup to construct a polymer chain with exact tacticity
In the previous section it was shown that in PySIMM one can easily construct a 'no simulation' polymer chain. The simple (but illustrative for tacticity explanations) chain is made by adding the monomers to each other without force field optimizations.
The tacticity (ratio of meso-/racemo- diads) of that chain is easy to set to be exact, unlike the tacticity of a chain build with enabled forcefield optimizations.

The robustness of the forcefield functional form allows one to run the simulations using the 'no simulation' chain as an initial structure. The long enough MD simulations with the fixed (via SHAKE) angle between the side radicals will result a relaxed polymer chain with exactly the same tacticity as it was constructed at the beginning.

Please note, that depending on geometry of your repetitive unit the initial structure of chain of concatenated monomers might be defined more or less approximate.
We recommend (if it is possible) to put linker atoms (tail and head atoms) into the anti-periplanar positions (see e.g. pCBMA repetitive unit in this tutorial).

Below is the small code listing that sets up (using PySIMM) the MD simulations starting with 'no simulation' configuration and result the relaxed polymer chain.
Can you write Python code for it?

pmer_shake = polymer_nosim.copy()

sim = lmps.Simulation(pmer_shake, name='shake_relax', log='shake.log')

# This command adds a SHAKE fix to LAMMPS simulations which fixes an angle
#  between two side chains of the pCBMA, and bonds that make that angle
sim.add_custom('fix shck_fix all shake 0.001 40 0 b {} {} a {}'.format(
                    polymer.bond_types.get('CG2O2,CG301')[0].tag,
                    polymer.bond_types.get('CG301,CG331')[0].tag,
                    polymer.angle_types.get('CG331,CG301,CG2O2')[0].tag))

sim.add_md(ensemble='nve', limit=0.1, length=30000)
sim.run()
pmer_shake.unwrap()

display_system(pmer_shake)

tacticity_stat = check_tacticity(pmer_shake, [1, 2, 8, 10], len(sst.particles))

fig, ax = mplp.subplots(1, 1, figsize=(8, 4))
display_diad_distrib(tacticity_stat[1], ax);
print(tacticity_stat[1])


I am working on the file "Examples/12_tacticities/polycarboxybetaine/scripts/rnd_walk.pilot_tutorial.ipynb".
The first task is:
The input parameters of the method are:
  - A pysimm system that represents a macromolecule to analyze.
  - A list with 4 integers that defines the indices of the node atoms in a repetitive unit of the macromolecule. Indices in that order represent: (1) first atom of the backbone; (2) second atom  of the backbone; (3) the first atom of the first side chain (or methyl, or hydrogen); (4) the first atom of the second side chain.
Note: the colors of the atom indices match the colors of the vectors on the figure below.
  - Length of the repetitive unit of the macromolecule
The second variable of check_tacticity() output is the list that shows whether the two consecutive repetitive units in the chain form either a meso (True) or a racemo (False) diad.
Let's examine obtained oligomer and print the result in the form of simple 2-column histogram that will show the ratio of meso to racemo diads.
Indices for the analyzed repetitive unit are highlighted on the first image of the tutorial, and they are 1, 2, 8, and 10.
Can you write Python code for it?

display.Image('../figures/figure2.png', embed=True, retina=True)

tacticity_stat = check_tacticity(sngl_chain, [1, 2, 8, 10], len(sst.particles))

fig, ax = mplp.subplots(1, 1, figsize=(8, 4))
display_diad_distrib(tacticity_stat[1], ax);


I am working on the file "Examples/12_tacticities/polycarboxybetaine/scripts/rnd_walk.pilot_tutorial.ipynb".
The first task is:
To control tacticity the method has tacticity keyword argument that accepts a real number $n \in [0; 1]$, which defines the relative number of isotactic insertions, so that $n = 1$ will be a fully isotactic chain, $n = 0$ will be syndiotactic, and $n = 0.5$ will be a chain with equal number of isotactic and syndiotactic insertions.

The method also accepts those specific strings as values of tacticity key. One can use either $n=1$ or 'isotactic', $n=0$ or 'syndiotactic', and $n=0.5$ or 'atactic'.
For more details and more options of random_walk_tacticity() please see the method description in pysimm documentation.

First, let's run the random_walk_tacticity() in no simulation mode. Next monomer will be put to an approximately correct, geometrically calculated position without force field optimization and NVE simulations.
Can you write Python code for it?

polymer_nosim = random_walk_tacticity(new_sst, 15, forcefield=ff,
                                      tacticity='syndiotactic', sim='no', density=0.01)

display_system(polymer_nosim, 'straight_chain.pdb')


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
Additional Manipulations
The DataFrame stores data in a structured format which can be accessed by attribute names, combined with other data, or further filtered by the values of these attributes. These processes are convenient as they provide a means to rearrange the way our data is formatted. Additional detail on DataFrame objects can be found in the official Pandas documentation. In this section we demonstrate a couple of the manipulations that can be performed.
Accessing Elements
Elements can be accessed by column (attribute):
Can you write Python code for it?


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
By document (job):
Can you write Python code for it?


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
Establish Connection To MongoDB
In this section, we demonstrate how to open a connection to MongoDB using pymongo. To do so, we will use the name of the application, 'squall', to connect to the correct database and the name of the experiment we want to query to extract the correct collection of documents (job results). Additional details for pymongo can be found in the official documentation.
Can you write Python code for it?

# Database Parameters
db_name = 'squall'  # Name of Application
coll_name = 'Fibonacci Demo'  # Name of Experiment

# Connect to database and extract collection
client = MongoClient("localhost", 27017)
db = client[db_name]
coll = db[coll_name]


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
Exploration & Analysis
In this section, we provide examples of how easy it is to analyze, explore, and visualize your data once it is in the DataFrame object.
Summary Stats
DataFrames provide a number of buiult-in methods to ease analysis and exploration. As an example, we demonstrate some of these methods by accessing summary statistics for certain attributes. Note that we first filter the dataframes so that our summary statistics are for a narrowed subset of all of the documents (jobs). Further details on these methods can be found in the official documentation.
Can you write Python code for it?

param = 20
print(matlab[matlab['A'] == param]['cpuA'].mean())
print(python[python['A'] == param]['cpuA'].mean())
print(r[r['A'] == param]['cpuA'].mean())

print(matlab[matlab['A'] == param]['cpuA'].std())
print(python[python['A'] == param]['cpuA'].std())
print(r[r['A'] == param]['cpuA'].std())


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
Python CPU Times
Can you write Python code for it?


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
Python Results
Note that in this section, we had to adjust the 'p' in Product to be lower case. This is due to the fact that the implementation at the time of the batch had been written to store the field as an upper case
Can you write Python code for it?

lang = "Python"
query = { "$where": "this.params.A == this.params.B", "Tags": lang }
python = pd.DataFrame(np.array([(job['product'],
                                 job['params']['A']+job['params']['B'],
                                 job['params']['A']*job['params']['B'],
                                 job['cpuA'] + job['cpuB'],
                                 job['cpuA'] * job['cpuB'],
                                 job['params']['A'],
                                 job['fibA'],
                                 job['cpuA'],
                                 job['params']['B'],
                                 job['fibB'],
                                 job['cpuB'])
                                 for job in coll.find(query)]),
                       columns=['product', 'A+B', 'AxB', 'cpu+', 'cpu*',
                                'A', 'fibA', 'cpuA',
                                'B', 'fibB', 'cpuB'])


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
R Results
Note that in this section, there are some adjustments made to the way data is being manipulated before being stored in the dataframe. This is because in R everything is a vector and thus, even scalar values were written to json as if they were a list. To avoid running into errors while analyzing the data, this must be accounted for.
Can you write Python code for it?

lang = "R"
query = { "$where": "this.params.A == this.params.B", "Tags": lang }
r = pd.DataFrame(np.array([(job['product'][0],
                            job['params']['A']+job['params']['B'],
                            job['params']['A']*job['params']['B'],
                            job['cpuA'][0] + job['cpuB'][0],
                            job['cpuA'][0] * job['cpuB'][0],
                            job['params']['A'],
                            job['fibA'][0],
                            job['cpuA'][0],
                            job['params']['B'],
                            job['fibB'][0],
                            job['cpuB'][0])
                                 for job in coll.find(query)]),
                       columns=['product', 'A+B', 'AxB', 'cpu+', 'cpu*',
                                'A', 'fibA', 'cpuA',
                                'B', 'fibB', 'cpuB'])


I am working on the file "Examples/Analysis/FibAnalysis.ipynb".
The first task is:
Scatter Plots Of Sequence
Can you write Python code for it?

results.plot(kind='scatter', x='A', y='fibA', title="Fibonacci Sequence Growth")
plt.show()


I am working on the file "Examples/AnalyzeNetwork.ipynb".
The first task is:
Analyse
Can you write Python code for it?

net = read_network("test.pkl")

draw(net)

gene_knock_out(net,1)

draw(net)


I am working on the file "Examples/AnalyzeNetwork.ipynb".
The first task is:
Definition and proxy for usefull functions
Can you write Python code for it?

MODEL = "adaptation"
sim = AT.Simulation(MODEL)

read_network = AT.main_functions.read_network

def write_network(net,filename): net.store_to_pickle(filename)

def draw(net,label=False): net.draw(edgeLegend=label)

def fitness(net,trial): return sim.run_dynamics(net,trial,erase_buffer=False,return_treatment_fitness=True)

def gene_knock_out(net,label): net.delete_clean(label,target='species')

def remove_interaction(net,label): net.delete_clean(label,target='interaction')


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
1.1 Loading structure from PDB file
Can you write Python code for it?

pdb_file = '1ubq.pdb'

pdb_parser = PDBParser()

st = pdb_parser.get_structure('1UBQ', '1ubq.pdb')

import nglview as nv

view = nv.show_biopython(st)
view


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
1.2 Get Headers
Can you write Python code for it?


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
1.4 Get Extended data from mmCIF file (very long output, structure defined at PDB mmCIF format)
Can you write Python code for it?

mmcif_dict = MMCIF2Dict.MMCIF2Dict(cif_fn)
mmcif_dict


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
3.2 Chains and length of the first model
We iterate along the list of chains contained in the first model (st[0]), len(chain) gives the length of the array, i.e. the number of residues that contains.
Can you write Python code for it?

for chain in st[0]:
    print(chain.id, len(chain))


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
3.4 List of atoms of first residue and complete information of first atom
get_atoms() gives the list of atoms of any element. Can be used with structures, models, chains or residues. Here st[0]['A'][1] stands for the residue labelled as 1 in chain A of the first model (st[0]).
Vars is a standart python function to give the contents of a given dictionary/object. The atom dictionary is described in Bio.PDB.Atom.
Note that the information in Atom corresponds to one line in the PDB format, in this case N atom on residue A 1
ATOM      1  N   MET A   1      27.340  24.430   2.614  1.00  9.67           N
Can you write Python code for it?

for atom in st[0]['A'][1].get_atoms():
    print(atom.id)

vars(st[0]['A'][1]['N'])


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
9: Select some chain and discard others (e.g. to get a Biological assembly from an assymetrical unit)
This is the "Biopython" way of selecting parts of the structure. Can be used also to select residues, or atoms, but it can become complicated. Most experienced users will just edit the PDB file and remove the chains, residues or atoms manually.
Here we use as example 6axg, a complex of two proteins (the biological assembly). Version downloaded from PDB (the asymmetric unit) has 6 copies of the complex. Chains A and C correspond to one of such copies.
Can you write Python code for it?

st_6axg = pdb_parser.get_structure('6AXG', '6axg.pdb')

view = nv.show_biopython(st_6axg)
view


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
Find contacts below some distance cut-off, using NeighborSearch

This can be solved iterating over all combination of pairs of residues and selecting those that are closer than MAXDIST.
NeighborSearch uses a different internal structure that makes this search much faster, especially in large structures.
Can you write Python code for it?

MAXDIST = 10

select = []

#Select only CA atoms for short
for at in st.get_atoms():
    if at.id == 'CA':
        select.append(at)

# Preparing search
nbsearch = NeighborSearch(select)

ncontact = 0

for at1, at2 in nbsearch.search_all(MAXDIST): # All pairs with a distance less than MAXDIST
    ncontact += 1
    print("Contact: ", ncontact, atom_id(at1), atom_id(at2), at2 - at1)


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
List of atoms and coordinates for a given list of residues and atoms

First loop goes through all structure (note that here get_atoms() is applied to st, could be also applied to other selections), selecting atoms and residues included in residues and atoms lists.
Second loop goes through the selection and prints atom id and coordinates (as lists). It can be done in a single loop, but in this way select can be re-used for other purposes
Can you write Python code for it?

residues = ['ARG', 'ASP']
atoms = ['CA']
select = []
for atom in st.get_atoms():
    if atom.get_parent().get_resname() in residues and atom.id in atoms:
        select.append(atom)

for atom in select:
    print(atom.get_parent().get_resname(), atom.get_parent().id[1],
          atom.get_name(), atom.get_coord())


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
Print all distances from atoms in the residue to a given point

Version of a same script using a central point of reference.
Can you write Python code for it?

center = np.array([10, 10, 10])

for at1 in chain_A[residue_1].get_atoms():
    vect = at1.coord - center
    distance = np.sqrt(np.sum(vect ** 2))
    print(atom_id(at1), distance)


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
Save it on a PDB file
Can you write Python code for it?

pdbio = PDBIO()

output_pdb_path = '6axg_AC.pdb'

pdbio.set_structure(st_6axg)
pdbio.save(output_pdb_path)


I am working on the file "Examples/Biopython_Examples.ipynb".
The first task is:
selecting chains A and C (one biological assembly)
Can you write Python code for it?

chains_ok = ['A', 'C']
chain_orig = [ch.id for ch in st_6axg[0]]

for chain_id in chain_orig:
    if chain_id not in chains_ok:
        st_6axg[0].detach_child(chain_id)

view = nv.show_biopython(st_6axg)
view


I am working on the file "Examples/Calibracion_Barbosa_NSGAII.ipynb".
The first task is:
Condiciones almacenamiento
Can you write Python code for it?

#Condiciones
cu.set_Storage(wmf.models.max_capilar*0.07,0) #quiero entender esto..
cu.set_Storage(1500,3)


I am working on the file "Examples/Calibracion_Barbosa_NSGAII.ipynb".
The first task is:
Ejecución:
Calibracion : Parametros de calibracion del modelo, orden:.
            - Evaporacion.
            - Infiltracion.
            - Percolacion.
            - Perdidas.
            - Vel Superficial .
            - Vel Sub-superficial.
            - Vel Subterranea.
            - Vel Cauce.
            - Max Capilar.
            - Max Gravitacional.
Ensayo NSGAII
Desde wmf se trae una nueva clase la cual es la herramienta que va a dar las ordenes a SimuBasin para realizar el proceso de calibración automático multi-objetivo.
A continuación se establece la cantidad de pasos, el inicio de la simulación y variables básicas para la misma, tales como:
- ruta de la lluvia binaria.
- Caudal observado con el cual se compara el algorítmo.
- el objeto de simulación cu.
- Los rangos aleatorios de construcción de individuos, y sus rangos de mutación.
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Example KML.ipynb".
The first task is:
Cesium is packed with example data. Let's look at some GDP per captia data from 2008.
Can you write Python code for it?


cesium.kml_url = '/nbextensions/CesiumWidget/cesium/Apps/SampleData/kml/gdpPerCapita2008.kmz'

# if running in binder use the following instead
!cp /home/main/.local/share/jupyter//nbextensions/CesiumWidget/cesium/Apps/SampleData/kml/gdpPerCapita2008.kmz .
cesium.kml_url = 'gdpPerCapita2008.kmz'


I am working on the file "Examples/CesiumWidget Example KML.ipynb".
The first task is:
Create widget object
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Example KML.ipynb".
The first task is:
Display the widget:
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Example KML.ipynb".
The first task is:
Example flyto
Can you write Python code for it?

cesium.fly_to(14, 90, 20000001)

cesium._flyto


I am working on the file "Examples/CesiumWidget Example KML.ipynb".
The first task is:
Example zoomto
Can you write Python code for it?

for lon in np.arange(0, 360, 0.5):
    cesium.zoom_to(lon, 0, 36000000, 0 ,-90, 0)

cesium._zoomto


I am working on the file "Examples/CesiumWidget Example with CZML library.ipynb".
The first task is:
Create widget object
Can you write Python code for it?

cesiumExample = CesiumWidget(width="100%", czml=tuple(doc.data()))


I am working on the file "Examples/CesiumWidget Example with CZML library.ipynb".
The first task is:
Display the widget:
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Example with CZML library.ipynb".
The first task is:
Some data for the viewer to display
Can you write Python code for it?

# Initialize a document
doc = czml.CZML()

# Create and append the document packet
packet1 = czml.CZMLPacket(id='document',version='1.0')
doc.packets.append(packet1)

p3 = czml.CZMLPacket(id='test')
p3.position = czml.Position(cartographicDegrees = [18.07,59.33, 20])
point = czml.Point(pixelSize=20, show=True)
point.color = czml.Color(rgba=(223, 150, 47, 128))
point.show = True
p3.point = point

l = czml.Label(show=True, text='Stockholm')
l.scale = 0.5
p3.label = l
doc.packets.append(p3)


I am working on the file "Examples/CesiumWidget Interact-Example.ipynb".
The first task is:
A more complex CZML example
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Interact-Example.ipynb".
The first task is:
Add some data to the viewer

A simple czml
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Interact-Example.ipynb".
The first task is:
Display the widget:
Can you write Python code for it?


I am working on the file "Examples/CesiumWidget Interact-Example.ipynb".
The first task is:
Now let's make some interactive widget:
Can you write Python code for it?

from __future__ import print_function
from ipywidgets import interact, interactive, fixed
from ipywidgets import widgets


I am working on the file "Examples/CesiumWidget Interact-Example.ipynb".
The first task is:
The code:
from czml_example import simple_czml, complex_czml
Simply import some CZML data for the viewer to display.
Create widget object
Can you write Python code for it?

cesiumExample = CesiumWidget(width="100%", enable_lighting=True)


I am working on the file "Examples/CesiumWidget Interact-Example.ipynb".
The first task is:
store the CZML objet in a dictionary and use their name as keys
define a function to switch between CZML
bind the IPython intercat class to the function
Can you write Python code for it?

myczml = {'simple_czml':simple_czml, 'complex_czml':complex_czml}

myplace = {'Eboli, IT':'', 'Woods Hole, MA':'', 'Durham, NH':''}

import geocoder
import time
for i in myplace.keys():
    g = geocoder.google(i)
    print(g.latlng)
    myplace[i]=g.latlng

myplace

@interact(z=(0,20000000), heading=(-180,180), pitch=(-90,90), roll=(-360,360),
          Location=('Eboli, IT','Woods Hole, MA','Durham, NH'))
def f(z=1000000, heading=0, pitch=-90, roll=0, Location='Eboli, IT'):
    cesiumExample.zoom_to(myplace[Location][1], myplace[Location][0], z, heading, pitch, roll)
@interact(CZML=('simple_czml','complex_czml'))
def c(CZML=None):
    cesiumExample.czml = myczml[CZML]

cesiumExample


I am working on the file "Examples/Combining Pseudorange and Phase.ipynb".
The first task is:
This plot is uncorrected, it is a remake of the plot in Anthea's email on Wed, Jun 15, 2016 at 7:06 AM. The phase calculated TEC appears to match the pseudorange calculated TEC in parts, but other parts are offset by a bias. The next script and plot shows how the phase data can be shifted to line up with the average of the pseudorange data. It does so according to how large the slice value is. Basically it slices up the whole data set into a specifiable range of points and does the averaging on each range of points individually in order to fix phase lock cycle slips. This isn't the best way of doing it, the program should check for loss of lock and shift the phase data in chunks based on that. I am going to read more about cycle slips now.
Can you write Python code for it?

sl=200
plt.figure(figsize=(15,15))
ax1=plt.subplot(211)
ax1.xaxis.set_major_formatter(fmt)

plt.plot(2.85E9*(poop[:,23,'P2','data']/3.0E8
                 -poop[:,23,'C1','data']/3.0E8),'b.',label='prtec',markersize=3)

for i in range(int(len(poop[:,23,'L1','data'])/sl)):

    phtec = 2.85E9*(poop[poop.labels[sl*i:sl*(i+1)],23,'L1','data']/f1
                   -poop[poop.labels[sl*i:sl*(i+1)],23,'L2','data']/f2)

    prtec = 2.85E9*(poop[poop.labels[sl*i:sl*(i+1)],23,'P2','data']/3.0E8
                   -poop[poop.labels[sl*i:sl*(i+1)],23,'C1','data']/3.0E8)

    b = np.average((phtec-prtec)[np.logical_not(np.isnan(phtec-prtec))])
    plt.plot(phtec-b,'r-',linewidth=3,label='')

plt.axis([poop.labels[10],poop.labels[10000],-50,50])
plt.title('bias corrected phase data')
plt.xlabel('time')
plt.ylabel('TECu')
plt.grid()
plt.legend()
plt.show()


I am working on the file "Examples/Combining Pseudorange and Phase.ipynb".
The first task is:
try some stuff out from "An Automatic Editing Algorithm for GPS Data" by Blewitt
Can you write Python code for it?

f1 = 1575.42E6
f2 = 1227.6E6

svn = 23
L1 = -1*3.0E8*poop[:,svn,'L1','data']/f1  #(1a)
L2 = -1*3.0E8*poop[:,svn,'L2','data']/f2  #(1b)
P1 = poop[:,svn,'C1','data']              #(1c)
P2 = poop[:,svn,'P2','data']              #(1d)

#wide lane combination
wld = 3.0E8/(f1-f2)
Ld = (f1*L1-f2*L2)/(f1-f2)                #(3)
prd = (f1*P1+f2*P2)/(f1+f2)               #(4)
bd = (Ld-prd)/wld                         #(5)

#wide lane cycle slip detection
bdmean = bd[1]
rms = 0
nans_in_a_row = 0
bdindex=2
ph_con_arc=0
avb=[]
rmsb=[]
lowers=[]
uppers=[]
inarc=False

while(nans_in_a_row<10):

    if not np.isnan(bd[bdindex]):

        if(bd[i]>4*np.sqrt(rms)):
            avb.append(bdmean)
            rmsb.append(np.sqrt(rms))
            bdmean=bd[i+1]
            rms=0
            ph_con_arc=0
            uppers.append(bdindex)
            inarc=False

        else:
            if not inarc:
                inarc=True
                lowers.append(bdindex)
            ph_con_arc+=1
            nans_in_a_row=0
            rms = rms+((bd[bdindex]-bdmean)**2-rms)/(ph_con_arc) #(8b)
            bdmean = bdmean+(bd[bdindex]-bdmean)/(ph_con_arc)    #(8a)

    else:
        if inarc:
            avb.append(bdmean)
            rmsb.append(rms)
            ph_con_arc=0
            bdmean=bd[bdindex+1]
            rms=0
            inarc=False
            uppers.append(bdindex)
        nans_in_a_row += 1
    bdindex+=1

print('ended at index {}'.format(bdindex-nans_in_a_row))

lengths=[a-b for a,b in zip(uppers,lowers)]
eim = [np.sqrt(a)/np.sqrt(b-1) for a,b in zip(rmsb,lengths)]

#ionospheric combination
LI = L1-L2      #(6)
PI = P2-P1      #(7)

#ionospheric cycle slip detection
# get x and y vectors
mask=~np.isnan(PI)
x = np.arange(len(PI[mask]))
y = PI[mask]

# calculate polynomial
z = np.polyfit(x, y, 6)
f = np.poly1d(z)

# calculate new x's and y's
x_new = np.linspace(x[0], x[-1], len(x))
Q = f(x_new)

residual =...


I am working on the file "Examples/Continuum_Mechanics/constitutive_props.ipynb".
The first task is:
Ir05()
</h3> Provides the vector $I_{r05} = \left( \begin{array}{ccc} 1 \ 1 \ 1 \ 0.5 \ 0.5 \ 0.5 \end{array} \right)$. Return a vec. Example:
Can you write Python code for it?


I am working on the file "Examples/Continuum_Mechanics/constitutive_props.ipynb".
The first task is:
L_iso
Provides the elastic stiffness tensor for an isotropic material.
The two first arguments are a couple of elastic properties. The third argument specifies which couple has been provided and the nature and order of coefficients.
Exhaustive list of possible third argument :
‘Enu’,’nuE,’Kmu’,’muK’, ‘KG’, ‘GK’, ‘lambdamu’, ‘mulambda’, ‘lambdaG’, ‘Glambda’.
Return a numpy ndarray.
Example :
Can you write Python code for it?

E = 70000.0
nu = 0.3
L = sim.L_iso(E,nu,"Enu")
print np.array_str(L, precision=4, suppress_small=True)

d = sim.check_symetries(L)
print(d['umat_type'])
print(d['props'])

x = sim.L_iso_props(L)
print(x)


I am working on the file "Examples/Continuum_Mechanics/constitutive_props.ipynb".
The first task is:
L_isotrans
Provides the elastic stiffness tensor for an isotropic transverse material.
Arguments are longitudinal Young modulus EL, transverse young modulus, Poisson’s ratio for loading along the longitudinal axis nuTL, Poisson’s ratio for loading along the transverse axis nuTT, shear modulus GLT and the axis of symmetry.
Can you write Python code for it?

EL = 70000.0
ET = 20000.0
nuTL = 0.08
nuTT = 0.3
GLT = 12000.0
axis = 3
L = sim.L_isotrans(EL,ET,nuTL,nuTT,GLT,axis)
print np.array_str(L, precision=2)

d = sim.check_symetries(L)
print(d['umat_type'])
print(d['axis'])
print np.array_str(d['props'], precision=2)

x = sim.L_isotrans_props(L,axis)
print np.array_str(x, precision=2)


I am working on the file "Examples/Continuum_Mechanics/constitutive_relations.ipynb".
The first task is:
L_iso
Provides the elastic stiffness tensor for an isotropic material.
The two first arguments are a couple of elastic properties. The third argument specifies which couple has been provided and the nature and order of coefficients.
Exhaustive list of possible third argument :
‘Enu’,’nuE,’Kmu’,’muK’, ‘KG’, ‘GK’, ‘lambdamu’, ‘mulambda’, ‘lambdaG’, ‘Glambda’.
Return a numpy ndarray.
Example :
Can you write Python code for it?


I am working on the file "Examples/Continuum_Mechanics/constitutive_relations.ipynb".
The first task is:
L_isotrans
Provides the elastic stiffness tensor for an isotropic transverse material.
Arguments are longitudinal Young modulus EL, transverse young modulus, Poisson’s ratio for loading along the longitudinal axis nuTL, Poisson’s ratio for loading along the transverse axis nuTT, shear modulus GLT and the axis of symmetry.
Can you write Python code for it?


I am working on the file "Examples/Continuum_Mechanics/constitutive_relations.ipynb".
The first task is:
L_ortho
Provides the elastic stiffness tensor for an orthotropic material.
Arguments are either (convention 'EnuG'):

The Young modulus of axis 1 $E_1$,
The Young modulus of axis 2 $E_2$,
The Young modulus of axis 3 $E_3$,
The Poisson ratio of direction 1 with respect to 2 $\nu_{12}$,
The Poisson ratio of direction 1 with respect to 3 $\nu_{13}$,
The Poisson ratio of direction 2 with respect to 3 $\nu_{13}$,
The shear modulus of direction 1 with respect to 2 $G_{12}$,
The shear modulus of direction 1 with respect to 3 $G_{13}$,
The shear modulus of direction 2 with respect to 3 $G_{23}$,
The axial coefficient of thermal expansion in direction 1 $\alpha_1$,
The axial coefficient of thermal expansion in direction 1 $\alpha_2$,
The axial coefficient of thermal expansion in direction 1 $\alpha_3$,

or the list of Cii (C11, C12, C13, C22, C23, C33, C44, C55, C66) (convention 'Cii')
Can you write Python code for it?

E_1 = 4500.0
E_2 = 2300.0
E_3 = 2700.0
nu_12 = 0.06
nu_13 = 0.08
nu_23 = 0.3
G_12 = 2200.0
G_13 = 2100.0
G_23 = 2400.0

L = sim.L_ortho(E_1,E_2,E_3,nu_12,nu_13,nu_23,G_12,G_13,G_23,'EnuG')
print np.array_str(L, precision=2)

d = sim.check_symetries(L)
print(d['umat_type'])
print(d['axis'])
print np.array_str(d['props'], precision=2)

x = sim.L_ortho_props(L)
print np.array_str(x, precision=2)


I am working on the file "Examples/Continuum_Mechanics/constitutive_relations.ipynb".
The first task is:
M_iso
Provides the elastic compliance tensor for an isotropic material.
The two first arguments are a couple of elastic properties. The third argument specify which couple has been provided and the nature and order of coefficients.
Exhaustive list of possible third argument :
‘Enu’,’nuE,’Kmu’,’muK’, ‘KG’, ‘GK’, ‘lambdamu’, ‘mulambda’, ‘lambdaG’, ‘Glambda’.
Can you write Python code for it?

E = 70000.0
nu = 0.3
M = sim.M_iso(E,nu,"Enu")
print np.array_str(M, precision=2)

L_inv = np.linalg.inv(M)
d = sim.check_symetries(L_inv)
print(d['umat_type'])
print(d['props'])

x = sim.M_iso_props(M)
print(x)


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
J2_stress(vec)
Provides the second invariant of a second order stress tensor written as a vector v in the SMART+ formalism. Return a double.
$$ J_2 (\boldsymbol{\sigma}) = \frac{1}{2} \sigma_{ij} \sigma_{ij} $$
Can you write Python code for it?

v = np.random.rand(6)
J2 = sim.J2_stress(v)
print v
print J2


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
J3_strain(vec)
Provides the third invariant of a second order strain tensor written as a vector v in the SMART+ formalism. Return a vec.
$$ J_3 (\boldsymbol{\varepsilon}) = \frac{1}{2} \varepsilon_{ij} \varepsilon_{jk} \varepsilon_{ki} $$
Can you write Python code for it?

v = np.random.rand(6)
J3 = sim.J3_strain(v)
print v
print J3


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
J3_stress(vec)
Provides the third invariant of a second order stress tensor written as a vector v in the SMART+ formalism. Return a double.
$$ J_3 (\boldsymbol{\sigma}) = \frac{1}{2} \sigma_{ij} \sigma_{jk} \sigma_{ki} $$
Can you write Python code for it?

v = np.random.rand(6)
J3 = sim.J3_stress(v)
print v
print J3


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
Mises_strain(vec)
Provides the Von Mises strain $\varepsilon^{Mises}$ of a second order stress tensor written as a vector v in the SMART+ formalism. Return a double.
Can you write Python code for it?

v = np.random.rand(6)
Mises_eps = sim.Mises_strain(v)
print v
print Mises_eps


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
Mises_stress(vec)
Provides the Von Mises stress $\sigma^{Mises}$ of a second order stress tensor written as a vector v in the SMART+ formalism. Return a double.
Can you write Python code for it?

v = np.random.rand(6)
Mises_sig = sim.Mises_stress(v)
print v
print Mises_sig


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
dev(vec)
Provides the deviatoric part of a second order tensor written as a vector v in the SMART+ formalism. Return a vec. Example:
Can you write Python code for it?

v = np.random.rand(6)
v_dev = sim.dev(v)
print v
print v_dev


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
eta_stress(vec)
Provides the stress flow:
$$\eta_{stress}=\frac{3/2\sigma_{dev}}{\sigma_{Mises}}$$
from a second order stress tensor written as a vector v in the SMART+ formalism (i.e. the shear terms are multiplied by 2, providing shear angles). Return a vec.
Can you write Python code for it?

v = np.random.rand(6)
sigma_f = sim.eta_stress(v)
print v
print sigma_f


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
t2v_strain(vec)
Converts a second order strain tensor written as a matrix m in the SMART+ formalism into a second order strain tensor written as a vector v. Return a vec.
Can you write Python code for it?

m = np.random.rand(3,3)
m_symm = (m + m.T)/2
v = sim.t2v_strain(m_symm);
print m_symm
print v


I am working on the file "Examples/Continuum_Mechanics/contimech.ipynb".
The first task is:
tr(vec)
Provides the trace of a second order tensor written as a vector v in the SMART+ formalism. Return a double. Example:
Can you write Python code for it?

v = np.random.rand(6)
trace = sim.tr(v)
print v
print trace


I am working on the file "Examples/Ejemplo_Cuencas_Basico_1.ipynb".
The first task is:
Cada entrada en Qmax y Qmin corresponde al periodo de retorno Tr [2.33, 5, 10, 25, 50, 100], estos pueden ser cambiados al interior de la función al cambiar la propiedad Tr en el momento en que esta es invocada.
Can you write Python code for it?

# Plot del caudal máximo para un periodo de retorno de 2.33
cuenca.Plot_basin(Qmax[0])

# Plot del caudal máximo para un periodo de retorno de 100
cuenca.Plot_basin(Qmax[5])


I am working on the file "Examples/Ejemplo_Cuencas_Basico_1.ipynb".
The first task is:
Ejemplo cuencas
En el siguiente ejemplo se presentan las funcionalidades básicas de la herramienta wmf.Stream y wmf.Basin
dentro de los temas tocados se presenta:

Trazado de corrientes.
Perfil de corrientes.
Trazado de cuencas.
Balances para estimación de caudal.
Análisis Geomorfológico de cuencas.
Can you write Python code for it?

#Paquete Watershed Modelling Framework (WMF) para el trabajo con cuencas.
from wmf import wmf


I am working on the file "Examples/Ejemplo_Cuencas_Basico_1.ipynb".
The first task is:
El perfil de una corriente puede ser utilizado como punto de referencia para la búsqueda de puntos de trazado
Can you write Python code for it?

# Mediante el comando de busqueda hemos buscado donde se localizan las coordenadas que cumplen la propiedad de estar a
#una distancia de la salida que oscila entre 10000 y 10100 metros.
np.where((st.structure[3]>10000) & (st.structure[3]<10100))


I am working on the file "Examples/Ejemplo_Cuencas_Basico_1.ipynb".
The first task is:
La ultima cuenca tiene un conteo de celdas igual a 1, lo cual significa que no se ha trazado nada y que por esta celda no pasa ninguna otra celda, por lo tanto esto no es una cuenca, y no debe ser usado para ningún tipo de cálculos, en la siguiente línea este elemento es eliminado:
Can you write Python code for it?


I am working on the file "Examples/Ejemplo_Cuencas_Basico_1.ipynb".
The first task is:
Trazado de corrientes
Importantes para determinar por donde se acumula el flujo y por lo tanto por donde se debe trazar la cuenca, estos elementos operan más como una guia que como un resultado final, pueden ser usados directamente para el trazado de las cuencas acudiendo a su propiedad structure, en la cual en las dos primeras entradas se alojan la coordenada X y la coordenada Y
Trazado de una corriente
Can you write Python code for it?

st = wmf.Stream(-75.618,6.00,DEM=DEM,DIR=DIR,name ='Rio Medellin')

st.structure

st.Plot_Profile()


I am working on the file "Examples/Ejemplo_Cuencas_Basico_1.ipynb".
The first task is:
Trazado de cuenca
Trazado de cuencas mediante el objeto Basin
La cuenca se traza a partir de un par de coordenadas, de un DEM y de un DIR, se le pueden agregar parametros como el nombre o el umbral para producir corrientes, pero este es opcional, este tipo de cuencas no se dejan simular, para ello se debe usar la herramienta SimuBasin
Can you write Python code for it?

# Las coordenadas en la entrada 289 son:
print st.structure[0,289]
print st.structure[1,289]

# La cuenca puede ser trtazada utilizando las coordenadas de forma implicita (como en este ejemplo), o de una
# manera explicita como se realizaría en la segunda línea de código.
cuenca = wmf.Basin(-75.6364,6.11051,DEM,DIR,name='ejemplo',stream=st)
# en esta segunda linea estamos trazando una cuenca con unas coordenadas que no son exactas y no se sabe si estan
# sobre la corriente, este problema se corrige al pasarle la corriente al trazador mediante el comando stream, el cual
# recibe como entrada el objeto corriente previamente obtenido.
cuenca2 = wmf.Basin(-75.6422,6.082,DEM,DIR,name='ejemplo',stream=st)
# Cuenca error: en este caso no se para el argumento stream, por lo que la cuenca se traza sobre las coordenadas
# que se han dado, lo cual probablemente produzca un error.
cuenca3 = wmf.Basin(-75.6364,6.11051,DEM,DIR,name='ejemplo',stream=st)

# Se imprime la cantidad de celdas que comprenden a cada una de las cuencas obtenidas, esto para ver que efectivamente
# existe una diferencia entre ambas debida a las diferencias de coordenadas.
print cuenca.ncells
print cuenca2.ncells
print cuenca3.ncells


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Caudales Máximos
Adicional a los caudales máximos estimados por regionalización, se estiman los caudales máximos por el método de hidrógrafa unitaria sintética:

sneyder.
scs.
williams
Can you write Python code for it?

cuCap.GetGeo_Parameters()

#Parametros pára maximos
TcCap = np.median(cuCap.Tc.values())
#CN = 50
CN=80
print 'tiempo viaje medio Captacion:', TcCap
print u'Número de curva:', CN

# Obtención de la lluvia de diseño.
Intensidad = [40.9, 49.5, 55.5, 60.6, 67.4, 75.7]
# Lluvia efectiva
lluviaTr,lluvEfect,S = cuCap.GetHU_DesingStorm(np.array(Intensidad),
	TcCap,
	CN=CN,
	plot='si',
	ruta=ruta_images + 'Q_max_LLuvia_Efectiva_descarga.png',
	Tr=[2.33, 5, 10, 25, 50, 75, 100])


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Caudales extremos por regionalización
Se calculan los caudales extremos máximos y mínimos para los periodos de retorno de:
- 2.33, 5, 10, 25, 50, 75 y 100
Se utilizan las siguientes metodologías:

Regionalización con gumbel y lognormal.
Can you write Python code for it?

#Periodos de retrorno para obtener maximos y minimos
Tr=[2.33, 5, 10, 25, 50, 100]

QmaxRegGum = cuCap.GetQ_Max(cuCap.CellQmed, Dist='gumbel',  Tr= Tr, Coef = [6.71, 3.29], Expo = [1.2, 0.9])
QmaxRegLog = cuCap.GetQ_Max(cuCap.CellQmed, Dist='lognorm',  Tr= Tr, Coef = [6.71, 3.29], Expo = [1.2, 0.9])
QminRegLog = cuCap.GetQ_Min(cuCap.CellQmed, Dist='lognorm',  Tr= Tr,)
QminRegGum = cuCap.GetQ_Min(cuCap.CellQmed, Dist='gumbel',  Tr= Tr,)


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
El cauce principal presenta un desarrollo típico de una cuenca mediana-grande, en donde de ve claramente una zona de producción de sedimentos entre los 0 y 10 km, y de los 10km en adelante se presenta una zona de transporte y depositación con pendientes que oscilan entre 0.0 y 0.8 %
Can you write Python code for it?


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
El histograma de pendientes muestra que gran parte de las pendientes son inferiores al 0.6, por lo cual se considera que el cauce ppal de la cuenca se desarrolla ppalmente sobre un valle.
Mapas de Variables Geomorfo
Can you write Python code for it?

cuCap.GetGeo_HAND()

cuCap.Plot_basin(cuCap.CellHAND_class, ruta=ruta_images+'Map_HAND_class.png', lines_spaces=0.01)

cuCap.Plot_basin(cuCap.CellSlope, ruta=ruta_images + 'Map_Slope.png', lines_spaces=0.01)


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Este mapa debe ser recalculado con una mayor cantidad de iteraciones, lo dejamos haciendo luego, ya que toma tiempo, de momento esta malo.
Can you write Python code for it?

ruta_images = '/media/nicolas/discoGrande/01_SIATA/ParamCuencas/AltaVistaAbajo/'

cuCap.Plot_Travell_Hist(ruta=ruta_images + 'Histogram_IsoCronas.png')


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Hidrogramas unitarios calibrados para la cuenca, williams muestra un rezago en este caso con las demás metodologías
Can you write Python code for it?

#QmaxRegGum = cuCap.GetQ_Max(cuCap.CellQmed, Dist='gumbel',  Tr= Tr, Coef = [6.71, 3.29], Expo = [1.2, 0.9])
#QmaxRegLog = cuCap.GetQ_Max(cuCap.CellQmed, Dist='lognorm',  Tr= Tr, Coef = [6.71, 3.29], Expo = [1.2, 0.9])

#Realiza la convolucion de los hidrogramas sinteticos con la tormenta de diseno
HidroSnyder,QmaxSnyder,Tsnyder = cuCap.GetHU_Convolution(Tsnyder,Qsnyder,lluvEfect)
HidroWilliam,QmaxWilliam,Twilliam = cuCap.GetHU_Convolution(Twilliam,Qwilliam,lluvEfect)
HidroSCS,QmaxSCS,Tscs = cuCap.GetHU_Convolution(Tscs,Qscs,lluvEfect)

DicQmax = {#'Snyder':QmaxSnyder,
    #'Williams':QmaxWilliam,
    #'SCS': QmaxSCS,
    'Gumbel': QmaxRegGum[:,-1],
    'Log-Norm': QmaxRegLog[:,-1]}
#Plot de maximos
pyt.PlotQmaxTr(DicQmax,Tr,ruta=ruta_images + 'Q_max_Metodos_Tr_descarga.png')
#Tablas de maximos
DataQmax = pd.DataFrame(DicQmax, index=Tr)
#Escritura en excel.
writer = pd.ExcelWriter(ruta_images + 'Qmax_captacion.xlsx')
DataQmax.to_excel(ruta_images + 'Qmax_captacion.xls')

cuCap.GeoParameters


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Lectura de mapas de direcciones y de elevación:

Trazado de cuencas y corrientes
Can you write Python code for it?

cuCap = wmf.SimuBasin(0,0,0,0,rute='/media/nicolas/discoGrande/01_SIATA/nc_cuencas/Picacha_Abajo.nc')

#Guarda Vector de la cuenca
cuCap.Save_Basin2Map('/media/nicolas/discoGrande/01_SIATA/vector/Cuenca_AltaVista2.shp')

cuCap.Save_Net2Map('/media/nicolas/discoGrande/01_SIATA/vector/Red_Altavista_Abajo.shp',dx = 12.7, umbral=470)


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Precipitación
A continuación se realiza el análisis de la precipitación en la zona, de esta manera se conocen las condiciones climáticas en la región.
Procedimiento para Desagregar lluvia (obtener IDF)
Lee la estación de epm con datos horarios
Caudales
Calculo de caudales medio de largo plazo mediante el campo de precipitación estimado para la zona mediante el uso de las estaciones del IDEAM

Q medio por balance.
Qmax por regionalización y HU sintéticas
Qmin por regionalización y análisis de serie de caudales simulada a la salida de la cuenca

Caudal Medio Largo Plazo
Can you write Python code for it?

Precip = 1650
cuCap.GetQ_Balance(Precip)
cuCap.Plot_basin(cuCap.CellETR, ruta=ruta_images+'Map_ETR_Turc.png', lines_spaces=0.01)

cuCap.GetQ_Balance(Precip)
print 'Caudal Captacion:', cuCap.CellQmed[-1]

cuCap.Plot_basin(Precip - cuCap.CellETR, ruta = ruta_images+'Map_RunOff_mm_ano.png',
    lines_spaces=0.01,
    colorTable = 'jet_r')


I am working on the file "Examples/Ejemplo_Hidrologia_Maximos.ipynb".
The first task is:
Tiempo de viaje
Can you write Python code for it?

cuCap.GetGeo_Parameters()

cuCap.Tc

#Parametros Geomorfologicos de las cuencas
cuCap.GetGeo_Parameters(rutaParamASC=ruta_images+'param_cap.txt',
    plotTc=True,
    rutaTcPlot=ruta_images+'Tc_cap.png')


I am working on the file "Examples/Ejemplo_Uso_radar.ipynb".
The first task is:
La lluvia cuando es transformada queda expresada en intensidad, para ser expresada en cantidad debe ser dividida por 12.
Can you write Python code for it?

# Obtencion de la conversion a lluvia
rad.DBZ2Rain()
# Suma de la lluvia en todo el campo del radar
rad.ppt = rad.ppt/12.0
print rad.ppt.sum()

# Plot de la matriz de radar
rad.plot.plot_radar_elegant(rad.ppt)


I am working on the file "Examples/External/Diffusion1DSpherical_Analytic-vs-FVTool-vs-Fipy/diffusion1Dspherical_analytic_vs_FVTool_vs_Fipy.ipynb".
The first task is:
Diffusion of an initial sphere into an infinite medium
M.H.V. Werts, 2020
Here, we study the diffusion equation in an infinite medium with the initial condition that all matter is homogeneously distributed in a sphere radius $a$, and no matter is outside of this sphere.
Reference: J. Crank (1975) "The Mathematics of Diffusion", 2nd Ed.,
      Clarendon Press (Oxford), pages 29-30, Equation 3.8, Figure 3.1
A system of spherical symmetry in a spherical coordinate system, i.e. "1D spherical", space coordinate $r$. Time $t$.
System parameters:
$a$ : radius of initial sphere;
$c_0$ : concentration in initial sphere;
$D$ : diffusion coefficient
Simple diffusion equation:
$$\frac{\partial c}{\partial t} = D \nabla^2 c$$
with initial condition:
$$
c(t = 0, r \leqslant a) = c_0 \quad \
c(t = 0, r > a) = 0
$$
Can you write Python code for it?


I am working on the file "Examples/External/Diffusion1DSpherical_Analytic-vs-FVTool-vs-Fipy/diffusion1Dspherical_analytic_vs_FVTool_vs_Fipy.ipynb".
The first task is:
Fipy
Fipy does not have a 1D spherical grid. Therefore we use a 2D cylindrical grid with a variable rs which contains the distance to the origin for each cell center.
We experimented with number of cells (moving to 200x200 instead of 120x120 does not change final result much)
Can you write Python code for it?

boxsize = 120
msh = fp.CylindricalGrid2D(nr = boxsize, nz = boxsize, Lr = 10.0, Lz = 10.0)
rc = msh.cellCenters[0]
zc = msh.cellCenters[1]
rs = numerix.sqrt(rc**2 + zc**2)


I am working on the file "Examples/External/Diffusion1DSpherical_Analytic-vs-FVTool-vs-Fipy/diffusion1Dspherical_analytic_vs_FVTool_vs_Fipy.ipynb".
The first task is:
For plotting the Fipy solution after this first careful timestep, we only plot the values of the cells at the lowest $z$ ($z = 0$).
Can you write Python code for it?

plt.plot(rs.value[0:boxsize],c.value[0:boxsize])
plt.title('t = {0:.5f}'.format(t))


I am working on the file "Examples/External/Diffusion1DSpherical_Analytic-vs-FVTool-vs-Fipy/diffusion1Dspherical_analytic_vs_FVTool_vs_Fipy.ipynb".
The first task is:
Now, we initialize time $t$, and take the first time step.
Can you write Python code for it?

t = 0. # master time
deltat = 0.0625/10

eq.solve(dt=deltat); t+=deltat


I am working on the file "Examples/External/Diffusion1DSpherical_Analytic-vs-FVTool-vs-Fipy/diffusion1Dspherical_analytic_vs_FVTool_vs_Fipy.ipynb".
The first task is:
The boundary conditions may be Dirichlet on the outer boundaries (right and top), and zero-flux on the inner boundaries (bottom and left). Zero-flux is implicit: if nothing is specified we have zero-flux.
Anyway, in the present case there is no difference between Dirichlet and zero-flux, showing that our grid is large enough to emulate an infinite medium.
Can you write Python code for it?

## if this is commented out, we are using zero-flux!
#c.constrain(0., where = msh.facesRight)
#c.constrain(0., where = msh.facesTop)


I am working on the file "Examples/External/Diffusion1DSpherical_Analytic-vs-FVTool-vs-Fipy/diffusion1Dspherical_analytic_vs_FVTool_vs_Fipy.ipynb".
The first task is:
We still need to define our transport equation
Can you write Python code for it?

eq = fp.TransientTerm(var = c) == fp.DiffusionTerm(coeff = D, var = c)


I am working on the file "Examples/FusionExample.ipynb".
The first task is:
Read in Data
Using GeoData we first have to get the data into the proper format. This example uses RISR data retrived from Madrigal, which can be read in by using the readMat_hdf5 function. This will create the GeoData instance labeled as risr. The OMTI file is read in using readOMTI function, which creates the GeoData instance named omti. Lastly to get linearly scaled electron density we use the method changedata from GeoData.
Can you write Python code for it?

def revpower(x1,x2):
    return x2**x1

omtiName = 'OMTIdata.h5'
risrName = 'ran120219.004.hdf5'
isrparams=['nel']
#creating GeoData objects of the 2 files, given a specific parameter

omti = GeoData.GeoData(utilityfuncs.readOMTI,(omtiName, ['optical']))

risr = GeoData.GeoData(utilityfuncs.readMad_hdf5,(risrName, isrparams))
#converting logarthmic electron density (nel) array into electron density (ne) array
risr.changedata('nel','ne',revpower,[10.0])


I am working on the file "Examples/FusionExample.ipynb".
The first task is:
Time Register Data
The next step once the data has been read into memory is registering the data in time and space. The method timeregister will find all of the instances where the data from risr instance overlap with data from omti. The data is then reduced to only the instances where there are common times between omti and risr. The other times are tossed out so when we interpolate we don't have to interpolate data we have no use for.
Can you write Python code for it?

reglistlist = omti.timeregister(risr)
keepomti = [i  for i in range(len(reglistlist)) if len(reglistlist[i])>0]
reglist = list(set(np.concatenate(reglistlist)))

# Reduce the size of the GeoData Instances.
risr_classred =risr.timeslice(reglist,'Array')
omti = omti.timeslice(keepomti,'Array')
# Re-register in time
reglistfinal = omti.timeregister(risr_classred)


I am working on the file "Examples/FusionExamplempl.ipynb".
The first task is:
Interpolation
The reduced data is now interpolated into a common Cartesian frame. The method interpolate performs changes the coordinate systems, and interpolates the data and changes the instance of the class to the new coordinate system. For the omti instance we only interpolate in two dimensions as we only have azimuth and elevation info. We pick an altitude to interpolate over and file any gaps with NaNs. The risr data is in three dimensions as we have range information for it. Currently the fastest method of interpolation is the linear interpolation method.
Can you write Python code for it?

xvec,yvec,zvec = [np.linspace(-100.0,500.0,25),np.linspace(0.0,600.0,25),np.linspace(100.0,500.0,25)]
x,y,z = np.meshgrid(xvec,yvec,zvec)
x2d,y2d = np.meshgrid(xvec,yvec)
new_coords =np.column_stack((x.ravel(),y.ravel(),z.ravel()))
new_coords2 = np.column_stack((x2d.ravel(),y2d.ravel(),140.0*np.ones(y2d.size)))
#%% interpolate risr data
risr_classred.interpolate(new_coords, newcoordname='Cartesian', method='linear', fill_value=np.nan)
#%% interpolate omti data
omti.interpolate(new_coords2, newcoordname='Cartesian', twodinterp = True,method='linear', fill_value=np.nan)


I am working on the file "Examples/FusionExamplempl.ipynb".
The first task is:
Read in Data
Using GeoData we first have to get the data into the proper format. This example uses RISR data retrived from Madrigal, which can be read in by using the readMat_hdf5 function. This will create the GeoData instance labeled as risr. The OMTI file is read in using readOMTI function, which creates the GeoData instance named omti. Lastly to get linearly scaled electron density we use the method changedata from GeoData.
Can you write Python code for it?


I am working on the file "Examples/HowTo_manualNetwork.ipynb".
The first task is:
Adding a gene
A species itself is of no use for the evolution algorithm. The architecture of a networks associates a TModule and a CorePromoter to a a species to build an cluster representing a gene for the program. The TModule is there so that other transcription factors can bind to it and regulate S0.
Can you write Python code for it?

L = mutation.Mutable_Network(g) ## Clear the network

## Gene 0
parameters=[['Degradable',0.5]]
parameters.append(['TF',1])
parameters.append(['Complexable'])
TM0,prom0,S0 = L.new_gene(0.5,5,parameters) ## Adding a new gene creates a TModule, a CorePromoter and a species

# Gene 1
parameters=[['Degradable',0.5]]
parameters.append(['TF',0])
parameters.append(['Complexable'])
TM1,prom1,S1 = L.new_gene(0.5,5,parameters)

# Gene 2
parameters=[['Degradable',0.5]]
parameters.append(['TF',1])
parameters.append(['Phosphorylable'])
TM2,prom2,S2 = L.new_gene(0.5,5,parameters)

# Gene 3
parameters=[['Degradable',0.5]]
parameters.append(['TF',0])
TM3,prom3,S3 = L.new_gene(0.5,5,parameters)


I am working on the file "Examples/HowTo_manualNetwork.ipynb".
The first task is:
Create a new  species S0
S0 is a reference to access quickly to the newly created species latter in the code. Note that one can add attributes to a species by adding elements to a parameter array that is passed to the new_species method.
ATTENTION : This way of creating a species is not recommanded as it does not handle the interaction between the network's different species (see next section). It is here as to get a feeling on how the intern code works.
Can you write Python code for it?

parameters=[['Degradable',0.5]] ## The species is degradable with a rate 0.5
parameters.append(['Input',0]) ## The species cannot serve as an input for the evolution algorithm
parameters.append(['Complexable']) ## The species can be involved in a complex
parameters.append(['Kinase']) ## The specise can phosphorilate another species.
parameters.append(['TF',1]) ## 1 for activator 0 for repressor
S0 = L.new_Species(parameters)


I am working on the file "Examples/HowTo_manualNetwork.ipynb".
The first task is:
Create an empty network
Can you write Python code for it?

g = random.Random(20160225) # This define a new random number generator
L = mutation.Mutable_Network(g) # Create an empty network


I am working on the file "Examples/HowTo_manualNetwork.ipynb".
The first task is:
Regulate the production of S1 by S3 and S5
Note that since the parameter L.fixed_activity_for_TF is True, the activity setting in new_TFHill  is not taken into account. Only the TF activity counts.
Can you write Python code for it?

S5.change_type("TF",[1]) # Note this is already the default value for a phosphorilated species
tfhill1 = L.new_TFHill( S3, 1, 0.5, TM1,activity=1)
tfhill2 = L.new_TFHill( S5, 1, 0.5, TM1,activity=1)


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Analysis plots - ray-fan analysis plot as an example
To plot the ray-fan analysis graph using ipzCaptureWindow() function, we need to provide the 3-letter button code of the corresponding analysis function. If we don't remember the exact button code, we can use a helper function in pyzdde to get some help:
Can you write Python code for it?

pyz.findZButtonCode('ray')

l1.ipzCaptureWindow('Ray', gamma=0.4)


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Bingo! AMAG is the operand we want. Now we can use
Can you write Python code for it?

l1.zOperandValue('AMAG', 1) # the argument "1" is for the wavelength


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Connecting to another Zemax session simultaneously
Now, a second pyzdde object is created to communicate with a second ZEMAX server. Note that the first object is still present.
Can you write Python code for it?

l2 = pyz.createLink() # create a second DDE communication link object


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Create PyZDDE object
Can you write Python code for it?

l1 = pyz.createLink() # create a DDE link object for communication


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Executing ZPL macro
Lastly, here is an example of how execute a ZPL macro using the PyZDDE.
Since ZEMAX can execute ZPL macros present in a set folder (generally the default macro folder in the data folder), the appropriate macro folder path needs to be set if it is not the default macro folder path.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Few DDE dataitems that return information about the system have duplicate functions with the prefix ipz. They generally produce outputs that are suitable for interactive environments, and more suitable for human understanding. One example of such function pair is zGetFirst() and ipzGetFirst():
Can you write Python code for it?

# Paraxial/ first order properties of the system
l1.zGetFirst()

# duplicate of zGetFirst() for use in the notebook
l1.ipzGetFirst()

# ... another example is the zGetSystemAper() that returns information about the aperture.
# The aperture type is retuned as a code which we might not remember always ...
l1.zGetSystemAper()

# ...with the duplicate, ipzGetSystemAper(), we can immediately know that
# the aperture type is the Entrance Pupil Diameter (EPD)
l1.ipzGetSystemAper()

# information about the field definition
l1.ipzGetFieldData()


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Just to demonstrate that the first lens (in the first ZEMAX server) is still available, the Layout plot is rendered again.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Layout plot of the second lens
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
MTF plot of the first lens
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Now, lets say that we want to find the angular magnification of the above optics and we want to find if Zemax provides any operand whose value we can directly read. For that we can use another module level helper function to find all operands related to angular magnification:
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/00 Using ZEMAX and PyZDDE with IPython notebook.ipynb".
The first task is:
Spot diagram of the second lens
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
A few others .... just for show
Can you write Python code for it?

l.ipzCaptureWindowLQ(4) # Shaded Model

l.close()


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Example of a Layout plot
Using ipzCaptureWindow to directly embed a Layout plot into the notebook.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Examples of using ipzCaptureWindowLQ() function in Zemax 13.2 or earlier
ipzCaptureWindowLQ() is useful for quickly capturing a graphic window, and embedding into an IPython notebook or QtConsole.
In order to use this function, please copy the ZPL macros from "PyZDDE\ZPLMacros" to the macro directory where Zemax is expecting the ZPL macros to be (i.e. the folder set in Zemax->Preference->Folders->ZPL).
For this particular example, the macro folder path is set to "C:\PROGRAMSANDEXPERIMENTS\ZEMAX\Macros"
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Load a lens file
Can you write Python code for it?

zfile = os.path.join(l.zGetPath()[1], 'Sequential', 'Objectives', 'Cooke 40 degree field.zmx')
l.zLoadFile(zfile)


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Next, we will create a figure and direct PyZDDE to render the Layout plot in the provided figure and axes. We can then annotate the figure as we like.
But first we will get some first-order properties of the lens
Can you write Python code for it?

l.ipzGetFirst()

fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)

# Render the array
pyz.imshow(arr, cropBorderPixels=(5, 5, 1, 90), fig=fig, faxes=ax)

ax.set_title('Layout plot', fontsize=16)
# Annotate Lens numbers
ax.text(41, 70, "L1", fontsize=12)
ax.text(98, 105, "L2", fontsize=12)
ax.text(149, 89, "L3", fontsize=12)

# Annotate the lens with radius of curvature information
col = (0.08,0.08,0.08)
s1_r = 1.0/l.zGetSurfaceData(1,2)
ax.annotate("{:0.2f}".format(s1_r), (37, 232), (8, 265), fontsize=12,
                arrowprops=dict(arrowstyle="->", linewidth=0.45, color=col, relpos=(0.5,0.5)))
s2_r = 1.0/l.zGetSurfaceData(2,2)
ax.annotate("{:0.2f}".format(s2_r), (47, 232), (50, 265), fontsize=12,
                arrowprops=dict(arrowstyle="->", linewidth=0.45, color=col,  relpos=(0.5,0.5)))
s6_r = 1.0/l.zGetSurfaceData(6,2)
ax.annotate("{:0.2f}".format(s6_r), (156, 218), (160, 251), fontsize=12,
                arrowprops=dict(arrowstyle="->", linewidth=0.45, color=col,  relpos=(0.5,0.5)))
ax.text(5, 310, "Cooke Triplet, EFL = {} mm, F# = {}, Total track length = {} mm"
        .format(50, 5, 60.177), fontsize=14)
plt.show()


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Now open the layout analysis window in OpticStudio as before.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Now open the layout analysis window in Zemax. Assuming that this is the first analysis window that has been open, Zemax would have assigned the number 1 to it.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Now that we have the pixel array, we can either use the convenience function provided in PyZDDE to make a quick plot, or make our own figure and plot as we want it.
Let's first see how we can use the convenience function, imshow(), provided by PyZDDE to make a cropped plot. The functions takes as input the pixel array, a tuple indicating the number of pixels to crop from the left, right, top, bottom sides of the pixel array, a tuple indicating the matplotlib figure size (optional), and a title string (optional).
Can you write Python code for it?

pyz.imshow(arr, cropBorderPixels=(5, 5, 1, 90), figsize=(10,10), title='Layout Plot')


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Open FFT MTF analysis window
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Open the MTF analysis window in Zemax now.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Perform a quick-focus
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/01 Notes on ipzCaptureWindow functions.ipynb".
The first task is:
Why do we need to set gamma?

 Is there one gamma value good for all analysis window rendering?

Upto Zemax13 there was no way to control othe thickness of the lines produced by ZEMAX for the metafiles. Generally the lines produced were very thin and the rescaled version would be too light to be visible. One way in which this problem was addressed is to lowpass filter the original image, rescale and then use a gamma value less than one during the conversion from metafile to PNG. This is probably not the optimal solution. One obvious side effect is that the black text becomes very thick and ugly.
Instead of embedding the figure directly, we can also get a pixel array using PyZDDE. Plotting the returned array using matplotlib may allow more control and annotation options as shown below:
Can you write Python code for it?

arr = l.ipzCaptureWindow('Lay', percent=15, gamma=0.08, retArr=True)


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
From the above plots we can see that there is no <u>appreciable</u> change in the phase of the beam. In order to get a better picture, we will plot slices of the 2D data.
Can you write Python code for it?

fig = plt.figure(figsize=(8,8))

# irradiance data
ax = fig.add_subplot(2,2,1)
ax.set_title('Irradiance at STO', fontsize=14)
dx = irr_sur1[-2]/256
x = [-irr_sur1[-2]/2 + dx*i for i in range(256)]
ax.plot(x, irrGridDat_sur1[128])
ax.set_ylim(top=np.max(np.array(irrGridDat_sur1[128])))
ax.set_xlim(left=-irr_sur1[-2]/2, right=irr_sur1[-2]/2)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('Irradiance (Watts per square mm)', fontsize=13)

ax = fig.add_subplot(2,2,2)
dx = irr_sur4[-2]/256
x = [-irr_sur4[-2]/2 + dx*i for i in range(256)]
ax.set_title('Irradiance at fiber', fontsize=14)
ax.plot(x, irrGridDat_sur4[128])
ax.set_ylim(top=np.max(np.array(irrGridDat_sur4[128])))
ax.set_xlim(left=-irr_sur4[-2]/2, right=irr_sur4[-2]/2)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('Irradiance (Watts per square mm)', fontsize=13)

# phase data
ax = fig.add_subplot(2,2,3)
ax.set_title('Phase at STO', fontsize=14)
dx = pha_sur1[-2]/256
x = [-pha_sur1[-2]/2 + dx*i for i in range(256)]
ax.plot(x, phaGridDat_sur1[128])
ax.set_ylim(top=np.pi, bottom=-np.pi)
ax.set_xlim(left=-pha_sur1[-2]/2, right=pha_sur1[-2]/2)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('Phase (radians)', fontsize=13)

ax = fig.add_subplot(2,2,4)
ax.set_title('Phase at fiber', fontsize=14)
dx = pha_sur4[-2]/256
x = [-pha_sur4[-2]/2 + dx*i for i in range(256)]
ax.plot(x, phaGridDat_sur4[128])
ax.set_ylim(top=np.pi, bottom=-np.pi)
ax.set_xlim(left=-pha_sur4[-2]/2, right=pha_sur4[-2]/2)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('Phase (radians)', fontsize=13)

fig.tight_layout()
plt.show()


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
From the above we see that the system efficiency, which is the energy transported by the optical system is 1. The receiver efficiency is 0.999955, and the coupling efficiency is 0.999955.
Phase data at STO surface:
In order to get the phase information we need to use the function zSetPOPSettings() once again. This is mainly because there is (currently) no way to modify the "Data" parameter externally. So in PyZDDE, we create a new settings file when everytime we switch between "Phase" and "Irradiance" data. Since we have already retrieved the irradiance data, we will re-use the name of the previous settings file.
Can you write Python code for it?

sfile = ln.zSetPOPSettings(data=1, startSurf=1, endSurf=1, field=1,
                           wave=1, beamType=0, paramN=((1, 2), (2, 2)),
                           tPow=1, sampx=4,sampy=4, widex=40, widey=40)

pha_sur1, phaGridDat_sur1 = ln.zGetPOP(settingsFile=sfile, displayData=True)

pha_sur1


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
In the above plots, the gray lines represent the intensity and phase data slices when the beam originated at the STO surface (i.e. surface to beam = 0). The red lines represent the intensity and phase data slices when the beam originated 3000 mm to the left of the STO surface.
While the intensity plots for the two surfaces in both the cases are very similar, the phase plots show significant differences. Firstly, as the beam propagates from its waist position, 3000 mm behind the STO surface, to the STO surface its radius of curvature decreases from infinity to a finite value. Secondly, we can see the Gouy shift of phase profile of the Gaussian beam.
Coupling efficiency
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Intensity at Fiber
Can you write Python code for it?

ln.zModifyPOPSettings(settingsFile=sfile, endSurf=5, fibComp=1,
                      fibType=0, fparamN=((1, 2), (0.008, 0.008)))

new_irr_sur5, new_irrGridDat_sur5 = ln.zGetPOP(settingsFile=sfile, displayData=True)


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Layout plot
The layout plot shows where we will place the input Gaussian beam (at the location of STO). The beam, after passing through the lens should be focused into the fiber. The surface numbers for the stop and fiber are shown in parentheses.
Can you write Python code for it?

ln.zPushLens(1) # was pushed to LDE, and made sure that the "Frame suppress" was check on layout plot

lay = ln.ipzCaptureWindow('Lay', percent=15, gamma=0.1, retArr=True)

fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
# Render the array
pyz.imshow(lay, cropBorderPixels=(2, 5, 60, 90), fig=fig, faxes=ax)
ax.text(85, 75, "Lens", fontsize=12)
ax.text(20, 75, "STO (#1)", fontsize=12, rotation='vertical')
ax.text(510, 80, "Fiber (#4)", fontsize=12, rotation='vertical')
col = (0.08,0.08,0.08)
ax.annotate("{}".format('Beam origin'), (25, 163), (25, 230), fontsize=12,
                arrowprops=dict(arrowstyle="->", linewidth=0.45, color=col,
                                relpos=(0.0,0.5)))
ax.annotate("{}".format('Beam focus'), (516, 152), (448, 230), fontsize=12,
                arrowprops=dict(arrowstyle="->", linewidth=0.45, color=col,
                                relpos=(1.0,0.5)))
#ax.set_title('Layout plot', fontsize=14)
plt.show()


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Now, if we change the input beam waist to be 1.5 mm instead of 2 mm, which was the perfect coupling case, we will see that the coupling efficiency will decrease.
Can you write Python code for it?

sfile = ln.zSetPOPSettings(data=0, startSurf=1, endSurf=4, field=1,
                           wave=1, beamType=0, paramN=((1, 2), (1.5, 1.5)),
                           tPow=1, sampx=4, sampy=4, widex=40, widey=40,
                           fibComp=1, fibType=0, fparamN=((1, 2), (0.008, 0.008)))

irr_sur4_imperfect, irrGridDat_sur4_imperfect = ln.zGetPOP(settingsFile=sfile, displayData=True)

irr_sur4_imperfect


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Phase at Fiber
Can you write Python code for it?

ln.zModifyPOPSettings(settingsFile=sfile, endSurf=5, fibComp=1,
                      fibType=0, fparamN=((1, 2), (0.008, 0.008)))

new_pha_sur5, new_phaGridDat_sur5 = ln.zGetPOP(settingsFile=sfile, displayData=True)


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Phase data at surface 4:
Can you write Python code for it?

ln.zModifyPOPSettings(settingsFile=sfile, endSurf=4, fibComp=1, fibType=0,
                      fparamN=((1, 2), (0.008, 0.008))) # Change analysis surface to 4 and add fiber comp

pha_sur4, phaGridDat_sur4 = ln.zGetPOP(settingsFile=sfile, displayData=True)


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Plot of the irradiance and phase data at the two surfaces
In the following sections we shall use matplotlib to render the raw data which we have grabbed above. Consequently, will will have to write code for generating the plots. The advantage is that we have complete control of how we want to analyze and present the data. For quick notes, you can always use zCaptureWindow() to grab the POP graphic window from Zemax as shown in an example at the end of this article.
Can you write Python code for it?

fig = plt.figure(figsize=(8,8))

# irradiance data
ax = fig.add_subplot(2,2,1)
ax.set_title('Irradiance at STO', fontsize=14)
irrmax = np.max(irrGridDat_sur1)
ext = [-irr_sur1.widthX/2, irr_sur1.widthX/2,
       -irr_sur1.widthY/2, irr_sur1.widthY/2]
ax.imshow(irrGridDat_sur1, extent=ext, origin='lower',
          cmap=plt.cm.coolwarm, vmin=0, vmax=irrmax)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

ax = fig.add_subplot(2,2,2)
ax.set_title('Irradiance at fiber', fontsize=14)
irrmax = np.max(irrGridDat_sur4)
ext = [-irr_sur4.widthX/2, irr_sur4.widthX/2,
       -irr_sur4.widthY/2, irr_sur4.widthY/2]
ax.imshow(irrGridDat_sur4, extent=ext, origin='lower',
          cmap=plt.cm.coolwarm, vmin=0, vmax=irrmax)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

# phase data
ax = fig.add_subplot(2,2,3)
ax.set_title('Phase at source', fontsize=14)
ext = [-pha_sur1.widthX/2, pha_sur1.widthX/2,
       -pha_sur1.widthY/2, pha_sur1.widthY/2]
ax.imshow(phaGridDat_sur1, extent=ext, origin='lower',
          vmin=-np.pi, vmax=np.pi,  cmap=plt.cm.coolwarm)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

ax = fig.add_subplot(2,2,4)
ax.set_title('Phase at fiber', fontsize=14)
ext = [-pha_sur4.widthX/2, pha_sur4.widthX/2,
       -pha_sur4.widthY/2, pha_sur4.widthY/2]
ax.imshow(phaGridDat_sur4, extent=ext, origin='lower',
          vmin=-np.pi, vmax=np.pi,  cmap=plt.cm.coolwarm)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

fig.tight_layout()
plt.show()


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
Plots of irradiance and phase
Can you write Python code for it?

fig = plt.figure(figsize=(8,8))

# irradiance data
ax = fig.add_subplot(2,2,1)
ax.set_title('Irradiance at STO', fontsize=14)
ext = [-new_irr_sur2[-2]/2, new_irr_sur2[-2]/2,
       -new_irr_sur2[-1]/2, new_irr_sur2[-1]/2]
ax.imshow(new_irrGridDat_sur2, extent=ext, origin='lower',
          cmap=plt.cm.coolwarm)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

ax = fig.add_subplot(2,2,2)
ax.set_title('Irradiance at fiber', fontsize=14)
ext = [-new_irr_sur5[-2]/2, new_irr_sur5[-2]/2,
       -new_irr_sur5[-1]/2, new_irr_sur5[-1]/2]
ax.imshow(new_irrGridDat_sur5, extent=ext, origin='lower',
          cmap=plt.cm.coolwarm)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

# phase data
ax = fig.add_subplot(2,2,3)
ax.set_title('Phase at STO', fontsize=14)
ext = [-new_pha_sur2[-2]/2, new_pha_sur2[-2]/2,
       -new_pha_sur2[-1]/2, new_pha_sur2[-1]/2]
ax.imshow(new_phaGridDat_sur2, extent=ext, origin='lower',
          vmin=-np.pi, vmax=np.pi, cmap=plt.cm.coolwarm)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)

ax = fig.add_subplot(2,2,4)
ax.set_title('Phase at fiber', fontsize=14)
ext = [-new_pha_sur5[-2]/2, new_pha_sur5[-2]/2,
       -new_pha_sur5[-1]/2, new_pha_sur5[-1]/2]
ax.imshow(new_phaGridDat_sur5, extent=ext, origin='lower',
          vmin=-np.pi, vmax=np.pi,  cmap=plt.cm.coolwarm)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('y (mm)', fontsize=13)
fig.tight_layout()
plt.show()

fig = plt.figure(figsize=(8,8))

# irradiance data
ax = fig.add_subplot(2,2,1)
ax.set_title('Irradiance at STO', fontsize=14)
dx = irr_sur1[-2]/256
x = [-irr_sur1[-2]/2 + dx*i for i in range(256)]
ax.plot(x, irrGridDat_sur1[128],'gray')
ax.plot(x, new_irrGridDat_sur2[128], 'r')
ax.set_ylim(top=np.max(np.array(irrGridDat_sur1[128])))
ax.set_xlim(left=-irr_sur1[-2]/2, right=irr_sur1[-2]/2)
ax.set_xlabel('x (mm)', fontsize=13)
ax.set_ylabel('Irradiance (Watts per square mm)', fontsize=13)

ax = fig.add_subplot(2,2,2)
dx = irr_sur4[-2]/256
x =...


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
The above function creates .cfg file (if not provided with one) after setting the appropriate parameters for the POP analysis and returns the full name of the file.
Note that we want to analyze the effect of the beam that is propagating from surace 1 to surface 4. However, in the above function we set the endSurf to 1 in order to retrieve the POP data at the STO  surface. Also, in the above function call, we did not select the "compute fiber coupling integral" option. This is because we are interested in the fiber coupling computation at the surface 4.
Irradiance data at STO surface:
Can you write Python code for it?

irr_sur1, irrGridDat_sur1 = ln.zGetPOP(settingsFile=sfile, displayData=True)


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
The coupling efficiency reduced from 0.999955 to 0.985784.
The coupling efficiency decreases because the fiber's mode which we have defined as having a flat phase profile doesn't match that of the input (to the fiber) beam.
Grabbing POP window from Zemax
I used matplotlib to create the above plots using the raw data extracted from Zemax. However, it is entirely possible to grab the Zemax plots as shown with the following example.
Can you write Python code for it?

pyz.findZButtonCode('physical optics') # What is the button code?

sfile = ln.zSetPOPSettings(startSurf=1, endSurf=2, field=1, wave=1,
                           beamType=0, paramN=((1, 2), (2, 2)), tPow=1,
                           sampx=4, sampy=4, widex=40, widey=40)

sfile

ln.ipzCaptureWindow('Pop', percent=15, gamma=0.9)

ln.close()


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
The lens is truely diffraction limited, as shown by the Seidel aberration coefficients.
Can you write Python code for it?


I am working on the file "Examples/IPNotebooks/02 Simple fiber coupling analysis using Zemax's POP.ipynb".
The first task is:
We will now set up the POP analysis and see the irradiance and phase distribution of the input beam at surfaces 1 and 4, using the function zSetPOPSettings(). Please refer to the function's docstring for details on the function.
The input beam is defined as follows:
Beam type : Gaussian waist
Mode  : TEM00
Waist : 2 mm
Total power : 1 Watt
The beam waist of the input beam is located at the STO surface (surface to beam = 0).
The receiver fiber settings are as follows:
Beam type : Gaussian waist
Mode : TEM00
Waist : 8 microns (0.008 mm)
The modal radius that the receiving fiber supports is an 8 microns beam waist.
Can you write Python code for it?

sfile = ln.zSetPOPSettings(data=0, startSurf=1, endSurf=1, field=1, wave=1,
                           beamType=0, paramN=((1, 2), (2, 2)), tPow=1,
                           sampx=4, sampy=4, widex=40, widey=40)


I am working on the file "Examples/IPNotebooks/03 Generation of Speckle using Zemax Grid Sag Surface.ipynb".
The first task is:
After fitting a Gaussian distribution the $\text{FWHM}$ and $1/e^2$ widths may be estimated as follows:
If $F(x)$ is the Gaussian curve, and $F(x) \Big|{x=x^+} = \frac{1}{2}$  and $F(x) \Big|{x=x^-} = \frac{1}{2}$ on either side of the mean, then the $\text{FWHM}$ width is given by $x^+ - x^-$. Similarly, the $1/e^2$ width may be estimated by taking the difference between the abscissae where $F(x)=1/e^2=0.135335$
$$
\begin{array}{cl}
F_x & = & a e^{- \frac{(x - \mu)^2}{2\sigma^2} } \\
ln(F_x) & = & ln(a) - \frac{(x - \mu)^2}{2\sigma^2} \\
\frac{(x - \mu)^2}{2\sigma^2} & = & ln \left( \frac{a}{F_x} \right)\\
x & = & \mu + \sigma \sqrt{ \left[ 2 \, ln \left( \frac{a}{F_x} \right) \right]}
\end{array}
$$
If we represent
$$
x^{\pm} = \mu \pm \sigma \sqrt{ \left[ 2 \, ln \left( \frac{a}{F_x} \right) \right]}
$$
then,
$$
\Delta x = 2 \, \sigma \sqrt{ \left[ 2 \, ln \left( \frac{a}{F_x} \right) \right]}
$$
Can you write Python code for it?

# Helper functions for estimating the speckle size
# Most of the ideas for determining the speckle size is from
# "Speckle Size via Autocorrelation", by Joel
# mathworks.com/matlabcentral/fileexchange/
# 25046-speckle-size-via-autocorrelation/content//SpeckleSize.m

def xcov(x, y=None, scale='none'):
    """returns the cross-covariance of two discrete-time
    sequences, `x` and `y`.

    Parameters
    ----------
    x : ndarray
        1-dim ndarray
    y : ndarray, optional
        1-dim ndarray. If y is 'None', the autocovariance of
        the sequence `x`is returned
    scale : string, optional
        specifies a normalization option for the cross-
        covariance

    Returns
    -------
    crosscov : ndarray
        1-dim ndarray of the cross-covariance sequence. Length
        of `crosscov` is `2*m - 1`, where `m` is the length of
        `x` (and `y` is passed)

    Notes
    -----
    `xcov` emulates Matlab's `xcov()` function in a
    limited way. For details see _[1]

    References
    ----------
    .. [1] http://www.mathworks.com/help/signal/ref/xcov.html
    """
    m = len(x)
    y = x if y is None else y
    assert m == len(y), \
    'Sequences x and y must be of same length.'
    raw = np.correlate(x - np.mean(x),
                        y - np.mean(y), 'full')
    if scale == 'coeff':
        crosscov = raw/np.max(raw)
    elif scale == 'biased':
        crosscov = raw/m
    elif scale == 'unbiased':
        maxlag = m - 1
        k = np.arange(-maxlag, maxlag + 1)
        crosscov = raw/(m - np.abs(k))
    else:
        crosscov = raw
    return crosscov

def avg_autocov(x, axis=0, scale='coeff'):
    """returns the "average" autocovariance of x along
    the `axis` specified

    Parameters
    ----------
    x : ndarray
        2-dim ndarray
    axis : integer
        0 = average auto-covariance along the first dimension;
        1 = average auto-covariance along the second dimension
    scale : string, optional
        specifies a normalization option...


I am working on the file "Examples/IPNotebooks/03 Generation of Speckle using Zemax Grid Sag Surface.ipynb".
The first task is:
First order speckle statistics
Distribution of speckle intensity
The speckle intensity of the ideal Gaussian speckle pattern follows a negative exponential probability-density function [1]. The (negative) exponential probability density function is given as:
$$
P(x | \lambda) = \left { \begin{array}{cc}
\lambda e^{-\lambda x} & \hspace{4pt} & x \geq 0, \
0 & \hspace{4pt} & x < 0,
\end{array}\right .\
$$
Can you write Python code for it?

# histogram of speckle
numBins = 100
hist, binEdges = np.histogram(speckle.flatten(), bins=numBins, density=True)
binCenters = (binEdges[:-1] + binEdges[1:])/2
fig, ax = plt.subplots(1, 1)
ax.plot(binCenters, hist, label='speckle data')
# fit an exponential curve. Since the data is noise-free, we will use
# the method provided by Scipy
loc, scale = sps.expon.fit(speckle.flatten(), floc=0)
y = sps.expon.pdf(binCenters, loc=loc, scale=scale)
ax.plot(binCenters, y, linestyle='dashed', label='fitted expo curve')
ax.set_xlim(0, np.max(binCenters))
ax.legend(fontsize=12)
ax.set_xlabel('(scaled) intensity')
print(u'Rate parameter, \u03BB = {:.3f}'.format(1.0/scale))
plt.show()


I am working on the file "Examples/IPNotebooks/03 Generation of Speckle using Zemax Grid Sag Surface.ipynb".
The first task is:
NOTE: If the Zemax Error Message "The reference rays cannot be traced or are too close together", please check the maximum aboslute height of the grid sag surface. It is probably much larger (more than an order of magnitude) than the wavelength of the source.
Can you write Python code for it?

mmPerPxY, mmPerPxX = popinfo.widthY/popinfo.gridY, popinfo.widthX/popinfo.gridX
speckle = zoom(np.array(irrdata), amount=5.5)
pxY, pxX = irradiance.shape
h, w = mmPerPxY*pxY, mmPerPxX*pxX
plot_pop_display_data([speckle,], [h,], [w,], ['speckle pattern',])


I am working on the file "Examples/IPNotebooks/03 Generation of Speckle using Zemax Grid Sag Surface.ipynb".
The first task is:
Rayleigh range and Fraunhofer distances
Can you write Python code for it?

def rayleigh_fraunhofer(beamRadius, wavelength):
    """print the rayleigh range and Fraunhofer distance in mm
    """
    beamDia = 2.0*beamRadius
    rr = bou.GaussianBeam(waistDiameter=beamDia, wavelength=wavelength).rayleigh
    fd = fou.fraunhofer_distance(d=beamDia,  wavelen=wavelength)
    print('Rayleigh range = {:2.2f} mm'.format(rr))
    print('Fraunhofer distance (far-field) = {:2.2f} mm'.format(fd))

rayleigh_fraunhofer(beamRadius, wavelength)

# view the set parameters for POP analysis. Note that this analysis takes little more than a min
# because of large number of samples
popinfo, irrdata = ln.zGetPOP(settingsFile=cfgfile, displayData=True, timeout=3*60)
popinfo

mmPerPxY, mmPerPxX = popinfo.widthY/popinfo.gridY, popinfo.widthX/popinfo.gridX
irradiance = zoom(np.array(irrdata), amount=5.5)
pxY, pxX = irradiance.shape
h, w = mmPerPxY*pxY, mmPerPxX*pxX
plot_pop_display_data([irradiance,], [h,], [w,], ['Initial irradiance',])


I am working on the file "Examples/IPNotebooks/03 Generation of Speckle using Zemax Grid Sag Surface.ipynb".
The first task is:
Second order speckle statistics
The width of the autocorrelation of the intensity of the speckle distribution gives a reasonable measure of the "average" width of a speckle in the pattern [1].
Can you write Python code for it?

# Expected speckle size

def set_small_values_to_zero(tol, *values):
    """helper function to set infinitesimally small values to zero

    Parameters
    ----------
    tol : float
        threshold. All numerical values below abs(tol) is set to zero
    *values : unflattened sequence of values

    Returns
    -------

    """
    return [0.0 if abs(value) < tol else value for value in values]

#TODO!! Move this function to PyZDDE!! (could rename to specify it's a POP analysis helper function)
def get_beam_centroid_radius(ln, surf, update=True):
    """returns the beam width and position at surface ``surf`` using
    POP analysis

    Parameters
    ----------
    surf : integer
        surface number. 0 implies last surface
    udpate : bool
        if `True`, then Zemx will recompute all pupil positions and solves, etc
        and the data in the LDE will be updated before retrieving the POPD
        values.

    Returns
    -------
    para : namedtuple
        beam parameters (cx, cy, rx, ry) where cx, cy are the coordinates
        of the centroid of the beam w.r.t. the chief ray
    """
    CENTROID_X, CENTROID_Y, BEAM_RADIUS_X, BEAM_RADIUS_Y = 21, 22, 23, 24
    wave, field, xtr1, xtr2 = 0, 0, 0, 0
    if update:
        ln.zGetUpdate()
    cx = ln.zOperandValue('POPD', surf, wave, field, CENTROID_X, xtr1, xtr2)
    cy = ln.zOperandValue('POPD', surf, wave, field, CENTROID_Y, xtr1, xtr2)
    rx = ln.zOperandValue('POPD', surf, wave, field, BEAM_RADIUS_X, xtr1, xtr2)
    ry = ln.zOperandValue('POPD', surf, wave, field, BEAM_RADIUS_Y, xtr1, xtr2)
    cx, cy, rx, ry = set_small_values_to_zero(1e-12, cx, cy, rx, ry)
    beam = co.namedtuple('beam', ['cx', 'cy', 'rx', 'ry'])
    return beam(cx, cy, rx, ry)

beamDiameterAtDiff = 2.0*get_beam_centroid_radius(ln, SURF_DIFFSMOOTHFACE).rx
THICKNESS = 3
diffScDist = ln.zGetSurfaceData(surfNum=SURF_IMA - 1, code=THICKNESS)  # note this is not general
theorySpeckleWidth = wavelength*diffScDist/beamDiameterAtDiff
print('Beam diameter @...


I am working on the file "Examples/IPNotebooks/03 Generation of Speckle using Zemax Grid Sag Surface.ipynb".
The first task is:
Set up POP analysis
Can you write Python code for it?

# Function to set the POP analysis parameters
def set_POP(ln, data='irr', wide=50.0, waist=1.0, start=1, end=None):
    """helper function to set POP

    Parameters
    ----------
    ln : object
    data : string
        the display data type. 'irr' or 'phase'
    wide : float
        initial width and height of the region to display. See Note 2.
    waist : float
        beam radius at the waist (in mm)
    start : integer
        start surface
    end : integer
        end surface

    Return
    ------
    settinsfilename : string
        CFG settings file name

    Note
    ----
    1. Use the same name for the CFG settings file. This helps POPD to return the correct
       values of parameters (mostly)
    2. The ``auto`` parameter in the function ``zSetPOPSettings()`` does not seem to work
       as expected. Hence, we need to provide the ``widex`` and ``widey`` parameters
       explicitly. In order to get the appropriate values for these parameters, use the
       "Automatic" button in Zemax POP analysis window for the particular design file
    """
    setfile = ln.zGetFile().lower().replace('.zmx', '.CFG')
    datatype = 1 if data == 'phase' else 0
    GAUSS_WAIST, WAIST_X, WAIST_Y = 0, 1, 2
    S_1024, S_2048 = 6, 7

    cfgfile = ln.zSetPOPSettings(data=datatype, settingsFile=setfile,
                                 startSurf=start, endSurf=end, field=1, wave=1,
                                 beamType=GAUSS_WAIST,
                                 paramN=((WAIST_X, WAIST_Y), (waist, waist)),
                                 sampx=S_2048, sampy=S_2048, widex=wide, widey=wide)
    return cfgfile

# Helper functions to display POP display data
def plot_pop_display_data(popdata, height, width, title):
    """plot pop display data retrieved from Zemax application
    using `zGetPOP()` function

    Parameters
    ----------
    popdata : list
        list of speckle patterns or pop display data arrays
    height : list
        list of height of the speckle patterns
   ...


I am working on the file "Examples/Initial_analysis_example.ipynb".
The first task is:
Clean-up features
Can you write Python code for it?

# Drop features that are not meaningful for the analysis
drop_elements = ['ID', 'Unnamed: 0']
example_df = example_df.drop(drop_elements, axis=1)

# Group features by data types
categorical_cols = ['Marital_2', 'Education_2']
numerical_cols = [col for col in example_df.columns if col not in categorical_cols]

# Basic NA cleaning
example_df.fillna({x: 'unknwn' for x in categorical_cols}, inplace=True)
example_df.fillna({x: -1 for x in numerical_cols}, inplace=True)

# Correct dtypes
for col in categorical_cols:
    example_df[col] = example_df[col].astype(np.object)
for col in numerical_cols:
    example_df[col] = example_df[col].astype(np.float)

print('Corrected Data Types:\n{}'.format(example_df.dtypes))

# Review target
target = example_df['Y_2']
print('\nTarget Summary:')
stats.analyse_target(target=target)

# Review key stats for features
print('\nFeature Summary:')
print(example_df.describe(include='all'))

# Review NA values
data_na = stats.count_na(example_df)
print('\nMissing values:')
print(data_na)

# Drop features with > 90% missing
high_na = data_na.loc[data_na['Percent'] > 0.9].index.tolist()
print('\nFeatures with > 90% missing values to be dropped: {}'.format(len(high_na)))
example_df = example_df.drop(high_na, axis=1)

# Group infrequent values in categorical features to 'other' for visualisation
example_df = stats.clean_cats(example_df)


I am working on the file "Examples/Initial_analysis_example.ipynb".
The first task is:
Compare Numerical features to Target
The point bi-serial correlation coeficient compares numerical features to a dichotomous target. As before, the values vary from -1 to 1, and the closer the value is to 1 or -1, the stronger the linear relationship
Can you write Python code for it?

# Remaining Numerical Features vs Categorical Target

pbs_corr = stats.point_biserial_corr(df=example_df, target_col_str='Y_2')
title = 'Numerical feature correlation with target (top 20)'
score_label = 'correlation coefficient (r)'

stats.plot_score(df=pbs_corr, score_col='corr', n=20, title=title, log_scale=False, score_label=score_label)


I am working on the file "Examples/Initial_analysis_example.ipynb".
The first task is:
If you have a high number of features (100's or 1000's), a quick way to reduce dimensionality is to remove numerical features that are highly correlated with other numerical features. Below is a quick solution, but you could also use the strength of the relationship with the target to decide which features to keep.
Can you write Python code for it?

# Quick solution to reduce correlated features - drop the 2nd feature

high_corr_f2 = high_corr_df['feature 2'].unique().tolist()
example_df = example_df.drop(high_corr_f2, axis=1)

# Plot correlations between remaining features

f, ax = plt.subplots(figsize=(10, 10))
correlations = stats.correlation_plot(example_df, ax=ax)
plt.show()


I am working on the file "Examples/Initial_analysis_example.ipynb".
The first task is:
Review relationships in data
Compare Numerical Features using Correlation
Correlation measures the linear (straight line) relationship between two numerical features. The correlation coefficienct measures the strength and direction of that linear relationship, with values ranging from -1 to 1. A negative coefficient indicates a negative relationship (as one goes up, the other goes down), whereas a positive coefficient indicates a positive relationship (as one goes up, so does the other). The closer the correlation co-efficient is to 1 or -1, the stronger the relationship.
Can you write Python code for it?

# Numerical vs Numerical Features - Correlation

high_corr_df = stats.correlated_features(df=example_df, min_corr=0.85, target_col_str='Y_2')
high_corr_unique_f = list(set(high_corr_df['feature 1'].unique().tolist()
                              + high_corr_df['feature 2'].unique().tolist()))

print('Number of highly correlated features: {}'.format(len(high_corr_unique_f)))
print(high_corr_unique_f)


I am working on the file "Examples/Initial_analysis_example.ipynb".
The first task is:
Visual Analysis
The below plots the distribution of each categorical feature, as well as the distribution by the target. As before, visually analysing the features can help you better understand the data and identify data issues.
Can you write Python code for it?

# Visual analysis of Categorical Features

top_categorical_f = passed_chi_f + invalid_chi_f + ['Y_2']

categorical_df_p = example_df[top_categorical_f]
stats.plot_features(df=categorical_df_p, target_col_str='Y_2')
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()


I am working on the file "Examples/Initial_analysis_example.ipynb".
The first task is:
Visual Analysis
The below plots the distribution of each numerical feature, as well as the distribution by the target. Visually analysing the features can help you better understand the data and identify data issues such as outliers.
Can you write Python code for it?

# Visual analysis of Top Numerical Features vs Target

top_numerical_f = pbs_corr.sort_values(by='corr_abs', ascending=False)[0:20].index.tolist()
top_numerical_f.append('Y_2')
numerical_df_p = example_df[top_numerical_f]
stats.plot_features(df=numerical_df_p, target_col_str='Y_2')
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()


I am working on the file "Examples/LogisticRegression/notebook/LogisticRegressionApp.ipynb".
The first task is:
3. Logistic Regression Application
This example shows how our accelerated Logistic Regression library is called to train a LR model on the train set and then test its accuracy. If accel is set (accel = 1), the hardware accelerator is used for the computation of the gradients in each iteration.
Read data & parameters
The size of the train set, as well as the number of the iterations are intentionally picked small, to avoid large execution time in SW-only cases.
Can you write Python code for it?

chunkSize = 4000
alpha = 0.25
iterations = 5

train_file = "data/MNIST_train.dat"
test_file = "data/MNIST_test.dat"

sc.appName = "Python Logistic Regression"

print("* LogisticRegression Application *")
print(" # train file:               " + train_file)
print(" # test file:                " + test_file)


I am working on the file "Examples/LogisticRegression/notebook/LogisticRegressionApp.ipynb".
The first task is:
HW accelerated vs SW-only
Can you write Python code for it?

accel = int(input("Select mode (0: SW-only, 1: HW accelerated) : "))


I am working on the file "Examples/LogisticRegression/notebook/LogisticRegressionApp.ipynb".
The first task is:
Test the LR model
Can you write Python code for it?

testRDD = sc.textFile(test_file)

LR.test(testRDD)


I am working on the file "Examples/MD02-2515.McClymont.2012.Spectral.ipynb".
The first task is:

**NOTE**

The cell below will automatically "paste" the reservoir age correction table from this website. Enter the latitude/longitude from above and check the boxes 'ReservoirAge' and 'ReservoirErr'.

 Select the entire table, **including** the headers.

Make sure that the table is the last item copied to the clipboard. You will get a chance to check it before continuing.

A copy of the table is automatically saved as a text file as part of the routine.

 This **only** works with Safari or Google Chrome.

Can you write Python code for it?

import pandas as pd
import numpy as np
import math

data = pd.read_clipboard()

# check your work

print("Don't forget to check your work!")
data

# Calculate the average DR and standard error of the mean

corr = data.mean()['DeltaR']
corr_err = data.std()['DeltaR']
datapoints = data.shape[0]
max_dist = data.max()['distance (km)']

# Save to a text file

# Save the data to a textfile and generate a log
data.to_csv(os.path.join(dir,"Reservoir_Age.txt"),sep=' ', index=False,
           header = True)
text_file = open(os.path.join(dir,"ReservoirAgeCorr_Info.txt"),"w")
text_file.write("The reservoir age correction was estimated from the \
chrono database.\n")
text_file.write("The reservoir age corrections was estimated on " +
                str(datapoints) + " datapoints.\n")
text_file.write("DR = " + str(math.floor(corr)) + " years.\n")
text_file.write("DR sigma = " + str(math.floor(corr_err)) + " years, expressed as the standard \
error of the mean.\n")
text_file.write("The maximum distance between the core location and \
measured reservoir ages is " + str(max_dist) + "km.\n")
text_file.close()

# Perform the correction

corr_14C = np.floor((radiocarbon - corr).values)
err_14C = np.floor(np.sqrt(radiocarbonU**2 + corr_err**2).values)