Skip to content

Commit 0a714bf

Browse files
committed
Improve abstract method coverage approach
Signed-off-by: Keith Battocchi <[email protected]>
1 parent 39f3b20 commit 0a714bf

File tree

8 files changed

+46
-41
lines changed

8 files changed

+46
-41
lines changed

econml/_cate_estimator.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def fit(self, *args, inference=None, **kwargs):
117117
self
118118
119119
"""
120-
pass
120+
raise NotImplementedError("Abstract method")
121121

122122
def _wrap_fit(m):
123123
@wraps(m)
@@ -160,7 +160,7 @@ def effect(self, X=None, *, T0, T1):
160160
Note that when Y is a vector rather than a 2-dimensional array, the corresponding
161161
singleton dimension will be collapsed (so this method will return a vector)
162162
"""
163-
pass
163+
raise NotImplementedError("Abstract method")
164164

165165
@abc.abstractmethod
166166
def marginal_effect(self, T, X=None):
@@ -185,7 +185,7 @@ def marginal_effect(self, T, X=None):
185185
the corresponding singleton dimensions in the output will be collapsed
186186
(e.g. if both are vectors, then the output of this method will also be a vector)
187187
"""
188-
pass
188+
raise NotImplementedError("Abstract method")
189189

190190
def ate(self, X=None, *, T0, T1):
191191
"""
@@ -355,7 +355,7 @@ def effect_interval(self, X=None, *, T0=0, T1=1, alpha=0.05):
355355
lower, upper : tuple(type of :meth:`effect(X, T0, T1)<effect>`, type of :meth:`effect(X, T0, T1))<effect>` )
356356
The lower and the upper bounds of the confidence interval for each quantity.
357357
"""
358-
pass
358+
raise NotImplementedError("Defer to inference")
359359

360360
@_defer_to_inference
361361
def effect_inference(self, X=None, *, T0=0, T1=1):
@@ -379,7 +379,7 @@ def effect_inference(self, X=None, *, T0=0, T1=1):
379379
can on demand calculate confidence interval, z statistic and p value. It can also output
380380
a dataframe summary of these inference results.
381381
"""
382-
pass
382+
raise NotImplementedError("Defer to inference")
383383

384384
@_defer_to_inference
385385
def marginal_effect_interval(self, T, X=None, *, alpha=0.05):
@@ -403,7 +403,7 @@ def marginal_effect_interval(self, T, X=None, *, alpha=0.05):
403403
type of :meth:`marginal_effect(T, X)<marginal_effect>` )
404404
The lower and the upper bounds of the confidence interval for each quantity.
405405
"""
406-
pass
406+
raise NotImplementedError("Defer to inference")
407407

408408
@_defer_to_inference
409409
def marginal_effect_inference(self, T, X=None):
@@ -425,7 +425,7 @@ def marginal_effect_inference(self, T, X=None):
425425
can on demand calculate confidence interval, z statistic and p value. It can also output
426426
a dataframe summary of these inference results.
427427
"""
428-
pass
428+
raise NotImplementedError("Defer to inference")
429429

430430
@_defer_to_inference
431431
def ate_interval(self, X=None, *, T0, T1, alpha=0.05):
@@ -450,7 +450,7 @@ def ate_interval(self, X=None, *, T0, T1, alpha=0.05):
450450
lower, upper : tuple(type of :meth:`ate(X, T0, T1)<ate>`, type of :meth:`ate(X, T0, T1))<ate>` )
451451
The lower and the upper bounds of the confidence interval for each quantity.
452452
"""
453-
pass
453+
raise NotImplementedError("Defer to inference")
454454

455455
@_defer_to_inference
456456
def ate_inference(self, X=None, *, T0, T1):
@@ -474,7 +474,7 @@ def ate_inference(self, X=None, *, T0, T1):
474474
can on demand calculate confidence interval, z statistic and p value. It can also output
475475
a dataframe summary of these inference results.
476476
"""
477-
pass
477+
raise NotImplementedError("Defer to inference")
478478

479479
@_defer_to_inference
480480
def marginal_ate_interval(self, T, X=None, *, alpha=0.05):
@@ -498,7 +498,7 @@ def marginal_ate_interval(self, T, X=None, *, alpha=0.05):
498498
type of :meth:`marginal_ate(T, X)<marginal_ate>` )
499499
The lower and the upper bounds of the confidence interval for each quantity.
500500
"""
501-
pass
501+
raise NotImplementedError("Defer to inference")
502502

503503
@_defer_to_inference
504504
def marginal_ate_inference(self, T, X=None):
@@ -520,7 +520,7 @@ def marginal_ate_inference(self, T, X=None):
520520
can on demand calculate confidence interval, z statistic and p value. It can also output
521521
a dataframe summary of these inference results.
522522
"""
523-
pass
523+
raise NotImplementedError("Defer to inference")
524524

525525
@property
526526
def dowhy(self):
@@ -564,7 +564,7 @@ def const_marginal_effect(self, X=None):
564564
rather than a 2-dimensional array, the corresponding singleton dimensions in the output will be collapsed
565565
(e.g. if both are vectors, then the output of this method will also be a vector)
566566
"""
567-
pass
567+
raise NotImplementedError("Abstract method")
568568

569569
def effect(self, X=None, *, T0, T1):
570570
"""
@@ -697,7 +697,7 @@ def const_marginal_effect_interval(self, X=None, *, alpha=0.05):
697697
type of :meth:`const_marginal_effect(X)<const_marginal_effect>` )
698698
The lower and the upper bounds of the confidence interval for each quantity.
699699
"""
700-
pass
700+
raise NotImplementedError("Defer to inference")
701701

702702
@BaseCateEstimator._defer_to_inference
703703
def const_marginal_effect_inference(self, X=None):
@@ -717,7 +717,7 @@ def const_marginal_effect_inference(self, X=None):
717717
can on demand calculate confidence interval, z statistic and p value. It can also output
718718
a dataframe summary of these inference results.
719719
"""
720-
pass
720+
raise NotImplementedError("Defer to inference")
721721

722722
def const_marginal_ate(self, X=None):
723723
"""
@@ -759,7 +759,7 @@ def const_marginal_ate_interval(self, X=None, *, alpha=0.05):
759759
type of :meth:`const_marginal_ate(X)<const_marginal_ate>` )
760760
The lower and the upper bounds of the confidence interval for each quantity.
761761
"""
762-
pass
762+
raise NotImplementedError("Defer to inference")
763763

764764
@BaseCateEstimator._defer_to_inference
765765
def const_marginal_ate_inference(self, X=None):
@@ -779,20 +779,19 @@ def const_marginal_ate_inference(self, X=None):
779779
can on demand calculate confidence interval, z statistic and p value. It can also output
780780
a dataframe summary of these inference results.
781781
"""
782-
pass
783782

784783
def marginal_ate(self, T, X=None):
785784
return np.mean(self.marginal_effect(T, X=X), axis=0)
786785
marginal_ate.__doc__ = BaseCateEstimator.marginal_ate.__doc__
787786

788787
@BaseCateEstimator._defer_to_inference
789788
def marginal_ate_interval(self, T, X=None, *, alpha=0.05):
790-
pass
789+
raise NotImplementedError("Defer to inference")
791790
marginal_ate_interval.__doc__ = BaseCateEstimator.marginal_ate_interval.__doc__
792791

793792
@BaseCateEstimator._defer_to_inference
794793
def marginal_ate_inference(self, T, X=None):
795-
pass
794+
raise NotImplementedError("Defer to inference")
796795
marginal_ate_inference.__doc__ = BaseCateEstimator.marginal_ate_inference.__doc__
797796

798797
def shap_values(self, X, *, feature_names=None, treatment_names=None, output_names=None, background_samples=100):
@@ -1003,7 +1002,7 @@ def coef__interval(self, *, alpha=0.05):
10031002
lb, ub: tuple(type of :meth:`coef_()<coef_>`, type of :meth:`coef_()<coef_>`)
10041003
The lower and upper bounds of the confidence interval for each quantity.
10051004
"""
1006-
pass
1005+
raise NotImplementedError("Defer to inference")
10071006

10081007
@BaseCateEstimator._defer_to_inference
10091008
def coef__inference(self):
@@ -1015,7 +1014,7 @@ def coef__inference(self):
10151014
InferenceResults: object
10161015
The inference of the coefficients in the final linear model
10171016
"""
1018-
pass
1017+
raise NotImplementedError("Defer to inference")
10191018

10201019
@BaseCateEstimator._defer_to_inference
10211020
def intercept__interval(self, *, alpha=0.05):
@@ -1033,7 +1032,7 @@ def intercept__interval(self, *, alpha=0.05):
10331032
lower, upper: tuple(type of :meth:`intercept_()<intercept_>`, type of :meth:`intercept_()<intercept_>`)
10341033
The lower and upper bounds of the confidence interval.
10351034
"""
1036-
pass
1035+
raise NotImplementedError("Defer to inference")
10371036

10381037
@BaseCateEstimator._defer_to_inference
10391038
def intercept__inference(self):
@@ -1045,7 +1044,7 @@ def intercept__inference(self):
10451044
InferenceResults: object
10461045
The inference of the intercept in the final linear model
10471046
"""
1048-
pass
1047+
raise NotImplementedError("Defer to inference")
10491048

10501049
def summary(self, alpha=0.05, value=0, decimals=3, feature_names=None, treatment_names=None, output_names=None):
10511050
""" The summary of coefficient and intercept in the linear model of the constant marginal treatment
@@ -1274,7 +1273,7 @@ def coef__interval(self, T, *, alpha=0.05):
12741273
lower, upper: tuple(type of :meth:`coef_(T)<coef_>`, type of :meth:`coef_(T)<coef_>`)
12751274
The lower and upper bounds of the confidence interval for each quantity.
12761275
"""
1277-
pass
1276+
raise NotImplementedError("Defer to inference")
12781277

12791278
@BaseCateEstimator._defer_to_inference
12801279
def coef__inference(self, T):
@@ -1291,7 +1290,7 @@ def coef__inference(self, T):
12911290
InferenceResults: object
12921291
The inference of the coefficients in the final linear model
12931292
"""
1294-
pass
1293+
raise NotImplementedError("Defer to inference")
12951294

12961295
@BaseCateEstimator._defer_to_inference
12971296
def intercept__interval(self, T, *, alpha=0.05):
@@ -1311,7 +1310,7 @@ def intercept__interval(self, T, *, alpha=0.05):
13111310
lower, upper: tuple(type of :meth:`intercept_(T)<intercept_>`, type of :meth:`intercept_(T)<intercept_>`)
13121311
The lower and upper bounds of the confidence interval.
13131312
"""
1314-
pass
1313+
raise NotImplementedError("Defer to inference")
13151314

13161315
@BaseCateEstimator._defer_to_inference
13171316
def intercept__inference(self, T):
@@ -1329,7 +1328,7 @@ def intercept__inference(self, T):
13291328
The inference of the intercept in the final linear model
13301329
13311330
"""
1332-
pass
1331+
raise NotImplementedError("Defer to inference")
13331332

13341333
def summary(self, T, *, alpha=0.05, value=0, decimals=3,
13351334
feature_names=None, treatment_names=None, output_names=None):

econml/_ortho_learner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def _gen_ortho_learner_model_nuisance(self):
469469
If the estimator also provides a score method with the same arguments as fit, it will be used to
470470
calculate scores during training.
471471
"""
472-
pass
472+
raise NotImplementedError("Abstract method")
473473

474474
@abstractmethod
475475
def _gen_ortho_learner_model_final(self):
@@ -492,7 +492,7 @@ def _gen_ortho_learner_model_final(self):
492492
then the input ``T`` to both above calls will be the one-hot encoding of the original input ``T``,
493493
excluding the first column of the one-hot.
494494
"""
495-
pass
495+
raise NotImplementedError("Abstract method")
496496

497497
def _check_input_dims(self, Y, T, X=None, W=None, Z=None, *other_arrays):
498498
assert shape(Y)[0] == shape(T)[0], "Dimension mis-match!"

econml/_tree_exporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def _make_dot_exporter(self, *, out_file, feature_names, treatment_names, max_de
612612
Number of digits of precision for floating point in the values of
613613
impurity, threshold and value attributes of each node.
614614
"""
615-
pass
615+
raise NotImplementedError("Abstract method")
616616

617617
@abc.abstractmethod
618618
def _make_mpl_exporter(self, *, title=None, feature_names=None, treatment_names=None, max_depth=None,
@@ -650,7 +650,7 @@ def _make_mpl_exporter(self, *, title=None, feature_names=None, treatment_names=
650650
fontsize : int
651651
Fontsize for text
652652
"""
653-
pass
653+
raise NotImplementedError("Abstract method")
654654

655655
def export_graphviz(self, out_file=None, feature_names=None, treatment_names=None,
656656
max_depth=None,

econml/cate_interpreter/_interpreters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def interpret(self, cate_estimator, X):
3131
must be compatible shape-wise with the features used to fit
3232
the estimator
3333
"""
34-
pass
34+
raise NotImplementedError("Abstract method")
3535

3636

3737
class SingleTreeCateInterpreter(_SingleTreeInterpreter):

econml/dml/_rlearner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def _gen_model_y(self):
295295
model_y.fit(X, W, Y, sample_weight=sample_weight)
296296
model_y.predict(X, W)
297297
"""
298-
pass
298+
raise NotImplementedError("Abstract method")
299299

300300
@abstractmethod
301301
def _gen_model_t(self):
@@ -310,7 +310,7 @@ def _gen_model_t(self):
310310
model_t.fit(X, W, T, sample_weight=sample_weight)
311311
model_t.predict(X, W)
312312
"""
313-
pass
313+
raise NotImplementedError("Abstract method")
314314

315315
@abstractmethod
316316
def _gen_rlearner_model_final(self):
@@ -326,7 +326,7 @@ def _gen_rlearner_model_final(self):
326326
sample_weight=sample_weight, freq_weight=freq_weight, sample_var=sample_var)
327327
model_final.predict(X)
328328
"""
329-
pass
329+
raise NotImplementedError("Abstract method")
330330

331331
def _gen_ortho_learner_model_nuisance(self):
332332
return _ModelNuisance(self._gen_model_y(), self._gen_model_t())

econml/grf/_base_grf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(self,
103103
self.warm_start = warm_start
104104
self.max_samples = max_samples
105105

106+
@abstractmethod
106107
def _get_alpha_and_pointJ(self, X, T, y, **kwargs):
107108
""" This function must be implemented by child class and given input variables
108109
X, T, y and any auxiliary variables passed as keyword only, should be calculating
@@ -116,8 +117,9 @@ def _get_alpha_and_pointJ(self, X, T, y, **kwargs):
116117
J : array of shape (n_samples, n_outputs * n_outputs)
117118
The J matrix part of the moment equation, flattened in Fortran-contiguous format.
118119
"""
119-
pass
120+
raise NotImplementedError("Abstract method")
120121

122+
@abstractmethod
121123
def _get_n_outputs_decomposition(self, X, T, y, **kwargs):
122124
""" This function must be implemented by child class and given input variables
123125
X, T, y and any auxiliary variables passed as keyword only, should return a tuple
@@ -131,7 +133,7 @@ def _get_n_outputs_decomposition(self, X, T, y, **kwargs):
131133
n_relevant_outputs : int
132134
The length of the prefix of parameters that we care about (remainder are nuisance)
133135
"""
134-
pass
136+
raise NotImplementedError("Abstract method")
135137

136138
def apply(self, X):
137139
"""

econml/inference/_inference.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def fit(self, estimator, *args, **kwargs):
3333
3434
This is called after the estimator's fit.
3535
"""
36-
pass
36+
raise NotImplementedError("Abstract method")
3737

3838
def ate_interval(self, X=None, *, T0=0, T1=1, alpha=0.05):
3939
return self.effect_inference(X=X, T0=T0, T1=T1).population_summary(alpha=alpha).conf_int_mean()
@@ -738,7 +738,7 @@ def stderr(self):
738738
the corresponding singleton dimensions in the output will be collapsed
739739
(e.g. if both are vectors, then the output of this method will also be a vector)
740740
"""
741-
pass
741+
raise NotImplementedError("Abstract method")
742742

743743
@property
744744
def var(self):
@@ -776,7 +776,7 @@ def conf_int(self, alpha=0.05):
776776
the corresponding singleton dimensions in the output will be collapsed
777777
(e.g. if both are vectors, then the output of this method will also be a vector)
778778
"""
779-
pass
779+
raise NotImplementedError("Abstract method")
780780

781781
@abc.abstractmethod
782782
def pvalue(self, value=0):
@@ -796,7 +796,7 @@ def pvalue(self, value=0):
796796
the corresponding singleton dimensions in the output will be collapsed
797797
(e.g. if both are vectors, then the output of this method will also be a vector)
798798
"""
799-
pass
799+
raise NotImplementedError("Abstract method")
800800

801801
def zstat(self, value=0):
802802
"""
@@ -963,7 +963,7 @@ def _expand_outputs(self, n_rows):
963963
results: InferenceResults
964964
The expanded results
965965
"""
966-
pass
966+
raise NotImplementedError("Abstract method")
967967

968968
def translate(self, offset):
969969
"""

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ concurrency =
100100
source = econml
101101
omit = econml/tests/*
102102
relative_files = True
103+
104+
[coverage:report]
105+
exclude_lines =
106+
raise NotImplementedError\("(Abstract method|Defer to inference)"\)

0 commit comments

Comments
 (0)