Пример #1
0
 def setup(self):
   print "prepering to transfer data from local database to AgBase ..."
   self.agbase = AgBase()    
   self.agbase.set_logging_on(True)
   self.user = self.agbase.connect(self.testUser, self.testPwd , self.testServer)
   if self.user is None:
     print("login failed")
Пример #2
0
    def __init__(self):
       
        # fill out dbname host etc in psycopg2.connect 
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_host> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(self.user, self.pwd, self.server)
        self.pp = pprint.PrettyPrinter(indent=4)
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == "bayesy_andy_constrained_prior":
                self.alg = alg
Пример #3
0
    def setUp(self):
        self.agbase = AgBase()

        self.agbase.set_logging_on(True)

        self.user = self.agbase.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        self.farm = self.agbase.create_farm("Python Test Farm")
Пример #4
0
 def __init__(self):
     parser = argparse.ArgumentParser()
     parser.add_argument('--user', required=True, help="AgBase Username")
     parser.add_argument('--password', required=True, help="AgBase Password")
     #parser.add_argument('--firstDate', required=True, help="First date of the measurements you wish to upload (YYYY/MM/DD)")
     #parser.add_argument('--lastDate', required=True, help="Last date of the measurements you wish to upload (YYYY/MM/DD)")
     parser.add_argument('--server', required=True, help="AgBase Server")
     
     args = parser.parse_args() 
     self.agbase = AgBase()
     self.algorithm = AlgorithmAPI(self.agbase)
     self.animal = AnimalAPI(self.agbase)
     self.farm = FarmAPI(self.agbase)
     self.herd = HerdAPI(self.agbase)
     self.measurement = MeasurementAPI(self.agbase)
     self.measurement_category = MeasurementCategoryAPI(self.agbase)
     self.agbase.set_logging_on(True)
     self.user = self.agbase.connect(args.user, args.password , args.server)
Пример #5
0
    def setUp(self):
        print('TestAgBase.setUp')
        super(self.__class__, self).setUp()
        self.pp = pprint.PrettyPrinter(indent=4)
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)

        self.agbase.set_logging_on(True)

        self.user = self.agbase.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))
Пример #6
0
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--passwd', required=True, help="AgBase Password")
        parser.add_argument('--algorithm', required=True, help="The name of the data algorithm you wish "+
                            "to transfer from local moogle to agbase.(Must be an algorithm name in the moogle database)" + 
                            "and must also be added to the website.")

        parser.add_argument('--server', default="<website_address>", help="AgBase Server")
        
        args = parser.parse_args()        
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_address> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.passwd , args.server)
        self.pp = piprint.PrettyPrinter(indent=4)
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == args.algorithm:  # need to set meathod_id to local moogles method_id in get_day
                self.alg = alg
                self.moogle_alg_id = self.get_algorithm_id()
Пример #7
0
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--passwd', required=True, help="AgBase Password")
        parser.add_argument('--date', required=True, help="Date of the measurements you wish to upload (YYYY:MM:DD)")
        parser.add_argument('--server', default="<website_address>", help="AgBase Server")
        
        args = parser.parse_args()        
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_address> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.passwd , args.server)
        self.pp = pprint.PrettyPrinter(indent=4)
        datearr = args.date.split(':',2)
        print datearr[0], datearr[1], datearr[2]
        self.date = datetime.date(int(datearr[0]), int(datearr[1]), int(datearr[2]))
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == "bayesy_andy_constrained_prior": # need to change alg id in get_Day function
                self.alg = alg
Пример #8
0
class WeighBridgeEmulator:
    user = # 
    pwd = # 
    server = # 

    def __init__(self):
       
        # fill out dbname host etc in psycopg2.connect 
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_host> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(self.user, self.pwd, self.server)
        self.pp = pprint.PrettyPrinter(indent=4)
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == "bayesy_andy_constrained_prior":
                self.alg = alg

    def get_animal_list(self):
        query = "select a.id, a.eid from animal as a join animalfarm as af on(a.id = af.animal_id) where a.vid IS NULL;" #limit(500)
        self.cur.execute(query)
        return self.cur.fetchall()

    def get_Day(self, date, animalid):
        query = 'select * from weights where weights.animal_id = {animalid} and date <@ tsrange(\'{date}\', \'{date}\'::date + 1, \'[)\') and method_id = 70 and weighbridge_id = 8 order by DATE;'.format(animalid=animalid, date=date)
        self.cur.execute(query)
        return self.cur.fetchall()

    def clean_up(self):
        self.cur.close()
        self.conn.close()

    def create_measurements(self, animal, measurements):
        for measurement in measurements:
            if(not math.isnan(measurement[5]) or not math.isnan(measurement[6]) or not math.isnan(measurement[7])):
                self.measurement.create_measurement(animal, self.alg, self.user, str(measurement[4]), measurement[5], None, measurement[6], None, measurement[7], None)

    def upload_animal_measurements_by_day(self):
        date = datetime.date(2015, 01, 01) # sets date to first day of the year
        animals = self.get_animal_list() # get animal list from the local database
        animal_max = len(animals)
        self.pp.pprint(animals)        
        for i in range(0, 100):
            print "day: {}/100".format(i)
            animal_count = 0
            for animalID in animals:
                animal_count += 1
                print "animal {}/{}".format(animal_count, animal_max)
                animal = self.animal.get_animal_by_eid(self.frm, animalID[1])
                if(animal == None):
                    animal = self.animal.create_animal(self.frm, animalID[1]) # creates an animal if it dosnt exist
                measurements = self.get_Day(date, animalID[0]) # gets the days measurements for the above animals on the above day
                if(len(measurements) > 0): # check to see if there are any measurements on that day
                    self.create_measurements(animal, measurements)

            date += datetime.timedelta(days=1)
Пример #9
0
class TestHerd(TestCommon):

    def setUp(self):
        self.agbase = AgBase()

        self.agbase.set_logging_on(True)

        self.user = self.agbase.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        self.farm = self.agbase.create_farm("Python Test Farm")

    def tearDown(self):
        print('TestHerd.tearDown')
        self.agbase.remove_farm(self.farm)


    def test_get_nonexistent_herd(self):

        herds = self.agbase.get_herds(self.farm)

        if herds is not None and len(herds) != 0:
            self.fail()

        herd = self.agbase.create_herd(self.farm, "Python Test Herd")

        if herd is None:
            self.fail()

        herd2 = self.agbase.create_herd(self.farm, "Python Test Herd")

        if herd2 is None:
            self.fail()
            
        print herd2.id
            

    def test_herds(self):

        herd = self.agbase.create_herd(self.farm, "Python Test Herd")

        if herd is None:
            self.fail()

        print('created herd: {} with id: {}'.format(herd.name, herd.id))

        herds = self.agbase.get_herds()

        if herds is None:
            self.fail()

        for herd in herds:
            print('found herd: {} with id: {}'.format(herd.name, herd.id))

        herds = self.agbase.get_herds(self.farm)

        if herds is None:
            self.fail()

        for herd in herds:
            print('The farm "{}" has a herd named: {}'.format(self.farm.name, herd.name))

        deleted = self.agbase.remove_herd(herd)

        if not deleted:
            self.fail()
Пример #10
0
class TestAgBase(TestCommon):

    def setUp(self):
        print('TestAgBase.setUp')
        super(self.__class__, self).setUp()
        self.pp = pprint.PrettyPrinter(indent=4)
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)

        self.agbase.set_logging_on(True)

        self.user = self.agbase.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))


    def test_farms(self):

        test_farm = self.farm.create_farm("Python Test Farm")

        if test_farm is None:
            self.fail()

        print('created farm: {} with id: {}'.format(test_farm.name, test_farm.id))

        farms = self.farm.get_farms()

        if farms is None:
            self.fail()

        for farm in farms:
            print('found farm: {} with id: {}'.format(farm.name, farm.id))

        single_query_farm = self.farm.get_farm(test_farm.id)

        if single_query_farm.id != test_farm.id:
            self.fail()

        farms = self.farm.get_farms(self.user)

        if farms is None:
            self.fail()

        for farm in farms:
            print('The current user can access farm: {}'.format(farm.name))

        deleted = self.farm.remove_farm(test_farm)

        if not deleted:
            self.fail()


    def test_roles(self):

        roles = self.agbase.get_roles()

        if roles is None:
            self.fail()

        for role in roles:
            print('Found role named {}'.format(role.name))


    def test_users(self):

        roles = self.agbase.get_roles()

        admin_role = None

        for role in roles:
            if role.name == "Viewer":
                admin_role = role
                break

        test_user = self.agbase.create_user("Test", "Testor", "*****@*****.**", "testpass", admin_role)

        if test_user is None:
            self.fail()

        print('created user: {} with id: {}'.format(test_user.email, test_user.id))

        users = self.agbase.get_users()

        if users is None:
            self.fail()

        for user in users:
            print('found user: {} {} with email: {}'.format(user.first_name, user.last_name, user.email))

        deleted = self.agbase.remove_user(test_user)

        if not deleted:
            self.fail()


    def test_measurement_categories(self):

        test_category = self.measurement_category.create_measurement_category('Test Category')

        if test_category is None:
            self.fail()

        print('created measurement category: {} with id: {}'.format(test_category.name, test_category.id))

        single_query_category = self.measurement_category.get_measurement_category(test_category.id)

        if test_category.id != single_query_category.id:
            self.fail()

        categories = self.measurement_category.get_measurement_categories()

        if categories is None:
            self.fail()

        for category in categories:
            print('found category: {}'.format(category.name))

        deleted = self.measurement_category.remove_measurement_category(test_category)

        if not deleted:
            self.fail()


    def test_algorithms(self):

        test_category = self.measurement_category.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.algorithm.create_algorithm('Test Algorithm', test_category)
        print "test_algorithm", test_algorithm.to_json()
        if test_algorithm is None:
            self.fail()

        print('created algorithm {} with id: {}'.format(test_algorithm.name, test_algorithm.id))

        single_query_algorithm = self.algorithm.get_algorithm(test_algorithm.id)
        print "single_query_algorithm", single_query_algorithm.to_json()
        if test_algorithm.id != single_query_algorithm.id:
            self.fail()

        algorithms = self.algorithm.get_algorithms()

        if algorithms is None:
            self.fail()

        for algorithm in algorithms:
            print('found algorithm: {}'.format(algorithm.name))

        deleted = self.algorithm.remove_algorithm(test_algorithm)

        if not deleted:
            self.fail()

        self.measurement_category.remove_measurement_category(test_category)


    def test_animals(self):

        test_farm = self.farm.create_farm('Animal Test Farm')
        test_herd = self.herd.create_herd(test_farm, 'Animal Test Herd')

        test_eid = "AN-EID-FOR_TESTING"

        test_animal = self.animal.create_animal(test_farm, test_eid)

        if test_animal is None:
            self.fail()

        print('created animal {} with id: {}'.format(test_animal.eid, test_animal.id))

        result = self.animal.set_animal_herd(test_animal, test_herd)

        if result is None:
            self.fail()
        print "============farm==========",test_farm.to_json()
        print "============herd==========",test_herd.to_json()
        animals = self.animal.get_animals(test_farm, test_herd)

        if animals is None:
            self.fail()

        for animal in animals:
            print('found animal: {}'.format(animal.eid))

        updated = self.animal.update_animal_vid(test_animal, "My Pet Cow")

        if not updated:
            self.fail()

        expected_animal = self.animal.get_animal_by_eid(test_farm, test_eid)

        if expected_animal.id != test_animal.id:
            self.fail()

        deleted = self.animal.remove_animal(test_animal)

        if not deleted:
            self.fail()

        self.herd.remove_herd(test_herd)
        self.farm.remove_farm(test_farm)


    def test_measurements(self):

        test_farm = self.farm.create_farm('Animal Test Farm')
        test_eid = "AN-EID-FOR_TESTING"
        test_animal = self.animal.create_animal(test_farm, test_eid)
        test_category = self.measurement_category.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.algorithm.create_algorithm('Test Algorithm', test_category)

        measurement = self.measurement.create_measurement(test_animal, test_algorithm, self.user, time.strftime("%c"), None, None, 0.3344,None,None,None)

        if measurement is None:
            self.fail()

        print('created measurement with id {}'.format(measurement.id))

        animal_measurements = self.measurement.get_measurements_for_animal(test_animal)

        if animal_measurements[0].id != measurement.id:
            self.fail()

        deleted = self.measurement.remove_measurement(measurement)

        if not deleted:
            self.fail()

        eid_measurement = self.measurement.create_measurement_for_eid(test_animal.eid,test_farm, test_algorithm, self.user, time.strftime("%c"), None, None, 0.3344,None,None,None)

        if eid_measurement is None:
            self.fail()

        if eid_measurement.animal_id != test_animal.id:
            self.fail()
        #self.pp.pprint(repr(eid_measurement))
        for keys,values in eid_measurement.__repr__().items():
            print(keys)
            print(values)
        self.measurement.remove_measurement(eid_measurement)

        self.animal.remove_animal(test_animal)
        self.farm.remove_farm(test_farm)
        self.algorithm.remove_algorithm(test_algorithm)
        self.measurement_category.remove_measurement_category(test_category)


    def test_measurements_bulk_upload(self):

        test_farm = self.farm.create_farm('Animal Test Farm')
        test_eid = "AN-EID-FOR_TESTING"
        test_animal = self.animal.create_animal(test_farm, test_eid)
        test_category = self.measurement_category.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.algorithm.create_algorithm('Test Algorithm', test_category)

        measurement_list = self.measurement.create_bulk_measurement_upload_list(test_animal, test_algorithm, self.user)

        measurement_list.add_measurement(time.strftime("%c"), 0.3344)
        measurement_list.add_measurement(time.strftime("%c"), 0.4455)
        measurement_list.add_measurement(time.strftime("%c"), 0.5566)

        success = self.measurement.upload_measurement_list(measurement_list)

        if success is not True:
            self.fail()

        print('created bulk measurements')

        animal_measurements = self.measurement.get_measurements_for_animal(test_animal)

        if len(animal_measurements) != 3:
            self.fail()

        self.animal.remove_animal(test_animal)
        self.farm.remove_farm(test_farm)
        self.algorithm.remove_algorithm(test_algorithm)
        self.measurement_category.remove_measurement_category(test_category)
Пример #11
0
        row = worksheet.row(curr_row)

        n0 = row[0]
        eid0 = row[2]
        cell_type_str = ctype_text.get(n0.ctype, "unknown type")
        if cell_type_str == "number":
            matches.append([int(n0.value), eid0.value])

        n0 = row[5]
        eid0 = row[7]
        cell_type_str = ctype_text.get(n0.ctype, "unknown type")
        if cell_type_str == "number":
            matches.append([int(n0.value), eid0.value])

    # Connect to AgBase
    ab = AgBase()
    ab.set_logging_on(True)
    user = ab.connect(args.user, args.passwd, args.server)
    if user is None:
        print ("ERROR: Login failed.")
        exit(1)

    agFarm = FarmAPI(ab)
    # Find farm ID
    farm = agFarm.get_farm_by_name(user, args.farm)
    if farm is None:
        print ("ERROR: No such farm: %s" % args.farm)
        exit(1)

    agAnimal = AnimalAPI(ab)
    agMeasurement = MeasurementAPI(ab)
Пример #12
0
class WeighBridgeEmulator:
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--passwd', required=True, help="AgBase Password")
        parser.add_argument('--date', required=True, help="Date of the measurements you wish to upload (YYYY:MM:DD)")
        parser.add_argument('--server', default="<website_address>", help="AgBase Server")
        
        args = parser.parse_args()        
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_address> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.passwd , args.server)
        self.pp = pprint.PrettyPrinter(indent=4)
        datearr = args.date.split(':',2)
        print datearr[0], datearr[1], datearr[2]
        self.date = datetime.date(int(datearr[0]), int(datearr[1]), int(datearr[2]))
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == "bayesy_andy_constrained_prior": # need to change alg id in get_Day function
                self.alg = alg

    def get_animal_list(self):
        query = "select a.id, a.eid from animal as a join animalfarm as af on(a.id = af.animal_id) where a.vid IS NULL;" #limit(500)
        self.cur.execute(query)
        return self.cur.fetchall()

    def get_Day(self, date, animalid):
        query = 'select * from weights where weights.animal_id = {animalid} and date <@ tsrange(\'{date}\'::date - 1, \'{date}\', \'[)\') and method_id = 70 and weighbridge_id = 8 order by DATE;'.format(animalid=animalid, date=date)
        self.cur.execute(query)
        return self.cur.fetchall()

    def clean_up(self):
        self.cur.close()
        self.conn.close()

    def upload_animal_measurements_by_day(self):
        animals = self.get_animal_list() # get animal list from the local database
        animal_max = len(animals)
        self.pp.pprint(animals)
        listIndex = 0
        self.meas_list = []
        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))

        animal_count = 0
        for animalID in animals:
            animal_count += 1
            print "animal {}/{}".format(animal_count, animal_max)
            measurements = self.get_Day(self.date, animalID[0]) # gets the days measurements for the above animals on the above day
            if(len(measurements) > 0): # check to see if there are any measurements on that day
                for measurement in measurements:
                    if(self.meas_list[listIndex].get_measurement_count() >= 500):
                        self.agbase.log("meas_list Dump >>> " + json.dumps(self.meas_list[listIndex].get_json()))
                        listIndex += 1
                        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
                        
                    if(not math.isnan(measurement[5]) or not math.isnan(measurement[6]) or not math.isnan(measurement[7])):
                        self.meas_list[listIndex].add_measurement(str(measurement[4]), measurement[5], None, measurement[6], None, measurement[7], animalID[1])

        for measurementList in self.meas_list:
            self.measurement.upload_measurement_list(measurementList)

        self.meas_list = []
        listIndex = 0
        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
Пример #13
0
class LocalToAgBase:
  recordLimit = -1
  def __init__(self):
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', default=".agbase", help="AgBase Config File (username password e.t.c)")
    parser.add_argument('--user', required=True, help="AgBase Username")
    parser.add_argument('--passwd', required=True, help="AgBase Password")
    parser.add_argument('--server', default="https://agbase.elec.ac.nz/api/", help="AgBase Server")
    parser.add_argument('--print-weights' , action='store_true', help="Prints all the animals and their weights from the database")
    parser.add_argument('--upload-animals', action='store_true', help="Uploads all the animals from postgres to agbase")
    parser.add_argument('--upload-weights', action='store_true', help="Uploads all the weights from postgres to agbase")
    parser.add_argument('--print-animals', action='store_true', help="Print all animals from agbase")
    parser.add_argument('--limit', type=int, help="limit the number of animals that get added to agbase")
    parser.add_argument('--remove-animals', action='store_true', help="REMOVES ALL ANIMALS AND WEIGHTS")
    parser.add_argument('--db-host', default="postgres.elec.ac.nz", help="Local Database Host")
    parser.add_argument('--db-user', help="Local Database Username")
    parser.add_argument('--db-passwd', help="Local Database Password")
    
    args = parser.parse_args()
    self.testUser = args.user
    self.testPwd = args.passwd
    self.testServer = args.server

    conn = psycopg2.connect("dbname=moogle host=%s user=%s password=%s" % (args.db_host, args.db_user, args.db_passwd))
    cur = conn.cursor()  
    self.setup()
        
    if(args.limit):
      self.recordLimit = args.limit
      
    if(args.upload_animals):
      self.upLoadAnimalsToAgBase(cur)   
          
    if(args.upload_weights):
      self.uploadWeights(cur)
      
    if(args.print_weights):
      self.printWeightsFromAgBase()
    
    if(args.remove_animals):
      self.removeAllAnimals()
    
    if(args.print_animals):
      self.printAnimalsFromAgBase()
    #self.printWeightsFromAgBase()
    #self.printHerds(self.agbase.get_farm(31))
    cur.close()
    conn.close()
    
  def setup(self):
    print "prepering to transfer data from local database to AgBase ..."
    self.agbase = AgBase()    
    self.agbase.set_logging_on(True)
    self.user = self.agbase.connect(self.testUser, self.testPwd , self.testServer)
    if self.user is None:
      print("login failed")
      
  def removeAllAnimals(self):
    animals = self.agbase.get_animals(self.agbase.get_farm(31),None)
    for animal in animals:
      self.agbase.remove_animal(animal)
  
    
  def getWeights(self,cur,method_id,bridge):
    cur.execute("select distinct eid, date , w5 ,w50 ,w95 from weights join animal on (weights.animal_id = animal.id) join weighbridge on (weights.weighbridge_id = weighbridge.id) "+
                "where (weighbridge.name = '{}')and(weights.method_id = {});".format(bridge,method_id))
    return cur.fetchall()
  
  def getWeightsByEid(self,cur,method_id,bridge,eid):
    cur.execute("select distinct eid, date , w5 ,w50 ,w95 from weights join animal on (weights.animal_id = animal.id) join weighbridge on (weights.weighbridge_id = weighbridge.id) "+
                "where (weighbridge.name = '{}')and(weights.method_id = {})and (animal.eid = '{}');".format(bridge,method_id,eid))
    return cur.fetchall()
  
  def printWeights(self,cur,method_id,bridge):
    cur.execute("select distinct eid, date , w5 ,w50 ,w95 from weights join animal on (weights.animal_id = animal.id) join weighbridge on (weights.weighbridge_id = weighbridge.id)"+
                " where (weighbridge.name = '{}')and(weights.method_id = {});".format(bridge,method_id))
    pprint.pprint(cur.fetchall())
  
  
  def getMethod(self,cur,method_id):
    cur.execute("select * from method where method.id = %s;",([method_id]))
    return cur.fetchall()
    #self.upLoadToAgBase(cur.fetchall())
  
  def getHerds(self,farm):
    return self.agbase.get_herds(farm)
  
  def printHerds(self,farm):
    herds = self.agbase.get_herds(farm)
    for herd in herds:
      pprint.pprint("{} : {}".format(herd.name, herd.id))
  
  def getWeighBridges(self,cur,bridge):
    cur.execute("select * from weighbridge where (name = '{0}');".format(bridge))
    self.upLoadToAgBase(cur.fetchall())
    
  def getAnimals(self,cur,method_id,bridge):
    cur.execute('select distinct animal.eid, animal.id from animal join weights on(animal.id = weights.animal_id)join weighbridge on (weights.weighbridge_id = weighbridge.id) '+
        'where (weighbridge.name=%s)and(weights.method_id=%s) order by animal.id;', (bridge,method_id));
    return cur.fetchall()
  
  def printAnimals(self,cur,method_id,bridge):
    cur.execute('select distinct animal.eid, animal.id from animal join weights on(animal.id = weights.animal_id)join weighbridge on (weights.weighbridge_id = weighbridge.id) '+
        'where (weighbridge.name=%s)and(weights.method_id=%s) order by animal.id;', (bridge,method_id));
    pprint.pprint(cur.fetchall())
  
  def printAnimalsFromAgBase(self):
    farm = self.agbase.get_farms(self.user)
    animals = self.agbase.get_animals(farm)
    if animals is None:
      print"no animals in farm 31"
    
    for animal in animals:
      print'eid: {}'.format(animal.eid)
    print 'animals# {}'.format(len(animals))
  
  
  def printWeightsFromAgBase(self):
    farm = self.agbase.get_farm(31)
    animals = self.agbase.get_animals(farm)
    if animals is None:
      print"no animals in farm 31"
      
    i = 9
    for animal in animals:
      print'eid: {}'.format(animal.eid)
      animal_measurements = self.agbase.get_measurements_for_animal(animal)
      for weight in animal_measurements:
        print'        w5 {}, w50 {}, w95 {}'.format(weight.w05,weight.w50,weight.w95)
      print 'animals# {}'.format(len(animals))
    
  def upLoadToAgBase(self,cur): # ul = upload = stuff to put up to server
    cur.execute('select method_id from score order by cprs;')
    method_id = cur.fetchone()[0]
    algName = self.getMethod(cur,method_id)[0][1]
    
    cat = self.agbase.get_measurement_category(27)
    alg = self.agbase.create_algorithm(algName, cat)
    
    farm = self.agbase.get_farm(31)
    animals = self.getWeights(cur,method_id,"taieri")
    pprint.pprint(animals)
    for animal in animals:                   #eid                            #date            w5       w50       w95
      self.agbase.create_measurement_for_eid(animal[0], farm, alg, self.user, str(animal[1]),animal[2],animal[3],animal[4])
      
      
  def uploadWeights(self,cur):
    cur.execute('select method_id from score order by cprs;')
    method_id = cur.fetchone()[0]
    algName = self.getMethod(cur,method_id)[0][1]
    cat = self.agbase.create_measurement_category("Weight")
    cats = self.agbase.get_measurement_categories()
    for ccat in cats:
      pprint.pprint("{} {}".format(ccat.name,ccat.id))
      
    alg = self.agbase.create_algorithm(algName, cat)  
    
    farm = self.agbase.create_farm("Liquid Calcium")
    animals = self.agbase.get_animals(farm)
    amax = len(animals)
    acount = 0
    
    for animal in animals:
      if animal.eid != "":
        weights = self.getWeightsByEid(cur,method_id,"taieri",animal.eid)
        acount += 1
        print("animals {} / {}".format(acount,amax))
        wmax = len(weights)
        wcount = 0
      
        #make new animal_measurements      
        measurement_list = self.agbase.create_bulk_measurement_upload_list(animal, alg, self.user)
        for weight in weights:
          w = list(weight)
          wcount += 1
          print("measurement {} / {}".format(wcount,wmax))
          #self.agbase.create_measurement(animal, alg, self.user, str(w[1]),w[2],w[3],w[4])
          if math.isnan(w[2]):
            w[2] = None
          if math.isnan(w[3]):
            w[3] = None
          if math.isnan(w[4]):
            w[4] = None
          print "weight: {}".format(w[2])
          measurement_list.add_measurement(time.strftime(str(w[1])), w[2], w[3], w[4])
          
        success = self.agbase.upload_measurement_list(measurement_list)
    
  
  def upLoadAnimalsToAgBase(self,cur): # ul = upload = stuff to put up to server
    cur.execute('select method_id from score order by cprs;')
    method_id = cur.fetchone()[0]
    farm = self.agbase.create_farm("Liquid Calcium")
    herd = self.agbase.create_herd(farm, "Herd 1")
    animals = self.getAnimals(cur,method_id,"taieri")
    pprint.pprint(animals)
    count = 0
    minimum = 51
    for animal in animals:
      print "count: {}/{}".format(count,len(animals))
      count += 1
      if count >= minimum:
        self.agbase.create_animal(farm,animal[0])
        print "farm: {}, animal: {}".format(farm, animal)
        
      if count == self.recordLimit:
        break
       
    mAnimals = self.agbase.get_animals(farm)
    
    pprint.pprint(mAnimals)
    
    for animal in mAnimals:
      self.agbase.set_animal_herd(animal,herd)
Пример #14
0
class WeighBridgeEmulator:
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--passwd', required=True, help="AgBase Password")
        parser.add_argument('--algorithm', required=True, help="The name of the data algorithm you wish "+
                            "to transfer from local moogle to agbase.(Must be an algorithm name in the moogle database)" + 
                            "and must also be added to the website.")

        parser.add_argument('--server', default="<website_address>", help="AgBase Server")
        
        args = parser.parse_args()        
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_address> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.passwd , args.server)
        self.pp = piprint.PrettyPrinter(indent=4)
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == args.algorithm:  # need to set meathod_id to local moogles method_id in get_day
                self.alg = alg
                self.moogle_alg_id = self.get_algorithm_id()
                
                
    def get_algorithm_id(self):
        query = "select id from method where name = '{}';".format(self.alg.name)
        self.cur.execute(query)
        return self.cur.fetchone()[0]

    def get_animal_list(self):
        query = "select a.id, a.eid from animal as a join animalfarm as af on(a.id = af.animal_id) where a.vid IS NULL;" #limit(500)
        self.cur.execute(query)
        return self.cur.fetchall()

    def get_day(self, date, animalid):
        query = 'select * from weights where weights.animal_id = {animalid} and date <@ tsrange(\'{date}\', \'{date}\'::date + 1, \'[)\') and method_id = {methodid} and weighbridge_id = 8 order by DATE;'.format(animalid=animalid, date=date, methodid=self.moogle_alg_id)
        self.cur.execute(query)
        return self.cur.fetchall()

    def clean_up(self):
        self.cur.close()
        self.conn.close()

    def upload_animal_measurements_by_day(self):
        now = datetime.datetime.now()
        date = datetime.date(2015, 01, 01) # sets date to first day of the year
        animals = self.get_animal_list() # get animal list from the local database
        animal_max = len(animals)
        self.pp.pprint(animals)
        listIndex = 0
        self.meas_list = []
        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
               
        for i in range(0, 100): #change this if you want it to go for more days
            print "day: {}/100".format(i)
            animal_count = 0
            for animalID in animals:
                animal_count += 1
                print "animal {}/{}".format(animal_count, animal_max)
                measurements = self.get_day(date, animalID[0]) # gets the days measurements for the above animals on the above day
                if(len(measurements) > 0): # check to see if there are any measurements on that day
                    for measurement in measurements:
                        if(self.meas_list[listIndex].get_measurement_count() >= 500):
                            self.agbase.log("meas_list Dump >>> " + json.dumps(self.meas_list[listIndex].get_json()))
                            listIndex += 1
                            self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
                            
                        if(not math.isnan(measurement[5]) or not math.isnan(measurement[6]) or not math.isnan(measurement[7])):
                            self.meas_list[listIndex].add_measurement(str(measurement[4]), measurement[5], None, measurement[6], None, measurement[7], animalID[1])

            date += datetime.timedelta(days=1)
            for measurementList in self.meas_list:
                self.measurement.upload_measurement_list(measurementList)

            self.meas_list = []
            listIndex = 0
            self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
Пример #15
0
class getConditionScores:
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--password', required=True, help="AgBase Password")
        #parser.add_argument('--firstDate', required=True, help="First date of the measurements you wish to upload (YYYY/MM/DD)")
        #parser.add_argument('--lastDate', required=True, help="Last date of the measurements you wish to upload (YYYY/MM/DD)")
        parser.add_argument('--server', required=True, help="AgBase Server")
        
        args = parser.parse_args() 
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.password , args.server)
        #self.first_date = self.getDate(args.firstDate)
        #self.last_date = self.getDate(args.lastDate)

    def getScores(self):
        self.alg = self.algorithm.get_algorithm("DairyNZ BCS")        
        self.frm = self.farm.get_farm_by_name(self.user,"Demo Farm")
        animalList = []
        moreAnimals = True
        offset = 0
        limit = 500
        while moreAnimals is True: # amimals # more comlicated because there are more then the 1024 limit
            tempAnimalList = self.animal.get_animals(self.frm, None, limit, offset)
            if(len(tempAnimalList) != (limit+offset)):
                moreAnimals = False
            
            offset += limit    
            
            for animal in tempAnimalList:
                animalList.append(animal)
            
        measurementList = {}  # holds all of the measurements indexed by eid
        length = len(animalList)
        count = 0
        for animal in animalList: # goes through the animal list and sends a query to the datebase
            print "Animal {}/{}".format(count, length)
            count += 1
            measurementList[animal.eid] = self.measurement.get_measurements_for_animal(animal,self.alg)
        
        count = 0
        
        for animal in animalList:
            print "Animal {}/{}".format(count, length)
            count += 1
            for measurement in measurementList[animal.eid]:
                print "eid: {}, condition score:  {}, timestamp: {}".format(animal.eid,measurement.w50,measurement.time_stamp)

    def getDate(self, raw_date):
        date_parts = raw_date.split('/',2)
        if(len(date_parts) == 3):
            date = datetime.date(int(date_parts[0]),int(date_parts[1]),int(date_parts[2]))
            print 'date: ', date
            return int((date-datetime.date(1970,1,1)).total_seconds() * 1000)
        return None