• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LCA-ActivityBrowser / activity-browser / 14497405347

16 Apr 2025 04:10PM UTC coverage: 53.056% (-0.08%) from 53.134%
14497405347

Pull #1433

github

web-flow
Merge ead51b511 into 45e974aed
Pull Request #1433: Add 'cumulative percent' contribution analysis mode

69 of 161 new or added lines in 2 files covered. (42.86%)

9 existing lines in 1 file now uncovered.

8403 of 15838 relevant lines covered (53.06%)

0.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

16.13
/activity_browser/mod/bw2analyzer/contribution.py
1
from bw2analyzer import ContributionAnalysis
1✔
2

3
from typing import Optional
1✔
4
import numpy as np
1✔
5

6
class ABContributionAnalysis(ContributionAnalysis):
1✔
7
    """Activity Browser version of bw2analyzer.ContributionAnalysis"""
8
    def sort_array(self, data: np.array, limit: float = 25, limit_type: str = "number", total: Optional[float] = None) -> np.array:
1✔
9
        """Activity Browser version of bw2analyzer.ContributionAnalysis.sort_array.
10

11
        Should be removed once https://github.com/brightway-lca/brightway2-analyzer/pull/32 is merged.
12
        See PR above on why we overwrite this function.
13
        """
14
        if not total:
×
15
            total = np.abs(data).sum()
×
16

17
        if total == 0 and limit_type == "cum_percent":
×
NEW
18
            raise ValueError(
×
19
                "Cumulative percentage cannot be calculated to a total of 0, use a different limit type or total")
20

21
        if limit_type not in ("number", "percent", "cum_percent"):
×
22
            raise ValueError(f"limit_type must be either 'number', 'percent' or 'cum_percent' not '{limit_type}'.")
×
NEW
23
        if limit_type in ("percent", "cum_percent"):
×
24
            if not 0 < limit <= 1:
×
25
                raise ValueError("Percentage limits > 0 and <= 1.")
×
NEW
26
        if limit_type == "number":
×
NEW
27
            if not int(limit) == limit:
×
NEW
28
                raise ValueError("Number limit must a whole number.")
×
NEW
29
            if not 0 < limit:
×
NEW
30
                raise ValueError("Number limit must be < 0.")
×
31

32
        results = np.hstack(
×
33
            (data.reshape((-1, 1)), np.arange(data.shape[0]).reshape((-1, 1)))
34
        )
35

36
        if limit_type == "number":
×
37
            # sort and cut off at limit
38
            return results[np.argsort(np.abs(data))[::-1]][:limit, :]
×
39
        elif limit_type == "percent":
×
40
            # identify good values, drop rest and sort
41
            limit = (np.abs(data) >= (abs(total) * limit))
×
42
            results = results[limit, :]
×
43
            return results[np.argsort(np.abs(results[:, 0]))[::-1]]
×
NEW
44
        elif limit_type == "cum_percent":
×
45
            # if we would apply this on the 'correct' order, this would stop just before the limit,
46
            # we want to be on or the first step over the limit.
47
            results = results[np.argsort(np.abs(data))]  # sort low to high impact
×
48
            cumsum = np.cumsum(np.abs(results[:, 0])) / abs(total)
×
49
            limit = (cumsum >= (1 - limit))  # find items under limit
×
50
            return results[limit, :][::-1]  # drop items under limit and set correct order
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc