<commit_before>import django_oauth_consumer
from django_oauth_consumer import NoAccessToken

from django.conf import settings
from django.utils import simplejson as json

CONSUMER_KEY = settings.CONSUMER_KEY
CONSUMER_SECRET = settings.CONSUMER_SECRET

NAME = 'EmOAuth'

SERVER = settings.EMOCRACY_API_SERVER
API_SERVER = SERVER + "api/"

REQUEST_TOKEN_URL = '%soauth/request_token/' % SERVER
ACCESS_TOKEN_URL = '%soauth/access_token/' % SERVER
AUTHORIZATION_URL = '%soauth/authorize/' % SERVER
REALM = 'emo.buhrer.net'
SIGNATURE_METHOD = 'oauth.signature_method.plaintext.OAuthSignatureMethod_PLAINTEXT'

class EmoOAuthConsumerApp(django_oauth_consumer.OAuthConsumerApp):

    def  __init__(self,
        name=NAME,
        consumer_key=CONSUMER_KEY,
        consumer_secret=CONSUMER_SECRET,
        request_token_url=REQUEST_TOKEN_URL,
        authorization_url=AUTHORIZATION_URL,
        access_token_url=ACCESS_TOKEN_URL,
        realm=REALM,
        signature_method=SIGNATURE_METHOD):

        super(EmoOAuthConsumerApp, self).__init__(
        name=name,
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        request_token_url=request_token_url,
        authorization_url=authorization_url,
        access_token_url=access_token_url,
        realm=realm,
        signature_method=signature_method)

        self.response = None

    def store_access_token(self, request, token):
        if not request.user.is_anonymous():
            profile = request.user.get_profile()
            profile.access_token = token['oauth_token']
            profile.access_secret = token['oauth_token_secret']
            profile.save()
        else:
            request.session[self.ACCESS_TOKEN_NAME] = token

    def get_access_token(self, request):
        if not request.user.is_anonymous():
            profile = request.user.get_profile()
            if not profile.access_token:
                raise NoAccessToken()
            else:
                token = {}
                token['oauth_token'] = profile.access_token
                token['oauth_token_secret'] = profile.access_secret
                return token
        else:
            if self.ACCESS_TOKEN_NAME in request.session:
                return request.session[self.ACCESS_TOKEN_NAME]
            else:
                raise NoAccessToken()

    def get_resource(self, request, resource):
        try:
            token = self.get_access_token(request)
            return self.make_signed_req(url=resource, token=token, request=request)
        except NoAccessToken:
            return self.make_signed_req(url=resource)

    def ld(self):
        """Load data - parse json from last request"""
        data = self.response.read()
        return json.loads(data)

    def post_resource(self, request, resource, content=None):
        if resource==None:
            return false
        try:
            token = self.get_access_token(request)
            return self.make_signed_req(url=resource, method='POST', content=content, token=token, request=request)
        except NoAccessToken:
            raise

    def post_vote(self, request, vote_data):
        """
        Posts a vote to Emocracy Service Provider.

        `vote_data` should be a dictionary:
            { 'issue' : integer,
              'keep_private' : boolean,
              'vote' : integer,  # valid values -1, 1, 10-19 (see emocracy docs)
            }
        """
        api_url = API_SERVER+'votes/'
        self.response = self.post_resource(request, api_url, content=vote_data)
        return self.response.status

    def get_issue_list(self, request):
        api_url = API_SERVER+'issues/'
        self.response = self.get_resource(request, api_url)
        return self.ld()

    def get_issue(self, request, issue_no):
        api_url = API_SERVER+'issues/'+issue_no+'/'
        self.response = self.get_resource(request, api_url)
        return self.ld()

    def post_issue(self, request, issue_data):
        api_url = API_SERVER+'issues/'
        self.response = self.post_resource(request, api_url, content=issue_data)
        return self.response.status
<commit_msg>moved REALM to settings_local.py<commit_after>import django_oauth_consumer
from django_oauth_consumer import NoAccessToken

from django.conf import settings
from django.utils import simplejson as json

CONSUMER_KEY = settings.CONSUMER_KEY
CONSUMER_SECRET = settings.CONSUMER_SECRET

NAME = 'EmOAuth'

SERVER = settings.EMOCRACY_API_SERVER
API_SERVER = SERVER + "api/"

REQUEST_TOKEN_URL = '%soauth/request_token/' % SERVER
ACCESS_TOKEN_URL = '%soauth/access_token/' % SERVER
AUTHORIZATION_URL = '%soauth/authorize/' % SERVER
REALM = settings.REALM
SIGNATURE_METHOD = 'oauth.signature_method.plaintext.OAuthSignatureMethod_PLAINTEXT'

class EmoOAuthConsumerApp(django_oauth_consumer.OAuthConsumerApp):

    def  __init__(self,
        name=NAME,
        consumer_key=CONSUMER_KEY,
        consumer_secret=CONSUMER_SECRET,
        request_token_url=REQUEST_TOKEN_URL,
        authorization_url=AUTHORIZATION_URL,
        access_token_url=ACCESS_TOKEN_URL,
        realm=REALM,
        signature_method=SIGNATURE_METHOD):

        super(EmoOAuthConsumerApp, self).__init__(
        name=name,
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        request_token_url=request_token_url,
        authorization_url=authorization_url,
        access_token_url=access_token_url,
        realm=realm,
        signature_method=signature_method)

        self.response = None

    def store_access_token(self, request, token):
        if not request.user.is_anonymous():
            profile = request.user.get_profile()
            profile.access_token = token['oauth_token']
            profile.access_secret = token['oauth_token_secret']
            profile.save()
        else:
            request.session[self.ACCESS_TOKEN_NAME] = token

    def get_access_token(self, request):
        if not request.user.is_anonymous():
            profile = request.user.get_profile()
            if not profile.access_token:
                raise NoAccessToken()
            else:
                token = {}
                token['oauth_token'] = profile.access_token
                token['oauth_token_secret'] = profile.access_secret
                return token
        else:
            if self.ACCESS_TOKEN_NAME in request.session:
                return request.session[self.ACCESS_TOKEN_NAME]
            else:
                raise NoAccessToken()

    def get_resource(self, request, resource):
        try:
            token = self.get_access_token(request)
            return self.make_signed_req(url=resource, token=token, request=request)
        except NoAccessToken:
            return self.make_signed_req(url=resource)

    def ld(self):
        """Load data - parse json from last request"""
        data = self.response.read()
        return json.loads(data)

    def post_resource(self, request, resource, content=None):
        if resource==None:
            return false
        try:
            token = self.get_access_token(request)
            return self.make_signed_req(url=resource, method='POST', content=content, token=token, request=request)
        except NoAccessToken:
            raise

    def post_vote(self, request, vote_data):
        """
        Posts a vote to Emocracy Service Provider.

        `vote_data` should be a dictionary:
            { 'issue' : integer,
              'keep_private' : boolean,
              'vote' : integer,  # valid values -1, 1, 10-19 (see emocracy docs)
            }
        """
        api_url = API_SERVER+'votes/'
        self.response = self.post_resource(request, api_url, content=vote_data)
        return self.response.status

    def get_issue_list(self, request):
        api_url = API_SERVER+'issues/'
        self.response = self.get_resource(request, api_url)
        return self.ld()

    def get_issue(self, request, issue_no):
        api_url = API_SERVER+'issues/'+issue_no+'/'
        self.response = self.get_resource(request, api_url)
        return self.ld()

    def post_issue(self, request, issue_data):
        api_url = API_SERVER+'issues/'
        self.response = self.post_resource(request, api_url, content=issue_data)
        return self.response.status
<|endoftext|>


<commit_before>import pyfftw
import numpy as np
import tomviz.operators

class ReconConstrintedDFMOperator(tomviz.operators.CancelableOperator):

    def transform_scalars(self, dataset, Niter=None, Niter_update_support=None,
                          supportSigma=None, supportThreshold=None):
        """
        3D Reconstruct from a tilt series using constraint-based Direct Fourier
        Method
        """
        self.progress.maximum = 1

        from tomviz import utils
        import numpy as np

        supportThreshold = supportThreshold / 100.0

        nonnegativeVoxels = True
        tiltAngles = utils.get_tilt_angles(dataset) #Get Tilt angles

        tiltSeries = utils.get_array(dataset)
        if tiltSeries is None:
            raise RuntimeError("No scalars found!")

        self.progress.message = 'Initialization'
        #Direct Fourier recon without constraints
        (recon, recon_F) \
            = dfm3(tiltSeries, tiltAngles, np.size(tiltSeries, 0) * 2)

        kr_cutoffs = np.linspace(0.05, 0.5, 10)
        #average Fourier magnitude of tilt series as a function of kr
        I_data = radial_average(tiltSeries, kr_cutoffs)

        (Nx, Ny, Nz) = recon_F.shape
        #Note: Nz = np.int(Ny/2+1)
        Ntot = Nx * Ny * Ny
        f = pyfftw.n_byte_align_empty((Nx, Ny, Nz), 16, dtype='complex128')
        r = pyfftw.n_byte_align_empty((Nx, Ny, Ny), 16, dtype='float64')
        fft_forward = pyfftw.FFTW(r, f, axes=(0, 1, 2))
        fft_inverse = pyfftw.FFTW(
            f, r, direction='FFTW_BACKWARD', axes=(0, 1, 2))

        kx = np.fft.fftfreq(Nx)
        ky = np.fft.fftfreq(Ny)
        kz = ky[0:Nz]

        kX, kY, kZ = np.meshgrid(ky, kx, kz)
        kR = np.sqrt(kY**2 + kX**2 + kZ**2)

        sigma = 0.5 * supportSigma
        G = np.exp(-kR**2 / (2 * sigma**2))

        #create initial support using sw
        f = recon_F * G
        fft_inverse.update_arrays(f, r)
        fft_inverse.execute()
        cutoff = np.amax(r) * supportThreshold
        support = r >= cutoff

        recon_F[kR > kr_cutoffs[-1]] = 0

        x = np.random.rand(Nx, Ny, Ny) #initial solution

        self.progress.maximum = Niter
        step = 0

        for i in range(Niter):
            if self.canceled:
                return
            self.progress.message = 'Iteration No.%d/%d' % (i + 1, Niter)

            #image space projection
            y1 = x.copy()

            if nonnegativeVoxels:
                y1[y1 < 0] = 0  #non-negative constraint

            y1[np.logical_not(support)] = 0 #support constraint

            #Fourier space projection
            y2 = 2 * y1 - x

            r = y2.copy()
            fft_forward.update_arrays(r, f)
            fft_forward.execute()

            f[kR > kr_cutoffs[-1]] = 0 #apply low pass filter
            f[recon_F != 0] = recon_F[recon_F != 0] #data constraint

            #Fourier magnitude constraint
            #leave the inner shell unchanged
            for j in range(1, kr_cutoffs.size):
                shell = np.logical_and(
                    kR > kr_cutoffs[j - 1], kR <= kr_cutoffs[j])
                shell[recon_F != 0] = False
                I = np.sum(np.absolute(f[shell]))
                if I != 0:
                    I = I / np.sum(shell)
                    # lower magnitude for high frequency information to reduce
                    # artifacts
                    f[shell] = f[shell] / I * I_data[j] * 0.5

            fft_inverse.update_arrays(f, r)
            fft_inverse.execute()
            y2 = r.copy() / Ntot

            #update
            x = x + y2 - y1

            #update support
            if (i < Niter and np.mod(i, Niter_update_support) == 0):
                print("updating support")
                recon = (y2 + y1) / 2
                r = recon.copy()
                fft_forward.update_arrays(r, f)
                fft_forward.execute()
                f = f * G
                fft_inverse.update_arrays(f, r)
                fft_inverse.execute()
                cutoff = np.amax(r) * supportThreshold
                support = r >= cutoff
            step += 1
            self.progress.value = step

        recon = (y2 + y1) / 2
        recon = np.fft.fftshift(recon)

        print('Reconsruction Complete')

        # Set the result as the new scalars.
        utils.set_array(dataset, recon)

        # Mark dataset as volume
        utils.mark_as_volume(dataset)

def dfm3(input, angles, Npad):
    input = np.double(input)
    (Nx, Ny, Nproj) = input.shape
    angles = np.double(angles)
    pad_pre = int(np.ceil((Npad - Ny) / 2.0))
    pad_post = int(np.floor((Npad - Ny) / 2.0))

    # Initialization
    Nz = Ny // 2 + 1
    w = np.zeros((Nx, Ny, Nz)) #store weighting factors
    v = pyfftw.n_byte_align_empty((Nx, Ny, Nz), 16, dtype='complex128')
    v = np.zeros(v.shape) + 1j * np.zeros(v.shape)
    recon = pyfftw.n_byte_align_empty((Nx, Ny, Ny), 16, dtype='float64')
    recon_fftw_object = pyfftw.FFTW(
        v, recon, direction='FFTW_BACKWARD', axes=(0, 1, 2))

    p = pyfftw.n_byte_align_empty((Nx, Npad), 16, dtype='float64')
    pF = pyfftw.n_byte_align_empty((Nx, Npad // 2 + 1), 16, dtype='complex128')
    p_fftw_object = pyfftw.FFTW(p, pF, axes=(0, 1))

    dk = np.double(Ny) / np.double(Npad)

    for a in range(0, Nproj):
        ang = angles[a] * np.pi / 180
        projection = input[:, :, a] #2D projection image
        p = np.lib.pad(projection, ((0, 0), (pad_pre, pad_post)),
                       'constant', constant_values=(0, 0)) #pad zeros
        p = np.fft.ifftshift(p)
        p_fftw_object.update_arrays(p, pF)
        p_fftw_object()

        probjection_f = pF.copy()
        if ang < 0:
            probjection_f = np.conj(pF.copy())
            probjection_f[1:, :] = np.flipud(probjection_f[1:, :])
            ang = np.pi + ang

        # Bilinear extrapolation
        for i in range(0, np.int(np.ceil(Npad / 2)) + 1):
            ky = i * dk
            #kz = 0
            ky_new = np.cos(ang) * ky #new coord. after rotation
            kz_new = np.sin(ang) * ky
            sy = abs(np.floor(ky_new) - ky_new) #calculate weights
            sz = abs(np.floor(kz_new) - kz_new)
            for b in range(1, 5): #bilinear extrapolation
                pz, py, weight = bilinear(kz_new, ky_new, sz, sy, Ny, b)
                if (py >= 0 and py < Ny and pz >= 0 and pz < Nz / 2 + 1):
                    w[:, py, pz] = w[:, py, pz] + weight
                    v[:, py, pz] = v[:, py, pz] + weight * probjection_f[:, i]

    v[w != 0] = v[w != 0] / w[w != 0]
    recon_F = v.copy()
    recon_fftw_object.update_arrays(v, recon)
    recon_fftw_object()
    recon = np.fft.fftshift(recon)
    return (recon, recon_F)

# Bilinear extrapolation

def bilinear(kz_new, ky_new, sz, sy, N, p):
    if p == 1:
        py = np.floor(ky_new)
        pz = np.floor(kz_new)
        weight = (1 - sy) * (1 - sz)
    elif p == 2:
        py = np.ceil(ky_new)
        pz = np.floor(kz_new)
        weight = sy * (1 - sz)
    elif p == 3:
        py = np.floor(ky_new)
        pz = np.ceil(kz_new)
        weight = (1 - sy) * sz
    elif p == 4:
        py = np.ceil(ky_new)
        pz = np.ceil(kz_new)
        weight = sy * sz
    if py < 0:
        py = N + py
    else:
        py = py
    return (pz, py, weight)

def radial_average(tiltseries, kr_cutoffs):
    (Nx, Ny, Nproj) = tiltseries.shape

    f = pyfftw.n_byte_align_empty((Nx, Ny // 2 + 1), 16, dtype='complex128')
    r = pyfftw.n_byte_align_empty((Nx, Ny), 16, dtype='float64')
    p_fftw_object = pyfftw.FFTW(r, f, axes=(0, 1))
    Ir = np.zeros(kr_cutoffs.size)
    I = np.zeros(kr_cutoffs.size)

    kx = np.fft.fftfreq(Nx)
    ky = np.fft.fftfreq(Ny)
    ky = ky[0:int(np.ceil(Ny / 2) + 1)]

    kX, kY = np.meshgrid(ky, kx)
    kR = np.sqrt(kY**2 + kX**2)

    for a in range(0, Nproj):
        r = tiltseries[:, :, a].copy().astype('float64')
        p_fftw_object.update_arrays(r, f)
        p_fftw_object.execute()
        shell = kR <= kr_cutoffs[0]
        I[0] = np.sum(np.absolute(f[shell]))
        I[0] = I[0] / np.sum(shell)
        for j in range(1, kr_cutoffs.size):
            shell = np.logical_and(kR > kr_cutoffs[j - 1], kR <= kr_cutoffs[j])
            I[j] = np.sum(np.absolute(f[shell]))
            I[j] = I[j] / np.sum(shell)
        Ir = Ir + I
    Ir = Ir / Nproj
    return Ir
<commit_msg>Ensure array indexes are ints<commit_after>import pyfftw
import numpy as np
import tomviz.operators

class ReconConstrintedDFMOperator(tomviz.operators.CancelableOperator):

    def transform_scalars(self, dataset, Niter=None, Niter_update_support=None,
                          supportSigma=None, supportThreshold=None):
        """
        3D Reconstruct from a tilt series using constraint-based Direct Fourier
        Method
        """
        self.progress.maximum = 1

        from tomviz import utils
        import numpy as np

        supportThreshold = supportThreshold / 100.0

        nonnegativeVoxels = True
        tiltAngles = utils.get_tilt_angles(dataset) #Get Tilt angles

        tiltSeries = utils.get_array(dataset)
        if tiltSeries is None:
            raise RuntimeError("No scalars found!")

        self.progress.message = 'Initialization'
        #Direct Fourier recon without constraints
        (recon, recon_F) \
            = dfm3(tiltSeries, tiltAngles, np.size(tiltSeries, 0) * 2)

        kr_cutoffs = np.linspace(0.05, 0.5, 10)
        #average Fourier magnitude of tilt series as a function of kr
        I_data = radial_average(tiltSeries, kr_cutoffs)

        (Nx, Ny, Nz) = recon_F.shape
        #Note: Nz = np.int(Ny/2+1)
        Ntot = Nx * Ny * Ny
        f = pyfftw.n_byte_align_empty((Nx, Ny, Nz), 16, dtype='complex128')
        r = pyfftw.n_byte_align_empty((Nx, Ny, Ny), 16, dtype='float64')
        fft_forward = pyfftw.FFTW(r, f, axes=(0, 1, 2))
        fft_inverse = pyfftw.FFTW(
            f, r, direction='FFTW_BACKWARD', axes=(0, 1, 2))

        kx = np.fft.fftfreq(Nx)
        ky = np.fft.fftfreq(Ny)
        kz = ky[0:Nz]

        kX, kY, kZ = np.meshgrid(ky, kx, kz)
        kR = np.sqrt(kY**2 + kX**2 + kZ**2)

        sigma = 0.5 * supportSigma
        G = np.exp(-kR**2 / (2 * sigma**2))

        #create initial support using sw
        f = recon_F * G
        fft_inverse.update_arrays(f, r)
        fft_inverse.execute()
        cutoff = np.amax(r) * supportThreshold
        support = r >= cutoff

        recon_F[kR > kr_cutoffs[-1]] = 0

        x = np.random.rand(Nx, Ny, Ny) #initial solution

        self.progress.maximum = Niter
        step = 0

        for i in range(Niter):
            if self.canceled:
                return
            self.progress.message = 'Iteration No.%d/%d' % (i + 1, Niter)

            #image space projection
            y1 = x.copy()

            if nonnegativeVoxels:
                y1[y1 < 0] = 0  #non-negative constraint

            y1[np.logical_not(support)] = 0 #support constraint

            #Fourier space projection
            y2 = 2 * y1 - x

            r = y2.copy()
            fft_forward.update_arrays(r, f)
            fft_forward.execute()

            f[kR > kr_cutoffs[-1]] = 0 #apply low pass filter
            f[recon_F != 0] = recon_F[recon_F != 0] #data constraint

            #Fourier magnitude constraint
            #leave the inner shell unchanged
            for j in range(1, kr_cutoffs.size):
                shell = np.logical_and(
                    kR > kr_cutoffs[j - 1], kR <= kr_cutoffs[j])
                shell[recon_F != 0] = False
                I = np.sum(np.absolute(f[shell]))
                if I != 0:
                    I = I / np.sum(shell)
                    # lower magnitude for high frequency information to reduce
                    # artifacts
                    f[shell] = f[shell] / I * I_data[j] * 0.5

            fft_inverse.update_arrays(f, r)
            fft_inverse.execute()
            y2 = r.copy() / Ntot

            #update
            x = x + y2 - y1

            #update support
            if (i < Niter and np.mod(i, Niter_update_support) == 0):
                print("updating support")
                recon = (y2 + y1) / 2
                r = recon.copy()
                fft_forward.update_arrays(r, f)
                fft_forward.execute()
                f = f * G
                fft_inverse.update_arrays(f, r)
                fft_inverse.execute()
                cutoff = np.amax(r) * supportThreshold
                support = r >= cutoff
            step += 1
            self.progress.value = step

        recon = (y2 + y1) / 2
        recon = np.fft.fftshift(recon)

        print('Reconsruction Complete')

        # Set the result as the new scalars.
        utils.set_array(dataset, recon)

        # Mark dataset as volume
        utils.mark_as_volume(dataset)

def dfm3(input, angles, Npad):
    input = np.double(input)
    (Nx, Ny, Nproj) = input.shape
    angles = np.double(angles)
    pad_pre = int(np.ceil((Npad - Ny) / 2.0))
    pad_post = int(np.floor((Npad - Ny) / 2.0))

    # Initialization
    Nz = Ny // 2 + 1
    w = np.zeros((Nx, Ny, Nz)) #store weighting factors
    v = pyfftw.n_byte_align_empty((Nx, Ny, Nz), 16, dtype='complex128')
    v = np.zeros(v.shape) + 1j * np.zeros(v.shape)
    recon = pyfftw.n_byte_align_empty((Nx, Ny, Ny), 16, dtype='float64')
    recon_fftw_object = pyfftw.FFTW(
        v, recon, direction='FFTW_BACKWARD', axes=(0, 1, 2))

    p = pyfftw.n_byte_align_empty((Nx, Npad), 16, dtype='float64')
    pF = pyfftw.n_byte_align_empty((Nx, Npad // 2 + 1), 16, dtype='complex128')
    p_fftw_object = pyfftw.FFTW(p, pF, axes=(0, 1))

    dk = np.double(Ny) / np.double(Npad)

    for a in range(0, Nproj):
        ang = angles[a] * np.pi / 180
        projection = input[:, :, a] #2D projection image
        p = np.lib.pad(projection, ((0, 0), (pad_pre, pad_post)),
                       'constant', constant_values=(0, 0)) #pad zeros
        p = np.fft.ifftshift(p)
        p_fftw_object.update_arrays(p, pF)
        p_fftw_object()

        probjection_f = pF.copy()
        if ang < 0:
            probjection_f = np.conj(pF.copy())
            probjection_f[1:, :] = np.flipud(probjection_f[1:, :])
            ang = np.pi + ang

        # Bilinear extrapolation
        for i in range(0, np.int(np.ceil(Npad / 2)) + 1):
            ky = i * dk
            #kz = 0
            ky_new = np.cos(ang) * ky #new coord. after rotation
            kz_new = np.sin(ang) * ky
            sy = abs(np.floor(ky_new) - ky_new) #calculate weights
            sz = abs(np.floor(kz_new) - kz_new)
            for b in range(1, 5): #bilinear extrapolation
                pz, py, weight = bilinear(kz_new, ky_new, sz, sy, Ny, b)
                if (py >= 0 and py < Ny and pz >= 0 and pz < Nz / 2 + 1):
                    w[:, py, pz] = w[:, py, pz] + weight
                    v[:, py, pz] = v[:, py, pz] + weight * probjection_f[:, i]

    v[w != 0] = v[w != 0] / w[w != 0]
    recon_F = v.copy()
    recon_fftw_object.update_arrays(v, recon)
    recon_fftw_object()
    recon = np.fft.fftshift(recon)
    return (recon, recon_F)

# Bilinear extrapolation

def bilinear(kz_new, ky_new, sz, sy, N, p):
    if p == 1:
        py = np.floor(ky_new)
        pz = np.floor(kz_new)
        weight = (1 - sy) * (1 - sz)
    elif p == 2:
        py = np.ceil(ky_new)
        pz = np.floor(kz_new)
        weight = sy * (1 - sz)
    elif p == 3:
        py = np.floor(ky_new)
        pz = np.ceil(kz_new)
        weight = (1 - sy) * sz
    elif p == 4:
        py = np.ceil(ky_new)
        pz = np.ceil(kz_new)
        weight = sy * sz
    if py < 0:
        py = N + py
    else:
        py = py
    return (int(pz), int(py), weight)

def radial_average(tiltseries, kr_cutoffs):
    (Nx, Ny, Nproj) = tiltseries.shape

    f = pyfftw.n_byte_align_empty((Nx, Ny // 2 + 1), 16, dtype='complex128')
    r = pyfftw.n_byte_align_empty((Nx, Ny), 16, dtype='float64')
    p_fftw_object = pyfftw.FFTW(r, f, axes=(0, 1))
    Ir = np.zeros(kr_cutoffs.size)
    I = np.zeros(kr_cutoffs.size)

    kx = np.fft.fftfreq(Nx)
    ky = np.fft.fftfreq(Ny)
    ky = ky[0:int(np.ceil(Ny / 2) + 1)]

    kX, kY = np.meshgrid(ky, kx)
    kR = np.sqrt(kY**2 + kX**2)

    for a in range(0, Nproj):
        r = tiltseries[:, :, a].copy().astype('float64')
        p_fftw_object.update_arrays(r, f)
        p_fftw_object.execute()
        shell = kR <= kr_cutoffs[0]
        I[0] = np.sum(np.absolute(f[shell]))
        I[0] = I[0] / np.sum(shell)
        for j in range(1, kr_cutoffs.size):
            shell = np.logical_and(kR > kr_cutoffs[j - 1], kR <= kr_cutoffs[j])
            I[j] = np.sum(np.absolute(f[shell]))
            I[j] = I[j] / np.sum(shell)
        Ir = Ir + I
    Ir = Ir / Nproj
    return Ir
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand
from django.contrib.gis.geos import Point
from django.contrib.gis.geos import GEOSGeometry

from addressbase.models import Address, Blacklist
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id,
        internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):

    def handle(self, *args, **kwargs):

        print("updating Westminster Address...")
        westminster = Council.objects.get(pk='E09000033')
        westminster.address = "Electoral Services\nWestminster City Council\n2nd Floor\nCity Hall\n5 Strand\nLondon"
        westminster.postcode = "WC2N 5HR"
        westminster.save()
        print("..updated")

        print("updating Southend email address...")
        southend = Council.objects.get(pk='E06000033')
        southend.email = 'elections@southend.gov.uk'
        southend.save()
        print("..updated")

        print("updating Hull phone number...")
        hull = Council.objects.get(pk='E06000010')
        hull.phone = '01482 300 302'
        hull.save()
        print("..updated")

        print("updating point for: Alf Marshall Community Centre, Hull...")
        update_station_point(
            'E06000010',
            '8515',
            Point(-0.321628, 53.787659, srid=4326))

        print("updating point for: All Saints Church Hall, Hastings...")
        update_station_point(
            'E07000062',
            '23-all-saints-church-hall',
            Point(0.595763, 50.859451, srid=4326))

        print("updating point for: Salvation Army Hall, South Cambs...")
        update_station_point(
            'E07000012',
            '6555',
            Point(0.191974, 52.264660, srid=4326))

        # bug report #5
        print("updating point for: St. Luke's Church Hall, Sefton...")
        update_station_point(
            'E08000014',
            '4529',
            Point(-2.985813, 53.646988, srid=4326))

        # 2x reports from Halton council
        print("updating point for: St Marys Halton Ce Primary School, Halton...")
        update_station_point(
            'E06000006',
            '308',
            Point(-2.690746, 53.331598, srid=4326))

        print("updating: St Edwards Parish Centre, Halton...")
        stations = PollingStation.objects.filter(
            council_id='E06000006',
            internal_council_id='283'
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-2.728014, 53.335534, srid=4326)
            station.postcode = 'WA7 5PB'
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report #6
        print("updating point for: Holy Trinity Church Centre, Sutton...")
        update_station_point(
            'E09000029',
            'OB_1',
            Point(-0.151435, 51.364474, srid=4326))

        # bug report #7
        print("updating: Berkley Street Methodist Church, Huntingdonshire...")
        stations = PollingStation.objects.filter(
            council_id='E07000011',
            internal_council_id='5860'
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-0.266772, 52.223385, srid=4326)
            station.postcode = 'PE19 2NB'
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # request from council
        print("updating: St George`s Church, Reading...")
        stations = PollingStation.objects.filter(
            council_id='E06000038',
            internal_council_id='2701'
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "St George`s Church\nSt George`s Road\nTilehurst\nReading\nRG30 2RG"
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report #8
        print("updating point for: Macaulay CE Primary School, Lambeth...")
        update_station_point(
            'E09000022',
            'VHC',
            Point(-0.148103, 51.463782, srid=4326))

        # bug report #9
        print("updating point for: METHODIST CHURCH HALL, Great Yarmouth...")
        update_station_point(
            'E07000145',
            '24-methodist-church-hall',
            Point(1.714309, 52.572677, srid=4326))

        # 2x reports from Islington council
        print("updating point for: St Joan of Arc Community Centre, Islington...")
        update_station_point(
            'E09000019',
            '1401',
            Point(-0.0966823, 51.5559102, srid=4326))

        print("updating: St. Thomas` Church Hall, Islington...")
        stations = PollingStation.objects.filter(
            council_id='E09000019',
            internal_council_id='1404'
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = "N4 2QP"
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report #12
        print("updating point for: St Mary and Ambrose Church Hall, Birmingham...")
        update_station_point(
            'E08000025',
            '122-st-mary-and-ambrose-church-hall',
            Point(-1.904365, 52.458623, srid=4326))

        print("updating point for: St Cuthbert`s Centre, Kensington & Chelsea...")
        update_station_point(
            'E09000020',
            '672',
            Point(-0.200082, 51.491363, srid=4326))

        # bug report #13
        print("updating point for: Summertown United Reformed Church, Oxford...")
        update_station_point(
            'E07000178',
            '4793',
            Point(-1.265459, 51.778845, srid=4326))

        # bug report #14
        print("updating point for: Cutteslowe Community Centre, Oxford...")
        update_station_point(
            'E07000178',
            '4796',
            Point(-1.260158, 51.786878, srid=4326))

        # bug report 15
        print("updating: GUIDE & SCOUT H.Q, North Tyeside...")
        stations = PollingStation.objects.filter(
            council_id='E08000022',
            internal_council_id='OE'
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = ''
            station.location = None
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print('Deleting data for council %s...' % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print('..deleted')

        print("..done")
<commit_msg>grid ref update<commit_after>from django.core.management.base import BaseCommand
from django.contrib.gis.geos import Point
from django.contrib.gis.geos import GEOSGeometry

from addressbase.models import Address, Blacklist
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id,
        internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):

    def handle(self, *args, **kwargs):

        print("updating Westminster Address...")
        westminster = Council.objects.get(pk='E09000033')
        westminster.address = "Electoral Services\nWestminster City Council\n2nd Floor\nCity Hall\n5 Strand\nLondon"
        westminster.postcode = "WC2N 5HR"
        westminster.save()
        print("..updated")

        print("updating Southend email address...")
        southend = Council.objects.get(pk='E06000033')
        southend.email = 'elections@southend.gov.uk'
        southend.save()
        print("..updated")

        print("updating Hull phone number...")
        hull = Council.objects.get(pk='E06000010')
        hull.phone = '01482 300 302'
        hull.save()
        print("..updated")

        print("updating point for: Alf Marshall Community Centre, Hull...")
        update_station_point(
            'E06000010',
            '8515',
            Point(-0.321628, 53.787659, srid=4326))

        print("updating point for: All Saints Church Hall, Hastings...")
        update_station_point(
            'E07000062',
            '23-all-saints-church-hall',
            Point(0.595763, 50.859451, srid=4326))

        print("updating point for: Salvation Army Hall, South Cambs...")
        update_station_point(
            'E07000012',
            '6555',
            Point(0.191974, 52.264660, srid=4326))

        # bug report #5
        print("updating point for: St. Luke's Church Hall, Sefton...")
        update_station_point(
            'E08000014',
            '4529',
            Point(-2.985813, 53.646988, srid=4326))

        # 2x reports from Halton council
        print("updating point for: St Marys Halton Ce Primary School, Halton...")
        update_station_point(
            'E06000006',
            '308',
            Point(-2.690746, 53.331598, srid=4326))

        print("updating: St Edwards Parish Centre, Halton...")
        stations = PollingStation.objects.filter(
            council_id='E06000006',
            internal_council_id='283'
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-2.728014, 53.335534, srid=4326)
            station.postcode = 'WA7 5PB'
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report #6
        print("updating point for: Holy Trinity Church Centre, Sutton...")
        update_station_point(
            'E09000029',
            'OB_1',
            Point(-0.151435, 51.364474, srid=4326))

        # bug report #7
        print("updating: Berkley Street Methodist Church, Huntingdonshire...")
        stations = PollingStation.objects.filter(
            council_id='E07000011',
            internal_council_id='5860'
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-0.266772, 52.223385, srid=4326)
            station.postcode = 'PE19 2NB'
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # request from council
        print("updating: St George`s Church, Reading...")
        stations = PollingStation.objects.filter(
            council_id='E06000038',
            internal_council_id='2701'
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "St George`s Church\nSt George`s Road\nTilehurst\nReading\nRG30 2RG"
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report #8
        print("updating point for: Macaulay CE Primary School, Lambeth...")
        update_station_point(
            'E09000022',
            'VHC',
            Point(-0.148103, 51.463782, srid=4326))

        # bug report #9
        print("updating point for: METHODIST CHURCH HALL, Great Yarmouth...")
        update_station_point(
            'E07000145',
            '24-methodist-church-hall',
            Point(1.714309, 52.572677, srid=4326))

        # 2x reports from Islington council
        print("updating point for: St Joan of Arc Community Centre, Islington...")
        update_station_point(
            'E09000019',
            '1401',
            Point(-0.0966823, 51.5559102, srid=4326))

        print("updating: St. Thomas` Church Hall, Islington...")
        stations = PollingStation.objects.filter(
            council_id='E09000019',
            internal_council_id='1404'
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = "N4 2QP"
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report #12
        print("updating point for: St Mary and Ambrose Church Hall, Birmingham...")
        update_station_point(
            'E08000025',
            '122-st-mary-and-ambrose-church-hall',
            Point(-1.904365, 52.458623, srid=4326))

        print("updating point for: St Cuthbert`s Centre, Kensington & Chelsea...")
        update_station_point(
            'E09000020',
            '672',
            Point(-0.200082, 51.491363, srid=4326))

        # bug report #13
        print("updating point for: Summertown United Reformed Church, Oxford...")
        update_station_point(
            'E07000178',
            '4793',
            Point(-1.265459, 51.778845, srid=4326))

        # bug report #14
        print("updating point for: Cutteslowe Community Centre, Oxford...")
        update_station_point(
            'E07000178',
            '4796',
            Point(-1.260158, 51.786878, srid=4326))

        # bug report 15
        print("updating: GUIDE & SCOUT H.Q, North Tyeside...")
        stations = PollingStation.objects.filter(
            council_id='E08000022',
            internal_council_id='OE'
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = ''
            station.location = None
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # bug report 16
        print("updating point for: Ellenborough Table Tennis Club, Enfield...")
        update_station_point(
            'E09000010',
            '5624',
            Point(-0.069540, 51.652642, srid=4326))

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print('Deleting data for council %s...' % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print('..deleted')

        print("..done")
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand

from django.contrib.gis.geos import Point
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council
from addressbase.models import Address, Blacklist

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id, internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        print("updating Watford address...")
        watford = Council.objects.get(pk="E07000103")
        watford.address = "Electoral Services Office\nRoom 22\nTown Hall\nWatford"
        watford.postcode = "WD17 3EX"
        watford.save()
        print("..updated")

        print("updating Harrogate address...")
        harrogate = Council.objects.get(pk="E07000165")
        harrogate.address = "Civic Centre\nSt Lukes Avenue\nHarrogate"
        harrogate.postcode = "HG1 2AE"
        harrogate.save()
        print("..updated")

        print("updating Oadby & Wigston name...")
        oadby = Council.objects.get(pk="E07000135")
        oadby.name = "Oadby & Wigston Borough Council"
        oadby.save()
        print("..updated")

        print("updating Swansea name...")
        swansea = Council.objects.get(pk="W06000011")
        swansea.name = "City & County of Swansea"
        swansea.save()
        print("..updated")

        print("updating Wyre Forest phone number")
        wyre_forest = Council.objects.get(pk="E07000239")
        wyre_forest.phone = "01562 732928 / 01562 732733"
        wyre_forest.save()
        print("..updated")

        print("updating Chelmsford phone number")
        chelmsford = Council.objects.get(pk="E07000070")
        chelmsford.phone = "01245 606449"
        chelmsford.save()
        print("..updated")

        print("removing bad points from AddressBase")
        bad_uprns = [
            "10023906550",
            "2630153843",
            "14059977",
            "10007921239",
            "10007920387",
            "10007920390",
            "10024288188",
            "10024288190",
            "10024288189",
            "10023117526",
            "10009922833",
            "10014047099",
            "28033460",
            "200004338783",
            "117134558",
            "138055880",
            "120036643",
            "120033242",
            "120033702",
            "120024279",
            "120047832",
            "120034278",
            "118191899",
            "118192006",
            "118190963",
            "118052842",
            "118126542",
            "118174746",
            "130075135",
            "130075151",
            "10091827317",
            "130142788",
            "130009944",
            "130137997",
            "130111463",
            "130038545",
            "130084948",
            "130059878",
            "130074399",
            "130074400",
            "130093312",
            "130148126",
            "130086434",
            "130148125",
            "130074398",
            "130105180",
            "130103488",
            "130127660",
            "130167898",
            "130077044",
            "130167897",
            "130077043",
            "130167902",
            "130148803",
            "130164482",
            "130088352",
            "130137432",
            "130168686",
            "130119378",
            "130148651",
            "130109313",
            "130145384",
            "130128377",
            "130144376",
            "130140027",
            "130144375",
            "130126560",
            "130144377",
            "130128375",
            "130128378",
            "130148146",
            "130113711",
            "130133033",
            "130128376",
            "130145383",
            "130113709",
            "130085106",
            "130116030",
            "130102440",
            "130136303",
            "130058258",
            "130058257",
            "130125391",
            "130166645",
            "130138981",
            "130168221",
            "130085141",
            "130099734",
            "130099735",
            "130136306",
            "130058259",
            "130151706",
            "130092504",
            "10091804094",
            "130058260",
            "130136304",
            "130136305",
            "130060270",
            "130180034",
            "130060273",
            "130168645",
            "130060274",
            "130060271",
            "130083808",
            "130060272",
            "130060275",
            "130168589",
            "130060268",
            "130061433",
            "130063761",
            "130147945",
            "130103286",
            "130104545",
            "130127885",
            "130103285",
            "130103287",
            "130075028",
            "130083586",
            "130149747",
            "130136566",
            "130086506",
            "130118347",
            "130143875",
            "130085874",
            "130066045",
            "130117060",
            "130135866",
            "130177987",
            "130073907",
            "130073905",
            "130073906",
            "130099688",
            "151651178",
        ]
        addresses = Address.objects.filter(pk__in=bad_uprns)
        for address in addresses:
            print(address.uprn)
            address.delete()
        print("..deleted")

        print("removing dodgy blacklist entries (result of bad point in AddressBase)..")
        blacklist = Blacklist.objects.filter(postcode="RH122LP")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        print(
            "removing dodgy blacklist entries (result of bad points in AddressBase).."
        )
        blacklist = Blacklist.objects.filter(postcode="S24AX")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print("Deleting data for council %s..." % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print("..deleted")

        print("..done")

        # User issue 140
        print("updating: Kilsby Village Hall (Kilsby Room)...")
        update_station_point(
            "E07000151", "6718", Point(-1.174253, 52.336931, srid=4326)
        )

        # User issue 141
        print("updating:  The Albert Underwood Room, St Peters Church...")
        stations = PollingStation.objects.filter(
            council_id="E07000152", internal_council_id="8545"
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = "NN9 5WB"
            station.location = Point(-0.61056, 52.32575, srid=4326)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 149
        print("updating: The Mount View Hotel...")
        update_station_point(
            "E06000052", "3241", Point(-5.499815, 50.129318, srid=4326)
        )

        # User issue 139
        print("updating:  LARGS ACADEMY,...")
        stations = PollingStation.objects.filter(
            council_id="S12000021", internal_council_id="N804"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Largs Campus Gymnasium\nAlexander Avenue\nLargs"
            station.postcode = "KA30 9EU"
            station.location = Point(-4.855903, 55.8003773, srid=4326)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 151
        print("updating: Holy Trinity Church Hall...")
        update_station_point(
            "E07000081", "2657", Point(-2.2127903, 51.8753716, srid=4326)
        )

        # User issue 153
        print("updating: NETHERWITTON VILLAGE HALL...")
        update_station_point(
            "E06000057", "B37HAR", Point(-1.845624, 55.206558, srid=4326)
        )
        update_station_point(
            "E06000057", "B39MEL", Point(-1.845624, 55.206558, srid=4326)
        )
        update_station_point(
            "E06000057", "B41NET", Point(-1.845624, 55.206558, srid=4326)
        )

        # User issue 154/5
        print("updating:  Bolton Road United Reformed Church...")
        update_station_point(
            "E06000008", "3077", Point(-2.4615102, 53.6832739, srid=4326)
        )

        # User issue 157
        print("updating:  County Children`s Centre...")
        update_station_point(
            "E08000012", "5909", Point(-2.968792, 53.442462, srid=4326)
        )

        # User issue 158
        print("updating:  Caythorpe and Frieston Village Hall...")
        update_station_point("E07000141", "4056", Point(-0.60214, 53.02241, srid=4326))

        # Lambeth Council update / User issue 156
        print("updating: Raleigh Park Centre...")
        stations = PollingStation.objects.filter(
            council_id="E09000022", internal_council_id="SOB"
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-0.1134, 51.452, srid=4326)
            station.address = "Jubilee Primary School & Children`s Centre\nTulse Hill"
            station.postcode = "SW2 2JE"
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 162
        print("updating:  Newport Pagnell Town Footbal Club...")
        update_station_point(
            "E06000042", "7370", Point(-0.721871, 52.077798, srid=4326)
        )
<commit_msg>2x Sheffield fixes: Walkley community hall and library<commit_after>from django.core.management.base import BaseCommand

from django.contrib.gis.geos import Point
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council
from addressbase.models import Address, Blacklist

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id, internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        print("updating Watford address...")
        watford = Council.objects.get(pk="E07000103")
        watford.address = "Electoral Services Office\nRoom 22\nTown Hall\nWatford"
        watford.postcode = "WD17 3EX"
        watford.save()
        print("..updated")

        print("updating Harrogate address...")
        harrogate = Council.objects.get(pk="E07000165")
        harrogate.address = "Civic Centre\nSt Lukes Avenue\nHarrogate"
        harrogate.postcode = "HG1 2AE"
        harrogate.save()
        print("..updated")

        print("updating Oadby & Wigston name...")
        oadby = Council.objects.get(pk="E07000135")
        oadby.name = "Oadby & Wigston Borough Council"
        oadby.save()
        print("..updated")

        print("updating Swansea name...")
        swansea = Council.objects.get(pk="W06000011")
        swansea.name = "City & County of Swansea"
        swansea.save()
        print("..updated")

        print("updating Wyre Forest phone number")
        wyre_forest = Council.objects.get(pk="E07000239")
        wyre_forest.phone = "01562 732928 / 01562 732733"
        wyre_forest.save()
        print("..updated")

        print("updating Chelmsford phone number")
        chelmsford = Council.objects.get(pk="E07000070")
        chelmsford.phone = "01245 606449"
        chelmsford.save()
        print("..updated")

        print("removing bad points from AddressBase")
        bad_uprns = [
            "10023906550",
            "2630153843",
            "14059977",
            "10007921239",
            "10007920387",
            "10007920390",
            "10024288188",
            "10024288190",
            "10024288189",
            "10023117526",
            "10009922833",
            "10014047099",
            "28033460",
            "200004338783",
            "117134558",
            "138055880",
            "120036643",
            "120033242",
            "120033702",
            "120024279",
            "120047832",
            "120034278",
            "118191899",
            "118192006",
            "118190963",
            "118052842",
            "118126542",
            "118174746",
            "130075135",
            "130075151",
            "10091827317",
            "130142788",
            "130009944",
            "130137997",
            "130111463",
            "130038545",
            "130084948",
            "130059878",
            "130074399",
            "130074400",
            "130093312",
            "130148126",
            "130086434",
            "130148125",
            "130074398",
            "130105180",
            "130103488",
            "130127660",
            "130167898",
            "130077044",
            "130167897",
            "130077043",
            "130167902",
            "130148803",
            "130164482",
            "130088352",
            "130137432",
            "130168686",
            "130119378",
            "130148651",
            "130109313",
            "130145384",
            "130128377",
            "130144376",
            "130140027",
            "130144375",
            "130126560",
            "130144377",
            "130128375",
            "130128378",
            "130148146",
            "130113711",
            "130133033",
            "130128376",
            "130145383",
            "130113709",
            "130085106",
            "130116030",
            "130102440",
            "130136303",
            "130058258",
            "130058257",
            "130125391",
            "130166645",
            "130138981",
            "130168221",
            "130085141",
            "130099734",
            "130099735",
            "130136306",
            "130058259",
            "130151706",
            "130092504",
            "10091804094",
            "130058260",
            "130136304",
            "130136305",
            "130060270",
            "130180034",
            "130060273",
            "130168645",
            "130060274",
            "130060271",
            "130083808",
            "130060272",
            "130060275",
            "130168589",
            "130060268",
            "130061433",
            "130063761",
            "130147945",
            "130103286",
            "130104545",
            "130127885",
            "130103285",
            "130103287",
            "130075028",
            "130083586",
            "130149747",
            "130136566",
            "130086506",
            "130118347",
            "130143875",
            "130085874",
            "130066045",
            "130117060",
            "130135866",
            "130177987",
            "130073907",
            "130073905",
            "130073906",
            "130099688",
            "151651178",
        ]
        addresses = Address.objects.filter(pk__in=bad_uprns)
        for address in addresses:
            print(address.uprn)
            address.delete()
        print("..deleted")

        print("removing dodgy blacklist entries (result of bad point in AddressBase)..")
        blacklist = Blacklist.objects.filter(postcode="RH122LP")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        print(
            "removing dodgy blacklist entries (result of bad points in AddressBase).."
        )
        blacklist = Blacklist.objects.filter(postcode="S24AX")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print("Deleting data for council %s..." % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print("..deleted")

        print("..done")

        # User issue 140
        print("updating: Kilsby Village Hall (Kilsby Room)...")
        update_station_point(
            "E07000151", "6718", Point(-1.174253, 52.336931, srid=4326)
        )

        # User issue 141
        print("updating:  The Albert Underwood Room, St Peters Church...")
        stations = PollingStation.objects.filter(
            council_id="E07000152", internal_council_id="8545"
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = "NN9 5WB"
            station.location = Point(-0.61056, 52.32575, srid=4326)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 149
        print("updating: The Mount View Hotel...")
        update_station_point(
            "E06000052", "3241", Point(-5.499815, 50.129318, srid=4326)
        )

        # User issue 139
        print("updating:  LARGS ACADEMY,...")
        stations = PollingStation.objects.filter(
            council_id="S12000021", internal_council_id="N804"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Largs Campus Gymnasium\nAlexander Avenue\nLargs"
            station.postcode = "KA30 9EU"
            station.location = Point(-4.855903, 55.8003773, srid=4326)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 151
        print("updating: Holy Trinity Church Hall...")
        update_station_point(
            "E07000081", "2657", Point(-2.2127903, 51.8753716, srid=4326)
        )

        # User issue 153
        print("updating: NETHERWITTON VILLAGE HALL...")
        update_station_point(
            "E06000057", "B37HAR", Point(-1.845624, 55.206558, srid=4326)
        )
        update_station_point(
            "E06000057", "B39MEL", Point(-1.845624, 55.206558, srid=4326)
        )
        update_station_point(
            "E06000057", "B41NET", Point(-1.845624, 55.206558, srid=4326)
        )

        # User issue 154/5
        print("updating:  Bolton Road United Reformed Church...")
        update_station_point(
            "E06000008", "3077", Point(-2.4615102, 53.6832739, srid=4326)
        )

        # User issue 157
        print("updating:  County Children`s Centre...")
        update_station_point(
            "E08000012", "5909", Point(-2.968792, 53.442462, srid=4326)
        )

        # User issue 158
        print("updating:  Caythorpe and Frieston Village Hall...")
        update_station_point("E07000141", "4056", Point(-0.60214, 53.02241, srid=4326))

        # Lambeth Council update / User issue 156
        print("updating: Raleigh Park Centre...")
        stations = PollingStation.objects.filter(
            council_id="E09000022", internal_council_id="SOB"
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-0.1134, 51.452, srid=4326)
            station.address = "Jubilee Primary School & Children`s Centre\nTulse Hill"
            station.postcode = "SW2 2JE"
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 162
        print("updating:  Newport Pagnell Town Footbal Club...")
        update_station_point(
            "E06000042", "7370", Point(-0.721871, 52.077798, srid=4326)
        )

        # User issue 163
        print("updating: Walkley Parish Community Hall...")
        stations = PollingStation.objects.filter(
            council_id="E08000019",
            internal_council_id="194-walkley-parish-community-hall",
        )
        if len(stations) == 1:
            station = stations[0]
            station.location = Point(-1.49913, 53.39145, srid=4326)
            station.address = " St. Mary’s Walkley Community Hall\nWalkley"
            station.postcode = ""
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 167
        print("updating: Walkley Library...")
        update_station_point(
            "E08000019", "198-walkley-library", Point(-1.502873, 53.394934, srid=4326)
        )
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand

# from django.contrib.gis.geos import Point
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council
from addressbase.models import Address, Blacklist

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id, internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        print("updating Wyre Forest email...")
        wyre_forest = Council.objects.get(pk="E07000239")
        wyre_forest.email = "customerservice@wyreforestdc.gov.uk"
        wyre_forest.save()
        print("..updated")

        print("updating Watford address...")
        watford = Council.objects.get(pk="E07000103")
        watford.address = "Electoral Services Office\nRoom 22\nTown Hall\nWatford"
        watford.postcode = "WD17 3EX"
        watford.save()
        print("..updated")

        print("removing bad points from AddressBase")
        bad_uprns = [
            "10023906550",
            "2630153843",
            "14059977",
            "10007921239",
            "10007920387",
            "10007920390",
            "10024288188",
            "10024288190",
            "10024288189",
            "10023117526",
        ]
        addresses = Address.objects.filter(pk__in=bad_uprns)
        for address in addresses:
            print(address.uprn)
            address.delete()
        print("..deleted")

        print("removing dodgy blacklist entries (result of bad point in AddressBase)..")
        blacklist = Blacklist.objects.filter(postcode="RH122LP")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        print(
            "removing dodgy blacklist entries (result of bad points in AddressBase).."
        )
        blacklist = Blacklist.objects.filter(postcode="S24AX")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print("Deleting data for council %s..." % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print("..deleted")

        print("..done")
<commit_msg>update Southend email<commit_after>from django.core.management.base import BaseCommand

# from django.contrib.gis.geos import Point
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council
from addressbase.models import Address, Blacklist

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id, internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        print("updating Southend email...")
        southend = Council.objects.get(pk="E06000033")
        southend.email = "elections@southend.gov.uk"
        southend.save()
        print("..updated")

        print("updating Wyre Forest email...")
        wyre_forest = Council.objects.get(pk="E07000239")
        wyre_forest.email = "customerservice@wyreforestdc.gov.uk"
        wyre_forest.save()
        print("..updated")

        print("updating Watford address...")
        watford = Council.objects.get(pk="E07000103")
        watford.address = "Electoral Services Office\nRoom 22\nTown Hall\nWatford"
        watford.postcode = "WD17 3EX"
        watford.save()
        print("..updated")

        print("removing bad points from AddressBase")
        bad_uprns = [
            "10023906550",
            "2630153843",
            "14059977",
            "10007921239",
            "10007920387",
            "10007920390",
            "10024288188",
            "10024288190",
            "10024288189",
            "10023117526",
        ]
        addresses = Address.objects.filter(pk__in=bad_uprns)
        for address in addresses:
            print(address.uprn)
            address.delete()
        print("..deleted")

        print("removing dodgy blacklist entries (result of bad point in AddressBase)..")
        blacklist = Blacklist.objects.filter(postcode="RH122LP")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        print(
            "removing dodgy blacklist entries (result of bad points in AddressBase).."
        )
        blacklist = Blacklist.objects.filter(postcode="S24AX")
        if len(blacklist) == 2:
            for b in blacklist:
                b.delete()
                print("..deleted")
        else:
            print("..NOT deleted")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print("Deleting data for council %s..." % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print("..deleted")

        print("..done")
<|endoftext|>


<commit_before>#!/usr/bin/python

## Binary Analysis Tool
## Copyright 2010-2013 Armijn Hemel for Tjaldur Software Governance Solutions
## Licensed under Apache 2.0, see LICENSE file for details

'''
This script tries to take strings from a binary and match it with files from
a Linux kernel source tree, and if available, configurations from that same
source tree, to trace which files were used to compile the kernel. This is not
fool proof since there are many different kernel trees.
'''

import os, sys, re, subprocess, magic
import os.path
import sqlite3
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-a", "--architecture", dest="arch", help="hardware architecture (optional)")
parser.add_option("-f", "--found", dest="found", action="store_true", help="print symbols that can be found (default)")
parser.add_option("-c", "--configindex", dest="configindex", help="path to database with configs", metavar="DIR")
parser.add_option("-d", "--database", dest="database", help="path to database with kernel strings", metavar="FILE")
parser.add_option("-k", "--kernel", dest="kernel", help="path to Linux kernel image", metavar="FILE")
parser.add_option("-m", "--missing", dest="missing", action="store_true", help="print symbols that can't be found", metavar=None)
parser.add_option("-s", "--size", dest="stringsize", help="stringsize (default 6)")
(options, args) = parser.parse_args()
if options.database == None:
	## check if this directory actually exists
	parser.error("Path to database with kernel strings needed")
if options.kernel == None:
	parser.error("Path to Linux kernel image needed")
if options.missing == None and options.found == None:
	options.found = True
if options.stringsize == None:
	stringsize = 6
else:
	stringsize = int(options.stringsize)

## TODO: if we have a ELF file instead, we can do a much better job at picking the right strings from the binary
## TODO: remove things like initrds or extra file systems appended to the file
try:
	p = subprocess.Popen(['/usr/bin/strings', '-n', str(stringsize), options.kernel], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
        (stanout, stanerr) = p.communicate()
except Exception, e:
	sys.exit(1)

kernelstrings = stanout.split("\n")

conn = sqlite3.connect(options.database)
c = conn.cursor()

## if we have a database with configuration mappings open it
if options.configindex != None:
	configconn = sqlite3.connect(options.configindex)
	configc = configconn.cursor()

seenlinux = False
seenstrings = []
#seenaaaaaa = False
for kernelstring in kernelstrings:
	## TODO: we should not strip. extractkernelstrings.py and this method should be synced
	#kstring = kernelstring.strip()
	kstring = kernelstring
	if "inux" in kstring:
		seenlinux = True
	#if kstring == "AAAAAA":
	#	seenaaaaaa = True
	## no need to really dig into things if we haven't seen the string 'inux' (seriously? TODO)
	if seenlinux:
		## shouldn't we try to first check to see if <\d> is part of the original string?
		## later 2.6 kernels introduced KERN_CONT (<c>) and KERN_DEFAULT (<d>)
		if re.match("(\<\d\>)", kstring) != None:
			searchstring = kstring[3:]
		elif re.match("(\<c\>)", kstring) != None:
			searchstring = kstring[3:]
		elif re.match("(\<d\>)", kstring) != None:
			searchstring = kstring[3:]
		else:
			searchstring = kstring
		if searchstring in seenstrings:
			continue
		found = False
		res = []
		c.execute("SELECT filename FROM extracted WHERE printstring=?", (searchstring.strip(),))
		res = res + map(lambda x: x[0], c.fetchall())
		c.execute("SELECT filename FROM symbol WHERE symbolstring=?", (searchstring.strip(),))
		res = res + map(lambda x: x[0], c.fetchall())
		c.execute("SELECT filename FROM function WHERE functionstring=?", (searchstring.strip(),))
		res = res + map(lambda x: x[0], c.fetchall())
		if len(res) != 0:
			if options.found:
				print 'found string "%s"' % (searchstring,)
			for d in res:
				if options.found:
					if options.arch != None:
						if "arch/" in d and options.arch not in d:
							continue
						if "asm-" in d and options.arch not in d:
							continue
					if options.configindex != None:
						configc.execute("SELECT configstring FROM config WHERE filename=?", (d,))
						configres = map(lambda x: x[0], configc.fetchall())

						if len(configres) == 0:
								print '    This string is defined in path:', d
						for cres in configres:
							if cres != None:
								print '    This string is defined in path: %s (config %s)' % (d, cres)
					else:
								print '    This string is defined in path:', d

				found = True
		#if not found and options.missing and not seenaaaaaa:
		if not found and options.missing:
			print 'did not find string "%s"' % (kstring,)
		seenstrings.append(kstring)

sys.exit()
<commit_msg>remove unused code<commit_after><|endoftext|>


<commit_before>from django.core.management.base import BaseCommand

from django.contrib.gis.geos import Point
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council
from addressbase.models import Address

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id, internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        # User issue 230
        print("updating: West Oxford Community Centre (Oxford)...")
        update_station_point(
            "E07000178", "5354", Point(-1.274921, 51.752621, srid=4326)
        )

        # User issue 237
        print("updating: ATHERSLEY COMMUNITY SHOP (Barnsley)...")
        update_station_point("E08000016", "45", Point(-1.479578, 53.583410, srid=4326))

        # User issue 238
        print("updating: YMCA - Lawnswood Branch (Leeds)...")
        update_station_point(
            "E08000035", "7664", Point(-1.595989, 53.844380, srid=4326)
        )

        # User issue 239
        print("updating: Merrion House (Leeds)...")
        stations = PollingStation.objects.filter(
            council_id="E08000035", internal_council_id="7387"
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = "LS2 8PD"
            station.location = None
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # Correction from Tunbridge Wells
        print("updating: District JJ (Tunbridge Wells)...")
        stations = PollingStation.objects.filter(
            council_id="E07000116", internal_council_id="JJ"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Pantiles Baptist Church, 73 Frant Road, Tunbridge Wells"
            station.postcode = "TN2 5LH"
            station.location = Point(558296, 137921, srid=27700)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 253
        print("updating: SOUTHFIELDS COMMUNITY CENTRE (Bedford)...")
        stations = PollingStation.objects.filter(
            council_id="E06000055",
            internal_council_id__in=["BAR_1", "BAR_2", "BAR_3", "BAR_4", "BAR_5"],
        )
        if len(stations) == 5:
            for station in stations:
                station.location = Point(-0.482075, 52.113823, srid=4326)
                station.save()
                print("..updated")
        else:
            print("..NOT updated")

        # User issue 261
        print("updating: West Ardsley Methodist Church (Leeds)...")
        update_station_point(
            "E08000035", "7800", Point(-1.5725806, 53.71417, srid=4326)
        )

        # User issue 272
        print("updating: Meadowbank Church of Scotland (Edinburgh)...")
        stations = PollingStation.objects.filter(
            council_id="S12000036", internal_council_id="EN12K"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Norton Park Conference Centre, 53 Albion Road"
            station.postcode = "EH7 5QY"
            station.location = None
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 273
        print("updating: Maitland Park Gym (Camden)...")
        stations = PollingStation.objects.filter(
            council_id="E09000007", internal_council_id="LA"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Rhyl Primary School, 7-31 Rhyl Street, London"
            station.postcode = "NW5 3HB"
            station.location = Point(-0.150516, 51.547817, srid=4326)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 285
        print("updating: Memorial Hall (Middlesbrough)...")
        update_station_point(
            "E06000002", "8607", Point(-1.259325, 54.520594, srid=4326)
        )

        # User issue 288
        print("updating: Thame Snooker Club...")
        update_station_point(
            "E07000179", "8327", Point(-0.9743458, 51.7464972, srid=4326)
        )

        print("removing bad points from AddressBase")
        bad_uprns = [
            # nothing yet
        ]
        addresses = Address.objects.filter(pk__in=bad_uprns)
        for address in addresses:
            print(address.uprn)
            address.delete()
        print("..deleted")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print("Deleting data for council %s..." % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print("..deleted")

        print("..done")
<commit_msg>Fix issue 290<commit_after>from django.core.management.base import BaseCommand

from django.contrib.gis.geos import Point
from pollingstations.models import PollingStation, PollingDistrict, ResidentialAddress
from councils.models import Council
from addressbase.models import Address

def update_station_point(council_id, station_id, point):
    stations = PollingStation.objects.filter(
        council_id=council_id, internal_council_id=station_id
    )
    if len(stations) == 1:
        station = stations[0]
        station.location = point
        station.save()
        print("..updated")
    else:
        print("..NOT updated")

class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        # User issue 230
        print("updating: West Oxford Community Centre (Oxford)...")
        update_station_point(
            "E07000178", "5354", Point(-1.274921, 51.752621, srid=4326)
        )

        # User issue 237
        print("updating: ATHERSLEY COMMUNITY SHOP (Barnsley)...")
        update_station_point("E08000016", "45", Point(-1.479578, 53.583410, srid=4326))

        # User issue 238
        print("updating: YMCA - Lawnswood Branch (Leeds)...")
        update_station_point(
            "E08000035", "7664", Point(-1.595989, 53.844380, srid=4326)
        )

        # User issue 239
        print("updating: Merrion House (Leeds)...")
        stations = PollingStation.objects.filter(
            council_id="E08000035", internal_council_id="7387"
        )
        if len(stations) == 1:
            station = stations[0]
            station.postcode = "LS2 8PD"
            station.location = None
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # Correction from Tunbridge Wells
        print("updating: District JJ (Tunbridge Wells)...")
        stations = PollingStation.objects.filter(
            council_id="E07000116", internal_council_id="JJ"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Pantiles Baptist Church, 73 Frant Road, Tunbridge Wells"
            station.postcode = "TN2 5LH"
            station.location = Point(558296, 137921, srid=27700)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 253
        print("updating: SOUTHFIELDS COMMUNITY CENTRE (Bedford)...")
        stations = PollingStation.objects.filter(
            council_id="E06000055",
            internal_council_id__in=["BAR_1", "BAR_2", "BAR_3", "BAR_4", "BAR_5"],
        )
        if len(stations) == 5:
            for station in stations:
                station.location = Point(-0.482075, 52.113823, srid=4326)
                station.save()
                print("..updated")
        else:
            print("..NOT updated")

        # User issue 261
        print("updating: West Ardsley Methodist Church (Leeds)...")
        update_station_point(
            "E08000035", "7800", Point(-1.5725806, 53.71417, srid=4326)
        )

        # User issue 272
        print("updating: Meadowbank Church of Scotland (Edinburgh)...")
        stations = PollingStation.objects.filter(
            council_id="S12000036", internal_council_id="EN12K"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Norton Park Conference Centre, 53 Albion Road"
            station.postcode = "EH7 5QY"
            station.location = None
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 273
        print("updating: Maitland Park Gym (Camden)...")
        stations = PollingStation.objects.filter(
            council_id="E09000007", internal_council_id="LA"
        )
        if len(stations) == 1:
            station = stations[0]
            station.address = "Rhyl Primary School, 7-31 Rhyl Street, London"
            station.postcode = "NW5 3HB"
            station.location = Point(-0.150516, 51.547817, srid=4326)
            station.save()
            print("..updated")
        else:
            print("..NOT updated")

        # User issue 285
        print("updating: Memorial Hall (Middlesbrough)...")
        update_station_point(
            "E06000002", "8607", Point(-1.259325, 54.520594, srid=4326)
        )

        # User issue 288
        print("updating: Thame Snooker Club...")
        update_station_point(
            "E07000179", "8327", Point(-0.9743458, 51.7464972, srid=4326)
        )

        # User issue 290
        print("updating: Temporary at Wingfield Rd...")
        update_station_point("E08000016", "48", None)

        print("removing bad points from AddressBase")
        bad_uprns = [
            # nothing yet
        ]
        addresses = Address.objects.filter(pk__in=bad_uprns)
        for address in addresses:
            print(address.uprn)
            address.delete()
        print("..deleted")

        deleteme = [
            # nothing yet
        ]
        for council_id in deleteme:
            print("Deleting data for council %s..." % (council_id))
            # check this council exists
            c = Council.objects.get(pk=council_id)
            print(c.name)

            PollingStation.objects.filter(council=council_id).delete()
            PollingDistrict.objects.filter(council=council_id).delete()
            ResidentialAddress.objects.filter(council=council_id).delete()
            print("..deleted")

        print("..done")
<|endoftext|>


<commit_before>from thinglang.compiler.references import Reference
from thinglang.lexer.operators.casts import LexicalCast
from thinglang.lexer.values.identifier import Identifier
from thinglang.parser.nodes import BaseNode
from thinglang.parser.rule import ParserRule
from thinglang.parser.values.named_access import NamedAccess
from thinglang.parser.values.method_call import MethodCall
from thinglang.utils.type_descriptors import ValueType

class CastOperation(BaseNode):
    """
    Explicitly cast from one type to another
    Expects a conversion method on the source class
    """

    @staticmethod
    def create(source: Reference, destination_type: Identifier) -> MethodCall:
        return MethodCall(NamedAccess([source.type, Identifier('convert_') + destination_type]), [source], stack_args=True)

    @staticmethod
    @ParserRule.mark
    def parse_inline_cast_op(value: ValueType, _: LexicalCast, target_type: Identifier):
        return MethodCall(NamedAccess([value, Identifier('convert_') + target_type]), [])
<commit_msg>Fix explicit CastOperation construction<commit_after>from thinglang.compiler.references import Reference
from thinglang.lexer.operators.casts import LexicalCast
from thinglang.lexer.values.identifier import Identifier
from thinglang.parser.nodes import BaseNode
from thinglang.parser.rule import ParserRule
from thinglang.parser.values.named_access import NamedAccess
from thinglang.parser.values.method_call import MethodCall
from thinglang.utils.type_descriptors import ValueType

class CastOperation(BaseNode):
    """
    Explicitly cast from one type to another
    Expects a conversion method on the source class
    """

    @staticmethod
    def create(source: Reference, destination_type: Identifier) -> MethodCall:
        return MethodCall(NamedAccess([source.type, Identifier('convert_') + destination_type]), [source], stack_args=True)

    @staticmethod
    @ParserRule.mark
    def parse_inline_cast_op(value: ValueType, _: LexicalCast, target_type: Identifier):
        return MethodCall(NamedAccess.extend(value, Identifier('convert_') + target_type), [])
<|endoftext|>