Esempio n. 1
0
    def create_test_observation(self, plant_id, user_id):
        # create and insert new observation
        observation = Observation(user_id=user_id,
                                  date=self.test_observation['date'],
                                  plant_id=plant_id,
                                  notes=self.test_observation['notes'])
        observation.insert()

        return observation.id
Esempio n. 2
0
def drop_all_data(session):
    from models import Patient, Encounter, Observation, Procedure

    Patient.clear_all_records(session)
    Encounter.clear_all_records(session)
    Observation.clear_all_records(session)
    Procedure.clear_all_records(session)

    session.execute(f"DROP INDEX IF EXISTS {Patient.SRC_INDEX}")
    session.execute(f"DROP INDEX IF EXISTS {Encounter.SRC_INDEX}")

    session.commit()
Esempio n. 3
0
def stat(stat_id):
    if request.method == "GET":
        records = session.query(Observation).filter(
            Observation.survey_id == stat_id)
        result = []
        for record in records:
            data = {
                "id": record.id,
                "survey_id": record.survey_id,
                "value": record.value,
                "frequency": record.frequency
            }
            result.append(data)
        return make_response(json.dumps(result))
    if request.method == 'PUT' or request.method == "POST":
        value = request.get_json().get('value')
        frequency = request.get_json().get('frequency')
        record = session.query(Observation).filter(Observation.id == stat_id)
        if record:
            record.update({
                Observation.value: value,
                Observation.frequency: frequency
            })
            output = "Record Updated for Observation: %s" % stat_id
        else:
            session.add(
                Observation(value=value, id=stat_id, frequency=frequency))
            output = "Record Added for %s" % stat_id
            session.commit()
        return make_response(output)
    elif request.method == 'DELETE':
        session.query(Observation).filter(Observation.id == stat_id).delete()
        output = "Record Deleted for Observation: %s" % stat_id
        session.commit()
        return make_response(output)
Esempio n. 4
0
def foi_popup_data():
    data = []
    args = request.args.to_dict()
    if 'foi' in args:
        foi = args["foi"]
        filter_by = args.get("filter_by")
        data = Observation().get_filtered_for_foi(foi, filter_by)
    return jsonify(data)
Esempio n. 5
0
    def run(self):
        """
        Executes the scenario run by first preparing the scenario and then awaiting the scenario to start.
        During scenario run, Supervisor is within this run method scheduling the observations by starting
        local observations if all observations before were already executed. Further, it resolves the before
        dependencies of still existing observations to be executed by an agent under this supervisor if receiving
        an observation_done message. It sends out an observation_done message itself if one observation was executed
        by one agent under this supervisor.

        :rtype: None
        """
        self.prepare_scenario()
        self.assert_scenario_start()
        while self.scenario_runs:
            observation_dict = next((obs for obs in self.observations_to_exec if len(obs["before"]) == 0), None)
            if observation_dict is not None:
                observation = Observation(**observation_dict)
                ip, port = self.discovery[observation.receiver].split(":")
                client_thread = AgentClient(ip, int(port), json.dumps(observation_dict))
                self.threads_client.append(client_thread)
                client_thread.start()
                self.observations_to_exec.remove(observation_dict)
            observation_done_dict = next((obs for obs in self.observations_done), None)
            if observation_done_dict is not None:
                done_message = {
                    "type": "observation_done",
                    "scenario_run_id": self.scenario_run_id,
                    "observation_id": observation_done_dict["observation_id"],
                    "receiver": observation_done_dict["receiver"],
                    "trust_log": '<br>'.join(self.logger.read_lines_from_trust_log_str()),
                    "receiver_trust_log": '<br>'.join(self.logger.read_lines_from_agent_trust_log_str(
                        observation_done_dict["receiver"])),
                }
                self.send_queue.put(done_message)
                self.remove_observation_dependency([observation_done_dict["observation_id"]])
                self.observations_done.remove(observation_done_dict)
                print(f"Exec after exec observation: {self.observations_to_exec}")
            if self.receive_pipe.poll():
                message = self.receive_pipe.recv()
                if message['type'] == 'observation_done':
                    self.remove_observation_dependency(message["observations_done"])
                    print(f"Exec after done message: {self.observations_to_exec}")
                if message['type'] == 'scenario_end':
                    for thread in self.threads_client:
                        if thread.is_alive():
                            thread.join()
                    for thread in self.threads_server:
                        thread.end_server()
                        if thread.is_alive():
                            thread.join()
                    self.scenario_runs = False
        end_message = {
            'type': 'scenario_end',
            'scenario_run_id': self.scenario_run_id
        }
        self.supervisor_pipe.send(end_message)
Esempio n. 6
0
    def post_plant_observation_api(jwt):
        '''
        Handles API POST requests for adding new observation.
        '''

        # get request body
        body = request.get_json()

        # get user table id from session or jwt
        if 'profile' in session:
            user_id = session['profile']['user_table_id']
        else:
            auth0_user_id = jwt['sub']
            user_id = User.query.filter_by(
                user_id=auth0_user_id).one_or_none().id

        # load observation body data
        plant_id = body.get('plantID')
        date = body.get('date')
        notes = body.get('notes')

        # ensure required fields have data
        if ((date == '') or (plant_id == '')):
            abort(422)

        # create a new observation
        observation = Observation(user_id=user_id,
                                  date=date,
                                  plant_id=plant_id,
                                  notes=notes)

        try:
            # add observation to the database
            observation.insert()
        except Exception as e:
            print('ERROR: ', str(e))
            abort(422)

        # flash success message
        flash('Observation successfully created!')

        # return observation
        return jsonify({'success': True, 'observation': observation.format()})
def make_quarterback_action(qb_data, collaboration, i):
	quarterback_name_number = qb_data['QB'][i]
	if quarterback_name_number == None or quarterback_name_number.strip() == '':
		return
	athlete = find_athlete(quarterback_name_number)
	action, created_ = Action.get_or_create(
		collaboration = collaboration,
		athlete = athlete,
		action_type = 'QB' 
	)
	for key in qb_data.keys():
		if key != 'QB':
			value = qb_data[key][i]
			if value not in [None, '']:
				print(qb_data[key][i])
				Observation.get_or_create(
					action = action,
					name = key,
					value = qb_data[key][i]
				)
Esempio n. 8
0
def copyProjectExperiments(project, new_project):

    t_experiments = Experiment.objects.filter(project=project)
    for experiment in t_experiments:

        new_experiment = Experiment(project=new_project,
                                    name=str(experiment.name),
                                    notes=str(experiment.notes))
        new_experiment.save()
        t_conditions = Condition.objects.filter(experiment=experiment)
        for condition in t_conditions:
            new_condition = Condition(experiment=new_experiment,
                                      name=str(condition.name),
                                      notes=str(condition.notes))
            new_condition.save()

            t_observations = Observation.objects.filter(condition=condition)
            for t_observation in t_observations:
                new_observation = Observation(
                    condition=new_condition,
                    species=t_observation.species,
                    time=t_observation.time,
                    value=t_observation.value,
                    stddev=t_observation.stddev,
                    steady_state=t_observation.steady_state,
                    min_steady_state=t_observation.min_steady_state,
                    max_steady_state=t_observation.max_steady_state)

                new_observation.save()

            t_treatments = Treatment.objects.filter(condition=condition)
            for t_treatment in t_treatments:
                new_treatment = Treatment(condition=new_condition,
                                          species=t_treatment.species,
                                          time=t_treatment.time,
                                          value=t_treatment.value)

                new_treatment.save()

            new_condition.save()
        new_experiment.save()
Esempio n. 9
0
def foi_popup():
    args = request.args.to_dict()
    if 'foi' in args:
        foi = args["foi"]
        filter_by = args.get("filter_by")
        popup_id = random.randrange(1, 100)
        data = Observation().get_filtered_for_foi(foi, filter_by)
        return render_template("popup.html",
                               data=data,
                               foi=foi,
                               popup_id=popup_id)
    else:
        return ""
Esempio n. 10
0
def observation(request, carerid, listenid):
    try:
        observation = Observation.objects.get(carer_id=carerid,
                                              listen_id=listenid)
    except ObjectDoesNotExist:
        observation = Observation(carer_id=carerid, listen_id=listenid)
    if request.method == "POST":
        form = ObservationForm(request.POST, instance=observation)
        form.save()
        return redirect('view_user', carerid, observation.listen.user.id)
    else:
        form = ObservationForm(instance=observation)
        return render(request, 'observation.html', {'form': form})
Esempio n. 11
0
def import_observations(observations):
    print("Importing observations")
    import_count = 0
    record_count = 0
    session = Session()
    # TODO: Commit intermittently, say after every 100 records
    for observation in observations:
        record_count += 1
        source_id = observation['subject']['reference'].split('/')[1]
        try:
            enc_id = observation['context']['reference'].split('/')[1]
        except KeyError as e:
            print(f"{e}: {source_id}")
        try:
            patient_id = session.query(
                Patient.id).where(Patient.source_id == source_id).one().id
        except NoResultFound:
            print(f"Patient with source_id {source_id} not found.")
            continue
        try:
            encounter_id = session.query(
                Encounter.id).where(Encounter.source_id == enc_id).one().id
        except NoResultFound:
            print(f"Encounter with source_id {enc_id} not found.")
            continue

        try:
            observation_date = observation['effectiveDateTime']
            observation_obj = Observation(
                source_id=observation['id'],
                patient_id=patient_id,
                encounter_id=encounter_id,
                observation_date=observation_date,
                type_code=observation['code']['coding'][0]['code'],
                type_code_system=observation['code']['coding'][0]['system'])
        except KeyError as e:
            print(e)
            import pprint
            pprint.pprint(observation)
            continue
        session.add(observation_obj)
        import_count += 1
        print(".", end='', flush=True)  # Show progress

    session.commit()
    session.close()
    print(f"\nImported {import_count} observations out of {record_count}")
Esempio n. 12
0
    def deserialize(self, session):
        patient_src_id = self.raw_data["subject"]["reference"].split("/")[0]
        patient_id = Patient.get_id_for_source_id(session, patient_src_id)

        encounter_src_id = self.raw_data["context"]["reference"].split("/")[0]
        encounter_id = Encounter.get_id_for_source_id(session,
                                                      encounter_src_id)

        if "component" in self.raw_data:
            components = [{
                "code": component["code"],
                "valueQuantity": component["valueQuantity"]
            } for component in self.raw_data["component"]]
        else:
            components = [{
                "code":
                self.raw_data["code"],
                "valueQuantity":
                self.raw_data.get("valueQuantity", None),
            }]

        return [
            Observation(
                source_id=self.raw_data["id"] + "-" + str(i),
                patient_id=patient_id,
                encounter_id=encounter_id,
                observation_date=self.raw_data["effectiveDateTime"].split("T")
                [0],
                type_code=component["code"]["coding"][0]["code"],
                type_code_system=component["code"]["coding"][0]["system"],
                value=component["valueQuantity"]["value"]
                if component["valueQuantity"] else None,
                unit_code=component["valueQuantity"]["unit"]
                if component["valueQuantity"] else None,
                unit_code_system=component["valueQuantity"]["system"]
                if component["valueQuantity"] else None,
            ) for i, component in enumerate(components)
        ]
Esempio n. 13
0
def refreshDBwithFHIR(request):
    #get json from dir
    with open("hit_server/FHIRJsonResponse.json") as data_file:
        data = json.load(data_file)

    for p in data.keys():
        print p, data[p]["Contact Info"]["first_name"], data[p][
            "Contact Info"]["last_name"]
        patientobj = PatientInfo()  # create a patient object.
        patientobj.pid = p
        patientobj.first_name = data[p]["Contact Info"]["first_name"]
        patientobj.last_name = data[p]["Contact Info"]["last_name"]
        patientobj.save()

        #first add all conditions to patient?

        all_conditions = data[p]["Conditions"].keys()
        for con in all_conditions:
            conditionobj = Condition()
            conditionobj.condition_name = data[p]["Conditions"][con]["name"]
            conditionobj.onset_date = data[p]["Conditions"][con]["onset_date"]
            conditionobj.condition_code = data[p]["Conditions"][con]["code"]
            conditionobj.condition_desc = data[p]["Conditions"][con]["text"]
            conditionobj.condition_for_patients = patientobj  # how to use related object

            conditionobj.save()

        all_medications = data[p]["Medications"].keys()

        if len(all_medications) == 0:
            pass

        else:
            for meds in all_medications:
                medicationobj = Medication()
                medicationobj.med_status = data[p]["Medications"][meds][
                    "status"]
                medicationobj.med_name = data[p]["Medications"][meds]["name"]
                medicationobj.med_code = data[p]["Medications"][meds]["code"]
                medicationobj.med_dosage_value = data[p]["Medications"][meds][
                    "dosage_value"]
                medicationobj.med_dosage_text = data[p]["Medications"][meds][
                    "dosage_text"]
                medicationobj.med_dosage_units = data[p]["Medications"][meds][
                    "dosage_units"]
                medicationobj.med_date_written = data[p]["Medications"][meds][
                    "date_written"]
                medicationobj.med_code_system = data[p]["Medications"][meds][
                    "code_system"]

                medicationobj.medications_for_patients = patientobj  # how to use related object

                medicationobj.save()

        all_observations = data[p]["Observations"].keys()
        for obs in all_observations:
            observationobj = Observation()
            observationobj.obs_name = data[p]["Observations"][obs]["name"]
            observationobj.obs_code = data[p]["Observations"][obs]["code"]
            observationobj.obs_desc = data[p]["Observations"][obs]["text"]

            check_existance_of_value = data[p]["Observations"][obs].keys()
            if "value" in check_existance_of_value:
                observationobj.obs_value = data[p]["Observations"][obs][
                    "value"]
            else:
                observationobj.obs_value = 0.0

            if "units" in check_existance_of_value:
                observationobj.obs_units = data[p]["Observations"][obs][
                    "units"]
            else:
                observationobj.obs_units = "NULL"

            observationobj.obs_date = data[p]["Observations"][obs]["date_time"]

            observationobj.obs_for_patients = patientobj  # how to use related object

            observationobj.save()

    return HttpResponse()
    '''
Esempio n. 14
0
def refreshDBwithFHIR(request):
	#get json from dir
	with open("hit_server/FHIRJsonResponse.json") as data_file:
		data = json.load(data_file)

	for p in data.keys():
		print p, data[p]["Contact Info"]["first_name"], data[p]["Contact Info"]["last_name"]
		patientobj = PatientInfo()  			# create a patient object.
		patientobj.pid = p
		patientobj.first_name = data[p]["Contact Info"]["first_name"]
		patientobj.last_name = data[p]["Contact Info"]["last_name"]
		patientobj.save()

		#first add all conditions to patient?

		all_conditions = data[p]["Conditions"].keys()
		for con in all_conditions:
			conditionobj = Condition()
			conditionobj.condition_name = data[p]["Conditions"][con]["name"]
			conditionobj.onset_date = data[p]["Conditions"][con]["onset_date"]
			conditionobj.condition_code = data[p]["Conditions"][con]["code"]
			conditionobj.condition_desc = data[p]["Conditions"][con]["text"]
			conditionobj.condition_for_patients = patientobj   # how to use related object

			conditionobj.save()

		all_medications = data[p]["Medications"].keys()

		if len(all_medications) == 0:
			pass

		else:
			for meds in all_medications:
				medicationobj = Medication()
				medicationobj.med_status = data[p]["Medications"][meds]["status"]
				medicationobj.med_name = data[p]["Medications"][meds]["name"]
				medicationobj.med_code = data[p]["Medications"][meds]["code"]
				medicationobj.med_dosage_value = data[p]["Medications"][meds]["dosage_value"]
				medicationobj.med_dosage_text = data[p]["Medications"][meds]["dosage_text"]
				medicationobj.med_dosage_units = data[p]["Medications"][meds]["dosage_units"]
				medicationobj.med_date_written = data[p]["Medications"][meds]["date_written"]
				medicationobj.med_code_system = data[p]["Medications"][meds]["code_system"]


				medicationobj.medications_for_patients = patientobj   # how to use related object

				medicationobj.save()


		all_observations = data[p]["Observations"].keys()
		for obs in all_observations:
			observationobj = Observation()
			observationobj.obs_name = data[p]["Observations"][obs]["name"]
			observationobj.obs_code = data[p]["Observations"][obs]["code"]
			observationobj.obs_desc = data[p]["Observations"][obs]["text"]

			check_existance_of_value = data[p]["Observations"][obs].keys()
			if "value" in check_existance_of_value:
				observationobj.obs_value = data[p]["Observations"][obs]["value"]
			else:
				observationobj.obs_value = 0.0

			if "units" in check_existance_of_value:
				observationobj.obs_units = data[p]["Observations"][obs]["units"]
			else:
				observationobj.obs_units = "NULL"


			observationobj.obs_date = data[p]["Observations"][obs]["date_time"]

			observationobj.obs_for_patients = patientobj   # how to use related object

			observationobj.save()

	return HttpResponse()

	'''
def range_hourly(station_id, year_start, year_end, month_start, month_end,
                 day_start, local_tz_name):
    """
    Calls Environment Canada endpoint and parses the returned XML into 
    StationData objects.
        
    Keyword arguments:
    station_id -- Integer corresponding to an Environment Canada station ID 
                  (ie. location of weather reading).
    year_start -- Integer indicating the year of the first weather history
                  request.
    year_end -- Integer indicating the year of the last weather history 
                request (inclusive). In combination with month_start and 
                month_end, all weather history between start and end times 
                will be requested.
    month_start -- Integer indicating the month of the first weather history 
                   request.
    month_end -- Integer indicating the month of the last weather history 
                 request (inclusive). In combination with year_start and 
                 year_end, all weather history between start and end times 
                 will be requested.
    day_start -- Integer indicating the starting day of the forecast, 
                 though multiple days of forecasted data will be returned.
    local_tz_name -- String representation of local timezone name 
                     (eg. 'America/Toronto').
                         
    Return:
    Two two-item vector [station, observations] where station is a 
    model.Station object and observations is a list of hourly 
    model.Observation objects.
    """
    # Instantiate objects that are returned by this method
    station = None
    observations = list()
    
    y = year_start
    m = month_start
    d = day_start
    req_date = datetime(y, m, d)
    end_date = datetime(year_end, month_end, day_start)
    while req_date <= end_date:
        xml_response = fetch_content(station_id=station_id, year_num=y,
                                    month_num=m, day_num_start=d, timeframe=1,
                                    frmt='xml')
        xml_string = xml_response.read().decode('utf-8')
        weather_root = ElementTree.fromstring(xml_string)
        
        # Only populate Station once
        if station == None:
            station = Station()
            station.station_id = station_id
            station.local_tz_str = local_tz_name
            station_local_tz = pytz.timezone(local_tz_name)
            epoch = datetime.utcfromtimestamp(0)
            offset_delta = station_local_tz.utcoffset(epoch)
            station_std_tz = timezone(offset_delta)
            for si_elmnt in weather_root.iter('stationinformation'):
                name_txt = si_elmnt.find('name').text
                if name_txt and name_txt != ' ':
                    station.name = name_txt
                    
                province_txt = si_elmnt.find('province').text
                if province_txt and province_txt != ' ':
                    station.province = province_txt
                
                latitude_txt = si_elmnt.find('latitude').text
                if latitude_txt and latitude_txt != ' ':
                    station.latitude = float(latitude_txt)
                
                longitude_txt = si_elmnt.find('longitude').text
                if longitude_txt and longitude_txt != ' ':
                    station.longitude = float(longitude_txt)
                    
                elevation_txt = si_elmnt.find('elevation').text
                if elevation_txt and elevation_txt != ' ':
                    station.elevation = float(elevation_txt)
                    
                climate_id_txt = si_elmnt.find('climate_identifier').text
                if climate_id_txt and climate_id_txt != ' ':
                    station.climate_identifier = int(climate_id_txt)
        
        # Iterate stationdata XML elements and append Observations to list
        for sd_elmnt in weather_root.iter('stationdata'):
            observation = Observation()
            
            # Get portions of date_time for observation
            year_txt = sd_elmnt.attrib['year']
            month_txt = sd_elmnt.attrib['month']
            day_txt = sd_elmnt.attrib['day']
            hour_txt = sd_elmnt.attrib['hour']
            minute_txt = sd_elmnt.attrib['minute']
            if year_txt and month_txt and day_txt and hour_txt and minute_txt:
                observation.obs_datetime_std = datetime(year=int(year_txt),
                                                         month=int(month_txt),
                                                         day=int(day_txt),
                                                         hour=int(hour_txt),
                                                         minute=int(minute_txt),
                                                         second=0,
                                                         microsecond=0,
                                                         tzinfo=station_std_tz)
                observation.obs_datetime_dst = observation.obs_datetime_std.astimezone(station_local_tz)
    
            if 'quality' in sd_elmnt.attrib:
                quality_txt = sd_elmnt.attrib['quality']
            else:
                quality_txt = None
            if quality_txt and quality_txt != ' ':
                observation.obs_quality = quality_txt
            
            # Set StationData fields based on child elements' values
            observation.station_id = station_id
            
            temp_txt = sd_elmnt.find('temp').text
            if temp_txt and temp_txt != ' ':
                observation.temp_c = float(temp_txt)
            
            dptemp_txt = sd_elmnt.find('dptemp').text
            if dptemp_txt and dptemp_txt != ' ':
                observation.dewpoint_temp_c = float(dptemp_txt)
            
            relhum_txt = sd_elmnt.find('relhum').text
            if relhum_txt and relhum_txt != ' ':
                observation.rel_humidity_pct = int(relhum_txt)
                
            winddir_txt = sd_elmnt.find('winddir').text
            if winddir_txt and winddir_txt != ' ':
                observation.wind_dir_deg = int(winddir_txt) * 10
                
            windspd_txt = sd_elmnt.find('windspd').text
            if windspd_txt and windspd_txt != ' ':
                observation.wind_speed_kph = int(windspd_txt)
                
            visibility_txt = sd_elmnt.find('visibility').text
            if visibility_txt and visibility_txt != ' ':
                observation.visibility_km = float(visibility_txt)
                
            stnpress_txt = sd_elmnt.find('stnpress').text
            if stnpress_txt and stnpress_txt != ' ':
                observation.station_pressure_kpa = float(stnpress_txt)
                
            humidex_txt = sd_elmnt.find('humidex').text
            if humidex_txt and humidex_txt != ' ':
                observation.humidex = float(humidex_txt)
                
            windchill_txt = sd_elmnt.find('windchill').text
            if windchill_txt and windchill_txt != ' ':
                observation.wind_chill = int(windchill_txt)
                
            observation.weather_desc = sd_elmnt.find('weather').text
            
            # Add StationData element to list
            observations.append(observation)
    
        # Increment year and month to populate date range
        if m < 12:
            m += 1
        else:
            y += 1
            m = 1
        req_date = datetime(y, m, d)
    
    # Return XML elements parsed into a list of StationData objects
    return [station, observations]
Esempio n. 16
0
# Initialise training environment and experience replay memory
env = GymEnv(cfg)
replay = Replay(cfg, env.action_size)
# Initialise dataset replay with S random seed episodes
for s in range(cfg['seed_episodes']):
    observation = env.reset()
    done = False
    while not done:
        next_observation, action, reward, done = env.step()
        replay.append(observation, action, reward, done)
        observation = next_observation

# Init PlaNet
transition_model = Transition(cfg)
observation_model = Observation(cfg)
reward_model = Reward(cfg)
encoder = Encoder(cfg)

optim = tf.train.AdamOptimizer(cfg['learning_rate'], epsilon=cfg['optim_eps'])
planner = MPCPlanner(cfg, env.action_size, transition_model, reward_model)
global_prior = tf.distributions.Normal(
    tf.zeros([cfg['batch_size'], cfg['state_size']]),
    tf.ones([cfg['batch_size'], cfg['state_size']]))  # Global prior N(0, I)
free_nats = tf.fill(dims=[
    1,
], value=cfg['free_nats'])  # Allowed deviation in KL divergence

# Training
for episode in trange(cfg['train']['episodes']):
    # Model fitting
Esempio n. 17
0
def create_observation(id):
    sos_res = Observation().create(id)
    response = make_response(sos_res.text, sos_res.status_code)
    response.headers['Content-Type'] = 'application/json; charset=utf-8'
    return response
Esempio n. 18
0
    def run(self):
        try:
            message = self.conn.recv(BUFFER_SIZE)
            if message != '':
                decoded_msg = message.decode('utf-8')
                if decoded_msg == "END":
                    self.conn.close()
                elif decoded_msg.startswith("aTLAS_trust_protocol::"):
                    trust_protocol_head, trust_protocol_message = decoded_msg.split(
                        "::")
                    trust_operation = trust_protocol_message.split("_")[0]
                    trust_value = 0.0
                    if trust_operation == "recommendation":
                        resource_id = trust_protocol_message.split("_")[1]
                        recency_str = trust_protocol_message.split("_")[-1]
                        recency_limit = datetime.strptime(
                            recency_str, BasicLogger.get_time_format_string())
                        trust_value = recommendation_response(
                            self.agent, resource_id, recency_limit, self.scale,
                            self.logger)
                    elif trust_operation == "popularity":
                        resource_id = trust_protocol_message.split("_")[1]
                        recency_str = trust_protocol_message.split("_")[-1]
                        recency_limit = datetime.strptime(
                            recency_str, BasicLogger.get_time_format_string())
                        trust_value = popularity_response(
                            self.agent, resource_id, recency_limit, self.scale,
                            self.logger)
                    trust_response = f"{trust_protocol_head}::{trust_protocol_message}::{trust_value}"
                    self.conn.send(bytes(trust_response, 'UTF-8'))
                else:
                    observation = Observation(**json.loads(decoded_msg))
                    resource_id = None
                    if 'uri' in observation.details:
                        resource_id = observation.details['uri']
                    self.logger.write_to_agent_message_log(observation)
                    if '__init__' in self.agent_behavior:
                        trust_value = eval_trust_with_init(
                            self.agent, observation.sender, observation.topic,
                            self.agent_behavior, self.scale, self.logger,
                            self.discovery)
                    else:
                        trust_value = eval_trust(self.agent,
                                                 observation.sender,
                                                 observation,
                                                 self.agent_behavior,
                                                 self.scale, self.logger,
                                                 self.discovery)
                    self.logger.write_to_agent_history(self.agent,
                                                       observation.sender,
                                                       trust_value,
                                                       resource_id)
                    self.logger.write_to_trust_log(self.agent,
                                                   observation.sender,
                                                   trust_value, resource_id)

                    # topic trust log is written within the trust evaluation
                    # self.logger.write_to_agent_topic_trust(self.agent, observation.sender, observation.topic, trust_value, resource_id)

                    # TODO: how to work with trust decisions in general?
                    # if float(trust_value) < self.scenario.trust_thresholds['lower_limit']:
                    #     untrustedAgents.append(other_agent)
                    #     print("+++" + current_agent + ", nodes beyond redemption: " + other_agent + "+++")
                    # if float(trust_value) > self.scenario.trust_thresholds['upper_limit'] or float(trust_value) > 1:
                    #     self.scenario.authority.append(current_agent[2:3])
                    # print("Node " + str(self.id) + " Server received data:", observation[2:-1])
                    self.conn.send(bytes('standard response', 'UTF-8'))
                    self.observations_done.append(observation.serialize())
        except BrokenPipeError:
            pass
        return True
def range_hourly(station_id, year_start, year_end, month_start, month_end,
                 day_start, local_tz_name):
    """
    Calls Environment Canada endpoint and parses the returned XML into 
    StationData objects.
        
    Keyword arguments:
    station_id -- Integer corresponding to an Environment Canada station ID 
                  (ie. location of weather reading).
    year_start -- Integer indicating the year of the first weather history
                  request.
    year_end -- Integer indicating the year of the last weather history 
                request (inclusive). In combination with month_start and 
                month_end, all weather history between start and end times 
                will be requested.
    month_start -- Integer indicating the month of the first weather history 
                   request.
    month_end -- Integer indicating the month of the last weather history 
                 request (inclusive). In combination with year_start and 
                 year_end, all weather history between start and end times 
                 will be requested.
    day_start -- Integer indicating the starting day of the forecast, 
                 though multiple days of forecasted data will be returned.
    local_tz_name -- String representation of local timezone name 
                     (eg. 'America/Toronto').
                         
    Return:
    Two two-item vector [station, observations] where station is a 
    model.Station object and observations is a list of hourly 
    model.Observation objects.
    """
    # Instantiate objects that are returned by this method
    station = None
    observations = list()

    y = year_start
    m = month_start
    d = day_start
    req_date = datetime(y, m, d)
    end_date = datetime(year_end, month_end, day_start)
    while req_date <= end_date:
        xml_response = fetch_content(station_id=station_id,
                                     year_num=y,
                                     month_num=m,
                                     day_num_start=d,
                                     timeframe=1,
                                     frmt='xml')
        xml_string = xml_response.read().decode('utf-8')
        weather_root = ElementTree.fromstring(xml_string)

        # Only populate Station once
        if station == None:
            station = Station()
            station.station_id = station_id
            station.local_tz_str = local_tz_name
            station_local_tz = pytz.timezone(local_tz_name)
            epoch = datetime.utcfromtimestamp(0)
            offset_delta = station_local_tz.utcoffset(epoch)
            station_std_tz = timezone(offset_delta)
            for si_elmnt in weather_root.iter('stationinformation'):
                name_txt = si_elmnt.find('name').text
                if name_txt and name_txt != ' ':
                    station.name = name_txt

                province_txt = si_elmnt.find('province').text
                if province_txt and province_txt != ' ':
                    station.province = province_txt

                latitude_txt = si_elmnt.find('latitude').text
                if latitude_txt and latitude_txt != ' ':
                    station.latitude = float(latitude_txt)

                longitude_txt = si_elmnt.find('longitude').text
                if longitude_txt and longitude_txt != ' ':
                    station.longitude = float(longitude_txt)

                elevation_txt = si_elmnt.find('elevation').text
                if elevation_txt and elevation_txt != ' ':
                    station.elevation = float(elevation_txt)

                climate_id_txt = si_elmnt.find('climate_identifier').text
                if climate_id_txt and climate_id_txt != ' ':
                    station.climate_identifier = int(climate_id_txt)

        # Iterate stationdata XML elements and append Observations to list
        for sd_elmnt in weather_root.iter('stationdata'):
            observation = Observation()

            # Get portions of date_time for observation
            year_txt = sd_elmnt.attrib['year']
            month_txt = sd_elmnt.attrib['month']
            day_txt = sd_elmnt.attrib['day']
            hour_txt = sd_elmnt.attrib['hour']
            minute_txt = sd_elmnt.attrib['minute']
            if year_txt and month_txt and day_txt and hour_txt and minute_txt:
                observation.obs_datetime_std = datetime(year=int(year_txt),
                                                        month=int(month_txt),
                                                        day=int(day_txt),
                                                        hour=int(hour_txt),
                                                        minute=int(minute_txt),
                                                        second=0,
                                                        microsecond=0,
                                                        tzinfo=station_std_tz)
                observation.obs_datetime_dst = observation.obs_datetime_std.astimezone(
                    station_local_tz)

            if 'quality' in sd_elmnt.attrib:
                quality_txt = sd_elmnt.attrib['quality']
            else:
                quality_txt = None
            if quality_txt and quality_txt != ' ':
                observation.obs_quality = quality_txt

            # Set StationData fields based on child elements' values
            observation.station_id = station_id

            temp_txt = sd_elmnt.find('temp').text
            if temp_txt and temp_txt != ' ':
                observation.temp_c = float(temp_txt)

            dptemp_txt = sd_elmnt.find('dptemp').text
            if dptemp_txt and dptemp_txt != ' ':
                observation.dewpoint_temp_c = float(dptemp_txt)

            relhum_txt = sd_elmnt.find('relhum').text
            if relhum_txt and relhum_txt != ' ':
                observation.rel_humidity_pct = int(relhum_txt)

            winddir_txt = sd_elmnt.find('winddir').text
            if winddir_txt and winddir_txt != ' ':
                observation.wind_dir_deg = int(winddir_txt) * 10

            windspd_txt = sd_elmnt.find('windspd').text
            if windspd_txt and windspd_txt != ' ':
                observation.wind_speed_kph = int(windspd_txt)

            visibility_txt = sd_elmnt.find('visibility').text
            if visibility_txt and visibility_txt != ' ':
                observation.visibility_km = float(visibility_txt)

            stnpress_txt = sd_elmnt.find('stnpress').text
            if stnpress_txt and stnpress_txt != ' ':
                observation.station_pressure_kpa = float(stnpress_txt)

            humidex_txt = sd_elmnt.find('humidex').text
            if humidex_txt and humidex_txt != ' ':
                observation.humidex = float(humidex_txt)

            windchill_txt = sd_elmnt.find('windchill').text
            if windchill_txt and windchill_txt != ' ':
                observation.wind_chill = int(windchill_txt)

            observation.weather_desc = sd_elmnt.find('weather').text

            # Add StationData element to list
            observations.append(observation)

        # Increment year and month to populate date range
        if m < 12:
            m += 1
        else:
            y += 1
            m = 1
        req_date = datetime(y, m, d)

    # Return XML elements parsed into a list of StationData objects
    return [station, observations]