@@ -371,7 +371,7 @@ def _subtotal_row(self, subtotal):
371371 return addend_sum - subtrahend_sum
372372
373373
374- class WaveDiffSubtotal ( _BaseSubtotals ) :
374+ class WaveDiffSubtotal :
375375 """Subtotal "blocks" created by adding and subtracting terms for wave differences.
376376
377377 This class handles a special case for wave differences when a CAT_DATE variable is
@@ -381,54 +381,60 @@ class WaveDiffSubtotal(_BaseSubtotals):
381381 percentages level: (count1/base1) - (count2/base2).
382382 """
383383
384- def __init__ (
385- self ,
386- base_values ,
387- counts ,
388- default_insertions ,
389- dimensions ,
390- diff_cols_nan = False ,
391- diff_rows_nan = False ,
392- ):
393- super (WaveDiffSubtotal , self ).__init__ (base_values , dimensions )
384+ def __init__ (self , base_values , counts , default_insertions , dimensions ):
385+ self ._base_values = base_values
394386 self ._counts = counts
395387 self ._default_insertions = default_insertions
396- self ._diff_cols_nan = diff_cols_nan
397- self ._diff_rows_nan = diff_rows_nan
388+ self ._dimensions = dimensions
398389
399390 @classmethod
400- def subtotal_rows (
401- cls ,
402- base_values ,
403- counts ,
404- default_insertions ,
405- dimensions ,
406- diff_cols_nan = False ,
407- diff_rows_nan = False ,
408- ):
391+ def subtotal_columns (cls , base_values , counts , default_insertions , dimensions ):
392+ """Return (n_column_subtotals, n_base_rows) ndarray of subtotal columns."""
393+ return cls (
394+ base_values , counts , default_insertions , dimensions
395+ )._subtotal_columns
396+
397+ @classmethod
398+ def subtotal_rows (cls , base_values , counts , default_insertions , dimensions ):
409399 """Return (n_row_subtotals, n_base_cols) ndarray of subtotal rows.
410400 Keyword arguments:
411401 `diff_cols_nan` -- Overrides subtotal differences in the columns direction eg
412402 for column bases (default False)
413403 `diff_rows_nan` -- Overrides subtotal differences in the rows direction eg for
414404 row bases (default False)
415405 """
416- return cls (
417- base_values ,
418- counts ,
419- default_insertions ,
420- dimensions ,
421- diff_cols_nan ,
422- diff_rows_nan ,
423- )._subtotal_rows
406+ return cls (base_values , counts , default_insertions , dimensions )._subtotal_rows
407+
408+ @lazyproperty
409+ def _column_subtotals (self ):
410+ """Sequence of _Subtotal object for each subtotal in columns-dimension."""
411+ return self ._dimensions [- 1 ].subtotals
412+
413+ def _multiple_subtrahends_or_addends (self , subtotal ):
414+ """Returns true if the subtotal has multiple addend or subtrahend terms."""
415+ return any (subtotal .subtrahend_idxs ) and (
416+ len (subtotal .subtrahend_idxs ) > 1 or len (subtotal .addend_idxs ) > 1
417+ )
418+
419+ def _nan_subtotals (self , axis ):
420+ """Generate an array filled with NaN values.
421+
422+ Matches the size of the specified axis of the base values.
423+ """
424+ return np .full (self ._base_values .shape [axis ], np .nan )
425+
426+ @lazyproperty
427+ def _row_subtotals (self ):
428+ """Sequence of _Subtotal object for each subtotal in rows-dimension."""
429+ return self ._dimensions [0 ].subtotals
424430
425431 @lazyproperty
426432 def _subtotal_rows (self ):
427433 """(n_row_subtotals, n_cols) ndarray of subtotal rows."""
428434 subtotals = self ._row_subtotals
429-
435+ n_cols = self . _base_values . shape [ 1 ]
430436 if len (subtotals ) == 0 :
431- return np .empty ((0 , self . _ncols ))
437+ return np .empty ((0 , n_cols ))
432438
433439 return np .vstack (
434440 [
@@ -437,45 +443,24 @@ def _subtotal_rows(self):
437443 ]
438444 )
439445
440- @classmethod
441- def subtotal_columns (
442- cls ,
443- base_values ,
444- counts ,
445- default_insertions ,
446- dimensions ,
447- diff_cols_nan = False ,
448- diff_rows_nan = False ,
449- ):
450- """Return (n_column_subtotals, n_base_rows) ndarray of subtotal columns."""
451- return cls (
452- base_values ,
453- counts ,
454- default_insertions ,
455- dimensions ,
456- diff_cols_nan ,
457- diff_rows_nan ,
458- )._subtotal_columns
459-
460446 @lazyproperty
461447 def _subtotal_columns (self ):
462448 """(n_rows, n_col_subtotals) matrix of subtotal columns."""
463449 subtotals = self ._column_subtotals
450+ n_rows = self ._base_values .shape [0 ]
464451 if len (subtotals ) == 0 :
465- return np .empty ((self . _nrows , 0 ))
452+ return np .empty ((n_rows , 0 ))
466453 return np .hstack (
467454 [
468- self ._subtotal_column (subtotal , default ).reshape (self . _nrows , 1 )
455+ self ._subtotal_column (subtotal , default ).reshape (n_rows , 1 )
469456 for subtotal , default in zip (subtotals , self ._default_insertions .T )
470457 ]
471458 )
472459
473460 def _subtotal_column (self , subtotal , default ):
474461 """Return (n_rows,) ndarray of values for `subtotal` column."""
475- if (
476- self ._dimensions [- 1 ].dimension_type == DT .CAT_DATE
477- and len (subtotal .subtrahend_idxs ) > 0
478- and len (subtotal .addend_idxs ) > 0
462+ if self ._dimensions [- 1 ].dimension_type == DT .CAT_DATE and any (
463+ subtotal .subtrahend_idxs
479464 ):
480465 if self ._multiple_subtrahends_or_addends (subtotal ):
481466 return self ._nan_subtotals (axis = 0 )
@@ -496,10 +481,8 @@ def _subtotal_column(self, subtotal, default):
496481 def _subtotal_row (self , subtotal , default ):
497482 """Return (n_cols,) ndarray of values for `subtotal` row."""
498483
499- if (
500- self ._dimensions [0 ].dimension_type == DT .CAT_DATE
501- and len (subtotal .subtrahend_idxs ) > 0
502- and len (subtotal .addend_idxs ) > 0
484+ if self ._dimensions [0 ].dimension_type == DT .CAT_DATE and any (
485+ subtotal .subtrahend_idxs
503486 ):
504487 if self ._multiple_subtrahends_or_addends (subtotal ):
505488 return self ._nan_subtotals (axis = 1 )
@@ -517,19 +500,6 @@ def _subtotal_row(self, subtotal, default):
517500
518501 return default
519502
520- def _multiple_subtrahends_or_addends (self , subtotal ):
521- """Returns true if the subtotal has multiple addend or subtrahend terms."""
522- return any (subtotal .subtrahend_idxs ) and (
523- len (subtotal .subtrahend_idxs ) > 1 or len (subtotal .addend_idxs ) > 1
524- )
525-
526- def _nan_subtotals (self , axis ):
527- """Generate an array filled with NaN values.
528-
529- Matches the size of the specified axis of the base values.
530- """
531- return np .full (self ._base_values .shape [axis ], np .nan )
532-
533503
534504class OverlapSubtotals (SumSubtotals ):
535505 """Subtotal blocks used exclusively for the "overlap" cube measure.
0 commit comments