Last active
May 22, 2020 20:34
-
-
Save paul121/afdf1575f2d9400a2668fc36180c8d06 to your computer and use it in GitHub Desktop.
Generate semi accurate cow weights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, timedelta | |
import os | |
import random | |
import math | |
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' | |
from farmOS import farmOS | |
farm = farmOS(hostname='http://localhost', username='user', password='pass', client_id='farm', scope='user_access') | |
asset_ids = [] | |
num_assets = 5 | |
for i in range(num_assets): | |
asset = { | |
'name': f"Cow #{i}", | |
'type': 'animal', | |
'animal_type': {'name': 'cow'} | |
} | |
res = farm.asset.send(asset) | |
if res is not None and 'id' in res: | |
asset_ids.append(res['id']) | |
log_name = 'Log Animal Weight' | |
log_type = 'farm_observation' | |
quantity_template = [{ | |
'measure': 'weight', | |
'value': 0, | |
'unit': {'id': 12} | |
}] | |
log_done = 1 | |
start_date = date(2017, 1, 1) | |
end_date = date(2019, 10, 1) | |
delta = timedelta(days=30) | |
month = 1 | |
first_count = 120 | |
for asset in asset_ids: | |
loop_date = start_date | |
month_count = 1 | |
last_count = first_count | |
while loop_date <= end_date: | |
new_count = last_count + int(last_count * random.uniform(.05/math.pow(month_count,1.5), .08/math.pow(month_count,1.5))) | |
last_count = new_count | |
print(new_count) | |
quantity = quantity_template | |
quantity[0]['value'] = new_count | |
log = { | |
'name': log_name, | |
'type': log_type, | |
'asset': [{'id': asset}], | |
'quantity': quantity, | |
'timestamp': loop_date.strftime("%s"), | |
'log_category': [ | |
{'name': 'animal_weight_test'}, | |
], | |
'done': 1 | |
} | |
farm.log.send(log) | |
# Print weights, useful for debugging the math for creating semi-realistic weights. | |
print(str(asset) + ' : ' + str(new_count)) | |
loop_date = loop_date + delta | |
month_count = month_count + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment