markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
3. Initialize and run the merger | # init & run polygon merger
pm = Polygon_merger_v2(contours_df, verbose=1)
pm.unique_groups.remove("roi")
pm.run() | docs/examples/polygon_merger_using_rtree.ipynb | DigitalSlideArchive/HistomicsTK | apache-2.0 |
NOTE:
The following steps are only "aesthetic", and just ensure the contours look nice when posted to Digital Slide Archive for viewing with GeoJS. | # add colors (aesthetic)
for group in pm.unique_groups:
cs = contours_df.loc[contours_df.loc[:, "group"] == group, "color"]
pm.new_contours.loc[
pm.new_contours.loc[:, "group"] == group, "color"] = cs.iloc[0]
# get rid of nonenclosed stroma (aesthetic)
pm.new_contours = _discard_nonenclosed_background_... | docs/examples/polygon_merger_using_rtree.ipynb | DigitalSlideArchive/HistomicsTK | apache-2.0 |
This is the result | pm.new_contours.head() | docs/examples/polygon_merger_using_rtree.ipynb | DigitalSlideArchive/HistomicsTK | apache-2.0 |
4. Visualize results on HistomicsTK | # deleting existing annotations in target slide (if any)
existing_annotations = gc.get('/annotation/item/' + POST_SLIDE_ID)
for ann in existing_annotations:
gc.delete('/annotation/%s' % ann['_id'])
# get list of annotation documents
annotation_docs = get_annotation_documents_from_contours(
pm.new_contours.copy... | docs/examples/polygon_merger_using_rtree.ipynb | DigitalSlideArchive/HistomicsTK | apache-2.0 |
Clusterized ranking | M = np.array([
[5, 3, 1, 2, 8, 4, 6, 7],
[5, 4, 3, 1, 8, 2, 6, 7],
[1, 7, 5, 4, 8, 2, 3, 6],
[6, 4, 2.5, 2.5, 8, 1, 7, 5],
[8, 2, 4, 6, 3, 5, 1, 7],
[5, 6, 4, 3, 2, 1, 7, 8],
[6, 1, 2, 3, 5, 4, 8, 7],
[5, 1, 3, 2, 7, 4, 6, 8],
[6, 1, 3, 2, 5, 4, 7, 8],
[5, 3, 2, 1, 8, 4, 6, 7],
... | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Here is how we find average ranking. | average_rank = rankdata(np.average(M, axis=0))
average_rank | decision_theory/lab2.ipynb | lionell/laboratories | mit |
And this way we can get median ranking. | median_rank = rankdata(np.median(M, axis=0))
median_rank | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Next we need to compute kernel of disagreement. | adj = np.zeros((m, m), dtype=np.bool)
kernel = []
for i in range(m):
for j in range(i + 1, m):
if (average_rank[i] - average_rank[j])*(median_rank[i] - median_rank[j]) < 0:
kernel.append([i, j])
adj[i][j] = adj[j][i] = True
kernel | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Now that we have a graph of the disagreement, we can easily find a full component via Depth First Search. | def dfs(i, used):
if i in used:
return []
used.add(i)
res = [i]
for j in range(m):
if adj[i][j]:
res += dfs(j, used)
return res | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Last thing to do, is to iterate in the correct order, and don't forget to print a whole cluster when needed. | order = sorted(range(m), key=lambda i: (average_rank[i], median_rank[i]))
order
result = []
used = set()
for i in order:
cluster = dfs(i, used)
if len(cluster) > 0:
result.append(cluster)
result | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Kemeny distance | rankings = np.array([
[[1], [2, 3], [4], [5], [6, 7]],
[[1, 3], [4], [2], [5], [7], [6]],
[[1], [4], [2], [3], [6], [5], [7]],
[[1], [2, 4], [3], [5], [7], [6]],
[[2], [3], [4], [5], [1], [6], [7]],
[[1], [3], [2], [5], [6], [7], [4]],
[[1], [5], [3], [4], [2], [6], [7]]
])
n = rankings.shap... | decision_theory/lab2.ipynb | lionell/laboratories | mit |
We need to be able to build relation matrix out of the ranking. | def build(x):
n = sum(map(lambda r: len(r), x)) # Total amount of objects
m = np.zeros((n, n), dtype=np.bool)
for r in x:
for i in r:
for j in range(n):
if not m[j][i - 1] or j + 1 in r:
m[i - 1][j] = True
return m | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Now we can calculate Kemedy distances between each two rankings. | dist = np.zeros((n, n))
for i in range(n):
for j in range(n):
dist[i][j] = np.sum(build(rankings[i]) ^ build(rankings[j]))
dist | decision_theory/lab2.ipynb | lionell/laboratories | mit |
Let's find Kemeny median for the ranks. | median = np.argmin(np.sum(dist, axis=1))
rankings[median] | decision_theory/lab2.ipynb | lionell/laboratories | mit |
"Classic" use with cell magic | %%ferret -s 600,400
set text/font=arial
use monthly_navy_winds.cdf
show data/full
plot uwnd[i=@ave,j=@ave,l=@sbx:12] | notebooks/ferretmagic_06_InteractWidget.ipynb | PBrockmann/ipython_ferretmagic | mit |
Explore interactive widgets | from ipywidgets import interact
@interact(var=['uwnd','vwnd'], smooth=(1, 20), vrange=(0.5,5,0.5))
def plot(var='uwnd', smooth=5, vrange=1) :
%ferret_run -s 600,400 'ppl color 6, 70, 70, 70; plot/grat=(dash,color=6)/vlim=-%(vrange)s:%(vrange)s %(var)s[i=@ave,j=@ave], %(var)s[i=@ave,j=@ave,l=@sbx:%(smooth)s]' % loc... | notebooks/ferretmagic_06_InteractWidget.ipynb | PBrockmann/ipython_ferretmagic | mit |
Another example with a map | # The line of code to make interactive
%ferret_run -q -s 600,400 'cancel mode logo; \
ppl color 6, 70, 70, 70; \
shade/grat=(dash,color=6) %(var)s[l=%(lstep)s] ; \
go land' % {'var':'uwnd','lstep':'3'}
import ipywidgets as widgets
fro... | notebooks/ferretmagic_06_InteractWidget.ipynb | PBrockmann/ipython_ferretmagic | mit |
A variety of tools employing different methodologies have been developed over the years to compute multi-group cross sections for certain applications, including NJOY (LANL), MC$^2$-3 (ANL), and Serpent (VTT). The openmc.mgxs Python module is designed to leverage OpenMC's tally system to calculate multi-group cross sec... | import numpy as np
import matplotlib.pyplot as plt
import openmc
import openmc.mgxs as mgxs
%matplotlib inline | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
First we need to define materials that will be used in the problem. Before defining a material, we must create nuclides that are used in the material. | # Instantiate some Nuclides
h1 = openmc.Nuclide('H-1')
o16 = openmc.Nuclide('O-16')
u235 = openmc.Nuclide('U-235')
u238 = openmc.Nuclide('U-238')
zr90 = openmc.Nuclide('Zr-90') | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
With the nuclides we defined, we will now create a material for the homogeneous medium. | # Instantiate a Material and register the Nuclides
inf_medium = openmc.Material(name='moderator')
inf_medium.set_density('g/cc', 5.)
inf_medium.add_nuclide(h1, 0.028999667)
inf_medium.add_nuclide(o16, 0.01450188)
inf_medium.add_nuclide(u235, 0.000114142)
inf_medium.add_nuclide(u238, 0.006886019)
inf_medium.add_nuclide... | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
With our material, we can now create a MaterialsFile object that can be exported to an actual XML file. | # Instantiate a MaterialsFile, register all Materials, and export to XML
materials_file = openmc.MaterialsFile()
materials_file.default_xs = '71c'
materials_file.add_material(inf_medium)
materials_file.export_to_xml() | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Now let's move on to the geometry. This problem will be a simple square cell with reflective boundary conditions to simulate an infinite homogeneous medium. The first step is to create the outer bounding surfaces of the problem. | # Instantiate boundary Planes
min_x = openmc.XPlane(boundary_type='reflective', x0=-0.63)
max_x = openmc.XPlane(boundary_type='reflective', x0=0.63)
min_y = openmc.YPlane(boundary_type='reflective', y0=-0.63)
max_y = openmc.YPlane(boundary_type='reflective', y0=0.63) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
With the surfaces defined, we can now create a cell that is defined by intersections of half-spaces created by the surfaces. | # Instantiate a Cell
cell = openmc.Cell(cell_id=1, name='cell')
# Register bounding Surfaces with the Cell
cell.region = +min_x & -max_x & +min_y & -max_y
# Fill the Cell with the Material
cell.fill = inf_medium | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
OpenMC requires that there is a "root" universe. Let us create a root universe and add our square cell to it. | # Instantiate Universe
root_universe = openmc.Universe(universe_id=0, name='root universe')
root_universe.add_cell(cell) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
We now must create a geometry that is assigned a root universe, put the geometry into a GeometryFile object, and export it to XML. | # Create Geometry and set root Universe
openmc_geometry = openmc.Geometry()
openmc_geometry.root_universe = root_universe
# Instantiate a GeometryFile
geometry_file = openmc.GeometryFile()
geometry_file.geometry = openmc_geometry
# Export to "geometry.xml"
geometry_file.export_to_xml() | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Next, we must define simulation parameters. In this case, we will use 10 inactive batches and 40 active batches each with 2500 particles. | # OpenMC simulation parameters
batches = 50
inactive = 10
particles = 2500
# Instantiate a SettingsFile
settings_file = openmc.SettingsFile()
settings_file.batches = batches
settings_file.inactive = inactive
settings_file.particles = particles
settings_file.output = {'tallies': True, 'summary': True}
bounds = [-0.63, ... | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Now we are ready to generate multi-group cross sections! First, let's define a 2-group structure using the built-in EnergyGroups class. | # Instantiate a 2-group EnergyGroups object
groups = mgxs.EnergyGroups()
groups.group_edges = np.array([0., 0.625e-6, 20.]) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
We can now use the EnergyGroups object, along with our previously created materials and geometry, to instantiate some MGXS objects from the openmc.mgxs module. In particular, the following are subclasses of the generic and abstract MGXS class:
TotalXS
TransportXS
AbsorptionXS
CaptureXS
FissionXS
NuFissionXS
ScatterXS
... | # Instantiate a few different sections
total = mgxs.TotalXS(domain=cell, domain_type='cell', groups=groups)
absorption = mgxs.AbsorptionXS(domain=cell, domain_type='cell', groups=groups)
scattering = mgxs.ScatterXS(domain=cell, domain_type='cell', groups=groups) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Each multi-group cross section object stores its tallies in a Python dictionary called tallies. We can inspect the tallies in the dictionary for our Absorption object as follows. | absorption.tallies | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
The Absorption object includes tracklength tallies for the 'absorption' and 'flux' scores in the 2-group structure in cell 1. Now that each MGXS object contains the tallies that it needs, we must add these tallies to a TalliesFile object to generate the "tallies.xml" input file for OpenMC. | # Instantiate an empty TalliesFile
tallies_file = openmc.TalliesFile()
# Add total tallies to the tallies file
for tally in total.tallies.values():
tallies_file.add_tally(tally)
# Add absorption tallies to the tallies file
for tally in absorption.tallies.values():
tallies_file.add_tally(tally)
# Add scatteri... | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Now we a have a complete set of inputs, so we can go ahead and run our simulation. | # Run OpenMC
executor = openmc.Executor()
executor.run_simulation() | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
In addition to the statepoint file, our simulation also created a summary file which encapsulates information about the materials and geometry. This is necessary for the openmc.mgxs module to properly process the tally data. We first create a Summary object and link it with the statepoint. | # Load the summary file and link it with the statepoint
su = openmc.Summary('summary.h5')
sp.link_with_summary(su) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
The statepoint is now ready to be analyzed by our multi-group cross sections. We simply have to load the tallies from the StatePoint into each object as follows and our MGXS objects will compute the cross sections for us under-the-hood. | # Load the tallies from the statepoint into each MGXS object
total.load_from_statepoint(sp)
absorption.load_from_statepoint(sp)
scattering.load_from_statepoint(sp) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Voila! Our multi-group cross sections are now ready to rock 'n roll!
Extracting and Storing MGXS Data
Let's first inspect our total cross section by printing it to the screen. | total.print_xs() | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Since the openmc.mgxs module uses tally arithmetic under-the-hood, the cross section is stored as a "derived" Tally object. This means that it can be queried and manipulated using all of the same methods supported for the Tally class in the OpenMC Python API. For example, we can construct a Pandas DataFrame of the mult... | df = scattering.get_pandas_dataframe()
df.head(10) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Each multi-group cross section object can be easily exported to a variety of file formats, including CSV, Excel, and LaTeX for storage or data processing. | absorption.export_xs_data(filename='absorption-xs', format='excel') | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
The following code snippet shows how to export all three MGXS to the same HDF5 binary data store. | total.build_hdf5_store(filename='mgxs', append=True)
absorption.build_hdf5_store(filename='mgxs', append=True)
scattering.build_hdf5_store(filename='mgxs', append=True) | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Comparing MGXS with Tally Arithmetic
Finally, we illustrate how one can leverage OpenMC's tally arithmetic data processing feature with MGXS objects. The openmc.mgxs module uses tally arithmetic to compute multi-group cross sections with automated uncertainty propagation. Each MGXS object includes an xs_tally attribute... | # Use tally arithmetic to compute the difference between the total, absorption and scattering
difference = total.xs_tally - absorption.xs_tally - scattering.xs_tally
# The difference is a derived tally which can generate Pandas DataFrames for inspection
difference.get_pandas_dataframe() | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Similarly, we can use tally arithmetic to compute the ratio of AbsorptionXS and ScatterXS to the TotalXS. | # Use tally arithmetic to compute the absorption-to-total MGXS ratio
absorption_to_total = absorption.xs_tally / total.xs_tally
# The absorption-to-total ratio is a derived tally which can generate Pandas DataFrames for inspection
absorption_to_total.get_pandas_dataframe()
# Use tally arithmetic to compute the scatte... | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
Lastly, we sum the derived scatter-to-total and absorption-to-total ratios to confirm that they sum to unity. | # Use tally arithmetic to ensure that the absorption- and scattering-to-total MGXS ratios sum to unity
sum_ratio = absorption_to_total + scattering_to_total
# The scattering-to-total ratio is a derived tally which can generate Pandas DataFrames for inspection
sum_ratio.get_pandas_dataframe() | docs/source/pythonapi/examples/mgxs-part-i.ipynb | mjlong/openmc | mit |
From raw data to dSPM on SPM Faces dataset
Runs a full pipeline using MNE-Python:
- artifact removal
- averaging Epochs
- forward model computation
- source reconstruction using dSPM on the contrast : "faces - scrambled"
<div class="alert alert-info"><h4>Note</h4><p>This example does quite a bit of processing, so even... | # Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr>
# Denis Engemann <denis.engemann@gmail.com>
#
# License: BSD-3-Clause
import matplotlib.pyplot as plt
import mne
from mne.datasets import spm_face
from mne.preprocessing import ICA, create_eog_epochs
from mne import io, combine_evoked
from mne.minim... | 0.24/_downloads/5ac2a3ff8baa6aba4bf6dd1d047703e2/spm_faces_dataset_sgskip.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Load and filter data, set up epochs | raw_fname = data_path + '/MEG/spm/SPM_CTF_MEG_example_faces%d_3D.ds'
raw = io.read_raw_ctf(raw_fname % 1, preload=True) # Take first run
# Here to save memory and time we'll downsample heavily -- this is not
# advised for real data as it can effectively jitter events!
raw.resample(120., npad='auto')
picks = mne.pick... | 0.24/_downloads/5ac2a3ff8baa6aba4bf6dd1d047703e2/spm_faces_dataset_sgskip.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Visualize fields on MEG helmet | # The transformation here was aligned using the dig-montage. It's included in
# the spm_faces dataset and is named SPM_dig_montage.fif.
trans_fname = data_path + ('/MEG/spm/SPM_CTF_MEG_example_faces1_3D_'
'raw-trans.fif')
maps = mne.make_field_map(evoked[0], trans_fname, subject='spm',
... | 0.24/_downloads/5ac2a3ff8baa6aba4bf6dd1d047703e2/spm_faces_dataset_sgskip.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Look at the whitened evoked daat | evoked[0].plot_white(noise_cov) | 0.24/_downloads/5ac2a3ff8baa6aba4bf6dd1d047703e2/spm_faces_dataset_sgskip.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Compute forward model | src = data_path + '/subjects/spm/bem/spm-oct-6-src.fif'
bem = data_path + '/subjects/spm/bem/spm-5120-5120-5120-bem-sol.fif'
forward = mne.make_forward_solution(contrast.info, trans_fname, src, bem) | 0.24/_downloads/5ac2a3ff8baa6aba4bf6dd1d047703e2/spm_faces_dataset_sgskip.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Compute inverse solution | snr = 3.0
lambda2 = 1.0 / snr ** 2
method = 'dSPM'
inverse_operator = make_inverse_operator(contrast.info, forward, noise_cov,
loose=0.2, depth=0.8)
# Compute inverse solution on contrast
stc = apply_inverse(contrast, inverse_operator, lambda2, method, pick_ori=None)
# stc.sav... | 0.24/_downloads/5ac2a3ff8baa6aba4bf6dd1d047703e2/spm_faces_dataset_sgskip.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Representamos ambos diámetro y la velocidad de la tractora en la misma gráfica | datos.ix[:, "Diametro X":"Diametro Y"].plot(figsize=(16,10),ylim=(0.5,3)).hlines([1.85,1.65],0,3500,colors='r')
#datos['RPM TRAC'].plot(secondary_y='RPM TRAC')
datos.ix[:, "Diametro X":"Diametro Y"].boxplot(return_type='axes') | medidas/11082015/Análisis de datos.ipynb | darkomen/TFG | cc0-1.0 |
En el boxplot, se ve como la mayoría de los datos están por encima de la media (primer cuartil). Se va a tratar de bajar ese porcentaje. La primera aproximación que vamos a realizar será la de hacer mayores incrementos al subir la velocidad en los tramos que el diámetro se encuentre entre $1.80mm$ y $1.75 mm$(caso 5) h... | plt.scatter(x=datos['Diametro X'], y=datos['Diametro Y'], marker='.') | medidas/11082015/Análisis de datos.ipynb | darkomen/TFG | cc0-1.0 |
How many assets in a Cisco Router?
As some of you may have heard, HPE IMC is a multi-vendor tool and offers support for many of the common devices you'll see in your daily travels.
In this example, we're going to use a Cisco 2811 router to showcase the basic function.
Routers, like chassis switches have multiple compo... | len(ciscorouter) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
What's in the box???
Now we know that we've got an idea of how many assets are in here, let's take a look to see exactly what's in one of the asset records to see if there's anything useful in here. | ciscorouter[0] | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
What can we do with this?
With some basic python string manipulation we could easily print out some of the attributes that we want into what could easily turn into a nicely formated report.
Again realise that the example below is just a subset of what's available in the JSON above. If you want more, just add it to the... | for i in ciscorouter:
print ("Device Name: " + i['deviceName'] + " Device Model: " + i['model'] +
"\nAsset Name is: " + i['name'] + " Asset Serial Number is: " +
i['serialNum']+ "\n") | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
Why not just write that to disk?
Although we could go directly to the formated report without a lot of extra work, we would be losing a lot of data which we may have use for later. Instead why don't we export all the available data from the JSON above into a CSV file which can be later opened in your favourite spreadsh... | keys = ciscorouter[0].keys()
with open('ciscorouter.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(ciscorouter) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
Reading it back
Now we'll read it back from disk to make sure it worked properly. When working with data like this, I find it useful to think about who's going to be consuming the data. For example, when looking at this remember this is a CSV file which can be easily opened in python, or something like Microsoft Excel... | with open('ciscorouter.csv') as file:
print (file.read()) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
What about all my serial numbers at once?
That's a great question! I'm glad you asked. One of the most beautiful things about learning to automate things like asset gathering through an API is that it's often not much more work to do something 1000 times than it is to do it a single time.
This time instead of using t... | all_assets = get_dev_asset_details_all(auth.creds, auth.url)
len (all_assets) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
That's a lot of assets!
Exactly why we automate things. Now let's write the all_assets list to disk as well.
**note for reasons unknown to me at this time, although the majority of the assets have 27 differnet fields, a few of them actually have 28 different attributes. Something I'll have to dig into later. | keys = all_assets[0].keys()
with open('all_assets.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_assets) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
Well That's not good....
So it looks like there are a few network assets that have a different number of attributes than the first one in the list. We'll write some quick code to figure out how big of a problem this is. | print ("The length of the first items keys is " + str(len(keys)))
for i in all_assets:
if len(i) != len(all_assets[0].keys()):
print ("The length of index " + str(all_assets.index(i)) + " is " + str(len(i.keys()))) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
Well that's not so bad
It looks like the items which don't have exactly 27 attribues have exactly 28 attributes. So we'll just pick one of the longer ones to use as the headers for our CSV file and then run the script again.
For this one, I'm going to ask you to trust me that the file is on disk and save us all the tro... | keys = all_assets[879].keys()
with open ('all_assets.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_assets) | examples/.ipynb_checkpoints/Working with Network Assets-checkpoint.ipynb | netmanchris/PYHPEIMC | apache-2.0 |
http://www.sciencedirect.com/science/article/pii/S0092867412014080
Table S1. trans-Splice Sites, Transcription Start Sites, and csRNA Loci for Protein-Coding Genes and Transcription Start Sites for pri-miRNAs, Related to Figure 2. Analysis of C. elegans CapSeq and CIP-TAP, containing lists of trans-splice sites, trans... | #!cd ~/relmapping/wget; wget -m --no-parent https://ars.els-cdn.com/content/image/1-s2.0-S0092867412014080-mmc1.xlsx
fp_ = 'wget/ars.els-cdn.com/content/image/1-s2.0-S0092867412014080-mmc1_B._TS_sites_for_protein_genes.csv'
df_ = pd.read_csv(fp_, skiprows=11)
df_['assigned_to_an_annotation'] = df_['transcript'].map(lam... | annot/notebooks/Fig2S3_import_Gu2012.ipynb | jurgjn/relmapping | gpl-2.0 |
Using a cutoff of one CapSeq read per 10 million total reads, and a requirement for a YR motif, our CapSeq data predicted approximately 64,000 candidate TS sites genome wide (Table S1B). | print(df_['transcript type'].value_counts())
m_ = df_['transcript type'] == "coding"
df_ = df_.loc[m_].reset_index(drop=True)
print('%d records with annotated as "coding"' % (len(df_.query('transcript == transcript')),))
# Raw (Gu et al., 2012) TSS sites (=many assigned to multiple transcripts)
df_gu = pd.DataFrame()
... | annot/notebooks/Fig2S3_import_Gu2012.ipynb | jurgjn/relmapping | gpl-2.0 |
Skew-T with Complex Layout
Combine a Skew-T and a hodograph using Matplotlib's GridSpec layout capability. | import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import pandas as pd
import metpy.calc as mpcalc
from metpy.cbook import get_test_data
from metpy.plots import add_metpy_logo, Hodograph, SkewT
from metpy.units import units | v0.12/_downloads/0c4dbfdebeb6fcd2f5364a69f0c6d4a8/Skew-T_Layout.ipynb | metpy/MetPy | bsd-3-clause |
Upper air data can be obtained using the siphon package, but for this example we will use
some of MetPy's sample data. | col_names = ['pressure', 'height', 'temperature', 'dewpoint', 'direction', 'speed']
df = pd.read_fwf(get_test_data('may4_sounding.txt', as_file_obj=False),
skiprows=5, usecols=[0, 1, 2, 3, 6, 7], names=col_names)
# Drop any rows with all NaN values for T, Td, winds
df = df.dropna(subset=('temperature... | v0.12/_downloads/0c4dbfdebeb6fcd2f5364a69f0c6d4a8/Skew-T_Layout.ipynb | metpy/MetPy | bsd-3-clause |
We will pull the data out of the example dataset into individual variables and
assign units. | p = df['pressure'].values * units.hPa
T = df['temperature'].values * units.degC
Td = df['dewpoint'].values * units.degC
wind_speed = df['speed'].values * units.knots
wind_dir = df['direction'].values * units.degrees
u, v = mpcalc.wind_components(wind_speed, wind_dir)
# Create a new figure. The dimensions here give a g... | v0.12/_downloads/0c4dbfdebeb6fcd2f5364a69f0c6d4a8/Skew-T_Layout.ipynb | metpy/MetPy | bsd-3-clause |
To create a Series we need to set the column (using usecols) to use and set the parameter squeeze to True. | data = pd.read_csv("data/input.csv", usecols=["name"], squeeze=True)
print type(data)
data.head()
data.index | 01_SERIES/CSV-Reader.ipynb | topix-hackademy/pandas-for-dummies | mit |
If the input file has only 1 column we don't need to provide the usecols argument. | data = pd.read_csv("data/input_with_one_column.csv", squeeze=True)
print type(data)
# HEAD
print data.head(2), "\n"
# TAIL
print data.tail() | 01_SERIES/CSV-Reader.ipynb | topix-hackademy/pandas-for-dummies | mit |
On Series we can perform classic python operation using Built-In Functions! | list(data)
dict(data)
max(data)
min(data)
dir(data)
type(data)
sorted(data)
data = pd.read_csv("data/input_with_two_column.csv", index_col="name", squeeze=True)
data.head()
data[["Alex", "asd"]]
data["Alex":"Vale"] | 01_SERIES/CSV-Reader.ipynb | topix-hackademy/pandas-for-dummies | mit |
Loading data | import numpy, pandas
from rep.utils import train_test_split
from sklearn.metrics import roc_auc_score
sig_data = pandas.read_csv('toy_datasets/toyMC_sig_mass.csv', sep='\t')
bck_data = pandas.read_csv('toy_datasets/toyMC_bck_mass.csv', sep='\t')
labels = numpy.array([1] * len(sig_data) + [0] * len(bck_data))
data = p... | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Training variables | variables = ["FlightDistance", "FlightDistanceError", "IP", "VertexChi2", "pt", "p0_pt", "p1_pt", "p2_pt", 'LifeTime', 'dira']
data = data[variables] | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Folding strategy - stacking algorithm
It implements the same interface as all classifiers, but with some difference:
all prediction methods have additional parameter "vote_function" (example folder.predict(X, vote_function=None)), which is used to combine all classifiers' predictions. By default "mean" is used as "vot... | from rep.estimators import SklearnClassifier
from sklearn.ensemble import GradientBoostingClassifier | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Define folding model | from rep.metaml import FoldingClassifier
n_folds = 4
folder = FoldingClassifier(GradientBoostingClassifier(), n_folds=n_folds, features=variables)
folder.fit(train_data, train_labels) | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Default prediction (predict i_th_ fold by i_th_ classifier) | folder.predict_proba(train_data) | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Voting prediction (predict i-fold by all classifiers and take value, which is calculated by vote_function) | # definition of mean function, which combines all predictions
def mean_vote(x):
return numpy.mean(x, axis=0)
folder.predict_proba(test_data, vote_function=mean_vote) | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Comparison of folds
Again use ClassificationReport class to compare different results. For folding classifier this report uses only default prediction.
Report training dataset | from rep.data.storage import LabeledDataStorage
from rep.report import ClassificationReport
# add folds_column to dataset to use mask
train_data["FOLDS"] = folder._get_folds_column(len(train_data))
lds = LabeledDataStorage(train_data, train_labels)
report = ClassificationReport({'folding': folder}, lds) | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Signal distribution for each fold
Use mask parameter to plot distribution for the specific fold | for fold_num in range(n_folds):
report.prediction_pdf(mask="FOLDS == %d" % fold_num, labels_dict={1: 'sig fold %d' % fold_num}).plot() | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Background distribution for each fold | for fold_num in range(n_folds):
report.prediction_pdf(mask="FOLDS == %d" % fold_num, labels_dict={0: 'bck fold %d' % fold_num}).plot() | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
ROCs (each fold used as test dataset) | for fold_num in range(n_folds):
report.roc(mask="FOLDS == %d" % fold_num).plot() | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Report for test dataset
NOTE: Here vote function is None, so default prediction is used | lds = LabeledDataStorage(test_data, test_labels)
report = ClassificationReport({'folding': folder}, lds)
report.prediction_pdf().plot(new_plot=True, figsize = (9, 4))
report.roc().plot(xlim=(0.5, 1)) | howto/04-howto-folding.ipynb | Quadrocube/rep | apache-2.0 |
Creating MNE objects from data arrays
In this simple example, the creation of MNE objects from
numpy arrays is demonstrated. In the last example case, a
NEO file format is used as a source for the data. | # Author: Jaakko Leppakangas <jaeilepp@student.jyu.fi>
#
# License: BSD (3-clause)
import numpy as np
import neo
import mne
print(__doc__) | 0.13/_downloads/plot_objects_from_arrays.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Create arbitrary data | sfreq = 1000 # Sampling frequency
times = np.arange(0, 10, 0.001) # Use 10000 samples (10s)
sin = np.sin(times * 10) # Multiplied by 10 for shorter cycles
cos = np.cos(times * 10)
sinX2 = sin * 2
cosX2 = cos * 2
# Numpy array of size 4 X 10000.
data = np.array([sin, cos, sinX2, cosX2])
# Definition of channel typ... | 0.13/_downloads/plot_objects_from_arrays.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Creation of info dictionary. | # It is also possible to use info from another raw object.
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(data, info)
# Scaling of the figure.
# For actual EEG/MEG data different scaling factors should be used.
scalings = {'mag': 2, 'grad': 2}
raw.plot(n_channels=4, s... | 0.13/_downloads/plot_objects_from_arrays.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
EpochsArray | event_id = 1
events = np.array([[200, 0, event_id],
[1200, 0, event_id],
[2000, 0, event_id]]) # List of three arbitrary events
# Here a data set of 700 ms epochs from 2 channels is
# created from sin and cos data.
# Any data in shape (n_epochs, n_channels, n_times) can be used.
... | 0.13/_downloads/plot_objects_from_arrays.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
EvokedArray | nave = len(epochs_data) # Number of averaged epochs
evoked_data = np.mean(epochs_data, axis=0)
evokeds = mne.EvokedArray(evoked_data, info=info, tmin=-0.2,
comment='Arbitrary', nave=nave)
evokeds.plot(picks=picks, show=True, units={'mag': '-'},
titles={'mag': 'sin and cos averag... | 0.13/_downloads/plot_objects_from_arrays.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Extracting data from NEO file | # The example here uses the ExampleIO object for creating fake data.
# For actual data and different file formats, consult the NEO documentation.
reader = neo.io.ExampleIO('fakedata.nof')
bl = reader.read(cascade=True, lazy=False)[0]
# Get data from first (and only) segment
seg = bl.segments[0]
title = seg.file_origin... | 0.13/_downloads/plot_objects_from_arrays.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Process Instantiation
Let's start with most basic example of spawning new process to run a function | from multiprocessing import Process
print('Starting demo...')
p = Process(target=printer, args=('hello demo',))
p.start() | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
Process timing
Use printer's delay to see process timing
Track multiple process objects
Execute code in main process while chile process is running
Use Process.join() to wait for processes to finish | proc_list = []
for values in [('immediate', 0), ('delayed', 2), ('eternity', 5)]:
p = Process(target=printer, args=values)
proc_list.append(p)
p.start() # start execution of printer
print('Not waiting for proccesses to finish...')
[p.join() for p in proc_list]
print('After processes...') | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
Process Pool
Worker processes instead of direct instantiation
Context manager to handle starting/joining child processes
Pool.map() works like default python map(f, args) function
Pool.map() Does not unpack args | from multiprocessing.pool import Pool
with Pool(3) as pool:
pool.map(printer, ['Its', ('A', 5), 'Race'])
# each worker process executes one function | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
Process + args/kwargs iteration with starmap | with Pool(2) as pool:
pool.starmap(printer, [('Its',), ('A', 2), ('Race',)])
# one worker will execute 2 functions, one worker will execute the 'slow' function | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
Starmap is the bomb | def pretend_delete_method(provider, vm_name):
print('Pretend delete: {} on {}. (Pid: {})'
.format(vm_name, provider, getpid()))
# Assuming we fetched a list of vm names on providers we want to cleanup...
example_provider_vm_lists = dict(
vmware=['test_vm_1', 'test_vm_2'],
rhv=['test_vm_3'... | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
Locking
semaphore-type object that can be acquired and released
When acquired, only thread that has the lock can run
Necessary when using shared objects | # Printing is thread safe, but will sometimes print separate messages on the same line (above)
# Use a lock around print
from multiprocessing import Lock
lock = Lock()
def safe_printing_method(provider, vm_name):
with lock:
print('Pretend delete: {} on {}. (Pid: {})'
.format(vm_name, provider... | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
Queues
Store data/objects in child thread/processes and retrieve in parent
FIFO stack with put, get, and empty methods
multiprocessing.Queue
cannot be pickled and thus can't be passed to Pool methods
can deadlock with improper join use
multiprocessing.Manager.Queue
is proxy, can be pickled
can be shared between pr... | from multiprocessing import Manager
from random import randint
# Create instance of manager
manager = Manager()
def multiple_output_method(provider, vm_name, fail_queue):
# random success of called method
if randint(0, 1):
return True
else:
# Store our failure vm on the queue
fail_... | notebooks/MultiProcessing.ipynb | apagac/cfme_tests | gpl-2.0 |
TODO: Implement LeNet-5
Implement the LeNet-5 neural network architecture.
This is the only cell you need to edit.
Input
The LeNet architecture accepts a 32x32xC image as input, where C is the number of color channels. Since MNIST images are grayscale, C is 1 in this case.
Architecture
Layer 1: Convolutional. The outpu... | from tensorflow.contrib.layers import flatten
def LeNet(x):
# Arguments used for tf.truncated_normal, randomly defines variables for the weights and biases for each layer
mu = 0
sigma = 0.1
# TODO: Layer 1: Convolutional. Input = 32x32x1. Output = 28x28x6.
conv1_W = tf.Variable(tf.truncate... | LeNet-Lab.ipynb | darienmt/intro-to-tensorflow | mit |
Training Pipeline
Create a training pipeline that uses the model to classify MNIST data.
You do not need to modify this section. | rate = 0.001
logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y, logits=logits)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation) | LeNet-Lab.ipynb | darienmt/intro-to-tensorflow | mit |
Train the Model
Run the training data through the training pipeline to train the model.
Before each epoch, shuffle the training set.
After each epoch, measure the loss and accuracy of the validation set.
Save the model after training.
You do not need to modify this section. | with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
num_examples = len(X_train)
print("Training...")
print()
for i in range(EPOCHS):
X_train, y_train = shuffle(X_train, y_train)
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH... | LeNet-Lab.ipynb | darienmt/intro-to-tensorflow | mit |
Evaluate the Model
Once you are completely satisfied with your model, evaluate the performance of the model on the test set.
Be sure to only do this once!
If you were to measure the performance of your trained model on the test set, then improve your model, and then measure the performance of your model on the test set... | with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('./models'))
test_accuracy = evaluate(X_test, y_test)
print("Test Accuracy = {:.3f}".format(test_accuracy)) | LeNet-Lab.ipynb | darienmt/intro-to-tensorflow | mit |
Bungee jumping
In the previous case study, we simulated a bungee jump with a model that took into account gravity, air resistance, and the spring force of the bungee cord, but we ignored the weight of the cord.
It is tempting to say that the weight of the cord doesn't matter, because it falls along with the jumper. Bu... | m = UNITS.meter
s = UNITS.second
kg = UNITS.kilogram
N = UNITS.newton
params = Params(v_init = 0 * m / s,
g = 9.8 * m/s**2,
M = 75 * kg, # mass of jumper
m_cord = 75 * kg, # mass of cord
area = 1 * m**2, # frontal area ... | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
Now here's a version of make_system that takes a Params object as a parameter.
make_system uses the given value of v_term to compute the drag coefficient C_d.
It also computes mu and the initial State object. | def make_system(params):
"""Makes a System object for the given params.
params: Params object
returns: System object
"""
M, m_cord = params.M, params.m_cord
g, rho, area = params.g, params.rho, params.area
v_init, v_term = params.v_init, params.v_term
# back out the coeff... | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
Let's make a System | system = make_system(params) | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
drag_force computes drag as a function of velocity: | def drag_force(v, system):
"""Computes drag force in the opposite direction of `v`.
v: velocity
returns: drag force in N
"""
rho, C_d, area = system.rho, system.C_d, system.area
f_drag = -np.sign(v) * rho * v**2 * C_d * area / 2
return f_drag | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
Here's drag force at 20 m/s. | drag_force(20 * m/s, system) | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
The following function computes the acceleration of the jumper due to tension in the cord.
$a_{cord} = \frac{\mu v^2/2}{\mu(L+y) + 2L}$ | def cord_acc(y, v, system):
"""Computes the force of the bungee cord on the jumper:
y: height of the jumper
v: velocity of the jumpter
returns: acceleration in m/s
"""
L, mu = system.L, system.mu
a_cord = -v**2 / 2 / (2*L/mu + (L+y))
return a_cord | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
Here's acceleration due to tension in the cord if we're going 20 m/s after falling 20 m. | y = -20 * m
v = -20 * m/s
cord_acc(y, v, system) | notebooks/jump2.ipynb | AllenDowney/ModSimPy | mit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.