I want to run this python script. I installed the SDF reader in linux by the following command in my home directory
python -m pip install --upgrade sdf
and it seems to be installed successfully.
The python script is the following
import sdf
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import os
from numpy import ma
from matplotlib import colors, ticker, cm
from matplotlib.mlab import bivariate_normal
from matplotlib.colors import ListedColormap
if __name__ == "__main__":
print ('This is main of module "test2d.py"')
######## Constant defined here ########
pi = 3.1415926535897932384626
q0 = 1.602176565e-19 # C
m0 = 9.10938291e-31 # kg
v0 = 2.99792458e8 # m/s^2
kb = 1.3806488e-23 # J/K
mu0 = 4.0e-7*pi # N/A^2
epsilon0 = 8.8541878176203899e-12 # F/m
h_planck = 6.62606957e-34 # J s
wavelength= 1.0e-6
frequency = v0*2*pi/wavelength
exunit = m0*v0*frequency/q0
bxunit = m0*frequency/q0
denunit = frequency**2*epsilon0*m0/q0**2
print 'electric field unit: '+str(exunit)
print 'magnetic field unit: '+str(bxunit)
print 'density unit nc: '+str(denunit)
font = {'family' : 'helvetica',
'color' : 'black',
'weight' : 'normal',
'size' : 20,
}
n=47
data = sdf.read("./"+str(n).zfill(4)+".sdf",dict=True)
header=data['Header']
time=header['time']
x = data['Grid/Grid_mid'].data[0]/1.0e-6
y = data['Grid/Grid_mid'].data[1]/1.0e-6
y = y[600:1799]
X, Y = np.meshgrid(x, y)
It gives me following error:
Traceback (most recent call last):
File "epochvis.py", line 45, in <module>
data = sdf.read("./"+str(n).zfill(4)+".sdf",dict=True)
AttributeError: 'module' object has no attribute 'read'
Any ideas? Thank you in advance.
sdf does not have a read function.
try typing the following in your python shell
import sdf
help(sdf)
you will see
FUNCTIONS
load(filename, objectname='/', unit=None, scale_units=None)
Load a dataset or group from an SDF file
save(filename, group)
Save an SDF group to a file
validate(obj)
Validate an sdf.Group or sdf.Dataset
Related
I think it has to do something with my wb.DataReader. I've made sure the Ticker is correct and the time. Yahoo finance has "Adj Close" as "Adj Close**" and I've tried both with correct spelling and capitalization.
import locale
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
ticker = "BHIL"
data = pd.DataFrame()
data[ticker] = wb.DataReader(ticker, data_source = 'yahoo', start = '2021-2-17')['Adj Close']
#Plot
data.plot(figsize=(15,6))
log_return = np.log(1 + data.pct_change())
#Plot
sns.distplot(log_return.iloc[1:])
plt.xlabel("Daily Return")
plt.ylabel("Frequency")
u = log_return.mean()
var = log_return.var()
drift = u - (0.5*var)
stdev = log_return.std()
days = 50
trials = 10000
Z = norm.ppf(np.random.rand(days, trials)) #days, trials
daily_returns = np.exp(drift.values + stdev.values * Z)
price_paths = np.zeros_like(daily_returns)
price_paths[0] = data.iloc[-1]
for t in range(1, days):
price_paths[t] = price_paths[t-1]*daily_returns[t]
Traceback (most recent call last):
File "/Users/gknight/Desktop/Benson Hill CFA/Monte_Carlo.py", line 11, in <module>
data[ticker] = wb.DataReader(ticker, data_source = 'yahoo', start = '2022-10-10')['Adj Close**']
File "/Users/gknight/opt/anaconda3/lib/python3.9/site-packages/pandas/util/_decorators.py", line 207, in wrapper
return func(*args, **kwargs)
File "/Users/gknight/opt/anaconda3/lib/python3.9/site-packages/pandas_datareader/data.py", line 370, in DataReader
return YahooDailyReader(
File "/Users/gknight/opt/anaconda3/lib/python3.9/site-packages/pandas_datareader/base.py", line 253, in read
df = self._read_one_data(self.url, params=self._get_params(self.symbols))
File "/Users/gknight/opt/anaconda3/lib/python3.9/site-packages/pandas_datareader/yahoo/daily.py", line 153, in _read_one_data
data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
TypeError: string indices must be integers
(base) gknight#GK Benson Hill CFA %
You need to use y_finance and overide ```pdr_override()````. So chage you code to:
import locale
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import yfinance as yf
yf.pdr_override()
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
ticker = "BHIL"
data = pd.DataFrame()
data[ticker] = pdr.get_data_yahoo(ticker, start = '2021-2-17')['Adj Close']
#Plot
data.plot(figsize=(15,6))
log_return = np.log(1 + data.pct_change())
#Plot
sns.distplot(log_return.iloc[1:])
plt.xlabel("Daily Return")
plt.ylabel("Frequency")
u = log_return.mean()
var = log_return.var()
drift = u - (0.5*var)
stdev = log_return.std()
days = 50
trials = 10000
Z = norm.ppf(np.random.rand(days, trials)) #days, trials
daily_returns = np.exp(drift.values + stdev.values * Z)
price_paths = np.zeros_like(daily_returns)
price_paths[0] = data.iloc[-1]
for t in range(1, days):
price_paths[t] = price_paths[t-1]*daily_returns[t]
Note that ````distplot is a deprecated function and will be removed in seaborn v0.14.0. so update your version of seaborn, or change the distplot to sns.histplot(log_return.iloc[1:])
to get
I am writing this code for simulation of earths magnetic field:
import numpy as np
import matplotlib.pyplot as plt
import magpylib as magpy
import pyvista as pv
ts = np.linspace(-8,8, 150)
t = np.linspace(-6,6, 150)
axis = np.c_[2*np.cos(ts*2*np.pi), 2*np.sin(ts*2*np.pi), ts]
aux = np.c_[2*np.cos(ts*2*np.pi), 2*np.sin(ts*2*np.pi), t]
def make_coil(pos, vertices):
coil = magpy.current.Line(
current = 100,
vertices = vertices,
position= pos,
style_color="green",
)
return coil
theta = np.sqrt(2)/2
r = 4
coil1 = make_coil((0,0,0), axis)
coil2 = make_coil((r*1,0,0), aux)
coil3 = make_coil((r*theta,r*theta,0), aux)
coil4 = make_coil((0,1*r,0), aux)
coil5 = make_coil((-r*theta,r*theta,0), aux)
coil6 = make_coil((-r*1,0,0), aux)
coil7 = make_coil((-r*theta,-r*theta,0), aux)
coil8 = make_coil((0,-r*1,0), aux)
coil9 = make_coil((r*theta,-r*theta,0), aux)
coil = coil1 + coil2 + coil3 + coil4 + coil5 + coil6 + coil7 + coil8 + coil9
coil.show()
grid = pv.UniformGrid(
dimensions=(41, 41, 41),
spacing=(2, 2, 2),
origin=(-40, -40, -40),
)
# compute B-field and add as data to grid
grid["B"] = coil.getB(grid.points)
# compute field lines
seed = pv.Disc(inner=1, outer=5.2, r_res=3, c_res=12)
strl = grid.streamlines_from_source(
seed,
vectors='B',
max_time=180,
initial_step_length=0.01,
integration_direction='both',
)
# create plotting scene
pl = pv.Plotter()
# add field lines and legend to scene
legend_args = {
'title': 'B [mT]',
'title_font_size': 20,
'color': 'black',
'position_y': 0.25,
'vertical': True,
}
# draw coils
magpy.show(coil, color="orange", canvas=pl, backend='pyvista')
# add streamlines
pl.add_mesh(
strl.tube(radius=.2),
cmap="bwr",
scalar_bar_args=legend_args,
)
# display scene
pl.camera.position=(160, 10, -10)
pl.set_background("white")
pl.show()
and I get this error message
danieltran#eduroam-193-157-168-102 OneDrive-UniversitetetiOslo % /usr/local/bin/python3 "/Users/danieltran/Library/CloudStorage/OneDrive-UniversitetetiOslo/H22/FYS1120/Comp Essay/d
ouble_solenoids.py"
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/_vtk.py", line 547, in <module>
from vtk.vtkCommonKitPython import buffer_shared, vtkAbstractArray, vtkWeakReference
ModuleNotFoundError: No module named 'vtk'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/danieltran/Library/CloudStorage/OneDrive-UniversitetetiOslo/H22/FYS1120/Comp Essay/double_solenoids.py", line 4, in <module>
import pyvista as pv
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/__init__.py", line 12, in <module>
from pyvista.plotting import *
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/plotting/__init__.py", line 4, in <module>
from .charts import Chart2D, ChartMPL, ChartBox, ChartPie
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/plotting/charts.py", line 13, in <module>
from pyvista import _vtk
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/_vtk.py", line 549, in <module>
from vtk.vtkCommonCore import buffer_shared, vtkAbstractArray, vtkWeakReference
ModuleNotFoundError: No module named 'vtk'.
First, you need to post a question, not just code + error message.
Based off of your error message, this is what I would try:
Ensure VTK is installed. https://pypi.org/project/vtk/
Try a different Python version. 3.11 is fresh off the shelf and it looks like the VTK library was last updated prior to 3.11's release.
There are other things that you could try but this is likely the best starting point based on your post...
VTK currently does not have a supported python 3.11 compatible version published. See files available for latest version 9.2.2 https://pypi.org/project/vtk/9.2.2/#files which has no 3.11 compatible files [as of today]. Some options are to build VTK yourself (may or may not be possible without fixes to support python 3.11) or use a different python version or wait until a new vtk release that is compatible with python 3.11.
I am trying to use plotly to build a line chart. Here is the code I wrote:
from fredapi import Fred
import plotly.express as px
fred = Fred("1c242ee90989c94ffb93c923d94e855f")
tran = pd.DataFrame(fred.get_series("EMISSCO2TOTVTCTOUSA"))
tran.columns = ["CO2"]
tran = tran.reset_index()
fig = px.line(tran, x = "index", y = "CO2")
fig.show()
Why do I get the following error:
AttributeError: 'object' object has no attribute 'layout'
Using the latest version of Plotly (version 5.4.0 at the time of posting), I can produce a line chart from your code.
from fredapi import Fred
import plotly.express as px
fred = Fred("1c242ee90989c94ffb93c923d94e855f")
tran = pd.DataFrame(fred.get_series("EMISSCO2TOTVTCTOUSA"))
tran.columns = ["CO2"]
tran = tran.reset_index()
fig = px.line(tran, x = "index", y = "CO2")
fig.show()
I want to mosaic/merge multiple swaths of the MODIS dataset (MOD06_L2) using python. I used the example (http://hdfeos.org/zoo/MORE/LAADS/MOD/MOD04_L2_merge.py) to read multiple files and merge. But I am getting an error while doing so, how to correct this error?
I would like to know is there any better way than this, to merge/mosaic MODIS HDF files into one?
import os
import glob
import matplotlib as mpl
import matplotlib.pyplot as plt
# import cartopy.crs as ccrs
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
# The first file in 3 swath files.
FILE_NAME = 'MOD06_L2.A2017126.0655.061.2019226193408.hdf'
GEO_FILE_NAME ='MOD06_L2.A2017126.0655.061.2019226193408.hdf'
DATAFIELD_NAME = 'Brightness_Temperature'
from pyhdf.SD import SD, SDC
i = 0
for file in list(glob.glob('MOD06*.hdf')):
reader = open(file)
hdf = SD(file, SDC.READ)
# Read dataset.
data2D = hdf.select(DATAFIELD_NAME)
data = data2D[:,:].astype(np.double)
hdf_geo = SD(GEO_FILE_NAME, SDC.READ)
# Read geolocation dataset.
lat = hdf_geo.select('Latitude')
latitude = lat[:,:]
lon = hdf_geo.select('Longitude')
longitude = lon[:,:]
# Retrieve attributes.
attrs = data2D.attributes(full=1)
lna=attrs["long_name"]
long_name = lna[0]
aoa=attrs["add_offset"]
add_offset = aoa[0]
fva=attrs["_FillValue"]
_FillValue = fva[0]
sfa=attrs["scale_factor"]
scale_factor = sfa[0]
vra=attrs["valid_range"]
valid_min = vra[0][0]
valid_max = vra[0][1]
ua=attrs["units"]
units = ua[0]
invalid = np.logical_or(data > valid_max,data < valid_min)
invalid = np.logical_or(invalid, data == _FillValue)
data[invalid] = np.nan
data = (data - add_offset) * scale_factor
datam = np.ma.masked_array(data, np.isnan(data))
if i == 0 :
data_m = datam
latitude_m = latitude
longitude_m = longitude
else:
data_m = np.vstack([data_m, datam])
latitude_m = np.vstack([latitude_m, latitude])
longitude_m = np.vstack([longitude_m, longitude])
i = i + 1
m = Basemap(projection='cyl', resolution='l',
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180)
m.drawcoastlines(linewidth=0.5)
m.drawparallels(np.arange(-90, 91, 45))
m.drawmeridians(np.arange(-180, 180, 45), labels=[True,False,False,True])
sc = m.scatter(longitude_m, latitude_m, c=data_m, s=0.1, cmap=plt.cm.jet,
edgecolors=None, linewidth=0)
cb = m.colorbar()
cb.set_label(units)
# Put title using the first file.
basename = os.path.basename(FILE_NAME)
plt.title('{0}\n{1}'.format(basename, DATAFIELD_NAME))
fig = plt.gcf()
# Save image.
pngfile = "{0}.py.png".format(basename)
fig.savefig(pngfile)
It showing an error
ValueError: 'c' argument has 4604040 elements, which is inconsistent with 'x' and 'y' with size 657720.
I am getting the below error when I try to plot a short time energy function, please I need your help solve this problem.
Code:
import os
from tqdm import tqdm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc
from python_speech_features import logfbank
import librosa
def plot_ste(ste):
fig, axes = plt.subplots(nrows=1, ncols=1, sharex=False,
sharey=False, figsize=(400, 50))
fig.suptitle('Short Time Energy', size=100, y=1.02)
i = 0
for x in range(1):
for y in range(1):
data = list(ste.values())[i]
x, win = data[0], data[1]
axes[x,y].set_title(list(ste.keys())[i])
axes[x,y].plot(win, x)
axes[x,y].get_xaxis().set_visible(False)
axes[x,y].get_yaxis().set_visible(False)
i+=1
def ste(x, win):
"""Compute short-time energy."""
if isinstance(win, str):
win = scipy.signal.get_window(win, max(1, len(x) // 8))
win = win / len(win)
return scipy.signal.convolve(x**2, win**2, mode="same")
df = pd.read_csv('/dir/to/a.csv')
df.set_index('fname', inplace=True)
classes = list(np.unique(df.ID))
df.reset_index(inplace=True)
ste = {}
for c in classes:
wav_file = df[df.ID==c].iloc[0, 0]
signal, rate = librosa.load('/dir/to/wav_file')
ste[c] = ste
plot_ste(ste)
plt.show()
Error:
File "/home/Desktop/Program/stft_plot_full_Dir.py", line 35, in plot_ste
x, win = data[0], data[1]
KeyError: 0