Skip to content
Snippets Groups Projects

Example aptfim curl zenodo

Merged Markus Kuehbach requested to merge example-aptfim-curl-zenodo into develop
All threads resolved!
1 file
+ 12
12
Compare changes
  • Side-by-side
  • Inline
%% Cell type:markdown id: tags:
## nomad-remote-tool jupyter testing
%% Cell type:markdown id: tags:
Upload data into local nomadOASIS. Process data inside a docker container.
%% Cell type:markdown id: tags:
### Initialize
%% Cell type:markdown id: tags:
Import Python modules
Import Python modules.
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
#plt.rcParams['figure.figsize'] = [12, 8]
#plt.rcParams['figure.dpi'] = 300
import h5py as h5
#needs shutils for decompressing zip archives, which is a default module/package in Python since >=v3.6
import shutil
```
%% Cell type:markdown id: tags:
Identify in which folder we are locally
#Use David Völgyes tool to download all files from a Zenodo repository with many files.
%% Cell type:code id: tags:
``` python
! echo $PWD
#! pip3 install zenodo-get
```
%% Cell type:markdown id: tags:
### Download a dataset for testing from Zenodo
Identify in which folder we are locally
%% Cell type:code id: tags:
``` python
#http://dx.doi.org/10.5281/zenodo.5562389
! curl -o PARAPROBE.Transcoder.Results.SimID.8.zip https://zenodo.org/record/5562389/files/PARAPROBE.Transcoder.Results.SimID.8.zip?download=1
! echo $PWD
```
%% Cell type:markdown id: tags:
### Decompress the dataset
### Download a dataset for testing from Zenodo
%% Cell type:code id: tags:
``` python
#needs shutils which is a default module/package in Python since >=v3.6
#http://dx.doi.org/10.5281/zenodo.5562389
! curl --output PARAPROBE.Transcoder.Results.SimID.8.zip https://zenodo.org/record/5562389/files/PARAPROBE.Transcoder.Results.SimID.8.zip
```
%% Cell type:code id: tags:
%% Cell type:markdown id: tags:
``` python
import shutil
```
### Decompress the dataset
%% Cell type:code id: tags:
``` python
shutil.unpack_archive('PARAPROBE.Transcoder.Results.SimID.8.zip') #unpacks in current path by if additional path argument is not given './')
```
%% Cell type:markdown id: tags:
Zenodo download processed and decompressed, check if required file is in local working directory.
%% Cell type:code id: tags:
``` python
MYDATASET = 'PARAPROBE.Transcoder.Results.SimID.8.h5'
#later load a dataset from local nomadOASIS data repository (shadow) folder.
! ls $MYDATASET
```
%% Cell type:markdown id: tags:
### Example 1, read data from an HDF5 file inside nomadOASIS, process data, and visualize these
%% Cell type:code id: tags:
``` python
#interact with h5py to load numerical data
hf = h5.File( MYDATASET, 'r' )
mq = hf['ReconstructionID8/Data/MassToChargeRatio'][:]
print('Mass-to-charge-state-ratio array loaded')
[mqmin, mqmax] = [0., np.max(mq)]
print('Dataset ranging from ['+str(mqmin)+', '+str(mqmax)+'] Da.')
mqincr = 0.01 #Da
print('Using a mass-to-charge-state ratio resolution of '+str(mqincr)+' Da.')
```
%% Cell type:code id: tags:
``` python
#transform collection of mass-to-charge-state ratios into a histogram
hst1d = np.unique(np.uint64(np.floor((mq[np.logical_and(mq >= mqmin, mq <= mqmax)]-mqmin)/mqincr)), return_counts=True)
nbins = np.uint64((mqmax - mqmin)/mqincr + 1)
print('Histogram has '+str(nbins)+' bins.')
```
%% Cell type:code id: tags:
``` python
#use matplotlib and numpy to plot histogram data
xy = np.zeros([nbins,2], np.float64)
xy[:,0] = np.linspace(mqmin+mqincr, mqmax+mqincr, nbins, endpoint=True)
xy[:,1] = 0.5 #*np.ones([nbins],np.float64) #0.5 to be able to plot logarithm you can not measure half an atom
for i in np.arange(0,len(hst1d[0])):
binidx = hst1d[0][i]
xy[binidx,1] = hst1d[1][i]
print('Mass-to-charge-state histogram created.')
```
%% Cell type:code id: tags:
``` python
[xmi, xmx, ymi, ymx] = [mqmin, mqmax, 0.5, 10**np.ceil(np.log10(np.max(xy[:,1])))]
fig, ( (myplot) ) = plt.subplots(1, 1)
myplot.plot( xy[:,0], xy[:,1], color='blue', alpha=0.5, linewidth=1.0 )
plt.legend( [r'Mass-to-charge-state ratio $\Delta\frac{m}{q} = $'+str(mqincr)+' Da'], loc='upper right')
plt.xlabel(r'Mass-to-charge-state-ratio (Da)')
plt.ylabel(r'Counts')
#plt.ylabel(r'$log_{10}$ Counts')
#xy.vlines([xmx], 0, 1, transform=xy.get_xaxis_transform(), colors=MYPARULA[2], linestyles='dotted') #'solid') #'dotted')
#plt.xticks([1, 2, 3, 4, 5, 6, 7, 8, 9], ['Min', '0.0025', '0.025', '0.25', '0.50', '0.75', '0.975', '0.9975', 'Max'])
plt.yscale('log')
print('Mass-to-charge-state histogram visualized.')
#scale bar with add margin to the bottom and top of the yaxis to avoid that lines fall on x axis
#polishing the margins
margin=0.01
plt.xlim([-margin*(xmx-xmi)+xmi, +margin*(xmx-xmi)+xmx])
plt.ylim([ymi, +margin*(ymx-ymi)+ymx])
#plot the figure
#figfn = MYDATASET+'.MassToChargeStateRatios.png'
#fig.savefig( figfn, dpi=300, facecolor='w', edgecolor='w', orientation='landscape', format='png',
# transparent=False, bbox_inches='tight', pad_inches=0.1, metadata=None)
#plt.close('all')
```
%% Cell type:code id: tags:
``` python
### Example 2, store files created inside jupyter session, where do these end up ?
```
%% Cell type:code id: tags:
``` python
#plot the figure
figfn = MYDATASET+'.MassToChargeStateRatios.png'
fig.savefig( figfn, dpi=300, facecolor='w', edgecolor='w', orientation='landscape', format='png',
transparent=False, bbox_inches='tight', pad_inches=0.1, metadata=None)
#plt.close('all')
print(figfn+' stored to disk.')
```
%% Cell type:code id: tags:
``` python
! ls
```
%% Cell type:code id: tags:
``` python
### Example 2, get a periodic table of the elements
```
%% Cell type:code id: tags:
``` python
### Example 3, interactive plotting (bokeh, ipywidgets?)
```
%% Cell type:code id: tags:
``` python
### Example 4, use nomad API to create an archive
#https://towardsdatascience.com/how-to-produce-interactive-matplotlib-plots-in-jupyter-environment-1e4329d71651
```
%% Cell type:code id: tags:
``` python
#small utility tool to download Zenodo repositories with many files #here is a sconsider maybe to use https://zenodo.org/record/1261813#.YW51TXuxXjA
```
%% Cell type:code id: tags:
``` python
```
Loading