The story of rubyist struggling with python :: Dict data with pycall

The story of rubyist struggling with python :: Dict data with pycall

PyCall Ruby version Tips has an almost perfect correspondence report. In addition, numpy-specific notation

{.example}


#Access to the last line m in python[-1,:]Where to write

And so on, rubyist even has an easy-to-understand explanation of numpy.

It looks like it can be done easily. .. ..

I'm sorry if it is a complicated data structure

However, when dealing with complex python data structures It's very difficult. The cause is that I don't know the key of dict. It's good when you take simple characters, but it doesn't seem to work well with slightly complicated characters.

Vasprun :: tdos or in pymatgen as follows Vasprun :: compolete \ _dos etc. can be taken.

However,

If p spd \ _dos = dosrun.complete \ _dos.get \ _spd \ _dos ()

{<OrbitalType.s: 0>: <pymatgen.electronic_structure.dos.Dos object at 0x102dd4710>, <OrbitalType.p: 1>: <pymatgen.electronic_structure.dos.Dos object at 0x102dc2dd8>, <OrbitalType.d: 2>: <pymatgen.electronic_structure.dos.Dos object at 0x102d962b0>}

Is output. I think this can be taken with spd \ _dos ('OrbitalType.s') No good. Therefore,

p s_d = spd_dos.to_a[0][1].densities

As

{.example}


array([  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
...
      ])

Make it python :: array and then

s_list = PyCall::List.(s_d.to_a[0][1]).to_a

As an array, hash is taken out, and vals is taken out, and it is converted into a data structure that can be interpreted by ruby with to \ _a.

Once you get used to it, you won't have much trouble because the same code is repeated, Maybe I'll forget it over time. I want you to incorporate it in lib, but it seems that only characters read dict directly. .. .. Would you like to write a code and try pull req?

Densities and energy can't be helped without knowing the name, You can find this by reading the source code of pymatgen. Apparently, to use pycall, you need to repeat this.

{.ruby}


require 'pycall/import'
include PyCall::Import

pyimport :sys
pyimport 'numpy', as: :np
pyfrom 'pymatgen.io.vasp.outputs', import: :Vasprun

# dos
dosrun = Vasprun.("../dos_calc_fine/vasprun.xml")
e_object = (dosrun.tdos.energies - dosrun.efermi)
energy = PyCall::List.(e_object).to_a
spd_dos = dosrun.complete_dos.get_spd_dos()
spd = PyCall::Dict.(spd_dos)
s_d = spd.to_a[0][1].densities
s_list = PyCall::List.(s_d.to_a[0][1]).to_a
p_d = spd.to_a[1][1].densities
p_list = PyCall::List.(p_d.to_a[0][1]).to_a
d_d = spd.to_a[2][1].densities
d_list = PyCall::List.(d_d.to_a[0][1]).to_a
tdos = dosrun.tdos.densities
t_list = PyCall::List.(tdos.to_a[0][1]).to_a

require 'numo/gnuplot'
Numo.gnuplot do
  set yrange: '[-10:10]'
  plot [s_list, energy, w: :l, title: 's'],
  [p_list, energy, w: :l, title: 'p'],
  [d_list, energy, w: :l, title: 'd'],
  [t_list, energy, w: :l, title: 'total']
end

Recommended Posts

The story of rubyist struggling with python :: Dict data with pycall
The story of reading HSPICE data in Python
Extract the band information of raster data with python
Try scraping the data of COVID-19 in Tokyo with Python
The story of Python and the story of NaN
[Homology] Count the number of holes in data with Python
March 14th is Pi Day. The story of calculating pi with python
The story of making a standard driver for db with python.
The story of making a module that skips mail with python
The story of verifying the open data of COVID-19
The story of making Python an exe
Check the existence of the file with python
The story of manipulating python global variables
Recommendation of Altair! Data visualization with Python
The story of blackjack A processing (python)
The story of making a university 100 yen breakfast LINE bot with Python
Try to image the elevation data of the Geographical Survey Institute with Python
The story of doing deep learning with TPU
Prepare the execution environment of Python3 with Docker
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
Check the date of the flag duty with Python
A story stuck with handling Python binary data
Image processing? The story of starting Python for
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
Align the number of samples between classes of data for machine learning with Python
[Introduction to Python] How to get the index of data with a for statement
Data analysis with python 2
the zen of Python
The story of sys.path.append ()
Data analysis with Python
Extract the table of image files with OneDrive & Python
The story of Python without increment and decrement operators.
The story of stopping the production service with the hostname command
Learn Nim with Python (from the beginning of the year).
The story of replacing Nvidia GTX 1650 with Linux Mint 20.1.
The story of sharing the pyenv environment with multiple users
Destroy the intermediate expression of the sweep method with Python
Visualize the range of interpolation and extrapolation with python
The story of FileNotFound in Python open () mode ='w'
Calculate the regression coefficient of simple regression analysis with python
Get the result in dict format with Python psycopg2
Challenge principal component analysis of text data with Python
Not being aware of the contents of the data in python
Let's use the open data of "Mamebus" in Python
Summary of the basic flow of machine learning with Python
Understand the status of data loss --Python vs. R
Get the operation status of JR West with Python
[Basics of data science] Collecting data from RSS with python
The story of automatic language conversion of TypeScript / JavaScript / Python
[Python Data Frame] When the value is empty, fill it with the value of another column.
I tried to find the entropy of the image with python
Sample data created with python
I tried "gamma correction" of the image with Python + OpenCV
The story of building Zabbix 4.4
Towards the retirement of Python2
I tried using the Python library from Ruby with PyCall
Visualize the results of decision trees performed with Python scikit-learn
[Apache] The story of prefork
The story of how the Python bottle worked on Sakura Internet