Git Product home page Git Product logo

pyrho's Introduction

mp-pyrho

๐Ÿ“„ Full Documentation

Installation

pip install mp-pyrho

Tools for re-griding volumetric quantum chemistry data for machine-learning purposes.

.github/workflows/testing.yml codecov DOI

If you use this package in your research, please cite the following:

Shen, J.-X., Munro, J. M., Horton, M. K., Huck, P., Dwaraknath, S., & Persson, K. A. (2022). 
A representation-independent electronic charge density database for crystalline materials. 
Sci Data, 9(661), 1โ€“7. doi: 10.1038/s41597-022-01746-z

Regridding data using PyRho

The PGrid Class

The PGrid object is defined by an N-dimensional numpy array grid_data and a N lattice vector given as a matrix lattice. The input array is a scalar field that is defined on a regularly spaced set of grid points starting at the origin. For example, you can construct a periodic field as follows:

import numpy as np
from pyrho.pgrid import PGrid
from pyrho.vis.scatter import get_scatter_plot


def func(X, Y):
    return np.sin(X) * np.cos(2 * Y)


a = np.linspace(0, np.pi, 27, endpoint=False)
b = np.linspace(0, np.pi, 28, endpoint=False)
X, Y = np.meshgrid(a, b, indexing="ij")
data = func(X, Y)
pg2d = PGrid(grid_data=data, lattice=[[np.pi, 0], [0, np.pi]])

The data can be examined using the helper plotting function which supports up to 3-D.

import matplotlib as mpl

mpl.rc("image", cmap="viridis")
get_scatter_plot(pg2d.grid_data, pg2d.lattice, marker_size=40)

The period data in the PGrid object must be fixed-scaled so if you half the number of points in the domain, the range of the data will stay the same. This is different from how the charge density is stored in codes like VASP where the values at each point change based on the number of grid points used to store the data.

The regridding capabilities allow the user to obtain the data in any arbitrary representation. For example, if we want to shift to the middle of the unit-cell and create a ((1,1), (1,-1)) super-cell, with a 30 by 32 grid, we can run:

pg_2x = pg2d.get_transformed([[1, 1], [1, -1]], origin=[0.5, 0.5], grid_out=[30, 32])
get_scatter_plot(pg_2x.grid_data, pg_2x.lattice, skips=1, opacity=1, marker_size=10)

png

Up-sampling with Fourier interpolation

The up-sampling capabilities allow the user to exploit the periodicity of the data to obtain a higher-resolution grid. As an example, we can take a sparsely sampled periodic data in 1-D:

def func1(X):
    return np.sin(6 * X)


a = np.linspace(0, np.pi, 10, endpoint=False)
data = func1(a)

pg1d = PGrid(grid_data=data, lattice=[[np.pi]])
get_scatter_plot(pg1d.grid_data, pg1d.lattice, marker_size=50)

png

This does not really resemble the np.sin(6*X) function we used to generate the data. However, if we use an up-sample factor of 8, we can obtain a more dense representation:

pg1d_fine = pg1d.get_transformed(
    sc_mat=[[2]],
    grid_out=[
        200,
    ],
    up_sample=8,
)
get_scatter_plot(pg1d_fine.grid_data, pg1d_fine.lattice, marker_size=10)

png

The ChargeDensity class

The ChargeDensity object can use the from_file construction method from pymatgen.io.vasp.outputs.Chgcar as shown below.

from pymatgen.io.vasp import Chgcar
from pyrho.charge_density import ChargeDensity

cden_uc = ChargeDensity.from_file(
    "../test_files/CHGCAR.uc.vasp"
)
cden_sc = ChargeDensity.from_file(
    "../test_files/CHGCAR.sc1.vasp"
)
chgcar_sc = Chgcar.from_file(
    "../test_files/CHGCAR.sc1.vasp"
)
cden_transformed = cden_uc.get_transformed(
    [[1, 1, 0], [1, -1, 0], [0, 0, 1]],
    grid_out=cden_sc.grid_shape,
    up_sample=2,
)

The normalized_data property contains a dictionary keyed with the same keys as Chgcar.data (typically "total" and "diff" for spin charge densities). This quantity is the fixed scalar field that should remain fixed after the transformation.

data = cden_uc.normalized_data["total"]
print(
    f"The normalized charge density data is has a range of {data.min():0.3f} --> {data.max():0.3f} e-/Ang^3"
)
The normalized charge density data is has a range of -0.188 --> 0.572 e-/Ang^3

Note that the PAW transformation sometimes results in negative charge densities.

trans_data = cden_transformed.normalized_data["total"]
print(
    f"The transformed normalized charge density data is has a range of {trans_data.min():0.3f} --> {trans_data.max():0.3f} e-/Ang^3"
)
The transformed normalized charge density data is has a range of -0.188 --> 0.572 e-/Ang^3
sc_data = cden_sc.normalized_data["total"]
print(
    f"The reference normalized charge density data is has a range of {sc_data.min():0.3f} --> {sc_data.max():0.3f} e-/Ang^3"
)
The reference normalized charge density data is has a range of -0.188 --> 0.570 e-/Ang^3

Credits

Jimmy-Xuan Shen: Project lead

Wennie Wang: For naming the package

pyrho's People

Contributors

dependabot[bot] avatar jmmshn avatar mkhorton avatar munrojm avatar shyamd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyrho's Issues

Issue when importing pyrho

Hi,

I would like to report an issue I encounter when I try to import pyrho in my jupyter notebook, running with Python 3.9.12, installed with pip3 install pyrho

import pyrho as phr is enough to reproduce the issue, which leads to a TypeError which I copy here down below.

Thank you

TypeError Traceback (most recent call last)
/home/massad/gitloc/EDNet/BuildingMPDataset.ipynb Cell 13 in <cell line: 2>()
1 import numpy as np
----> 2 import pyrho as phr

File ~/anaconda3/lib/python3.9/site-packages/pyrho/init.py:37, in
34 import numpy as np
36 # Place all submodule functions and variables into namespace
---> 37 from pyrho.parameters import *
38 from pyrho.utilities import *
39 from pyrho.loadData import * #import loadData

File ~/anaconda3/lib/python3.9/site-packages/pyrho/parameters.py:534, in
529 #squarePulses = ['custom', 'delta', 'step', 'rectifier', 'shortPulse', 'recovery'] #{'custom': True, 'delta': True, 'step': True, 'rectifier': True, 'shortPulse': True, 'recovery': True}
530 #arbitraryPulses = ['custom', 'sinusoid', 'chirp', 'ramp'] #{'custom': True, 'sinusoid': True, 'chirp': True, 'ramp':True} # Move custom here
532 smallSignalAnalysis = ['delta', 'step', 'sinusoid']
--> 534 protParams['custom'].add_many(('phis', [1e16,1e17], 0, None, molemm**-2second**-1, '\mathbf{\phi}', 'List of flux values'), #'photons/s/mm^2'
535 ('Vs', [-70,-20,10], None, None, mV, '\mathbf{\mathrm{V}}', 'List of voltage clamp values (if applied)'), #'mV'
536 ('delD', 25, 0, 1e9, ms, '\Delta t_{delay}', 'Delay duration before the first pulse'), #'ms'
537 ('cycles', [[150.,50.]], 0, None, ms, 'cycles', 'List of [on, off] durations for each pulse'))#, #'ms'#,
539 protParams['step'].add_many(('phis', [1e16,1e17], 0, None, molemm**-2second**-1, '\mathbf{\phi}', 'List of flux values'), #'photons/s/mm^2'
540 ('Vs', [-70,-40,-10,10,40,70], None, None, mV, '\mathbf{\mathrm{V}}', 'List of voltage clamp values (if applied)'), #'mV'
541 ('delD', 25, 0, 1e9, ms, '\Delta t_{delay}', 'Delay duration before the first pulse'), #'ms'
542 ('cycles', [[150.,100.]], 0, None, ms, 'cycles', 'List of [on, off] durations for each pulse')) #'ms'
...
236 raise ValueError("'%s' is not a Parameter" % par)
--> 237 OrderedDict.setitem(self, key, par)
238 par.name = key

TypeError: descriptor 'setitem' requires a 'collections.OrderedDict' object but received a 'PyRhOparameters'

Import fails in pyrho.parameters import (requires collections.OrderedDict)

I am unable to import the package. Below is the trace back and error that I am receiving.

Cell In[4], line 7
4 from pymatgen.core import Structure
5 from pymatgen.io.vasp import Chgcar
----> 7 import pyrho
9 import matplotlib.pyplot as plt
10 import seaborn as sns

File ~/*****/site-packages/pyrho/init.py:37
34 import numpy as np
36 # Place all submodule functions and variables into namespace
---> 37 from pyrho.parameters import *
38 from pyrho.utilities import *
39 from pyrho.loadData import * #import loadData

File ~//site-packages/pyrho/parameters.py:534
529 #squarePulses = ['custom', 'delta', 'step', 'rectifier', 'shortPulse', 'recovery'] #{'custom': True, 'delta': True, 'step': True, 'rectifier': True, 'shortPulse': True, 'recovery': True}
530 #arbitraryPulses = ['custom', 'sinusoid', 'chirp', 'ramp'] #{'custom': True, 'sinusoid': True, 'chirp': True, 'ramp':True} # Move custom here
532 smallSignalAnalysis = ['delta', 'step', 'sinusoid']
--> 534 protParams['custom'].add_many(('phis', [1e16,1e17], 0, None, mole*mm-2*second
-1, '\mathbf{\phi}', 'List of flux values'), #'photons/s/mm^2'
535 ('Vs', [-70,-20,10], None, None, mV, '\mathbf{\mathrm{V}}', 'List of voltage clamp values (if applied)'), #'mV'
536 ('delD', 25, 0, 1e9, ms, '\Delta t_{delay}', 'Delay duration before the first pulse'), #'ms'
537 ('cycles', [[150.,50.]], 0, None, ms, 'cycles', 'List of [on, off] durations for each pulse'))#, #'ms'#,
539 protParams['step'].add_many(('phis', [1e16,1e17], 0, None, mole*mm
*-2*second**-1, '\mathbf{\phi}', 'List of flux values'), #'photons/s/mm^2'
...
236 raise ValueError("'%s' is not a Parameter" % par)
--> 237 OrderedDict.setitem(self, key, par)
238 par.name = key

TypeError: descriptor 'setitem' requires a 'collections.OrderedDict' object but received a 'PyRhOparameters'

Invalid `factor` keyword in README

Hi Jimmy! In the README here, a factor keyword is provided to get_plotly_scatter_plot, but there is no factor keyword supported. Perhaps this was meant to be skips? Just a head's up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.