<commit_before>__author__ = 'monk-ee'

"""This module provides an interface to the billing report
in the amazon S3 billing bucket.
"""

import os
import boto
import zipfile
from boto.s3.connection import S3Connection
from datetime import datetime
from boto.s3.key import Key
import yaml
from flask import flash

class Fetch():
    configFile = os.path.abspath('NHDH/config.yml')
    csvFolder = os.path.abspath('NHDH/csv')

    def __init__(self):
        #load configuration
        self.configStr = open(self.configFile, 'r')
        self.configObj =  yaml.load(self.configStr)
        #now set detailed billing file
        self.billingCsv = str(self.configObj['s3']['account_number'])+"-aws-billing-detailed-line-items-with-resources-and-tags-"+self.datefilename()+".csv"
        self.billingFile = str(self.configObj['s3']['account_number'])+"-aws-billing-detailed-line-items-with-resources-and-tags-"+self.datefilename()+".csv.zip"
        self.billingZip = os.path.abspath('NHDH/csv/'+self.billingFile)

    def datefilename(self):
        dt = datetime.now()
        return dt.strftime("%Y-%m")

    def fetch(self):
        # get a bucket connection
        bucketConn = S3Connection(self.configObj['s3']['aws_access_key'], self.configObj['s3']['aws_secret_key'])
        #file name
        billingHandle = bucketConn.get_bucket(self.configObj['s3']['billing_bucket'])
        #key object fudging
        billingFileNameKey = Key(billingHandle)
        billingFileNameKey.key = self.billingFile
        #fetch the file to a temporary place on the filesystem
        try:
                retrieveFile = billingFileNameKey.get_contents_to_filename(self.billingZip)
                flash('Successfully got file from S3.')
                self.unzipper()
                self.unlink()
        except boto.exception.S3ResponseError, emsg:
                flash' S3ResponseError : '+self.billingFile+' '+str(emsg[0])+' '+emsg[1]+' '+str(emsg[2]))

    def unzipper(self):
        zipHandle = zipfile.ZipFile(self.billingZip, mode='r')
        for subfile in zipHandle.namelist():
            zipHandle.extract(subfile, self.csvFolder)

    def unlink(self):
        try:
            with open(self.billingZip):
             os.remove(self.billingZip)
        except IOError:
             flash('Could not delete downloaded zip file.')
<commit_msg>remove sijax and place flash messaging into logic - missed a bracket<commit_after>__author__ = 'monk-ee'

"""This module provides an interface to the billing report
in the amazon S3 billing bucket.
"""

import os
import boto
import zipfile
from boto.s3.connection import S3Connection
from datetime import datetime
from boto.s3.key import Key
import yaml
from flask import flash

class Fetch():
    configFile = os.path.abspath('NHDH/config.yml')
    csvFolder = os.path.abspath('NHDH/csv')

    def __init__(self):
        #load configuration
        self.configStr = open(self.configFile, 'r')
        self.configObj =  yaml.load(self.configStr)
        #now set detailed billing file
        self.billingCsv = str(self.configObj['s3']['account_number'])+"-aws-billing-detailed-line-items-with-resources-and-tags-"+self.datefilename()+".csv"
        self.billingFile = str(self.configObj['s3']['account_number'])+"-aws-billing-detailed-line-items-with-resources-and-tags-"+self.datefilename()+".csv.zip"
        self.billingZip = os.path.abspath('NHDH/csv/'+self.billingFile)

    def datefilename(self):
        dt = datetime.now()
        return dt.strftime("%Y-%m")

    def fetch(self):
        # get a bucket connection
        bucketConn = S3Connection(self.configObj['s3']['aws_access_key'], self.configObj['s3']['aws_secret_key'])
        #file name
        billingHandle = bucketConn.get_bucket(self.configObj['s3']['billing_bucket'])
        #key object fudging
        billingFileNameKey = Key(billingHandle)
        billingFileNameKey.key = self.billingFile
        #fetch the file to a temporary place on the filesystem
        try:
                retrieveFile = billingFileNameKey.get_contents_to_filename(self.billingZip)
                flash('Successfully got file from S3.')
                self.unzipper()
                self.unlink()
        except boto.exception.S3ResponseError, emsg:
                flash(' S3ResponseError : '+self.billingFile+' '+str(emsg[0])+' '+emsg[1]+' '+str(emsg[2]))

    def unzipper(self):
        zipHandle = zipfile.ZipFile(self.billingZip, mode='r')
        for subfile in zipHandle.namelist():
            zipHandle.extract(subfile, self.csvFolder)

    def unlink(self):
        try:
            with open(self.billingZip):
             os.remove(self.billingZip)
        except IOError:
             flash('Could not delete downloaded zip file.')
<|endoftext|>


<commit_before>#!/usr/bin/python

# Naomi Hiebert coded this

import string
import sys
import os

#import global variables
import globalVars

import macroExpand

	#Buffer for output instructions
IBuffer = []

	#Buffer for output data
DBuffer = []

# I know it looks weird having the main function hide
# all the way down here, but it avoids the need to
# force the interpreter to load all the other stuff
# first. Besides, a good main function is usually
# pretty sparse.

#main function

#parse argument vector
if len(sys.argv) < 2:
	print "no input file specified!"
	quit()
if len(sys.argv) < 3:
	print "no output file specified!"
	quit()

#Start the queue
globalVars.FList.append(sys.argv[1])

#open the appropriate files
outFile = open(sys.argv[2], "w")

#deal with files
while globalVars.FIndex < len(globalVars.FList):

	#first file breaks, everything breaks
	#other files break, not the worst thing
	try:
		inFile = open(globalVars.FList[globalVars.FIndex], "r")
	except OSError:
		if FIndex > 0:
			print "Serious exception: Included file not opened! " + globalVars.FList[gobalVars.FIndex] + '\n'
			print "Proceeding with other files. Output may not be valid.\n"
		else:
			print "Fatal exception: Base file not opened!"
			raise

	#keep enforcing the organisation with files, but not between them.
	globalVars.DataFields = False

	#comments good
	DBuffer.append("\n;Start of data from " + globalVars.FList[globalVars.FIndex] + "\n")
	IBuffer.append("\n;Start of code from " + globalVars.FList[globalVars.FIndex] + "\n")

	#use the functions we defined above
	for line in inFile.readlines():
		expandedLine = macroExpand.expandline(line.split())
		for expLine in expandedLine:
			if(globalVars.DataFields):
				DBuffer.append(expLine)
			else:
				IBuffer.append(expLine)

	#comments foamy
	DBuffer.append("\n;End of data from " + globalVars.FList[globalVars.FIndex])
	IBuffer.append("\n;End of code from " + globalVars.FList[globalVars.FIndex])

	#housekeeping
	globalVars.FIndex += 1
	inFile.close()

#Add the one line of INF header that we care about
outFile.write("INF " + str(globalVars.BAddr) + '\n')

#dump the buffers
for ILine in IBuffer:
	outFile.write(ILine + '\n')

outFile.write(";Start of data sections")

for DLine in DBuffer:
	outFile.write(DLine + '\n')

#declare macro scratch space
if globalVars.memUsed > 0:
	outFile.write("\n;memory space used by macros\n")
	outFile.write("macro: .data " + str(globalVars.memUsed + 1) + "\n\n")

#clean up after ourselves
outFile.close()

#done!

<commit_msg>I changed to mac4 months ago, but still need to maintain NKMacroASM... life is annoying.<commit_after>#!/usr/bin/python

# Naomi Hiebert coded this

import string
import sys
import os

#import global variables
import globalVars

import macroExpand

	#Buffer for output instructions
IBuffer = []

	#Buffer for output data
DBuffer = []

# I know it looks weird having the main function hide
# all the way down here, but it avoids the need to
# force the interpreter to load all the other stuff
# first. Besides, a good main function is usually
# pretty sparse.

#main function

#parse argument vector
if len(sys.argv) < 2:
	print "no input file specified!"
	quit()
if len(sys.argv) < 3:
	print "no output file specified!"
	quit()

#Start the queue
globalVars.FList.append(sys.argv[1])

#open the appropriate files
outFile = open(sys.argv[2], "w")

#deal with files
while globalVars.FIndex < len(globalVars.FList):

	#first file breaks, everything breaks
	#other files break, not the worst thing
	try:
		inFile = open(globalVars.FList[globalVars.FIndex], "r")
	except OSError:
		if FIndex > 0:
			print "Serious exception: Included file not opened! " + globalVars.FList[gobalVars.FIndex] + '\n'
			print "Proceeding with other files. Output may not be valid.\n"
		else:
			print "Fatal exception: Base file not opened!"
			raise

	#keep enforcing the organisation with files, but not between them.
	globalVars.DataFields = False

	#comments good
	DBuffer.append("\n;Start of data from " + globalVars.FList[globalVars.FIndex] + "\n")
	IBuffer.append("\n;Start of code from " + globalVars.FList[globalVars.FIndex] + "\n")

	#use the functions we defined above
	for line in inFile.readlines():
		expandedLine = macroExpand.expandline(line.split())
		for expLine in expandedLine:
			if(globalVars.DataFields):
				DBuffer.append(expLine)
			else:
				IBuffer.append(expLine)

	#comments foamy
	DBuffer.append("\n;End of data from " + globalVars.FList[globalVars.FIndex])
	IBuffer.append("\n;End of code from " + globalVars.FList[globalVars.FIndex])

	#housekeeping
	globalVars.FIndex += 1
	inFile.close()

#Add the one line of INF header that we care about
outFile.write("INF " + str(globalVars.BAddr) + '\n')

#dump the buffers
for ILine in IBuffer:
	outFile.write(ILine + '\n')

outFile.write(";Start of data sections")

for DLine in DBuffer:
	outFile.write(DLine + '\n')

#declare macro scratch space
if globalVars.memUsed >= 0:
	outFile.write("\n;memory space used by macros\n")
	outFile.write("macro: .data " + str(globalVars.memUsed + 1) + "\n\n")

#clean up after ourselves
outFile.close()

#done!

<|endoftext|>


<commit_before>import json

from OHA.Diabetes import Diabetes
from OHA.__assessments import assess_waist_hip_ratio, assess_smoking_status, assess_blood_pressure, assess_bmi, \
    has_condition, assess_diet, assess_physical_activity
from OHA.param_builders.diabetes_param_builder import DiabetesParamsBuilder

__author__ = 'indrajit'
__email__ = 'eendroroy@gmail.com'

class HEARTS(object):
    """

    """

    @staticmethod
    def estimate_cvd_risk(age, high_risk_condition):
        # If age > assessment_age and NO high risk conditions
        assessment_age = 40

        if age < assessment_age:
            return False, "Not for CVD Risk as Age < 40"
        elif high_risk_condition[0]:
            return False, "Has High Risk Condition"
        else:
            return True, "Continue"

    @staticmethod
    def calculate_diabetes_status(conditions, bsl_type, bsl_units, bsl_value):

        # "assessment": "blood pressure off target",
        # assessment_message
        # "assessment_code" : ""
        # "bp": "145/91",
        # "target": 130,
        # "target_message": "target set for patient with diabetes"

        _assessment = ""
        _assessment_code = ""
        _assessment_message = ""
        _target = ""
        _target_message = ""

        diabetes_data = {}
        # move to a helper function
        if bsl_units == 'mg/dl':
            bsl_value = round(float(bsl_value) / 18, 1)

        for condition in conditions:
            if condition == "diabetes":
                # return True
                _assessment = True
                _assessment_message = "History of Diabetes"
                _assessment_code = 'BSL-R'
                _target = '',
                _target_message = ''
            else:
                if bsl_type == "random":
                    if bsl_value >= 11.1:
                        _assessment = True
                        _assessment_message = "Looks like newly diagnosed diabetes"
                        _assessment_code = 'DM-NEW'
                        _target = ''
                        _target_message = ''
                        # diabetes_data['dx'] = True
                        # diabetes_data['diagnosis_type'] = 'DM-New'
                        # diabetes_data['code'] = 'BSL-R'
                        # return (diabetes_data, "Looks like newly diagnosed diabetes")
                    elif bsl_value > 7:
                        _assessment = False
                        _assessment_message = "You are at risk of developing Diabetes"
                        _assessment_code = 'DM-PRE'
                        _target = ''
                        _target_message = ''
                        # diabetes_data['dx'] = False
                        # diabetes_data['diagnosis_type'] = 'DM-Pre'
                        # diabetes_data['code'] = 'BSL-A'
                        # return (diabetes_data, "You are at risk of developing diabetes")
                    else:
                        _assessment = False
                        _assessment_message = "Normal Blood Sugar. No Diabetes"
                        _assessment_code = 'DM-NONE'
                        _target = ''
                        _target_message = ''
                        # diabetes_data['dx'] = False
                        # diabetes_data['diagnosis_type'] = 'DM-NA'
                        # diabetes_data['code'] = 'BSL-G'
                        # return (diabetes_data, "BSL-G", "Blood sugar normal")
            # return False
            diabetes_output = {
                'value': bsl_value,
                'assessment': _assessment,
                'assessment_code': _assessment_code,
                'assessment_message': _assessment_message,
                'target': _target,
                'target_message': _target_message
            }

            return diabetes_output

    @staticmethod
    def load_messages():
        file = 'guideline_content.json'
        with open(file) as json_data:
            data = json.load(json_data)

        return data["body"]["messages"]

    @staticmethod
    def load_guidelines(guideline_key):
        file = 'guideline_hearts.json'
        with open(file) as json_data:
            data = json.load(json_data)

        return data

    @staticmethod
    def high_risk_condition_check(age, blood_pressure, conditions):

        _assessment = ""
        _assessment_code = ""
        _target = ""
        _target_message = ""

        # Known heart disease, stroke, transient ischemic attack, DM, kidney disease (for assessment, if this has not been done)
        high_risk_conditions = ['CVD', 'CAD', 'AMI', 'HEART ATTACK', 'CVA', 'TIA', 'STROKE', 'CKD', 'PVD']
        # Return whether medical history contains any of these

        for condition in conditions:
            print(condition)
            if condition.upper() in high_risk_conditions:
                # _assessment = True
                # _assessment_message = 'Has High Risk Condition %s' % condition.upper()
                # _assessment_code = ""
                return (True, 'Has High Risk Condition %s' % condition.upper())

        # check for other high risk states such as BP > 160 and age > 60 + diabetes (including newly suggested)
        # if (assessment[])
        # blood pressure [value, observation_type]
        sbp = blood_pressure['sbp'][0]
        dbp = blood_pressure['dbp'][0]

        if ((sbp > 200) or (dbp > 120)):
            # _assessment = True
            # _assessment_code = "HRC-HTN"
            return (True, "HRC-HTN", 'Severely high blood pressure. Seek emergency care immediately')
        elif ((age < 40) and ((sbp >= 140) or (dbp >= 90))):
            # _assessment = True
            # _assessment_code = "HRC-AGE-BP"
            return (
                True, "HRC-AGE-BP",
                'High blood pressure in under 40, should be investigated for secondary hypertension')
        else:
            # _assessment = False
            # _assessment = "No High Risk Condition"
            return False, "No High Risk Condition"

    @staticmethod
    def calc_cvd_risk(inputParams):
        """
        Call the relevant WHO model and compute
        """

        # cvd_risk = (20, '10-20')
        cvd_risk = (30, '20-30')
        return cvd_risk

    @staticmethod
    def calculate():
        assessment = {}

        # load guidelines
        guidelines = HEARTS.load_guidelines('hearts')["body"]

        # load message
        messages = HEARTS.load_messages()

        # load in the response object
        with open('request.json') as json_data:
            data = json.load(json_data)

        # unpack the request, validate it and set up the params
        demographics = data['body']['demographics']
        measurements = data['body']['measurements']
        smoking = data['body']['smoking']
        physical_activity = data['body']['physical_activity']
        diet_history = data['body']['diet_history']
        medical_history = data['body']['medical_history']
        pathology = data['body']['pathology']
        medications = []

        print('--- Running lifestyle and risk factor assessment ---')
        bmi = assess_bmi(measurements['weight'], measurements['height'])
        whr = assess_waist_hip_ratio(measurements['waist'], measurements['hip'], demographics['gender'])
        smoker = assess_smoking_status(smoking)

        # assess diabetes status or risk
        print('--- Check diabetes status---')
        diabetes_status = HEARTS.calculate_diabetes_status(
            medical_history, pathology['bsl']['type'], pathology['bsl']['units'], pathology['bsl']['value']
        )

        print(diabetes_status)
        # If does not have diabetes
        if diabetes_status['assessment_code'] == 'DM-NONE':
            # calculate diabetes risk score
            diabetes_params = DiabetesParamsBuilder() \
                .gender(demographics['gender']) \
                .age(demographics['age']) \
                .waist(measurements['waist']) \
                .hip(measurements['hip']) \
                .height(measurements['height']) \
                .weight(measurements['weight']) \
                .sbp(measurements['sbp']) \
                .dbp(measurements['dbp']) \
                .build()
            diabetes_risk = Diabetes().calculate(diabetes_params)['risk']
        else:
            # newly diagnosed diabetes, add to existing conditions list
            conditions = medical_history['conditions']
            conditions.append('diabetes')
            medical_history['conditions'] = conditions
            diabetes_risk = "NA"
            # print('diabetes status is %s ' % assessment['diabetes_status'][1])

        diabetes_status['risk'] = diabetes_risk
        assessment['diabetes'] = diabetes_status
        '''assessment['diabetes'] = {
                        'risk' : diabetes_risk,
                        'status' : diabetes_status
                    }
                '''
        print('--- High Risk Condition Check ---')
        blood_pressure = {
            'sbp': measurements['sbp'],
            'dbp': measurements['dbp']
        }
        print(blood_pressure)

        bp_assessment = assess_blood_pressure(blood_pressure, medical_history['conditions'], medications)
        assessment['blood_pressure'] = bp_assessment
        '''
        assessment['blood_pressure'] = {
            'bp' : str(measurements['sbp'][0]) + "/" + str(measurements['dbp'][0]),
            'assessment_code' : bp_assessment[0],
            'assessment' : bp_assessment[1],
            'target' : bp_assessment[2],
            'target_message' : bp_assessment[3]
        }
        '''

        diet = assess_diet(diet_history, medical_history['conditions'])

        exercise = assess_physical_activity(physical_activity)

        assessment['lifestyle'] = {
            'bmi': bmi,
            'whr': whr,
            'diet': diet,
            'exercise': exercise,
            'smoking': smoker
        }

        age = demographics['age']
        # work out how to add in diabetes if newly diagnosed?
        print(medical_history['conditions'])
        high_risk_condition = HEARTS.high_risk_condition_check(demographics['age'], blood_pressure,
                                                               medical_history['conditions'])
        print(high_risk_condition)

        assessment['cvd_assessment'] = {
            'high_risk_condition': high_risk_condition
        }

        # Determine whether eligible for CVD risk assessment
        estimate_cvd_risk_calc = HEARTS.estimate_cvd_risk(age, high_risk_condition)
        # print('high risk output %s ' % assessment['high_risk'][0])
        # if not high_risk_condition[0]:
        if estimate_cvd_risk_calc[0]:
            # if not already at high risk then calculate CVD risk
            print('do cvd check')
            # use the WHO model to do the cvd risk assessment
            cvd_risk = HEARTS.calc_cvd_risk(assessment)
            # use the key to look up the guidelines output
            assessment['cvd_assessment']['cvd_risk_result'] = cvd_risk
            print(cvd_risk)
            assessment['cvd_assessment']['guidelines'] = guidelines['cvd_risk'][cvd_risk[1]]
            # print(guidelines['cvd_risk'][assessment['cvd_risk'][1]])
        else:
            cvd_calc = estimate_cvd_risk_calc[1]
            print(cvd_calc)
            # assessment['cvd_assessment']['guidelines'] = guidelines['cvd_risk']['Refer']

        # finally formualate guidelines
        print("--- Writing Your Results ----")
        with open('response.json', 'w') as fp:
            json.dump(assessment, fp)

if __name__ == "__main__":
    print('---Starting Hearts Assessment----')
    HEARTS().calculate()
<commit_msg>refactor<commit_after>import json

import os

from OHA.Diabetes import Diabetes
from OHA.__assessments import assess_waist_hip_ratio, assess_smoking_status, assess_blood_pressure, assess_bmi, \
    has_condition, assess_diet, assess_physical_activity
from OHA.param_builders.diabetes_param_builder import DiabetesParamsBuilder

__author__ = 'indrajit'
__email__ = 'eendroroy@gmail.com'

class HEARTS(object):
    """

    """

    @staticmethod
    def estimate_cvd_risk(age, high_risk_condition):
        # If age > assessment_age and NO high risk conditions
        assessment_age = 40

        if age < assessment_age:
            return False, "Not for CVD Risk as Age < 40"
        elif high_risk_condition[0]:
            return False, "Has High Risk Condition"
        else:
            return True, "Continue"

    @staticmethod
    def calculate_diabetes_status(conditions, bsl_type, bsl_units, bsl_value):

        # "assessment": "blood pressure off target",
        # assessment_message
        # "assessment_code" : ""
        # "bp": "145/91",
        # "target": 130,
        # "target_message": "target set for patient with diabetes"

        _assessment = ""
        _assessment_code = ""
        _assessment_message = ""
        _target = ""
        _target_message = ""

        diabetes_data = {}
        # move to a helper function
        if bsl_units == 'mg/dl':
            bsl_value = round(float(bsl_value) / 18, 1)

        for condition in conditions:
            if condition == "diabetes":
                # return True
                _assessment = True
                _assessment_message = "History of Diabetes"
                _assessment_code = 'BSL-R'
                _target = '',
                _target_message = ''
            else:
                if bsl_type == "random":
                    if bsl_value >= 11.1:
                        _assessment = True
                        _assessment_message = "Looks like newly diagnosed diabetes"
                        _assessment_code = 'DM-NEW'
                        _target = ''
                        _target_message = ''
                        # diabetes_data['dx'] = True
                        # diabetes_data['diagnosis_type'] = 'DM-New'
                        # diabetes_data['code'] = 'BSL-R'
                        # return (diabetes_data, "Looks like newly diagnosed diabetes")
                    elif bsl_value > 7:
                        _assessment = False
                        _assessment_message = "You are at risk of developing Diabetes"
                        _assessment_code = 'DM-PRE'
                        _target = ''
                        _target_message = ''
                        # diabetes_data['dx'] = False
                        # diabetes_data['diagnosis_type'] = 'DM-Pre'
                        # diabetes_data['code'] = 'BSL-A'
                        # return (diabetes_data, "You are at risk of developing diabetes")
                    else:
                        _assessment = False
                        _assessment_message = "Normal Blood Sugar. No Diabetes"
                        _assessment_code = 'DM-NONE'
                        _target = ''
                        _target_message = ''
                        # diabetes_data['dx'] = False
                        # diabetes_data['diagnosis_type'] = 'DM-NA'
                        # diabetes_data['code'] = 'BSL-G'
                        # return (diabetes_data, "BSL-G", "Blood sugar normal")
            # return False
            diabetes_output = {
                'value': bsl_value,
                'assessment': _assessment,
                'assessment_code': _assessment_code,
                'assessment_message': _assessment_message,
                'target': _target,
                'target_message': _target_message
            }

            return diabetes_output

    @staticmethod
    def load_messages():
        filename = 'guideline_content.json'
        file_path = ('%s/%s' % (
            os.path.dirname(os.path.realpath(__file__)),
            filename
        ))
        with open(file_path) as json_data:
            data = json.load(json_data)

        return data["body"]["messages"]

    @staticmethod
    def load_guidelines(guideline_key):
        filename = 'guideline_hearts.json'
        file_path = ('%s/%s' % (
            os.path.dirname(os.path.realpath(__file__)),
            filename
        ))
        with open(file_path) as json_data:
            data = json.load(json_data)

        return data

    @staticmethod
    def high_risk_condition_check(age, blood_pressure, conditions):
        # Known heart disease, stroke, transient ischemic attack, DM, kidney disease (for assessment, if this has not
        #  been done)
        high_risk_conditions = ['CVD', 'CAD', 'AMI', 'HEART ATTACK', 'CVA', 'TIA', 'STROKE', 'CKD', 'PVD']
        # Return whether medical history contains any of these

        for condition in conditions:
            print(condition)
            if condition.upper() in high_risk_conditions:
                return True, 'Has High Risk Condition %s' % condition.upper()

        # check for other high risk states such as BP > 160 and age > 60 + diabetes (including newly suggested)
        # if (assessment[])
        # blood pressure [value, observation_type]
        sbp = blood_pressure['sbp'][0]
        dbp = blood_pressure['dbp'][0]

        if sbp > 200 or dbp > 120:
            return True, "HRC-HTN", 'Severely high blood pressure. Seek emergency care immediately'
        elif age < 40 and (sbp >= 140 or dbp >= 90):
            return (
                True, "HRC-AGE-BP",
                'High blood pressure in under 40, should be investigated for secondary hypertension')
        else:
            return False, "No High Risk Condition"

    @staticmethod
    def calc_cvd_risk(inputParams):
        """
        Call the relevant WHO model and compute
        """

        # cvd_risk = (20, '10-20')
        cvd_risk = (30, '20-30')
        return cvd_risk

    @staticmethod
    def calculate():
        assessment = {}

        # load guidelines
        guidelines = HEARTS.load_guidelines('hearts')["body"]

        # load message
        messages = HEARTS.load_messages()

        # load in the response object

        file_path = ('%s/%s' % (
            os.path.dirname(os.path.realpath(__file__)),
            'request.json'
        ))
        with open(file_path) as json_data:
            data = json.load(json_data)

        # unpack the request, validate it and set up the params
        demographics = data['body']['demographics']
        measurements = data['body']['measurements']
        smoking = data['body']['smoking']
        physical_activity = data['body']['physical_activity']
        diet_history = data['body']['diet_history']
        medical_history = data['body']['medical_history']
        pathology = data['body']['pathology']
        medications = []

        print('--- Running lifestyle and risk factor assessment ---')
        bmi = assess_bmi(measurements['weight'], measurements['height'])
        whr = assess_waist_hip_ratio(measurements['waist'], measurements['hip'], demographics['gender'])
        smoker = assess_smoking_status(smoking)

        # assess diabetes status or risk
        print('--- Check diabetes status---')
        diabetes_status = HEARTS.calculate_diabetes_status(
            medical_history, pathology['bsl']['type'], pathology['bsl']['units'], pathology['bsl']['value']
        )

        print(diabetes_status)
        # If does not have diabetes
        if diabetes_status['assessment_code'] == 'DM-NONE':
            # calculate diabetes risk score
            diabetes_params = DiabetesParamsBuilder() \
                .gender(demographics['gender']) \
                .age(demographics['age']) \
                .waist(measurements['waist']) \
                .hip(measurements['hip']) \
                .height(measurements['height']) \
                .weight(measurements['weight']) \
                .sbp(measurements['sbp']) \
                .dbp(measurements['dbp']) \
                .build()
            diabetes_risk = Diabetes().calculate(diabetes_params)['risk']
        else:
            # newly diagnosed diabetes, add to existing conditions list
            conditions = medical_history['conditions']
            conditions.append('diabetes')
            medical_history['conditions'] = conditions
            diabetes_risk = "NA"
            # print('diabetes status is %s ' % assessment['diabetes_status'][1])

        diabetes_status['risk'] = diabetes_risk
        assessment['diabetes'] = diabetes_status
        '''assessment['diabetes'] = {
                        'risk' : diabetes_risk,
                        'status' : diabetes_status
                    }
                '''
        print('--- High Risk Condition Check ---')
        blood_pressure = {
            'sbp': measurements['sbp'],
            'dbp': measurements['dbp']
        }
        print(blood_pressure)

        bp_assessment = assess_blood_pressure(blood_pressure, medical_history['conditions'], medications)
        assessment['blood_pressure'] = bp_assessment
        diet = assess_diet(diet_history, medical_history['conditions'])
        exercise = assess_physical_activity(physical_activity)
        assessment['lifestyle'] = {
            'bmi': bmi,
            'whr': whr,
            'diet': diet,
            'exercise': exercise,
            'smoking': smoker
        }

        age = demographics['age']
        # work out how to add in diabetes if newly diagnosed?
        print(medical_history['conditions'])
        high_risk_condition = HEARTS.high_risk_condition_check(demographics['age'], blood_pressure,
                                                               medical_history['conditions'])
        print(high_risk_condition)

        assessment['cvd_assessment'] = {
            'high_risk_condition': high_risk_condition
        }

        # Determine whether eligible for CVD risk assessment
        estimate_cvd_risk_calc = HEARTS.estimate_cvd_risk(age, high_risk_condition)
        # print('high risk output %s ' % assessment['high_risk'][0])
        # if not high_risk_condition[0]:
        if estimate_cvd_risk_calc[0]:
            # if not already at high risk then calculate CVD risk
            # use the WHO model to do the cvd risk assessment
            cvd_risk = HEARTS.calc_cvd_risk(assessment)
            # use the key to look up the guidelines output
            assessment['cvd_assessment']['cvd_risk_result'] = cvd_risk
            assessment['cvd_assessment']['guidelines'] = guidelines['cvd_risk'][cvd_risk[1]]
            # print(guidelines['cvd_risk'][assessment['cvd_risk'][1]])
        else:
            cvd_calc = estimate_cvd_risk_calc[1]
            # assessment['cvd_assessment']['guidelines'] = guidelines['cvd_risk']['Refer']

        # finally formualate guidelines
        print("--- Writing Your Results ----")
        with open('response.json', 'w') as fp:
            json.dump(assessment, fp)

        return assessment

if __name__ == "__main__":
    print('---Starting Hearts Assessment----')
    HEARTS().calculate()
<|endoftext|>


<commit_before><commit_msg>feat: move Memory API to seperate file<commit_after>import ctypes
import typing as tg

try:
    import win32ui
    import win32process
    import win32api
    from win32file import GENERIC_READ
except ImportError:
    print("The tool requires `pypiwin32` module, try `pip install pypiwin32` to install.")
    exit(1)

rPM = ctypes.windll.kernel32.ReadProcessMemory

def read_helper(handle, address, ctypes_var, n_bytes: int) -> bytes:
    if (rPM(handle, address, ctypes.byref(ctypes_var), n_bytes, None)):
        return ctypes_var.value
    else:
        raise MemoryError
<|endoftext|>


<commit_before>'''
···································
·                                 ·
·          INP文件读取            ·
·                                 ·
·          Qi He                  ·
·                                 ·
·          Tsinghua University    ·
·                                 ·
·          2016                   ·
···································
'''

#----------输入文件位置------------
import sys,os
path = sys.path[0]
filename = input('Please input your filename:\n')
location = path[2:].replace('\\','/') + '/' + filename + '.inp'
#location = '/Users/Administrator/Desktop/INP/4Q_4.inp'
read = open(location,'r')

#----------定义变量----------------
#临时变量
i = 0
j = 0
#读取文件
readline = 1    #读入变量（成功读入则继续while语句）
line = []       #文件内容
#节点信息
node_num = []   #NODE信息-node序号
node_xyz = []   #NODE信息-节点xyz坐标
#单元信息
element_num = []    #ELEMENT信息-element序号
element_name = []   #ELEMENT信息-element名称
element_node = []   #ELEMENT信息-单元节点号
#部件信息
part = []       #包含对于不同部件的所有信息

#-----------读入文件到line-----------
while readline:
    readline=read.readline()
    line.append(readline[:-1])  #删除最后的回车符号
    i = i+1
read.close
line_len = len(line)            #INP文件全长度

#-----------读取特征关键词位置-------

#关键词位置信息
keyloc = [0 for x in range(line_len)]

for j in range(0,line_len-1):
    if line[j].find('*Part')!=-1:
        keyloc[j] = 1
    if line[j].find('*Node')!=-1:
        keyloc[j] = 2
    if line[j].find('*Element')!=-1:
        keyloc[j] = 3
    #继续添加关键字---------
print (keyloc)

'''
#-----------读取PART信息-------------
i = 0
k = 0
l = 0
for j in range(0,line_len-1):
    if line[j].find('*Part')!=-1:
        #l = l + 1
        part.append([])
        part[l].append(line[j][line[j].find('name')+5:])
'''

#-----------读取NODE信息-------------
i = 0
k = 0
for j in range(0,line_len-1):
    if line[j].find('*Node')!=-1:
        i = j + 1

        while line[i][0] == ' ':
            nodeline = line[i][line[i].find(',')+1:]    #'          -5.,          -5.,          10.' in 8H
            node_num.append(k+1)
            node_xyz.append([])                         #为了能添加内容所初始化

            while nodeline.find(',') != -1 :
                loc = nodeline.find(',')                #找出','的位置
                node_xyz[k].append(float(nodeline[:loc]))
                nodeline = nodeline[loc+1:]
            node_xyz[k].append(float(nodeline[:]))

            i = i + 1
            k = k + 1

print('-----节点对应序号-----')
print(node_num)
print('-----节点坐标-----')
print(node_xyz)

#-----------读取ELEMENT信息-------------
i = 0
k = 0
for j in range(0,line_len-1):
    if line[j].find('*Element')!=-1:
        i = j + 1
        element_name = line[j][line[j].find('type')+5:] #读取单元名称

        while line[i][0] != '*':
            elementline = line[i][line[i].find(',')+1:]    #' 10, 11, 14, 13,  1,  2,  5,  4' in 8H
            element_num.append(k+1)
            element_node.append([])                         #为了能添加内容所初始化

            while elementline.find(',') != -1 :
                loc = elementline.find(',')                #找出','的位置
                element_node[k].append(int(elementline[:loc]))
                elementline = elementline[loc+1:]
            element_node[k].append(int(elementline[:]))

            i = i + 1
            k = k + 1

print('-----单元类型-----')
print(element_name)
print('-----单元序号-----')
print(element_num)
print('-----单元节点号----')
print(element_node)
<commit_msg>重构输入转换的PY脚本，使其模块化； 增添功能：读入不同PART对应信息<commit_after>'''
···································
·                                 ·
·          INP文件读取            ·
·                                 ·
·          Qi He                  ·
·                                 ·
·          Tsinghua University    ·
·                                 ·
·          2016                   ·
···································
'''

#----------输入文件位置------------
import sys,os
path = sys.path[0]
filename = input('Please input your filename:\n')
location = path[2:].replace('\\','/') + '/' + filename + '.inp'
#location = '/Users/Administrator/Desktop/INP/8H_8.inp'
read = open(location,'r')

#----------定义变量----------------
#临时变量
i = 0
j = 0
k = 0
l = 0
#读取文件
readline = 1    #读入变量（成功读入则继续while语句）
line = []       #文件内容

#-----------读入文件到line-----------
while readline:
    readline=read.readline()
    line.append(readline[:-1])  #删除最后的回车符号
    i = i+1
read.close
line_len = len(line)            #INP文件全长度

#-----------读取特征关键词位置-------

#关键词位置信息
keylocation = [0 for x in range(line_len)]

for i in range(0,line_len-1):
    if line[i].find('*Part')!=-1:
        keylocation[i] = 1
    if line[i].find('*Node')!=-1:
        keylocation[i] = 2
    if line[i].find('*Element')!=-1:
        keylocation[i] = 3
#    if line[i].find('*Nset')!=-1:
#        keylocation[i] = 4
    #继续添加关键字---------
#print (keylocation)

keynum = max(keylocation)    #关键字个数
keyloc = [[] for x in range(keynum)]
for i in range(0,line_len-1):
    for j in range(0,keynum):
        if keylocation[i] == (j+1):
            keyloc[j].append(i)

partnum = len(keyloc[0])     #PART个数
#print(keyloc)

#-----------读取NODE信息-------------
def readnode(beg,end):

    #节点信息
    node_num = []   #NODE信息-node序号
    node_xyz = []   #NODE信息-节点xyz坐标
    #临时变量
    i = 0
    j = 0

    for i in range(beg,end+1):

        nodeline = line[i][line[i].find(',')+1:]        #'          -5.,          -5.,          10.' in 8H
        node_num.append(j+1)
        node_xyz.append([])                             #为了能添加内容所初始化

        while nodeline.find(',') != -1 :
            loc = nodeline.find(',')                    #找出','的位置
            node_xyz[j].append(float(nodeline[:loc]))
            nodeline = nodeline[loc+1:]
        node_xyz[j].append(float(nodeline[:]))
        j = j + 1

    return node_num,node_xyz

#-----------读取ELEMENT信息-------------
def readelement(beg,end):

    #单元信息
    element_num = []    #ELEMENT信息-element序号
    element_name = []   #ELEMENT信息-element名称
    element_node = []   #ELEMENT信息-单元节点号
    #临时变量
    i = 0
    j = 0

    for i in range(beg,end+1):

        elementline = line[i][line[i].find(',')+1:]    #' 10, 11, 14, 13,  1,  2,  5,  4' in 8H
        element_num.append(j+1)
        element_node.append([])                        #为了能添加内容所初始化

        while elementline.find(',') != -1 :
            loc = elementline.find(',')                #找出','的位置
            element_node[j].append(int(elementline[:loc]))
            elementline = elementline[loc+1:]
        element_node[j].append(int(elementline[:]))
        j = j + 1

    return element_num,element_node

#-----------读取PART名称-------------
def readpartname(beg):

    partname = line[beg][line[beg].find('name')+5:]
    return partname

#-----------读取ELEMENT名称----------
def readelementname(beg):

    elementname = line[beg][line[beg].find('type')+5:]
    return elementname

#-----------读取PART信息-------------
#部件信息
part = [[] for x in range(partnum)]       #包含对于不同部件的所有信息

for i in range(0,partnum):
    part[i].append(readpartname(keyloc[0][i]))
    part[i].append(readnode(keyloc[1][i]+1,keyloc[2][i]-1))
    part[i].append(readelementname(keyloc[2][i]))

#-----------输出PART信息-------------
for i in range(len(part)):
    print('···········第' + str(i+1) + '单元···········')
    print('-------------部件名称---------------')
    print(part[i][0])
    print('\n')     #换行
    print('-------------节点编号---------------')
    print(part[i][1][0])
    print('\n')
    print('-------------节点坐标---------------')
    print(part[i][1][1])
    print('\n')
    print('-------------单元类型---------------')
    print(part[i][2])

    print('·····························')
    print('\n'*5)

<|endoftext|>


<commit_before>import sublime
import sublime_plugin
import json
import sys

if sys.version_info > (2, 7, 0):
    import json
    from collections import OrderedDict
else:
    import simplejson as json
    from simplejson import OrderedDict

s = sublime.load_settings("Pretty JSON.sublime-settings")

class PrettyjsonCommand(sublime_plugin.TextCommand):
    """ Pretty Print JSON
    """
    def run(self, edit):
        for region in self.view.sel():
            # If no selection, use the entire file as the selection
            if region.empty() and s.get("use_entire_file_if_no_selection"):
                selection = sublime.Region(0, self.view.size())
            else:
                selection = region

            try:
                obj = json.loads(self.view.substr(selection), object_pairs_hook=OrderedDict)
                self.view.replace(edit, selection, json.dumps(obj, indent=s.get("indent", 4), ensure_ascii=s.get("ensure_ascii", False), sort_keys=s.get("sort_keys", False), separators=(',', ': ')))
            except Exception, e:
                sublime.status_message(str(e))
<commit_msg>fix issue #15 preserving real values as the user had it in json<commit_after>import sublime
import sublime_plugin
import json
import sys

if sys.version_info > (2, 7, 0):
    import json
    from json import encoder
    encoder.FLOAT_REPR = lambda o: format(o, ".15g")
    from collections import OrderedDict
else:
    import simplejson as json
    from simplejson import encoder
    encoder.FLOAT_REPR = lambda o: "%.15g" % o
    from simplejson import OrderedDict

s = sublime.load_settings("Pretty JSON.sublime-settings")

class PrettyjsonCommand(sublime_plugin.TextCommand):
    """ Pretty Print JSON
    """
    def run(self, edit):
        for region in self.view.sel():
            # If no selection, use the entire file as the selection
            if region.empty() and s.get("use_entire_file_if_no_selection"):
                selection = sublime.Region(0, self.view.size())
            else:
                selection = region

            try:
                obj = json.loads(self.view.substr(selection), object_pairs_hook=OrderedDict)
                self.view.replace(edit, selection, json.dumps(obj, indent=s.get("indent", 4), ensure_ascii=s.get("ensure_ascii", False), sort_keys=s.get("sort_keys", False), separators=(',', ': ')))
            except Exception, e:
                sublime.status_message(str(e))
<|endoftext|>


<commit_before>from fuzzywuzzy import fuzz
from PyTeX import tex, bib
import re

def loads(f):
    P_BIB = re.compile(r'.+\.bib')
    P_TEX = re.compile(r'.+\.tex')
    if P_BIB.match(f.name):
        pass
    elif P_TEX.match(f.name):
        return tex.Parser(tex.tokenize(tex.FileIn(f).read())).parse()
    else:
        raise OSError("File {} does not have a valid extension".format(f.name))

class Finder(object):

    def __init__(self, match_strength):
        self.match_strength = match_strength
        self.results = []

    def find(self, term, obj):
        if isinstance(obj, dict):
            for key, value in obj.items():
                if isinstance(value, dict):
                    if fuzz.ratio(term, key) > self.match_strength:
                        self.results.append(obj)
                    else:
                        self.find(term, value)
                else:
                    key_match = fuzz.ratio(term, key)
                    val_match = fuzz.ratio(term, value)
                    if key_match | val_match > self.match_strength:
                        self.results.append(obj)
        elif isinstance(obj, list):
            for item in obj:
                self.find(term, item)
        else:
            raise TypeError("Expected dict or list")

def find(term, obj, match_strength=75):
    search = Finder(match_strength)
    search.find(term, obj)
    return search.results
<commit_msg>json.loads call corrected for module changes<commit_after>from fuzzywuzzy import fuzz
from PyTeX import bib, lexer, tex
import re

def loads(f):
    P_BIB = re.compile(r'.+\.bib')
    P_TEX = re.compile(r'.+\.tex')
    if P_BIB.match(f.name):
        pass
    elif P_TEX.match(f.name):
        return tex.Parser(lexer.FileIn(f).tokenize()).parse()
    else:
        raise OSError("File {} does not have a valid extension".format(f.name))

class Finder(object):

    def __init__(self, match_strength):
        self.match_strength = match_strength
        self.results = []

    def find(self, term, obj):
        if isinstance(obj, dict):
            for key, value in obj.items():
                if isinstance(value, dict):
                    if fuzz.ratio(term, key) > self.match_strength:
                        self.results.append(obj)
                    else:
                        self.find(term, value)
                else:
                    key_match = fuzz.ratio(term, key)
                    val_match = fuzz.ratio(term, value)
                    if key_match | val_match > self.match_strength:
                        self.results.append(obj)
        elif isinstance(obj, list):
            for item in obj:
                self.find(term, item)
        else:
            raise TypeError("Expected dict or list")

def find(term, obj, match_strength=75):
    search = Finder(match_strength)
    search.find(term, obj)
    return search.results
<|endoftext|>


<commit_before>import logging
from time import sleep, time

try:
    import RPi.GPIO as IO
except ImportError:
    logging.warning("RPi.GPIO not installed, using stub instead.")

    class IO(object):
        IN=0
        OUT=1
        LOW=0
        HIGH=1

        def setup(*args, **kwargs): pass
        def input(*args, **kwargs): return 0
        def output(*args, **kwargs): pass
        def cleanup(): pass

QTR_EMITTERS_OFF        = 0
QTR_EMITTERS_ON         = 1
QTR_EMITTERS_ON_AND_OFF = 2
QTR_NO_EMITTER_PIN      = 255
QTR_MAX_SENSORS         = 16

class QTRSensors(object):
    def __init__(self):
        self._pins=[]
        self._numSensors=0
        self._emitterPin=-1
        self._maxValue=0
        self._lastValue=0

        self.calibratedMinimumOn=[0]*self._numSensors
        self.calibratedMaximumOn=[0]*self._numSensors
        self.calibratedMinimumOff=[0]*self._numSensors
        self.calibratedMaximumOff=[0]*self._numSensors
        self._calibrated=False

    def init(self, pins, emitterPin):
        self._pins=pins
        self._emitterPin=emitterPin
        self._numSensors=len(self._pins)
        assert self._numSensors<=QTR_MAX_SENSORS

    def _readPrivate(self): raise NotImplementedError

    def _calibrateOnOrOff(self, readMode):
        cMax=[0]*self._numSensors
        cMin=[self._maxValue]*self._numSensors
        svMax=[0]*self._numSensors
        svMin=[self._maxValue]*self._numSensors

        for j in range(10):
            sensorValues=self.read(readMode)
            for i in range(self._numSensors):
                if j==0 or svMax[i]<sensorValues[i]:
                    svMax[i]=sensorValues[i]

                if j==0 or svMin[i]>sensorValues[i]:
                    svMin[i]=sensorValues[i]

        for i in range(self._numSensors):
            if svMin[i]>cMax[i]: cMax[i]=svMin[i]
            if svMax[i]<cMin[i]: cMin[i]=svMax[i]

        return cMin,cMax

    def read(self, readMode=QTR_EMITTERS_ON):
        if readMode in (QTR_EMITTERS_ON, QTR_EMITTERS_ON_AND_OFF):
            self.emittersOn()
        else: self.emittersOff()

        sensorValues=self.readPrivate()
        self.emittersOff()

        if readMode==QTR_EMITTERS_ON_AND_OFF:
            offValues=self.readPrivate()
            for i in range(self._numSensors):
                sensorValues[i]=self._maxValue-offValues[i]

        return sensorValues

    def emittersOff(self):
        if self._emitterPin==QTR_NO_EMITTER_PIN: return

        IO.setup(self._emitterPin, IO.OUT)
        IO.output(self._emitterPin, IO.LOW)
        sleep(200./10e6) # 200 microseconds

    def emittersOn(self):
        if self._emitterPin==QTR_NO_EMITTER_PIN: return

        IO.setup(self._emitterPin, IO.OUT)
        IO.output(self._emitterPin, IO.HIGH)
        sleep(200/10e6) # 200 microseconds

    def calibrate(self, readMode=QTR_EMITTERS_ON):
        if readMode in (QTR_EMITTERS_ON,QTR_EMITTERS_ON_AND_OFF):
            self._calibratedMinimumOn, self._calibratedMaximumOn=self._calibrateOnOrOff(QTR_EMITTERS_ON)
        if readMode in (QTR_EMITTERS_OFF,QTR_EMITTERS_ON_AND_OFF):
            self._calibratedMinimumOff, self._calibratedMaximumOff=self._calibrateOnOrOff(QTR_EMITTERS_OFF)
        self._calibrated=True

    def resetCalibration(self):
        for i in range(self._numSensors):
            self.calibratedMinimumOn[i]=self._maxValue
            self.calibratedMinimumOff[i]=self._maxValue
            self.calibratedMaximumOn[i]=0
            self.calibratedMaximumOff[i]=0

    def readCalibrated(self, readMode=QTR_EMITTERS_ON):
        if not self._calibrated: return

        sensorValues=self.read(readMode)
        for i in range(self._numSensors):
            if readMode==QTR_EMITTERS_ON:
                calMax=self._calibratedMaximumOn[i]
                calMin=self._calibratedMinimumOn[i]
            elif readMode==QTR_EMITTERS_OFF:
                calMax=self._calibratedMaximumOff[i]
                calMin=self._calibratedMinimumOff[i]
            else: # QTR_EMITTERS_ON_AND_OFF
                if self._calibratedMinimumOff[i]<self._calibratedMinimumOn: calMin=self._maxValue
                else: calMin=self._calibratedMinimumOn[i]+self._maxValue-self._calibratedMinimumOff[i]

                if self._calibratedMaximumOff[i]<sef._calibratedMaximumOn[i]: calMax=self._maxValue
                else: calMax=self._calibratedMaximumOn[i]+self._maxValue-self._calibratedMaximumOff[i]

            denominator=calMax-calMin
            x=0
            if denominator!=0:
                x=(sensorValues[i]-calMin)*1000/denominator
            if x<0: x=0
            elif x>1000: x=1000
            sensorValues[i]=x

        return sensorValues

    def readLine(self, readMode=QTR_EMITTERS_ON, whiteLine=0):
        sensorValues=self.readCalibrated(readMode)
        _avg=_sum=_online=0

        for i in range(self._numSensors):
            v=sensorValues[i]
            if whiteLine: v=1000-v
            if v>200: _online=1
            if v>50:
                _avg+=v*i*1000
                _sum+=v

        if not _online:
            if self._lastValue<((self._numSensors-1)*1000/2): return 0
            else: return (self._numSensors-1)*1000

        self._lastValue=_avg/_sum

        return self._lastValue, sensorValues

class QTSensorsRC(QTRSensors):
    def __init__(self, pins, timeout=4000, emitterPin=QTR_NO_EMITTER_PIN):
        super().__init__()

        self.init(pins, timeout, emitterPin)

    def init(self, pins, timeout, emitterPin):
        super().init(pins, emitterPin)
        self._maxValue=timeout

    def readPrivate(self):
        sensorValues=[]
        for i in range(self._numSensors):
            sensorValues.append(self._maxValue)
            IO.setup(self._pins[i], IO.OUT, initial=IO.HIGH)

        sleep(10./10e6) # 10 microseconds

        for i in range(self._numSensors):
            IO.output(self._pins[i], IO.LOW)
            IO.setup(self._pins[i], IO.IN)

        startTime=time()
        t=0
        while t<self._maxValue:
            t=(time()-startTime)*10e6
            for i in range(self._numSensors):
                if IO.input(self._pins)==IO.LOW and t<sensorValues[i]:
                    sensorValues[i]=t

        return sensorValues

if __name__=="__main__":
    import atexit

    @atexit.register
    def cleanup():
        print("Cleanup", flush=True)
        IO.cleanup()

    qtrrc=QTSensorsRC([1,2,3,4,5,6,7,8], timeout=2500, emitterPin=9)

    print("Calibrating", end="")
    for i in range(400):
        if i%8==0: print('.',end="", flush=True)
        qtrrc.calibrate()
    print()
    print("readLine:",qtrrc.readLine())<commit_msg>Changing constants to enum<commit_after>from enum import IntEnum
import logging
from time import sleep, time

try:
    import RPi.GPIO as IO
except ImportError:
    logging.warning("RPi.GPIO not installed, using stub instead.")

    class IO(object):
        IN=0
        OUT=1
        LOW=0
        HIGH=1

        def setup(*args, **kwargs): pass
        def input(*args, **kwargs): return 0
        def output(*args, **kwargs): pass
        def cleanup(): pass

class QTR(IntEnum):
    EMITTERS_OFF        = 0
    EMITTERS_ON         = 1
    EMITTERS_ON_AND_OFF = 2
    NO_EMITTER_PIN      = 255
    MAX_SENSORS         = 16

class QTRSensors(object):
    def __init__(self):
        self._pins=[]
        self._numSensors=0
        self._emitterPin=-1
        self._maxValue=0
        self._lastValue=0

        self.calibratedMinimumOn=[0]*self._numSensors
        self.calibratedMaximumOn=[0]*self._numSensors
        self.calibratedMinimumOff=[0]*self._numSensors
        self.calibratedMaximumOff=[0]*self._numSensors
        self._calibrated=False

    def init(self, pins, emitterPin):
        self._pins=pins
        self._emitterPin=emitterPin
        self._numSensors=len(self._pins)
        assert self._numSensors<=QTR.MAX_SENSORS

    def _readPrivate(self): raise NotImplementedError

    def _calibrateOnOrOff(self, readMode):
        cMax=[0]*self._numSensors
        cMin=[self._maxValue]*self._numSensors
        svMax=[0]*self._numSensors
        svMin=[self._maxValue]*self._numSensors

        for j in range(10):
            sensorValues=self.read(readMode)
            for i in range(self._numSensors):
                if j==0 or svMax[i]<sensorValues[i]:
                    svMax[i]=sensorValues[i]

                if j==0 or svMin[i]>sensorValues[i]:
                    svMin[i]=sensorValues[i]

        for i in range(self._numSensors):
            if svMin[i]>cMax[i]: cMax[i]=svMin[i]
            if svMax[i]<cMin[i]: cMin[i]=svMax[i]

        return cMin,cMax

    def read(self, readMode=QTR.EMITTERS_ON):
        if readMode in (QTR.EMITTERS_ON, QTR.EMITTERS_ON_AND_OFF):
            self.emittersOn()
        else: self.emittersOff()

        sensorValues=self.readPrivate()
        self.emittersOff()

        if readMode==QTR.EMITTERS_ON_AND_OFF:
            offValues=self.readPrivate()
            for i in range(self._numSensors):
                sensorValues[i]=self._maxValue-offValues[i]

        return sensorValues

    def emittersOff(self):
        if self._emitterPin==QTR.NO_EMITTER_PIN: return

        IO.setup(self._emitterPin, IO.OUT)
        IO.output(self._emitterPin, IO.LOW)
        sleep(200./10e6) # 200 microseconds

    def emittersOn(self):
        if self._emitterPin==QTR.NO_EMITTER_PIN: return

        IO.setup(self._emitterPin, IO.OUT)
        IO.output(self._emitterPin, IO.HIGH)
        sleep(200/10e6) # 200 microseconds

    def calibrate(self, readMode=QTR.EMITTERS_ON):
        if readMode in (QTR.EMITTERS_ON,QTR.EMITTERS_ON_AND_OFF):
            self._calibratedMinimumOn, self._calibratedMaximumOn=self._calibrateOnOrOff(QTR.EMITTERS_ON)
        if readMode in (QTR.EMITTERS_OFF,QTR.EMITTERS_ON_AND_OFF):
            self._calibratedMinimumOff, self._calibratedMaximumOff=self._calibrateOnOrOff(QTR.EMITTERS_OFF)
        self._calibrated=True

    def resetCalibration(self):
        for i in range(self._numSensors):
            self.calibratedMinimumOn[i]=self._maxValue
            self.calibratedMinimumOff[i]=self._maxValue
            self.calibratedMaximumOn[i]=0
            self.calibratedMaximumOff[i]=0

    def readCalibrated(self, readMode=QTR.EMITTERS_ON):
        if not self._calibrated: return

        sensorValues=self.read(readMode)
        for i in range(self._numSensors):
            if readMode==QTR.EMITTERS_ON:
                calMax=self._calibratedMaximumOn[i]
                calMin=self._calibratedMinimumOn[i]
            elif readMode==QTR.EMITTERS_OFF:
                calMax=self._calibratedMaximumOff[i]
                calMin=self._calibratedMinimumOff[i]
            else: # QTR.EMITTERS_ON_AND_OFF
                if self._calibratedMinimumOff[i]<self._calibratedMinimumOn: calMin=self._maxValue
                else: calMin=self._calibratedMinimumOn[i]+self._maxValue-self._calibratedMinimumOff[i]

                if self._calibratedMaximumOff[i]<sef._calibratedMaximumOn[i]: calMax=self._maxValue
                else: calMax=self._calibratedMaximumOn[i]+self._maxValue-self._calibratedMaximumOff[i]

            denominator=calMax-calMin
            x=0
            if denominator!=0:
                x=(sensorValues[i]-calMin)*1000/denominator
            if x<0: x=0
            elif x>1000: x=1000
            sensorValues[i]=x

        return sensorValues

    def readLine(self, readMode=QTR.EMITTERS_ON, whiteLine=0):
        sensorValues=self.readCalibrated(readMode)
        _avg=_sum=_online=0

        for i in range(self._numSensors):
            v=sensorValues[i]
            if whiteLine: v=1000-v
            if v>200: _online=1
            if v>50:
                _avg+=v*i*1000
                _sum+=v

        if not _online:
            if self._lastValue<((self._numSensors-1)*1000/2): return 0
            else: return (self._numSensors-1)*1000

        self._lastValue=_avg/_sum

        return self._lastValue, sensorValues

class QTRSensorsRC(QTRSensors):
    def __init__(self, pins, timeout=4000, emitterPin=QTR.NO_EMITTER_PIN):
        super().__init__()

        self.init(pins, timeout, emitterPin)

    def init(self, pins, timeout, emitterPin):
        super().init(pins, emitterPin)
        self._maxValue=timeout

    def readPrivate(self):
        sensorValues=[]
        for i in range(self._numSensors):
            sensorValues.append(self._maxValue)
            IO.setup(self._pins[i], IO.OUT, initial=IO.HIGH)

        sleep(10./10e6) # 10 microseconds

        for i in range(self._numSensors):
            IO.output(self._pins[i], IO.LOW)
            IO.setup(self._pins[i], IO.IN)

        startTime=time()
        t=0
        while t<self._maxValue:
            t=(time()-startTime)*10e6
            for i in range(self._numSensors):
                if IO.input(self._pins)==IO.LOW and t<sensorValues[i]:
                    sensorValues[i]=t

        return sensorValues

if __name__=="__main__":
    import atexit

    @atexit.register
    def cleanup():
        print("Cleanup", flush=True)
        IO.cleanup()

    qtrrc=QTRSensorsRC([1,2,3,4,5,6,7,8], timeout=2500, emitterPin=9)

    print("Calibrating", end="")
    for i in range(400):
        if i%8==0: print('.',end="", flush=True)
        qtrrc.calibrate()
    print()
    print("readLine:",qtrrc.readLine())<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Trains MNIST using tf.contrib.learn.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import os
import time

import numpy
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

ARGFLAGS = None
DATA_SETS = None
BATCH_SIZE = 40

# comment out for less info during the training runs.
tf.logging.set_verbosity(tf.logging.INFO)

# example calls: generate_input_fn(DATA_SETS.train) or generate_input_fn(DATA_SETS.test)
def generate_input_fn(dataset, batch_size=BATCH_SIZE):
    def _input_fn():
        X = tf.constant(dataset.images)
        Y = tf.constant(dataset.labels.astype(numpy.int64))
        image_batch, label_batch = tf.train.shuffle_batch([X,Y],
                               batch_size=batch_size,
                               capacity=8*batch_size,
                               min_after_dequeue=4*batch_size,
                               enqueue_many=True
                              )
        return {'pixels': image_batch} , label_batch

    return _input_fn

def define_and_run_linear_classifier(num_steps, logdir, batch_size=BATCH_SIZE):
    """Run a linear classifier."""
    feature_columns = [tf.contrib.layers.real_valued_column(
        "pixels", dimension=784)]

    classifier = tf.contrib.learn.LinearClassifier(
        feature_columns=feature_columns,
        n_classes=10,
        model_dir=logdir
    )
    classifier.fit(
        input_fn=generate_input_fn(DATA_SETS.train, batch_size=batch_size),
        steps=num_steps)

    print("Finished training.")

    # Evaluate accuracy.
    accuracy_score = classifier.evaluate(input_fn=generate_input_fn(DATA_SETS.test, batch_size), steps=100)['accuracy']

    print('Linear Classifier Accuracy: {0:f}'.format(accuracy_score))

def define_and_run_dnn_classifier(num_steps, logdir, lr=.1, batch_size=40):
    """Run a DNN classifier."""
    feature_columns = [tf.contrib.layers.real_valued_column("pixels", dimension=784)]

    classifier = tf.contrib.learn.DNNClassifier(
        feature_columns=feature_columns,
        n_classes=10,
        hidden_units=[128, 32],
        optimizer=tf.train.ProximalAdagradOptimizer(learning_rate=lr),
        model_dir=logdir
    )
    # After you've done a training run with optimizer learning rate 0.1,
        # change it to 0.5 and run the training again.  Use TensorBoard to take
        # a look at the difference.  You can see both runs by pointing it to the
        # parent model directory, which by default is:
        #
        #   tensorboard --logdir=/tmp/tfmodels/mnist_tflearn

    classifier.fit(input_fn=generate_input_fn(DATA_SETS.train, batch_size=batch_size),
                   steps=num_steps)

    print("Finished running the deep training via the fit() method")

    print("\n---Evaluating DNN classifier accuracy...")
    accuracy_score = classifier.evaluate(input_fn=generate_input_fn(DATA_SETS.test, batch_size=batch_size),
                                         steps=100)['accuracy']

    print('DNN Classifier Accuracy: {0:f}'.format(accuracy_score))

def main(_):

    # read in data, downloading first if necessary
    global DATA_SETS
    print("Downloading and reading data sets...")
    DATA_SETS = input_data.read_data_sets(ARGFLAGS.data_dir)

    # Uncomment this if you'd like to run the linear classifier first.
    # print("\n-----Running linear classifier...")
    # model_dir = os.path.join("/tmp/tfmodels/mnist_tflearn", str(int(time.time())))
    # define_and_run_linear_classifier(ARGFLAGS.num_steps, model_dir)

    print("\n---- Running DNN classifier...")
    define_and_run_dnn_classifier(ARGFLAGS.num_steps, ARGFLAGS.model_dir)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', type=str, default='/tmp/MNIST_data',
                        help='Directory for storing data')
    parser.add_argument('--model_dir', type=str,
                        default=os.path.join(
                            "/tmp/tfmodels/mnist_tflearn",
                            str(int(time.time()))),
                        help='Directory for storing model info')
    parser.add_argument('--num_steps', type=int,
                        default=15000,
                        help='Number of training steps to run')
    ARGFLAGS = parser.parse_args()
    tf.app.run()
<commit_msg>removed numpy dep<commit_after>#!/usr/bin/env python
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Trains MNIST using tf.contrib.learn.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import os
import time

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

ARGFLAGS = None
DATA_SETS = None
BATCH_SIZE = 40

# comment out for less info during the training runs.
tf.logging.set_verbosity(tf.logging.INFO)

# example calls: generate_input_fn(DATA_SETS.train) or generate_input_fn(DATA_SETS.test)
def generate_input_fn(dataset, batch_size=BATCH_SIZE):
    def _input_fn():
        X = tf.constant(dataset.images)
        Y = tf.constant(dataset.labels, dtype=tf.int32)
        image_batch, label_batch = tf.train.shuffle_batch([X,Y],
                               batch_size=batch_size,
                               capacity=8*batch_size,
                               min_after_dequeue=4*batch_size,
                               enqueue_many=True
                              )
        return {'pixels': image_batch} , label_batch

    return _input_fn

def define_and_run_linear_classifier(num_steps, logdir, batch_size=BATCH_SIZE):
    """Run a linear classifier."""
    feature_columns = [tf.contrib.layers.real_valued_column(
        "pixels", dimension=784)]

    classifier = tf.contrib.learn.LinearClassifier(
        feature_columns=feature_columns,
        n_classes=10,
        model_dir=logdir
    )
    classifier.fit(
        input_fn=generate_input_fn(DATA_SETS.train, batch_size=batch_size),
        steps=num_steps)

    print("Finished training.")

    # Evaluate accuracy.
    accuracy_score = classifier.evaluate(input_fn=generate_input_fn(DATA_SETS.test, batch_size), steps=100)['accuracy']

    print('Linear Classifier Accuracy: {0:f}'.format(accuracy_score))

def define_and_run_dnn_classifier(num_steps, logdir, lr=.1, batch_size=40):
    """Run a DNN classifier."""
    feature_columns = [tf.contrib.layers.real_valued_column("pixels", dimension=784)]

    classifier = tf.contrib.learn.DNNClassifier(
        feature_columns=feature_columns,
        n_classes=10,
        hidden_units=[128, 32],
        optimizer=tf.train.ProximalAdagradOptimizer(learning_rate=lr),
        model_dir=logdir
    )
    # After you've done a training run with optimizer learning rate 0.1,
        # change it to 0.5 and run the training again.  Use TensorBoard to take
        # a look at the difference.  You can see both runs by pointing it to the
        # parent model directory, which by default is:
        #
        #   tensorboard --logdir=/tmp/tfmodels/mnist_tflearn

    classifier.fit(input_fn=generate_input_fn(DATA_SETS.train, batch_size=batch_size),
                   steps=num_steps)

    print("Finished running the deep training via the fit() method")

    print("\n---Evaluating DNN classifier accuracy...")
    accuracy_score = classifier.evaluate(input_fn=generate_input_fn(DATA_SETS.test, batch_size=batch_size),
                                         steps=100)['accuracy']

    print('DNN Classifier Accuracy: {0:f}'.format(accuracy_score))

def main(_):

    # read in data, downloading first if necessary
    global DATA_SETS
    print("Downloading and reading data sets...")
    DATA_SETS = input_data.read_data_sets(ARGFLAGS.data_dir)

    # Uncomment this if you'd like to run the linear classifier first.
    # print("\n-----Running linear classifier...")
    # model_dir = os.path.join("/tmp/tfmodels/mnist_tflearn", str(int(time.time())))
    # define_and_run_linear_classifier(ARGFLAGS.num_steps, model_dir)

    print("\n---- Running DNN classifier...")
    define_and_run_dnn_classifier(ARGFLAGS.num_steps, ARGFLAGS.model_dir)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', type=str, default='/tmp/MNIST_data',
                        help='Directory for storing data')
    parser.add_argument('--model_dir', type=str,
                        default=os.path.join(
                            "/tmp/tfmodels/mnist_tflearn",
                            str(int(time.time()))),
                        help='Directory for storing model info')
    parser.add_argument('--num_steps', type=int,
                        default=15000,
                        help='Number of training steps to run')
    ARGFLAGS = parser.parse_args()
    tf.app.run()
<|endoftext|>


<commit_before>"""module contains integration code for rust-gn3"""
import json
from functools import reduce
from utility.db_tools import mescape
from utility.db_tools import create_in_clause
from wqflask.correlation.correlation_functions\
    import get_trait_symbol_and_tissue_values
from wqflask.correlation.correlation_gn3_api import create_target_this_trait
from wqflask.correlation.correlation_gn3_api import lit_for_trait_list
from wqflask.correlation.correlation_gn3_api import do_lit_correlation
from gn3.computations.correlations import compute_all_lit_correlation
from gn3.computations.rust_correlation import run_correlation
from gn3.computations.rust_correlation import get_sample_corr_data
from gn3.computations.rust_correlation import parse_tissue_corr_data
from gn3.db_utils import database_connector

def chunk_dataset(dataset, steps, name):

    results = []

    query = """
            SELECT ProbeSetXRef.DataId,ProbeSet.Name
            FROM ProbeSet, ProbeSetXRef, ProbeSetFreeze
            WHERE ProbeSetFreeze.Name = '{}' AND
                  ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND
                  ProbeSetXRef.ProbeSetId = ProbeSet.Id
    """.format(name)

    with database_connector() as conn:
        with conn.cursor() as curr:
            curr.execute(query)
            traits_name_dict = dict(curr.fetchall())

    for i in range(0, len(dataset), steps):
        matrix = list(dataset[i:i + steps])
        trait_name = traits_name_dict[matrix[0][0]]

        strains = [trait_name] + [str(value)
                                  for (trait_name, strain, value) in matrix]
        results.append(",".join(strains))

    return results

def compute_top_n_sample(start_vars, dataset, trait_list):
    """check if dataset is of type probeset"""

    if dataset.type.lower() != "probeset":
        return {}

    def __fetch_sample_ids__(samples_vals, samples_group):
        all_samples = json.loads(samples_vals)
        sample_data = get_sample_corr_data(
            sample_type=samples_group, all_samples=all_samples,
            dataset_samples=dataset.group.all_samples_ordered())

        with database_connector() as conn:
            with conn.cursor() as curr:
                curr.execute(
                    """
                    SELECT Strain.Name, Strain.Id FROM Strain, Species
                    WHERE Strain.Name IN {}
                    and Strain.SpeciesId=Species.Id
                    and Species.name = '{}'
                    """.format(create_in_clause(list(sample_data.keys())),
                               *mescape(dataset.group.species)))
                return (sample_data, dict(curr.fetchall()))

    (sample_data, sample_ids) = __fetch_sample_ids__(
        start_vars["sample_vals"], start_vars["corr_samples_group"])

    with database_connector() as conn:
        with conn.cursor() as curr:
            # fetching strain data in bulk
            curr.execute(
                """
                SELECT * from ProbeSetData
                where StrainID in {}
                and id in (SELECT ProbeSetXRef.DataId
                FROM (ProbeSet, ProbeSetXRef, ProbeSetFreeze)
                WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id
                and ProbeSetFreeze.Name = '{}'
                and ProbeSet.Name in {}
                and ProbeSet.Id = ProbeSetXRef.ProbeSetId)
                """.format(
                    create_in_clause(list(sample_ids.values())),
                    dataset.name,
                    create_in_clause(trait_list)))

            corr_data = chunk_dataset(
                list(curr.fetchall()), len(sample_ids.values()), dataset.name)

        return run_correlation(
            corr_data, list(sample_data.values()), "pearson", ",")

def compute_top_n_lit(corr_results, this_dataset, this_trait) -> dict:
    (this_trait_geneid, geneid_dict, species) = do_lit_correlation(
        this_trait, this_dataset)

    geneid_dict = {trait_name: geneid for (trait_name, geneid)
                   in geneid_dict.items() if
                   corr_results.get(trait_name)}
    with database_connector() as conn:
        return reduce(
            lambda acc, corr: {**acc, **corr},
            compute_all_lit_correlation(
                conn=conn, trait_lists=list(geneid_dict.items()),
                species=species, gene_id=this_trait_geneid),
            {})

    return {}

def compute_top_n_tissue(this_dataset, this_trait, traits, method):

    # refactor lots of rpt

    trait_symbol_dict = dict({
        trait_name: symbol
        for (trait_name, symbol)
        in this_dataset.retrieve_genes("Symbol").items()
        if traits.get(trait_name)})

    corr_result_tissue_vals_dict = get_trait_symbol_and_tissue_values(
        symbol_list=list(trait_symbol_dict.values()))

    data = parse_tissue_corr_data(symbol_name=this_trait.symbol,
                                  symbol_dict=get_trait_symbol_and_tissue_values(
                                      symbol_list=[this_trait.symbol]),
                                  dataset_symbols=trait_symbol_dict,
                                  dataset_vals=corr_result_tissue_vals_dict)

    if data:
        return run_correlation(
            data[1], data[0], method, ",", "tissue")

    return {}

def merge_results(dict_a: dict, dict_b: dict, dict_c: dict) -> list[dict]:
    """code to merge diff corr  into individual dicts
    a"""

    def __merge__(trait_name, trait_corrs):
        return {
            trait_name: {
                **trait_corrs,
                **dict_b.get(trait_name, {}),
                **dict_c.get(trait_name, {})
            }
        }
    return [__merge__(tname, tcorrs) for tname, tcorrs in dict_a.items()]

def __compute_sample_corr__(
        start_vars: dict, corr_type: str, method: str, n_top: int,
        target_trait_info: tuple):
    """Compute the sample correlations"""
    (this_dataset, this_trait, target_dataset, sample_data) = target_trait_info
    all_samples = json.loads(start_vars["sample_vals"])
    sample_data = get_sample_corr_data(
        sample_type=start_vars["corr_samples_group"], all_samples=all_samples,
        dataset_samples=this_dataset.group.all_samples_ordered())
    target_dataset.get_trait_data(list(sample_data.keys()))

    target_data = []
    for (key, val) in target_dataset.trait_data.items():
        lts = [key] + [str(x) for x in val]
        r = ",".join(lts)
        target_data.append(r)

    return run_correlation(
        target_data, list(sample_data.values()), method, ",", corr_type,
        n_top)

def __compute_tissue_corr__(
        start_vars: dict, corr_type: str, method: str, n_top: int,
        target_trait_info: tuple):
    """Compute the tissue correlations"""
    (this_dataset, this_trait, target_dataset, sample_data) = target_trait_info
    trait_symbol_dict = this_dataset.retrieve_genes("Symbol")
    corr_result_tissue_vals_dict = get_trait_symbol_and_tissue_values(
        symbol_list=list(trait_symbol_dict.values()))

    data = parse_tissue_corr_data(
        symbol_name=this_trait.symbol,
        symbol_dict=get_trait_symbol_and_tissue_values(
            symbol_list=[this_trait.symbol]),
        dataset_symbols=trait_symbol_dict,
        dataset_vals=corr_result_tissue_vals_dict)

    if data:
        return run_correlation(data[1], data[0], method, ",", "tissue")
    return {}

def __compute_lit_corr__(
        start_vars: dict, corr_type: str, method: str, n_top: int,
        target_trait_info: tuple):
    """Compute the literature correlations"""
    (this_dataset, this_trait, target_dataset, sample_data) = target_trait_info
    target_dataset_type = target_dataset.type
    this_dataset_type = this_dataset.type
    (this_trait_geneid, geneid_dict, species) = do_lit_correlation(
        this_trait, this_dataset)

    with database_connector() as conn:
        return reduce(
            lambda acc, lit: {**acc, **lit},
            compute_all_lit_correlation(
                conn=conn, trait_lists=list(geneid_dict.items()),
                species=species, gene_id=this_trait_geneid),
            {})
    return {}

def compute_correlation_rust(
        start_vars: dict, corr_type: str, method: str = "pearson",
        n_top: int = 500, should_compute_all: bool = False):
    """function to compute correlation"""
    target_trait_info = create_target_this_trait(start_vars)
    (this_dataset, this_trait, target_dataset, sample_data) = (
        target_trait_info)

    # Replace this with `match ...` once we hit Python 3.10
    corr_type_fns = {
        "sample": __compute_sample_corr__,
        "tissue": __compute_tissue_corr__,
        "lit": __compute_lit_corr__
    }
    results = corr_type_fns[corr_type](
        start_vars, corr_type, method, n_top, target_trait_info)

    # END: Replace this with `match ...` once we hit Python 3.10

    top_a = top_b = {}

    if should_compute_all:

        if corr_type == "sample":

            top_a = compute_top_n_tissue(
                this_dataset, this_trait, results, method)

            top_b = compute_top_n_lit(results, this_dataset, this_trait)

        elif corr_type == "lit":

            # currently fails for lit

            top_a = compute_top_n_sample(
                start_vars, target_dataset, list(results.keys()))
            top_b = compute_top_n_tissue(
                this_dataset, this_trait, results, method)

        else:

            top_a = compute_top_n_sample(
                start_vars, target_dataset, list(results.keys()))

            top_b = compute_top_n_lit(results, this_dataset, this_trait)

    return {
        "correlation_results": merge_results(
            results, top_a, top_b),
        "this_trait": this_trait.name,
        "target_dataset": start_vars['corr_dataset'],
        "return_results": n_top
    }
<commit_msg>Limit literature correlation results to top N as specified in the form<commit_after>"""module contains integration code for rust-gn3"""
import json
from functools import reduce
from utility.db_tools import mescape
from utility.db_tools import create_in_clause
from wqflask.correlation.correlation_functions\
    import get_trait_symbol_and_tissue_values
from wqflask.correlation.correlation_gn3_api import create_target_this_trait
from wqflask.correlation.correlation_gn3_api import lit_for_trait_list
from wqflask.correlation.correlation_gn3_api import do_lit_correlation
from gn3.computations.correlations import compute_all_lit_correlation
from gn3.computations.rust_correlation import run_correlation
from gn3.computations.rust_correlation import get_sample_corr_data
from gn3.computations.rust_correlation import parse_tissue_corr_data
from gn3.db_utils import database_connector

def chunk_dataset(dataset, steps, name):

    results = []

    query = """
            SELECT ProbeSetXRef.DataId,ProbeSet.Name
            FROM ProbeSet, ProbeSetXRef, ProbeSetFreeze
            WHERE ProbeSetFreeze.Name = '{}' AND
                  ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND
                  ProbeSetXRef.ProbeSetId = ProbeSet.Id
    """.format(name)

    with database_connector() as conn:
        with conn.cursor() as curr:
            curr.execute(query)
            traits_name_dict = dict(curr.fetchall())

    for i in range(0, len(dataset), steps):
        matrix = list(dataset[i:i + steps])
        trait_name = traits_name_dict[matrix[0][0]]

        strains = [trait_name] + [str(value)
                                  for (trait_name, strain, value) in matrix]
        results.append(",".join(strains))

    return results

def compute_top_n_sample(start_vars, dataset, trait_list):
    """check if dataset is of type probeset"""

    if dataset.type.lower() != "probeset":
        return {}

    def __fetch_sample_ids__(samples_vals, samples_group):
        all_samples = json.loads(samples_vals)
        sample_data = get_sample_corr_data(
            sample_type=samples_group, all_samples=all_samples,
            dataset_samples=dataset.group.all_samples_ordered())

        with database_connector() as conn:
            with conn.cursor() as curr:
                curr.execute(
                    """
                    SELECT Strain.Name, Strain.Id FROM Strain, Species
                    WHERE Strain.Name IN {}
                    and Strain.SpeciesId=Species.Id
                    and Species.name = '{}'
                    """.format(create_in_clause(list(sample_data.keys())),
                               *mescape(dataset.group.species)))
                return (sample_data, dict(curr.fetchall()))

    (sample_data, sample_ids) = __fetch_sample_ids__(
        start_vars["sample_vals"], start_vars["corr_samples_group"])

    with database_connector() as conn:
        with conn.cursor() as curr:
            # fetching strain data in bulk
            curr.execute(
                """
                SELECT * from ProbeSetData
                where StrainID in {}
                and id in (SELECT ProbeSetXRef.DataId
                FROM (ProbeSet, ProbeSetXRef, ProbeSetFreeze)
                WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id
                and ProbeSetFreeze.Name = '{}'
                and ProbeSet.Name in {}
                and ProbeSet.Id = ProbeSetXRef.ProbeSetId)
                """.format(
                    create_in_clause(list(sample_ids.values())),
                    dataset.name,
                    create_in_clause(trait_list)))

            corr_data = chunk_dataset(
                list(curr.fetchall()), len(sample_ids.values()), dataset.name)

        return run_correlation(
            corr_data, list(sample_data.values()), "pearson", ",")

def compute_top_n_lit(corr_results, this_dataset, this_trait) -> dict:
    (this_trait_geneid, geneid_dict, species) = do_lit_correlation(
        this_trait, this_dataset)

    geneid_dict = {trait_name: geneid for (trait_name, geneid)
                   in geneid_dict.items() if
                   corr_results.get(trait_name)}
    with database_connector() as conn:
        return reduce(
            lambda acc, corr: {**acc, **corr},
            compute_all_lit_correlation(
                conn=conn, trait_lists=list(geneid_dict.items()),
                species=species, gene_id=this_trait_geneid),
            {})

    return {}

def compute_top_n_tissue(this_dataset, this_trait, traits, method):

    # refactor lots of rpt

    trait_symbol_dict = dict({
        trait_name: symbol
        for (trait_name, symbol)
        in this_dataset.retrieve_genes("Symbol").items()
        if traits.get(trait_name)})

    corr_result_tissue_vals_dict = get_trait_symbol_and_tissue_values(
        symbol_list=list(trait_symbol_dict.values()))

    data = parse_tissue_corr_data(symbol_name=this_trait.symbol,
                                  symbol_dict=get_trait_symbol_and_tissue_values(
                                      symbol_list=[this_trait.symbol]),
                                  dataset_symbols=trait_symbol_dict,
                                  dataset_vals=corr_result_tissue_vals_dict)

    if data:
        return run_correlation(
            data[1], data[0], method, ",", "tissue")

    return {}

def merge_results(dict_a: dict, dict_b: dict, dict_c: dict) -> list[dict]:
    """code to merge diff corr  into individual dicts
    a"""

    def __merge__(trait_name, trait_corrs):
        return {
            trait_name: {
                **trait_corrs,
                **dict_b.get(trait_name, {}),
                **dict_c.get(trait_name, {})
            }
        }
    return [__merge__(tname, tcorrs) for tname, tcorrs in dict_a.items()]

def __compute_sample_corr__(
        start_vars: dict, corr_type: str, method: str, n_top: int,
        target_trait_info: tuple):
    """Compute the sample correlations"""
    (this_dataset, this_trait, target_dataset, sample_data) = target_trait_info
    all_samples = json.loads(start_vars["sample_vals"])
    sample_data = get_sample_corr_data(
        sample_type=start_vars["corr_samples_group"], all_samples=all_samples,
        dataset_samples=this_dataset.group.all_samples_ordered())
    target_dataset.get_trait_data(list(sample_data.keys()))

    target_data = []
    for (key, val) in target_dataset.trait_data.items():
        lts = [key] + [str(x) for x in val]
        r = ",".join(lts)
        target_data.append(r)

    return run_correlation(
        target_data, list(sample_data.values()), method, ",", corr_type,
        n_top)

def __compute_tissue_corr__(
        start_vars: dict, corr_type: str, method: str, n_top: int,
        target_trait_info: tuple):
    """Compute the tissue correlations"""
    (this_dataset, this_trait, target_dataset, sample_data) = target_trait_info
    trait_symbol_dict = this_dataset.retrieve_genes("Symbol")
    corr_result_tissue_vals_dict = get_trait_symbol_and_tissue_values(
        symbol_list=list(trait_symbol_dict.values()))

    data = parse_tissue_corr_data(
        symbol_name=this_trait.symbol,
        symbol_dict=get_trait_symbol_and_tissue_values(
            symbol_list=[this_trait.symbol]),
        dataset_symbols=trait_symbol_dict,
        dataset_vals=corr_result_tissue_vals_dict)

    if data:
        return run_correlation(data[1], data[0], method, ",", "tissue")
    return {}

def __compute_lit_corr__(
        start_vars: dict, corr_type: str, method: str, n_top: int,
        target_trait_info: tuple):
    """Compute the literature correlations"""
    (this_dataset, this_trait, target_dataset, sample_data) = target_trait_info
    target_dataset_type = target_dataset.type
    this_dataset_type = this_dataset.type
    (this_trait_geneid, geneid_dict, species) = do_lit_correlation(
        this_trait, this_dataset)

    with database_connector() as conn:
        return reduce(
            lambda acc, lit: {**acc, **lit},
            compute_all_lit_correlation(
                conn=conn, trait_lists=list(geneid_dict.items()),
                species=species, gene_id=this_trait_geneid)[:n_top],
            {})
    return {}

def compute_correlation_rust(
        start_vars: dict, corr_type: str, method: str = "pearson",
        n_top: int = 500, should_compute_all: bool = False):
    """function to compute correlation"""
    target_trait_info = create_target_this_trait(start_vars)
    (this_dataset, this_trait, target_dataset, sample_data) = (
        target_trait_info)

    # Replace this with `match ...` once we hit Python 3.10
    corr_type_fns = {
        "sample": __compute_sample_corr__,
        "tissue": __compute_tissue_corr__,
        "lit": __compute_lit_corr__
    }
    results = corr_type_fns[corr_type](
        start_vars, corr_type, method, n_top, target_trait_info)

    # END: Replace this with `match ...` once we hit Python 3.10

    top_a = top_b = {}

    if should_compute_all:

        if corr_type == "sample":

            top_a = compute_top_n_tissue(
                this_dataset, this_trait, results, method)

            top_b = compute_top_n_lit(results, this_dataset, this_trait)

        elif corr_type == "lit":

            # currently fails for lit

            top_a = compute_top_n_sample(
                start_vars, target_dataset, list(results.keys()))
            top_b = compute_top_n_tissue(
                this_dataset, this_trait, results, method)

        else:

            top_a = compute_top_n_sample(
                start_vars, target_dataset, list(results.keys()))

            top_b = compute_top_n_lit(results, this_dataset, this_trait)

    return {
        "correlation_results": merge_results(
            results, top_a, top_b),
        "this_trait": this_trait.name,
        "target_dataset": start_vars['corr_dataset'],
        "return_results": n_top
    }
<|endoftext|>