factor package

factor.models module

class factor.models.EmissionsModel(*args, **kwargs)

Bases: django.db.models.base.Model

Add emissions calculation behaviour to a model

See _emissions() for more detail

_emissions(skip_negative=False, emissions_without_weighting=None, **kwargs)

Calculate emissions across all input_sources, with overrides

Parameters

skip_negative (bool) – replace negative results with NaN

Returns

dict of emissions results

For each item in input_sources, this method adds a corresponding key to the results dictionary from the relevant _emissions_SOURCE property, e.g.

>>> class Foo(EmissionsModel):
>>>     input_sources = ['foo',]
>>>     @property
>>>     def _emissions_foo(self):
>>>         return quantity('kg CO2e', Decimal('50.0'))
>>> f = Foo()
>>> f._emissions()
{'foo': <Quantity: 50.000 kg CO2e>}

Items in kwargs which exist in the input_sources list are assumed to have their own _emissions_total property (or be an iterable where each item has one) following the above example:

>>> class Bar(object):
>>>     emissions_total = quantity('kg CO2e', 5)
>>>
>>> a = Bar(); b = Bar(); c= Bar()
>>> f._emissions(foo=[a, b, c])  # sum emissions from `a`, `b`, and `c`
{'foo': <Quantity: 15.000 kg CO2e>}
>>> f._emissions(foo=a)  # just use a.emissions_total
{'foo': <Quantity: 5.000 kg CO2e>}

This is used e.g. for temporarily over-riding farm.models.Product.direct_energy when calculating live results.

If a kwarg item matches the name of an _emissions_SOURCE property on self, it is assumed to be a quantity.Quantity object, and is then used directly – instead of that property – in the result:

>>> f._emissions(_emissions_foo=quantity('kg CO2e', Decimal('100.0'))
{'foo': <Quantity: 100.000 kg CO2e>}

CropProductTreatmentView uses this version to pre-calculate fertiliser, management and paddy field emissions for live updates.

property emissions_percentage

Break-down of CO2e emissions as a percentage of total

Returns

dict

property emissions_total

Total CO2e emissions across all input_sources

Returns

quantity.Quantity

property emissions_values

CO2e emissions as values rather than quantity.Quantity

Returns

dict

class factor.models.Factor(*args, **kwargs)

Bases: django.db.models.base.Model

Generic emissions factor

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

factor_for_product(product)

Get the most specific RegionFactor available for a farm.Farm

factor_version(product=None, dataset=None)

Return the factor version associated to the product’s dataset if one is set up, otherwise it returns the factor version form the default dataset

Returns

factor.FactorVersion

get_region_grid_factor(product, target)

For a given product and region (country, continent or territory), return the RegionFactor with same region and same year as product reporting year. If none, return the most recent one for the region from the previous 5 years. If none, return the most recent World factor.

property valid_units

List units convertible to the numerator of impact.unit

class factor.models.FactorCategory(id, name)

Bases: django.db.models.base.Model

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

class factor.models.FactorVersion(id, creation_date, last_updated, factor, version, impact_value, impact_unit)

Bases: coolfarmtool.mixins.LastUpdatedMixin

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

class factor.models.RegionFactor(*args, **kwargs)

Bases: django.db.models.base.Model

farm.Continent-, locality.Country- or locality.Territory-specific emissions factor

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

factor.models.sum_emissions_list(l, k=False, canonical=True, unit=False)

Safely sum emissions from objects in a list (or other iterable)

“NaN” results from any items in l are skipped, but this method will return a “NaN” if the overall result is 0.

FIXME make consistent with expected NaN behaviour

Parameters
  • l (list etc.) – iterable to sum

  • k (str) – property to access on each item (expected to return a quantity.Quantity object) – emissions_total by default

  • canonical (bool, str) – convert results before summing (if not True, all items will be assumed to be in convertible units, and the overall total will have the units of the first item in the list, or the value of canonical itself if the total is 0)

  • unit (quantity.Unit) – name of a unit to use if all totals are 0

Returns

quantity.Quantity

factor.views module

class factor.views.FactorDetailView(**kwargs)

Bases: farm.views.FarmMixin, farm.views.AjaxResponseMixin, braces.views._ajax.JSONResponseMixin, django.views.generic.detail.DetailView

model

alias of factor.models.Factor