def compute(self, **kwargs):
        """
        Calculate the average costs for a district or the overall score for a plan.

        The score for a district is the average cost between all sections within a district.

        The score for a plan is a normalized sum of costs for districts within the plan.

        @keyword district: A L{District} whose cost ratio should be calculated

        @keyword plan: A L{Plan} whose total cost ratio should be calculated

        @keyword version: Optional. The version of the plan, defaults to 
        the most recent version.

        @keyword host: Optional. The host to connect to redis, defaults to value in settings.

        @keyword port: Optional. The port to connect to redis, defaults to value in settings.

        @keyword redis_db: Optional. The redis database number to connect to, defaults to value in settings.
        """
        # Get Redis Connection - might as well re-use it
        redis_host = kwargs.get('host', redis_settings['HOST'])
        redis_port = int(kwargs.get('port', redis_settings['PORT']))
        redis_db = int(kwargs.get('db', redis_settings['DB']))
        self.r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_db)
        
        districts = []
        if 'district' in kwargs:
            districts = [kwargs['district']]
            if districts[0].geom.empty:
                return

        elif 'plan' in kwargs:
            plan = kwargs['plan']
            version = kwargs['version'] if 'version' in kwargs else plan.version
            districts = plan.get_districts_at_version(version)

        score = 0

        if len(districts) == 1:
            score = 0 if districts[0].district_id == 0 else self._district_calculator(districts[0])

        elif len(districts) > 1:
            district_scores = []
            for district in districts:
                if district.district_id == 0:
                    continue
                district_score = self._district_calculator(district)
                district_scores.append(district_score)

            region = districts[0].plan.legislative_body.region.name

            region_key = key_gen(**{'region': region})
            region_score = float(self.r.get(region_key))
            num_districts = len(district_scores)

            for district in district_scores:
                numerator = (district - region_score/num_districts)**2
                denominator = (region_score/num_districts)**2
                score += numerator/denominator

        self.result = { 'value': score }
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/calculators.py/Adjacency.compute/original