<reponame>antl-mipt-ru/get
import numpy as np
import matplotlib.pyplot as plt
from collections import namedtuple
 
import bloodFunctions as blood

dataPath = '10-blood/data/'
plotsPath = '10-blood/plots/'


########################################
#   Dataset
########################################

BloodDataset = namedtuple("BloodDataset", 
    ['conditions',
    'filename',
    'systole',
    'diastole',
    'systoleTime',
    'diastoleTime',
    'pulseCutIndex',
    'pulseStart',
    'pulseFinish',
    'pulseCount',
    'plotColor']
)


########################################
#   Pressure calibration
########################################

def calibratePressure():

    p40, duration, count = blood.read(dataPath + '40 mmHg.txt')
    p80, duration, count = blood.read(dataPath + '80 mmHg.txt')
    p120, duration, count = blood.read(dataPath + '120 mmHg.txt')
    p160, duration, count = blood.read(dataPath + '160 mmHg.txt')

    pressure = [40, 80, 120, 160]
    adc = [int(p40.mean()), int(p80.mean()), int(p120.mean()), int(p160.mean())]

    linearFit = np.polyfit(adc, pressure, 1)

    adcFit = np.linspace(int(p40.mean()), int(p160.mean()), 100)
    pressureFit = np.polyval(linearFit, adcFit)

    # Plot

    plt.figure('Калибровка давления', figsize=(8, 6), dpi=200)

    plt.title('Калибровочный график зависимости\nпоказаний АЦП от давления')
    plt.xlabel('Далвение [Па]')
    plt.ylabel('Отсчёты АЦП')

    plt.plot(pressure, adc, '*', label='Измерения')
    plt.plot(pressureFit, adcFit, label='P = {:.3f} * N - {:.2f} [Па]'.format(linearFit[0], -linearFit[1]))
    plt.legend()

    plt.grid(which='major')
    plt.grid(which='minor', linestyle='--')
    plt.minorticks_on()

    plt.savefig(plotsPath + '! pressure-calibration.png')

    return linearFit


########################################
#   Data processing
########################################

def processDataset(data, linearFit):

    ########################################
    #   Reading data
    ########################################

    adc, duration, count = blood.read(dataPath + data.filename + '.txt')
    time = np.linspace(0, duration, count)


    ########################################
    #   Processing pressure
    ########################################

    pressure = np.polyval(linearFit, adc)

    # Plot

    pressureFig = plt.figure('Артериальное давление', figsize=(10, 7), dpi=200)

    plt.title('Аретриальное давление\n{}'.format(data.conditions))
    plt.xlabel('Время [с]')
    plt.ylabel('Давление [мм рт. ст.]')

    plt.plot(time, pressure, data.plotColor, label='Давление - {}/{} [мм рт. ст.]'.format(data.systole, data.diastole))
    plt.plot([data.systoleTime, data.diastoleTime], [data.systole, data.diastole], 'r*')
    plt.legend()

    plt.text(data.systoleTime, data.systole + 4, 'Systole')
    plt.text(data.diastoleTime, data.diastole + 4, 'Diastole')

    plt.grid(which='major')
    plt.grid(which='minor', linestyle='--')
    plt.minorticks_on()

    plt.xlim([int(data.systoleTime - 5), int(data.diastoleTime + 5)])
    plt.ylim([data.diastole - 20, data.systole + 40])

    plt.savefig(plotsPath + data.filename + '-pressure.png')
    plt.close(pressureFig)


    ########################################
    #   Processing pulse
    ########################################

    pulsePressure = pressure[data.pulseCutIndex:]
    pulseTime = time[data.pulseCutIndex:]

    meanPulsePressure = np.polyval(np.polyfit(pulseTime, pulsePressure, 9), pulseTime)
    pulsePeaks = pulsePressure - meanPulsePressure

    pulse = int(data.pulseCount * 60 / (data.pulseFinish - data.pulseStart))

    # Plot

    pulseFig = plt.figure('Пульс', figsize=(10, 7), dpi=200)

    plt.title('Пульс\n{}'.format(data.conditions))
    plt.xlabel('Время [с]')
    plt.ylabel('Изменение давления в артерии [мм рт. ст.]')

    plt.plot(pulseTime, pulsePeaks, data.plotColor, label='Пульс - {} [уд/мин]'.format(pulse))
    plt.legend()

    plt.grid(which='major')
    plt.grid(which='minor', linestyle='--')
    plt.minorticks_on()

    plt.xlim([data.pulseStart, data.pulseFinish])

    plt.savefig(plotsPath + data.filename + '-pulse.png')
    plt.close(pulseFig)



########################################
#   Main script
########################################

restDataset = BloodDataset('до физической нагрузки', 'rest', 128, 80, 8.5, 19.3, 3000, 10.0, 30.0, 21, '#1f77b4')
fitnessDataset = BloodDataset('после физической нагрузки', 'fitness', 145, 78, 4.7, 17.15, 3000, 10.0, 30.0, 22, '#ff7f0e')

linearFit = calibratePressure()

processDataset(restDataset, linearFit)
processDataset(fitnessDataset, linearFit)
import numpy as np
import matplotlib.pyplot as plt
from collections import namedtuple
 
import bloodFunctions as blood

dataPath = '10-blood/data/'
plotsPath = '10-blood/plots/'


########################################
#   Dataset
########################################

BloodDataset = namedtuple("BloodDataset", 
    ['conditions',
    'filename',
    'systole',
    'diastole',
    'systoleTime',
    'diastoleTime',
    'pulseCutIndex',
    'pulseStart',
    'pulseFinish',
    'pulseCount',
    'plotColor']
)


########################################
#   Pressure calibration
########################################

def calibratePressure():

    p40, duration, count = blood.read(dataPath + '40 mmHg.txt')
    p80, duration, count = blood.read(dataPath + '80 mmHg.txt')
    p120, duration, count = blood.read(dataPath + '120 mmHg.txt')
    p160, duration, count = blood.read(dataPath + '160 mmHg.txt')

    pressure = [40, 80, 120, 160]
    adc = [int(p40.mean()), int(p80.mean()), int(p120.mean()), int(p160.mean())]

    linearFit = np.polyfit(adc, pressure, 1)

    adcFit = np.linspace(int(p40.mean()), int(p160.mean()), 100)
    pressureFit = np.polyval(linearFit, adcFit)

    # Plot

    plt.figure('Калибровка давления', figsize=(8, 6), dpi=200)

    plt.title('Калибровочный график зависимости\nпоказаний АЦП от давления')
    plt.xlabel('Далвение [Па]')
    plt.ylabel('Отсчёты АЦП')

    plt.plot(pressure, adc, '*', label='Измерения')
    plt.plot(pressureFit, adcFit, label='P = {:.3f} * N - {:.2f} [Па]'.format(linearFit[0], -linearFit[1]))
    plt.legend()

    plt.grid(which='major')
    plt.grid(which='minor', linestyle='--')
    plt.minorticks_on()

    plt.savefig(plotsPath + '! pressure-calibration.png')

    return linearFit


########################################
#   Data processing
########################################

def processDataset(data, linearFit):

    ########################################
    #   Reading data
    ########################################

    adc, duration, count = blood.read(dataPath + data.filename + '.txt')
    time = np.linspace(0, duration, count)


    ########################################
    #   Processing pressure
    ########################################

    pressure = np.polyval(linearFit, adc)

    # Plot

    pressureFig = plt.figure('Артериальное давление', figsize=(10, 7), dpi=200)

    plt.title('Аретриальное давление\n{}'.format(data.conditions))
    plt.xlabel('Время [с]')
    plt.ylabel('Давление [мм рт. ст.]')

    plt.plot(time, pressure, data.plotColor, label='Давление - {}/{} [мм рт. ст.]'.format(data.systole, data.diastole))
    plt.plot([data.systoleTime, data.diastoleTime], [data.systole, data.diastole], 'r*')
    plt.legend()

    plt.text(data.systoleTime, data.systole + 4, 'Systole')
    plt.text(data.diastoleTime, data.diastole + 4, 'Diastole')

    plt.grid(which='major')
    plt.grid(which='minor', linestyle='--')
    plt.minorticks_on()

    plt.xlim([int(data.systoleTime - 5), int(data.diastoleTime + 5)])
    plt.ylim([data.diastole - 20, data.systole + 40])

    plt.savefig(plotsPath + data.filename + '-pressure.png')
    plt.close(pressureFig)


    ########################################
    #   Processing pulse
    ########################################

    pulsePressure = pressure[data.pulseCutIndex:]
    pulseTime = time[data.pulseCutIndex:]

    meanPulsePressure = np.polyval(np.polyfit(pulseTime, pulsePressure, 9), pulseTime)
    pulsePeaks = pulsePressure - meanPulsePressure

    pulse = int(data.pulseCount * 60 / (data.pulseFinish - data.pulseStart))

    # Plot

    pulseFig = plt.figure('Пульс', figsize=(10, 7), dpi=200)

    plt.title('Пульс\n{}'.format(data.conditions))
    plt.xlabel('Время [с]')
    plt.ylabel('Изменение давления в артерии [мм рт. ст.]')

    plt.plot(pulseTime, pulsePeaks, data.plotColor, label='Пульс - {} [уд/мин]'.format(pulse))
    plt.legend()

    plt.grid(which='major')
    plt.grid(which='minor', linestyle='--')
    plt.minorticks_on()

    plt.xlim([data.pulseStart, data.pulseFinish])

    plt.savefig(plotsPath + data.filename + '-pulse.png')
    plt.close(pulseFig)



########################################
#   Main script
########################################

restDataset = BloodDataset('до физической нагрузки', 'rest', 128, 80, 8.5, 19.3, 3000, 10.0, 30.0, 21, '#1f77b4')
fitnessDataset = BloodDataset('после физической нагрузки', 'fitness', 145, 78, 4.7, 17.15, 3000, 10.0, 30.0, 22, '#ff7f0e')

linearFit = calibratePressure()

processDataset(restDataset, linearFit)
processDataset(fitnessDataset, linearFit)
######################################## #   Dataset ######################################## ######################################## #   Pressure calibration ######################################## # Plot ######################################## #   Data processing ######################################## ######################################## #   Reading data ######################################## ######################################## #   Processing pressure ######################################## # Plot ######################################## #   Processing pulse ######################################## # Plot ######################################## #   Main script ########################################