Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 11.58 KB | None | 0 0
  1. //
  2. //  PulsatorTestVC.swift
  3. //  Vadia3
  4. //
  5. //  Created by Jan Ziółkowski on 22.08.2018.
  6. //  Copyright © 2018 Jan Ziółkowski. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Foundation
  11.  
  12. class PulsatorTestVC: UIViewController {
  13.  
  14.     override func viewDidLoad() {
  15.         super.viewDidLoad()
  16.     }
  17.  
  18.     public func testPulsator(_ channelsData: [[Double]], _ channelsDataTime: [Double], _ channels: inout [Int]) -> PulsatorTestDto? { //ms
  19.        
  20.         var result = PulsatorTestDto()
  21.         var channelPeakCounter = 0
  22.  
  23.         for i in 0..<4 {
  24.  
  25.             if !channels.contains(i + 1) {
  26.                 continue
  27.             }
  28.  
  29.             let channel = i
  30.             var channelData = channelsData[channel]
  31.  
  32.             var peakCounter = 0
  33.             var peak = false
  34.             let loopStartIndex = GetPeakIndex(channelData)
  35.  
  36.             // not enough data to create proper test
  37.             if channelData.count < loopStartIndex + 1400 || loopStartIndex == -1 { //?
  38. //                return nil
  39.                 continue
  40.             }
  41.  
  42.             // 7 sec from startDate
  43.             for k in 0..<loopStartIndex + 1400 {
  44.  
  45.                 // border value for peak at kPa
  46.                 if channelData[k] > 40 {
  47.                     peak = true
  48.                 }
  49.  
  50.                 // border value for bottom at kPa
  51.                 if channelData[k] < 2 && peak == true {
  52.                     peakCounter += 1
  53.                     peak = false
  54.  
  55.                     // 6 peaks is minimum to create a proper test
  56.                     if peakCounter > 5 {
  57.                         channelPeakCounter += 1
  58.                         break
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             var channelResult = TestPulsatorChannel(channelData)
  64.            
  65.             if channelPeakCounter < 1 {
  66.                 channelResult = nil
  67.             }
  68.                
  69.             if channelResult != nil {
  70.                 channelResult!.Scale = ""
  71.                 channelResult = CountPulsatorValue(channelResult!)
  72.                 channelResult!.startTestTime = channelsDataTime[loopStartIndex]
  73.                 channelResult!.ChannelNumber = channel + 1
  74.                 result.PulsatorTestChannelList.append(channelResult!)
  75.                 result = CountLimp(result)
  76.             }
  77.         }
  78.        
  79.         return result
  80.     }
  81.  
  82.     private func GetPeakIndex(_ channelData: [Double]) -> Int {
  83.  
  84.         for i in 0..<channelData.count {
  85.  
  86.             if channelData[i] > 40 {
  87.                 return i
  88.             }
  89.         }
  90.  
  91.         return -1
  92.     }
  93.  
  94.     private func TestPulsatorChannel(_ channelData: [Double]) -> PulsatorTestChannelDto? {
  95.         let cyclesToMeasure = 5
  96.         let phases = GetPhases(channelData, cyclesToMeasure)
  97.        
  98.         if phases == nil {
  99.             return nil
  100.         }
  101.        
  102.         let result = GetPulsatorChannelResult(phases!, cyclesToMeasure)
  103.         return result
  104.     }
  105.    
  106.     private func GetPulsatorChannelResult(_ phases: [PulsatorTestChannelDto], _ cyclesToMeasure: Int) -> PulsatorTestChannelDto {
  107.         let item = PulsatorTestChannelDto()
  108.        
  109.         item.TimeA = phases.map {$0.TimeA}.reduce(0, +) / Double(phases.count)
  110.         item.TimeB = phases.map {$0.TimeB}.reduce(0, +) / Double(phases.count)
  111.         item.TimeC = phases.map {$0.TimeC}.reduce(0, +) / Double(phases.count)
  112.         item.TimeD = phases.map {$0.TimeD}.reduce(0, +) / Double(phases.count)
  113.         item.Vmax = phases.map {$0.Vmax}.reduce(0, +) / Double(phases.count)
  114. //        item.Vmax = ScaleCalculate(item.Vmax)
  115.        
  116.         let sum = item.TimeA + item.TimeB + item.TimeC + item.TimeD
  117.        
  118.         if sum != 0 {
  119.             item.Rate = round(60000 / sum)
  120.             item.PercentA = round(item.TimeA * 100 / sum)
  121.             item.PercentB = round(item.TimeB * 100 / sum)
  122.             item.PercentC = round(item.TimeC * 100 / sum)
  123.             item.PercentD = round(item.TimeD * 100 / sum)
  124.         }
  125.            
  126.         else {
  127.             item.Rate = 0
  128.             item.PercentA = 0
  129.             item.PercentB = 0
  130.             item.PercentC = 0
  131.             item.PercentD = 0
  132.         }
  133.        
  134.         item.Ratio1 = item.PercentA + item.PercentB
  135.         item.Ratio2 = 100 - (item.PercentA + item.PercentB)
  136.        
  137.         return item
  138.     }
  139.  
  140.     private func GetPhases(_ channelData: [Double]?, _ cyclesToMeasure: Int) -> [PulsatorTestChannelDto]? {
  141.        
  142.         if channelData == nil {
  143.             return [PulsatorTestChannelDto]()
  144.         }
  145.  
  146.         var index = 0
  147.         var phases = [PulsatorTestChannelDto]()
  148.         for _ in 0..<cyclesToMeasure {
  149.             let startA = GetNextAPhaseStart(index, channelData!)
  150.             let startD = GetNextDPhaseStart(startA, channelData!)
  151.             let vMax = GetVmax(startA, startD, channelData!)
  152.             let startB = GetNextBPhaseStart(startA, startD, channelData!, vMax)
  153.             let startC = GetNextCPhaseStart(startB, startD, channelData!, vMax)
  154.             let endD = GetNextDPhaseEnd(startD, channelData!)
  155.            
  156. //            print("startA: \(startA) startB: \(startB) startC: \(startC) startD: \(startD) \n")
  157.  
  158.             let item = PulsatorTestChannelDto()
  159.             item.StartA = startA
  160.             item.StartB = startB
  161.             item.StartC = startC
  162.             item.StartD = startD
  163.             item.EndD = endD
  164.            
  165.             if [item.StartA, item.StartB, item.StartC, item.StartD, item.EndD].contains(where: { $0 == -1 }) {
  166.                 return nil
  167.             }
  168.            
  169.             item.TimeA = Double((startB - startA) * 5)
  170.             item.TimeB = Double((startC - startB) * 5)
  171.             item.TimeC = Double((startD - startC) * 5)
  172.             item.TimeD = Double((endD - startD) * 5)
  173.             item.Vmax = vMax
  174.             index = startD
  175.  
  176.             phases.append(item)
  177.         }
  178.  
  179.         for i in 0..<cyclesToMeasure - 1 {
  180.             for j in stride(from: Int(phases[i].StartB + 5), to: Int(phases[i].StartC - 5), by: 1) {
  181.                
  182.                 if abs(channelData![j] - (phases[i].Vmax * 10)) > 40 {
  183.                     phases[i].AnomalyCounterB += 1
  184.                 }
  185.             }
  186.  
  187.             for j in stride(from: Int(phases[i].StartD + 5), to: Int(phases[i + 1].StartA - 5), by: 1) {
  188.                
  189.                 if channelData![j] > 40 {
  190.                     phases[i].AnomalyCounterD += 1
  191.                 }
  192.             }
  193.         }
  194.  
  195.         return phases
  196.     }
  197.    
  198.     private func GetNextDPhaseEnd(_ startD: Int, _ channelData: [Double]) -> Int {
  199.        
  200.         if startD == -1 {
  201.             return -1
  202.         }
  203.  
  204.         for i in stride(from: startD, to: channelData.count, by: 1) {
  205.            
  206.             if channelData[i] > 4 {
  207.                 return i
  208.             }
  209.         }
  210.  
  211.         return -1
  212.     }
  213.  
  214.     private func GetNextCPhaseStart(_ startB: Int, _ startD: Int, _ channelData: [Double], _ vMax: Double) -> Int {
  215.        
  216.         for i in stride(from: startB, to: startD, by: 1) {
  217.            
  218.             if channelData[i] < vMax - 4 {
  219.                 return i
  220.             }
  221.         }
  222.  
  223.         return -1
  224.     }
  225.  
  226.     private func GetNextBPhaseStart(_ startA: Int, _ startD: Int, _ channelData: [Double], _ vMax: Double) -> Int {
  227.         for i in stride(from: startA, to: startD, by: 1) {
  228.            
  229.             if channelData[i] > vMax - 4 {
  230.                 return i
  231.             }
  232.         }
  233.  
  234.         return -1
  235.     }
  236.  
  237.     private func GetVmax(_ startA: Int, _ startD: Int, _ channelData: [Double]) -> Double {
  238.         var max = -1 as Double
  239.        
  240.         for i in stride(from: startA, to: startD, by: 1) {
  241.            
  242.             if max < channelData[i] {
  243.                 max = channelData[i]
  244.             }
  245.         }
  246.  
  247.         return max
  248.     }
  249.  
  250.     private func GetNextDPhaseStart(_ startA: Int, _ channelData: [Double]) -> Int {
  251.        
  252.         if startA == -1 {
  253.             return -1
  254.         }
  255.  
  256.         for i in startA..<channelData.count {
  257.            
  258.             if channelData[i] < 4 {
  259.                 return i
  260.             }
  261.         }
  262.        
  263.         return -1
  264.     }
  265.  
  266.     private func GetNextAPhaseStart(_ index: Int, _ channelData: [Double]) -> Int {
  267.         var next = false
  268.        
  269.         if index == -1 {
  270.             return -1
  271.         }
  272.  
  273.         for var i in index..<channelData.count {
  274.            
  275.             if channelData[i] < 4 {
  276.                 next = true
  277.             }
  278.  
  279.             if next && channelData[i] > 4 {
  280.                 return i
  281.             }
  282.  
  283.             i += 1
  284.         }
  285.  
  286.         return -1
  287.     }
  288.    
  289.     private func CountLimp(_ result: PulsatorTestDto) -> PulsatorTestDto {
  290.        
  291.         var channels = result.PulsatorTestChannelList
  292.         var percent = Array(repeating: Double(), count: 4)
  293.        
  294.         for i in 0..<channels.count {
  295.             let aib0 = channels[i].TimeA + channels[i].TimeB
  296.             let sum0 = channels[i].TimeA + channels[i].TimeB + channels[i].TimeC + channels[i].TimeD
  297.             percent[i] = (aib0 * 100) / sum0
  298.         }
  299.        
  300.         let limp = abs(percent.max()! - percent.min()!)
  301.        
  302.         for i in 0..<channels.count {
  303.             result.PulsatorTestChannelList[i].Limp = round(limp)
  304.         }
  305.        
  306.         return result
  307.     }
  308.  
  309.     public func CountPulsatorValue(_ channel: PulsatorTestChannelDto) -> PulsatorTestChannelDto {
  310.        
  311.         if channel.TimeA < 0 {
  312.             channel.TimeA = 0
  313.         }
  314.  
  315.         if channel.TimeB < 0 {
  316.             channel.TimeB = 0
  317.         }
  318.  
  319.         if channel.TimeC < 0 {
  320.             channel.TimeC = 0
  321.         }
  322.  
  323.         if channel.TimeD < 0 {
  324.             channel.TimeD = 0
  325.         }
  326.  
  327.         let sum = channel.TimeA + channel.TimeB + channel.TimeC + channel.TimeD
  328.        
  329.         if sum > 0 {
  330.             channel.PercentA = (channel.TimeA / sum) * 100
  331.             channel.PercentB = (channel.TimeB / sum) * 100
  332.             channel.PercentC = (channel.TimeC / sum) * 100
  333.             channel.PercentD = (channel.TimeD / sum) * 100
  334.             channel.Ratio1 = channel.PercentA + channel.PercentB
  335.             channel.Ratio2 = 100 - channel.Ratio1
  336.             channel.Dip = 0
  337.             channel.Rate = 60 / (sum / 1000)
  338.         }
  339.  
  340.         if channel.AnomalyCounterB > 0 {
  341.            channel.Dip = Double(channel.AnomalyCounterB)
  342.         }
  343.  
  344.         else if channel.AnomalyCounterD > 0 {
  345.            channel.Dip = Double(channel.AnomalyCounterD)
  346.         }
  347.  
  348.         return channel
  349.     }
  350.    
  351.     public enum Error { //?
  352.         case None
  353.         case NotEnoughData
  354.         case WrongChannelsNumber
  355.     }
  356.  
  357.     private func ScaleCalculate(_ inValue: Double) -> Double { // ?
  358.         if UserDefaults.standard.integer(forKey: "vacuumUnits") == 0 {
  359.             return inValue
  360.         }
  361.  
  362.         return 0.295299831 * inValue
  363.     }
  364. //
  365. //    public func GetData(_ start: Date, _ channel: Int, _ startDate: inout Date) -> [Double] {
  366. //        startDate = start
  367. //        if vadiaData[0].time > start {
  368. //            startDate = vadiaData[0].time
  369. //        }
  370. //
  371. //        return GetData(start, channel)!
  372. //    }
  373. //
  374. //    public func GetData(_ start: Date, _ channel: Int) -> [Double]? {
  375. //        if channel < 0 || channel > 3 {
  376. //            return nil
  377. //        }
  378. //        var data = vadiaData.filter {$0.time >= start}
  379. ////        var resultCount = data.count * 200
  380. //        var result = [Double]()
  381. //
  382. //        for i in 0..<data.count {
  383. //            result.append(contentsOf: data[i].channelsList[channel].prefix(200))
  384. //        }
  385. //
  386. //        return result
  387. //    }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement