【发布时间】:2020-03-29 20:13:45
【问题描述】:
我是 Python 中 OOP 的初学者。这是我的代码块。
#%% IMPORTING MODULES
import numpy as np
import netCDF4 as nc4
import datetime as dt
#%% RAINFALL CLASS
class Rainfall():
date_zero = dt.date(1901,1,1)
#Initialise the class attributes
def __init__(self,path):
self.path = path
self.dataset = nc4.Dataset(self.path, mode='r')
self.lon = self.dataset.variables['LON'][:]
self.lat = self.dataset.variables['LAT'][:]
self.time = self.dataset.variables['TIME'][:]
# self.rf = self.dataset.variables['RAIN'][:]
self.date = np.arange(dt.datetime(1979,1,1), dt.datetime(2019,1,1), dt.timedelta(days=1)).astype(dt.datetime)
self.index_jjas = []
for i,val in enumerate(self.date):
if val.month >= 6 and val.month<=9:
self.index_jjas.append(i)
self.jjas_rf = self.dataset.variables['RAIN'][self.index_jjas]
self.nonzero = self.nonzero_jjas()
self.sorted_rf = self.sorted_list(self.nonzero)
self.threshold = self.p95(self.sorted_rf)
self.val_abv_threshold = np.ma.masked_less(self.nonzero, self.threshold)
self.abv_threshold = np.ma.MaskedArray.count(self.val_abv_threshold, axis=0)
self.clim = self.simpleprob(self.abv_threshold, len(self.index_jjas))
#Method to find the non zero precipitation
def nonzero_jjas(self):
return np.ma.masked_less(self.jjas_rf, 0.2)
#Method to sort the non zero precipitation array
def sorted_list(self, nz):
return np.ma.MaskedArray.filled(np.ma.sort(nz, axis=0), np.nan)
#Method to obtain the 95th percentile for threshold value to identify extremes
def p95(self,ns):
return np.nanpercentile(ns, 95, axis=0)
#Method to obtain the probabiltiy
def simpleprob(self, a1, a2):
return np.divide(a1, a2)
#Method to identify ExtremeDays in Waves
def extr(self, a1, a2, data, clim):
m1 = a1.mask
m2 = a2.mask
m3 = m1 | m2
count = np.ma.MaskedArray.count(a2, axis=0)
data_new = np.ma.masked_where(m3, data)
data_count = np.ma.MaskedArray.count(data_new, axis=0)
data_prob = np.divide(data_count, count)
prob_diff = data_prob - clim
return (data_new, data_count, data_prob, prob_diff)
#%% Waves Class
class Waves(Rainfall):
#Initialise the class attributes
def __init__(self, path1, path2):
self.olr_path = path1
self.mvr_path = path2
self.olr_dataset = nc4.Dataset(self.olr_path, mode='r')
self.mvr_dataset = nc4.Dataset(self.mvr_path, mode='r')
self.date = np.arange(dt.datetime(1979,1,1), dt.datetime(2019,1,1), dt.timedelta(days=1)).astype(dt.datetime)
self.index_jjas = []
for i,val in enumerate(self.date):
if val.month >= 6 and val.month<=9:
self.index_jjas.append(i)
self.olr = self.olr_dataset.variables['olr'][self.index_jjas]
self.mvr = self.mvr_dataset.variables['var'][self.index_jjas]
self.mn_mvr = np.nanmean(self.mvr, axis=0)
self.std_olr = np.nanstd(self.olr, axis=0)
self.active = self.active_days()
self.dry = self.dry_days(self.active)
self.wet = self.wet_days(self.active)
self.ext_dry = self.extr(self.val_abv_threshold, self.dry, self.olr, self.clim)
self.ext_wet = self.extr(self.val_abv_threshold, self.wet, self.olr, self.clim)
#Method to find the active days
def active_days(self):
return np.ma.masked_where(np.ma.getmask(np.ma.masked_less(self.mvr, self.mn_mvr)), self.olr)
#Method to find the dry days
def dry_days(self, act1):
return np.ma.masked_less_equal(act1, (0.5*self.std_olr))
#Method to find the wet days
def wet_days(self, act1):
return np.ma.masked_greater_equal(act1, (-0.5*self.std_olr))
#%% Create Objects and other attributes
rain = Rainfall(path='rf_1979_2018.nc')
mjo = Waves(path1='/home/anik3t/Documents/Data/olr/0.25degfiles/mjo_final.nc', path2= '/home/anik3t/Documents/Data/olr/mjo_var.nc')
我需要从子 Waves 类中的父 Rainfall 类访问 val_abv_threshold,但我无法访问。运行代码会给我以下错误消息:
Traceback (most recent call last):
File "/home/anik3t/.config/spyder-py3/test_run1.py", line 109, in <module>
mjo = Waves(path1='/home/anik3t/Documents/Data/olr/0.25degfiles/mjo_final.nc', path2= '/home/anik3t/Documents/Data/olr/mjo_var.nc')
File "/home/anik3t/.config/spyder-py3/test_run1.py", line 91, in __init__
self.ext_dry = self.extr(self.val_abv_threshold, self.dry, self.olr, self.clim)
AttributeError: 'Waves' object has no attribute 'val_abv_threshold'
我认为 Waves 类无法从父类访问所需的属性。我试图使用 super() 函数,但我不确定它应该如何在这里使用。
【问题讨论】:
-
你需要在你的子类中调用 super: super().__init__(path1)
-
为什么
Waves是Rainfall的子类?鉴于您在任何方法中很少使用self,您是否需要任何类都是有争议的。 -
@nickthefreak 但 path1 是我的波形文件的路径。
-
@chepner 我按照我的想法创建了子类,这样我就可以使用父类的属性。
标签: python class oop parent-child