人工晶状体在线公式A常数优化

这篇具有很好参考价值的文章主要介绍了人工晶状体在线公式A常数优化。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


人工晶状体在线公式A常数优化

1. Preparation

1.1 Driver

  • 对应自己电脑Chrome版本的chromedriver.exe 1

1.2 Data

Data.xlsx

Name AL ACD K1 K1_angle K2 K2_angle △K △K_angle WTW LT IOL_Power Ref_postOP Optimized A_constant Sex
xx 23.41 2.64 42.88 44.12 21.5 0 1

2

1.3 Download

  • 项目下载链接1 3
  • 项目下载链接2 4

2. Barret Universal II

2.1 Optimize.go

package main

import (
    "os"
    "fmt"
    "strconv"
    "time"
    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/tebeka/selenium"
    "./BU2"
)

const (
    chromeDriverPath = "./chromedriver"
    port             = 8080
)

func main() {
    var n int
    fmt.Println("Please input from where to start,default input is 0:")
    fmt.Scanln(&n)
   // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        //selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    //selenium.SetDebug(true)
    selenium.SetDebug(false)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    if err != nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    if err != nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    // Navigate to the simple playground interface.
    err = wd.Get("https://calc.apacrs.org/barrett_universal2105/")
    if err != nil {
      fmt.Println("get page faild", err.Error())
      //return
    }

    f, err := excelize.OpenFile("Data.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get all the rows in the Sheet1.
    DataMap := make(map[string]string, 0)
    rows, err := f.GetRows("Sheet1")
    for i, row := range rows {
        if i>=n{
            if i == 0 {
                f.SetCellValue("Sheet1", "N1", "Optimized A_constant") 
                continue
            }else{

                fmt.Println("Processing patient:")
                fmt.Println(i)
                DataMap=map[string]string{
                            "MainContent_PatientName":row[0],
                            //"MainContent_PatientNo":row[1],
                            "MainContent_Aconstant":"118.80",
                            "MainContent_Axlength":row[1],
                            "MainContent_MeasuredK1":row[3],
                            "MainContent_MeasuredK2":row[5],
                            "MainContent_OpticalACD":row[2],
                            "MainContent_Refraction":"0",
                            "MainContent_LensThickness":row[10],
                            "MainContent_WTW":row[9],
                            "IOL":row[11],
                            "Ref_PostOP":row[12],

                        }

                /*
                for k,v := range DataMap{
                    fmt.Println(k,v)
                }
                */
                A_constant := BU2.Get_A_constant(wd, DataMap)
                A_constant_ :=strconv.FormatFloat(A_constant, 'f', 4, 64)
                f.SetCellValue("Sheet1", "N"+strconv.Itoa(i+1), A_constant_) 
                fmt.Println("Optimized A_constant :" + A_constant_)
                time.Sleep(5*time.Second)
            }
            err = f.Save()
            if err != nil {
                fmt.Println(err)
                os.Exit(1)
            }
        }
    }


}

2.2 BU2

package BU2

import (
    "fmt"
    "strconv"
    //"time"
    "math"
    "strings"
    "github.com/tebeka/selenium"
)


func Find_Send(wd selenium.WebDriver, ID string, key string){
   btn, err := wd.FindElement(selenium.ByID, ID)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(key)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
}

func ConvertStrSlice2Map(sl []string) map[string]struct{} {
    set := make(map[string]struct{}, len(sl))
    for _, v := range sl {
        set[v] = struct{}{}
    }
    return set
}


func InMap(m map[string]struct{}, s string) bool {
    _, ok := m[s]
    return ok
}

func FloatRound(f float64, n int) float64 {
    format := "%." + strconv.Itoa(n) + "f"
    res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
    return res
}

func Iterate(wd selenium.WebDriver, A_constant float64,IOL string)(Ref float64){
   Patient_Data, err := wd.FindElement(selenium.ByLinkText, "Patient Data")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Patient_Data.Click(); err != nil {
        panic(err)
   }

   btn, err := wd.FindElement(selenium.ByID, "MainContent_Aconstant")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(strconv.FormatFloat(A_constant,'f', 3, 64))
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   //time.Sleep(3 * time.Second)

   Calc, err := wd.FindElement(selenium.ByID, "MainContent_Button1")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Calc.Click(); err != nil {
        panic(err)
   }

   Formula, err := wd.FindElement(selenium.ByLinkText, "Universal Formula")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Formula.Click(); err != nil {
        panic(err)
    }

   t,err :=wd.FindElement(selenium.ByTagName, "tbody")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   Power_Refs := strings.Split(Results,"\n")

   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
             Power_Ref_:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            Power_ := Power_Ref_[0]
            Ref_ := Power_Ref_[2]
            if Power_ == IOL{
                Ref,_ = strconv.ParseFloat(Ref_,64)
            }
        } 
   }
   return Ref
}

func Ajust(wd selenium.WebDriver, A_constant float64,IOL string,Ref_Post float64, Ref float64)(float64, float64){
   A_constant = FloatRound(A_constant + Ref_Post - Ref,3)
   Ref = Iterate(wd, A_constant, IOL)
   return Ref, A_constant
}

func Micro_Ajust(wd selenium.WebDriver, A_constant float64, IOL string, _Ref float64, Ref float64,Step float64)(float64,float64, float64){
   A_constant =FloatRound(A_constant + Step,3)
   Ref = Iterate(wd, A_constant, IOL)
   //fmt.Println(_Ref,Ref)
   if _Ref > Ref{
    _Step := FloatRound(math.Abs(Step)/2,4)
    Step = _Step
    fmt.Println("Upaward!")
    //fmt.Println(Step)
   }else if _Ref < Ref{
    _Step := FloatRound(math.Abs(Step)/2,4)
    Step = - _Step
    fmt.Println("Downaward!")
    //fmt.Println(Step)
   }else{
    fmt.Println("Keep original direction!")
    //fmt.Println(Step)
   }
   fmt.Printf("Refraction (SE): %.3f A_constant: %.3f next step: %.3f\n", Ref, A_constant, Step)
   //time.Sleep(10* time.Second)
   return Ref, A_constant, Step

}

func Micro_Ajust_UpAndDown(wd selenium.WebDriver, A_constant float64,IOL string, Ref float64)(float64){
    fmt.Println("Micro_Ajust_UpAndDown!")
    A_constant_max,A_constant_min :=A_constant,A_constant
    Ref0 :=Ref
    Step := 0.016
    for{
        _Ref := Ref
        Ref, A_constant_max, Step = Micro_Ajust(wd, A_constant_max,IOL, _Ref, Ref, Step) 
        if _Ref >Ref &&  math.Abs(Step) <=0.001 {
            _Ref := Ref
            var A_constant_max0 float64
            Ref, A_constant_max0, Step = Micro_Ajust(wd, A_constant_max,IOL, _Ref, Ref, Step)
            if _Ref ==Ref{
                A_constant_max=A_constant_max0
            }
            break
        }
        
    }
    fmt.Println("A_constant_max:" +strconv.FormatFloat(A_constant_max, 'f', 3, 64))

    Ref = Ref0
    Step = -0.016
    for{
        _Ref := Ref
        Ref, A_constant_min, Step = Micro_Ajust(wd, A_constant_min,IOL,_Ref, Ref, Step)
        if _Ref < Ref &&  math.Abs(Step) <=0.001{
            _Ref := Ref
            var A_constant_min0 float64
            Ref, A_constant_min0, Step = Micro_Ajust(wd, A_constant_min,IOL,_Ref, Ref, Step)
            if _Ref ==Ref{
                A_constant_min=A_constant_min0
            }
            break
        }
    }
    fmt.Println("A_constant_min:" + strconv.FormatFloat(A_constant_min, 'f', 3, 64))
    A_constant = FloatRound ((A_constant_max+A_constant_min)/2,4)
    return A_constant
}

func Get_A_constant(wd selenium.WebDriver, DataMap map[string]string)(A_constant float64) {
    IOL := DataMap["IOL"]
    Ref_PostOP := DataMap["Ref_PostOP"]
    A_constant, _ = strconv.ParseFloat(DataMap["MainContent_Aconstant"],64)
    for k,v := range DataMap{
        switch k {
        case "IOL","Ref_PostOP":
            continue
        default:
            Find_Send(wd,k,v)
        }
    }
   //time.Sleep(15*time.Second)
   Calc, err := wd.FindElement(selenium.ByID, "MainContent_Button1")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Calc.Click(); err != nil {
        panic(err)
    }
   Formula, err := wd.FindElement(selenium.ByLinkText, "Universal Formula")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Formula.Click(); err != nil {
        panic(err)
    }

   t,err :=wd.FindElement(selenium.ByTagName, "tbody")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   //fmt.Println(Results)
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(Power_Refs)
   Power_Refs_Map := map[string](string){}
   Powers := []string{}
   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
            _Power_Ref:=strings.Split(Power_Ref," ")
            Power := _Power_Ref[0]
            Powers = append(Powers,Power)
            Ref:=_Power_Ref[2]
            Power_Refs_Map[Power] = Ref
        } 
   }
   /*
   for Power,Ref := range Power_Refs_Map{
    fmt.Println(Power,Ref)
   }
   */
   //fmt.Println(Powers)
   set := ConvertStrSlice2Map(Powers)
   if !InMap(set,IOL){
    fmt.Println("IOL not in Powers")
    Patient_Data, _:= wd.FindElement(selenium.ByLinkText, "Patient Data")
    if err := Patient_Data.Click(); err != nil {
        panic(err)
    }
    return 0
   }else{
        fmt.Printf("IOL in Powers:%s\n",IOL)
        fmt.Printf("Refraction post operation:%s\n",DataMap["Ref_PostOP"])
        Ref, _:= strconv.ParseFloat(Power_Refs_Map[IOL],64)
        Ref_Post, _ := strconv.ParseFloat(Ref_PostOP,64)
        //fmt.Println(Ref,Ref_Post)
        for {
                A_constant_ := FloatRound(A_constant + Ref_Post - Ref,3)
                //fmt.Println(A_constant_)
                if !(A_constant_ >=112 && A_constant_<=125){
                    fmt.Println("A_constant out of boundary!")
                    Patient_Data, _:= wd.FindElement(selenium.ByLinkText, "Patient Data")
                    if err := Patient_Data.Click(); err != nil {
                        panic(err)
                    }
                    return 0
                    break
                }else{
                    _Ref_D :=FloatRound(Ref_Post - Ref,3)
                    Ref, A_constant = Ajust(wd, A_constant,IOL,Ref_Post, Ref)
                    Ref_D := FloatRound(Ref_Post - Ref,3)
                    //fmt.Println(_Ref_D,Ref_D)
                    if math.Abs(Ref_D)<=0.020{
                        if Ref_D ==0 {
                            A_constant = Micro_Ajust_UpAndDown(wd, A_constant, IOL, Ref)
                            break
                        }else if _Ref_D*Ref_D<0 {
                            if Ref_D >0 {
                                fmt.Println("Micro_Ajust_Up")
                                Step := 0.002
                                for{
                                    _Ref := Ref
                                    _A_constant := A_constant
                                    Ref, A_constant, Step = Micro_Ajust(wd, A_constant,IOL, _Ref, Ref, Step)
                                    Ref_D = FloatRound(Ref_Post - Ref,2)
                                    if Ref_D ==0 {
                                        A_constant = Micro_Ajust_UpAndDown(wd, A_constant, IOL, Ref)
                                        break
                                    }
                                    if _Ref > Ref {
                                        fmt.Println(_A_constant,A_constant)
                                        A_constant = FloatRound ((_A_constant +A_constant)/2,4)
                                        break
                                    }
                                }
                                break
                            }
                            if Ref_D <0{
                                fmt.Println("Micro_Ajust_Down")
                                Step :=-0.002
                                for{
                                    _Ref := Ref
                                    _A_constant := A_constant
                                    Ref, A_constant, Step = Micro_Ajust(wd, A_constant,IOL, _Ref, Ref, Step)
                                    Ref_D = FloatRound(Ref_Post - Ref,2)
                                    if Ref_D ==0 {
                                        A_constant = Micro_Ajust_UpAndDown(wd, A_constant, IOL, Ref)
                                        break
                                    }
                                    if _Ref < Ref {
                                        fmt.Println(_A_constant,A_constant)
                                        A_constant = FloatRound ((_A_constant +A_constant)/2,4)
                                        break
                                    }
                                }
                                break
                            }

                        }
                    }
                }
            //time.Sleep(10* time.Second)
        }
        
    }
   //time.Sleep(15 * time.Second)
   //wd.Quit()
   Patient_Data, _:= wd.FindElement(selenium.ByLinkText, "Patient Data")
   if err := Patient_Data.Click(); err != nil {
        panic(err)
   }
   return A_constant
}





2.3 GetRef.go

package main

import (
    "os"
    "fmt"
    "strconv"
    //"time"
    "strings"
    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/tebeka/selenium"
)

const (
    chromeDriverPath = "./chromedriver"
    port             = 8080
)

func main() {
    var n int
    fmt.Println("Please input from where to start,default input is 0:")
    fmt.Scanln(&n)
   // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        //selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    //selenium.SetDebug(true)
    selenium.SetDebug(false)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    if err != nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    if err != nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    // Navigate to the simple playground interface.
    err = wd.Get("https://calc.apacrs.org/barrett_universal2105/")
    if err != nil {
      fmt.Println("get page faild", err.Error())
      //return
    }

    f, err := excelize.OpenFile("Data.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get all the rows in the Sheet1.
    DataMap := make(map[string]string, 0)
    rows, err := f.GetRows("Sheet1")

    for i, row := range rows {
        if i>=n{
            if i == 0 {
                f.SetCellValue("Sheet1", "R1", "Ref with A_constant Optimized ") 
                continue
            }else{

                fmt.Println("Processing patient:")
                fmt.Println(i)
                DataMap=map[string]string{
                            "MainContent_PatientName":row[0],
                            //"MainContent_PatientNo":row[1],
                            "MainContent_Aconstant":"118.56",
                            "MainContent_Axlength":row[1],
                            "MainContent_MeasuredK1":row[3],
                            "MainContent_MeasuredK2":row[5],
                            "MainContent_OpticalACD":row[2],
                            "MainContent_Refraction":"0",
                            "MainContent_LensThickness":row[10],
                            "MainContent_WTW":row[9],
                            "IOL":row[11],
                            "Ref_PostOP":row[12],

                        }

                /*
                for k,v := range DataMap{
                    fmt.Println(k,v)
                }
                */
                Ref := Get_Ref(wd, DataMap)
                
                f.SetCellValue("Sheet1", "R"+strconv.Itoa(i+1), Ref) 
                fmt.Println("Ref with A_constant Optimized :" + Ref)
                //time.Sleep(5*time.Second)
            }
            err = f.Save()
            if err != nil {
                fmt.Println(err)
                os.Exit(1)
            }
        }
    }


}


func Find_Send(wd selenium.WebDriver, ID string, key string){
   btn, err := wd.FindElement(selenium.ByID, ID)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(key)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
}

func ConvertStrSlice2Map(sl []string) map[string]struct{} {
    set := make(map[string]struct{}, len(sl))
    for _, v := range sl {
        set[v] = struct{}{}
    }
    return set
}


func InMap(m map[string]struct{}, s string) bool {
    _, ok := m[s]
    return ok
}


func Get_Ref(wd selenium.WebDriver, DataMap map[string]string)(Ref string) {
    IOL := DataMap["IOL"]
    for k,v := range DataMap{
        switch k {
        case "IOL","Ref_PostOP":
            continue
        default:
            Find_Send(wd,k,v)
        }
    }
   //time.Sleep(15*time.Second)
   Calc, err := wd.FindElement(selenium.ByID, "MainContent_Button1")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Calc.Click(); err != nil {
        panic(err)
    }
   Formula, err := wd.FindElement(selenium.ByLinkText, "Universal Formula")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Formula.Click(); err != nil {
        panic(err)
    }

   t,err :=wd.FindElement(selenium.ByTagName, "tbody")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   //fmt.Println(Results)
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(Power_Refs)
   Power_Refs_Map := map[string](string){}
   Powers := []string{}
   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
            _Power_Ref:=strings.Split(Power_Ref," ")
            Power := _Power_Ref[0]
            Powers = append(Powers,Power)
            Ref:=_Power_Ref[2]
            Power_Refs_Map[Power] = Ref
        } 
   }
   
   for Power,Ref := range Power_Refs_Map{
    fmt.Println(Power,Ref)
   }
   
   //fmt.Println(Powers)
   set := ConvertStrSlice2Map(Powers)
   if !InMap(set,IOL){
    fmt.Println("IOL not in Powers")
    Patient_Data, _:= wd.FindElement(selenium.ByLinkText, "Patient Data")
    if err := Patient_Data.Click(); err != nil {
        panic(err)
    }
    return "---"
   }else{
        fmt.Printf("IOL in Powers:%s\n",IOL)
        Ref = Power_Refs_Map[IOL]
        Patient_Data, _:= wd.FindElement(selenium.ByLinkText, "Patient Data")
        if err := Patient_Data.Click(); err != nil {
            panic(err)
        }
        return Ref
    }
}

3. EVO

3.1 Optimize.go

package main

import (
    "os"
    "fmt"
    "strconv"
    //"time"
    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/tebeka/selenium"
    "./EVO"
)

const (
    chromeDriverPath = "./chromedriver"
    port             = 8080
)

func main() {
    var n int
    fmt.Println("Please input from where to start,default input is 0:")
    fmt.Scanln(&n)

    // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        //selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    //selenium.SetDebug(true)
    selenium.SetDebug(false)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    if err != nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    if err != nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    // Navigate to the simple playground interface.
    err = wd.Get("https://www.evoiolcalculator.com/calculator.aspx")
    if err != nil {
      fmt.Println("get page faild", err.Error())
      //return
    }

    f, err := excelize.OpenFile("Data.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get all the rows in the Sheet1.
    DataMap := make(map[string]string, 0)
    rows, err := f.GetRows("Sheet1")

    for i, row := range rows {
        if i>=n{
            if i == 0 {
                f.SetCellValue("Sheet1", "N1", "Optimized A_constant") 
                continue
            }else{
                fmt.Println("Processing patient:")
                fmt.Println(i)
                DataMap=map[string]string{
                            "TextBoxName":row[0],
                            //"TextBoxID":row[1],
                            "txtAConstant":"118.80",
                            "txtAL":row[1],
                            "txtK1":row[3],
                            "txtK2":row[5],
                            "txtACD":row[2],
                            "txtRefraction":"0",
                            "txtLT":row[10],
                            //"MainContent_WTW":row[9],
                            "IOL":row[11],
                            "Ref_PostOP":row[12],

                        }

                /*
                for k,v := range DataMap{
                    fmt.Println(k,v)
                }
                */
                A_constant := EVO.Get_A_constant(wd, DataMap)
                A_constant_ :=strconv.FormatFloat(A_constant, 'f', 4, 64)
                f.SetCellValue("Sheet1", "N"+strconv.Itoa(i+1), A_constant_) 
                fmt.Println("Optimized A_constant :" + A_constant_)
                //time.Sleep(15*time.Second)
            }
            err = f.Save()
            if err != nil {
                fmt.Println(err)
                os.Exit(1)
            }
        }
    }


}

3.2 EVO

package EVO

import (
    "fmt"
    "strconv"
    //"time"
    "math"
    "strings"
    "github.com/tebeka/selenium"
)


func Find_Send(wd selenium.WebDriver, ID string, key string){
   btn, err := wd.FindElement(selenium.ByID, ID)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(key)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
}

func ConvertStrSlice2Map(sl []string) map[string]struct{} {
    set := make(map[string]struct{}, len(sl))
    for _, v := range sl {
        set[v] = struct{}{}
    }
    return set
}


func InMap(m map[string]struct{}, s string) bool {
    _, ok := m[s]
    return ok
}

func FloatRound(f float64, n int) float64 {
    format := "%." + strconv.Itoa(n) + "f"
    res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
    return res
}

func Iterate(wd selenium.WebDriver, A_constant float64,IOL string)(Ref float64){
   Back, err := wd.FindElement(selenium.ByID, "btnBack")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Back.Click(); err != nil {
        panic(err)
   }

   btn, err := wd.FindElement(selenium.ByID, "txtAConstant")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(strconv.FormatFloat(A_constant,'f', 3, 64))
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   //time.Sleep(3 * time.Second)

   Calc, err := wd.FindElement(selenium.ByID, "btnCalculate")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Calc.Click(); err != nil {
        panic(err)
   }
   div,err :=wd.FindElement(selenium.ByCSSSelector, "#PnPred")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   t,err :=div.FindElement(selenium.ByCSSSelector, "tbody")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   Power_Refs := strings.Split(Results,"\n")

   for i,Power_Ref := range Power_Refs{
       if i<=5 {
            continue
        } else if i<=10 {
            Power_Ref_:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            Power_ := Power_Ref_[0]
            Ref_ := Power_Ref_[1]
            if Power_ == IOL{
                Ref,_ = strconv.ParseFloat(Ref_,64)
            }
        } 
   }
   return Ref 
}

func Ajust(wd selenium.WebDriver, A_constant float64,IOL string,Ref_Post float64, Ref float64)(float64, float64){
   A_constant = FloatRound(A_constant + Ref_Post - Ref,3)
   Ref = Iterate(wd, A_constant, IOL)
   return Ref, A_constant
}

func Micro_Ajust(wd selenium.WebDriver, A_constant float64, IOL string, _Ref float64, Ref float64,Step float64)(float64,float64, float64){
   A_constant =FloatRound(A_constant + Step,3)
   Ref = Iterate(wd, A_constant, IOL)
   //fmt.Println(_Ref,Ref)
   if _Ref > Ref{
    _Step := FloatRound(math.Abs(Step)/2,3)
    Step = _Step
    fmt.Println("Upaward!")
    //fmt.Println(Step)
   }else if _Ref < Ref{
    _Step := FloatRound(math.Abs(Step)/2,3)
    Step = - _Step
    fmt.Println("Downaward!")
    //fmt.Println(Step)
   }else{
    fmt.Println("Keep original direction!")
    //fmt.Println(Step)
   }
   //fmt.Println(Ref, A_constant, Step)
   fmt.Printf("Refraction (SE): %.3f A_constant: %.3f next step: %.3f\n", Ref, A_constant, Step)
   return Ref, A_constant, Step

}

func Micro_Ajust_UpAndDown(wd selenium.WebDriver, A_constant float64,IOL string, Ref float64)(float64){
    fmt.Println("Micro_Ajust_UpAndDown!")
    A_constant_max,A_constant_min :=A_constant,A_constant
    Ref0 :=Ref
    Step := 0.01
    for{
        _Ref := Ref
        Ref, A_constant_max, Step = Micro_Ajust(wd, A_constant_max,IOL, _Ref, Ref, Step) 
        if _Ref >Ref {
            break
        }
        
    }
    fmt.Println("A_constant_max:" +strconv.FormatFloat(A_constant_max, 'f', 3, 64))

    Ref = Ref0
    Step = -0.01
    for{
        _Ref := Ref
        Ref, A_constant_min, Step = Micro_Ajust(wd, A_constant_min,IOL,_Ref, Ref, Step)
        if _Ref < Ref {
            break
        }
    }
    fmt.Println("A_constant_min:" + strconv.FormatFloat(A_constant_min, 'f', 3, 64))
    A_constant = FloatRound ((A_constant_max+A_constant_min)/2,4)
    return A_constant
}

func Get_A_constant(wd selenium.WebDriver, DataMap map[string]string)(A_constant float64) {
   IOL := DataMap["IOL"]
   Ref_PostOP := DataMap["Ref_PostOP"]
   A_constant, _ = strconv.ParseFloat(DataMap["txtAConstant"],64)
   for k,v := range DataMap{
    switch k {
    case "IOL","Ref_PostOP":
        continue
    default:
        Find_Send(wd,k,v)
    }
   }
   //time.Sleep(3* time.Second)
   Calc, err := wd.FindElement(selenium.ByID, "btnCalculate")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Calc.Click(); err != nil {
        panic(err)
    }

   //time.Sleep(3* time.Second)
   div,err :=wd.FindElement(selenium.ByCSSSelector, "#PnPred")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   t,err :=div.FindElement(selenium.ByCSSSelector, "tbody")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   //fmt.Println(Results)
   //time.Sleep(5* time.Second)
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(len(Power_Refs))
   //fmt.Println(Power_Refs)
   Power_Refs_Map := map[string](string){}
   Powers := []string{}
   for i,Power_Ref := range Power_Refs{
       if i<=5 {
            continue
        } else if i<=10 {
            _Power_Ref:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            //fmt.Println(_Power_Ref)
            Power := _Power_Ref[0]
            //fmt.Println(Power)
            Powers = append(Powers,Power)
            Ref:=_Power_Ref[1]
            Power_Refs_Map[Power] = Ref
            //fmt.Println(Ref)
        } 
   }
   /*
   for Power,Ref := range Power_Refs_Map{
    fmt.Println(Power,Ref)
   }
   */
   //fmt.Println(Powers)
   set := ConvertStrSlice2Map(Powers)
   if !InMap(set,IOL){
    fmt.Println("IOL not in Powers")
    Back, _ := wd.FindElement(selenium.ByID, "btnBack")
    if err := Back.Click(); err != nil {
        panic(err)
    }
    return 0
   }else{
        fmt.Printf("IOL in Powers:%s\n",IOL)
        fmt.Printf("Refraction post operation:%s\n",DataMap["Ref_PostOP"])
        Ref, _:= strconv.ParseFloat(Power_Refs_Map[IOL],64)
        Ref_Post, _ := strconv.ParseFloat(Ref_PostOP,64)
        //fmt.Println(Ref,Ref_Post)
        for {
                A_constant_ := FloatRound(A_constant + Ref_Post - Ref,3)
                //fmt.Println(A_constant_)
                if !(A_constant_ >=110 && A_constant_<=125){
                    fmt.Println("A_constant out of boundary!")
                    Back, _ := wd.FindElement(selenium.ByID, "btnBack")
                    if err := Back.Click(); err != nil {
                        panic(err)
                    }
                    return 0
                    break
                }else{
                    _Ref_D :=FloatRound(Ref_Post - Ref,3)
                    Ref, A_constant = Ajust(wd, A_constant,IOL,Ref_Post, Ref)
                    Ref_D := FloatRound(Ref_Post - Ref,3)
                    //fmt.Println(_Ref_D,Ref_D)
                    if math.Abs(Ref_D)<=0.02{
                        if Ref_D ==0 {
                            A_constant = Micro_Ajust_UpAndDown(wd, A_constant, IOL, Ref)
                            break
                        }else if _Ref_D*Ref_D<0 {

                            if Ref_D >0 {
                                fmt.Println("Micro_Ajust_Up")
                                Step := 0.01
                                for{
                                    _Ref := Ref
                                    _A_constant := A_constant
                                    Ref, A_constant, Step = Micro_Ajust(wd, A_constant,IOL, _Ref, Ref, Step)
                                    Ref_D = FloatRound(Ref_Post - Ref,2)
                                    if Ref_D ==0 {
                                        A_constant = Micro_Ajust_UpAndDown(wd, A_constant, IOL, Ref)
                                        break
                                    }
                                    if _Ref > Ref {
                                        fmt.Println(_A_constant,A_constant)
                                        A_constant = FloatRound ((_A_constant +A_constant)/2,4)
                                        break
                                    }
                                }
                                break
                            }
                            if Ref_D <0{
                                fmt.Println("Micro_Ajust_Down")
                                Step :=-0.01
                                for{
                                    _Ref := Ref
                                    _A_constant := A_constant
                                    Ref, A_constant, Step = Micro_Ajust(wd, A_constant,IOL, _Ref, Ref, Step)
                                    Ref_D = FloatRound(Ref_Post - Ref,2)
                                    if Ref_D ==0 {
                                        A_constant = Micro_Ajust_UpAndDown(wd, A_constant, IOL, Ref)
                                        break
                                    }
                                    if _Ref < Ref {
                                        fmt.Println(_A_constant,A_constant)
                                        A_constant = FloatRound ((_A_constant +A_constant)/2,4)
                                        break
                                    }
                                }
                                break
                            }
                        }
                    }
                }
            //time.Sleep(10* time.Second)
        }
        
    }
   //time.Sleep(15 * time.Second)
   //wd.Quit()
   Back, _ := wd.FindElement(selenium.ByID, "btnBack")
   if err := Back.Click(); err != nil {
        panic(err)
   }
   return A_constant
}





3.3 GetRef.go

package main

import (
    "os"
    "fmt"
    "strconv"
    //"time"
    "strings"
    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/tebeka/selenium"
)

const (
    chromeDriverPath = "./chromedriver"
    port             = 8080
)

func main() {
    var n int
    fmt.Println("Please input from where to start,default input is 0:")
    fmt.Scanln(&n)

    // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        //selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    //selenium.SetDebug(true)
    selenium.SetDebug(false)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    if err != nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    if err != nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    // Navigate to the simple playground interface.
    err = wd.Get("https://www.evoiolcalculator.com/calculator.aspx")
    if err != nil {
      fmt.Println("get page faild", err.Error())
      //return
    }

    f, err := excelize.OpenFile("Data.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get all the rows in the Sheet1.
    DataMap := make(map[string]string, 0)
    rows, err := f.GetRows("Sheet1")
    for i, row := range rows {
        if i>=n{
            if i == 0 {
                f.SetCellValue("Sheet1", "R1", "Ref with A_constant Optimized ") 
                continue
            }else{

                fmt.Println("Processing patient:")
                fmt.Println(i)
                DataMap=map[string]string{
                            "TextBoxName":row[0],
                            //"TextBoxID":row[1],
                            "txtAConstant":"118.63",
                            "txtAL":row[1],
                            "txtK1":row[3],
                            "txtK2":row[5],
                            "txtACD":row[2],
                            "txtRefraction":"0",
                            "txtLT":row[10],
                            //"txtCCT":row[9],
                            "IOL":row[11],
                            "Ref_PostOP":row[12],

                        }

                /*
                for k,v := range DataMap{
                    fmt.Println(k,v)
                }
                */
                Ref := Get_Ref(wd, DataMap)
                
                f.SetCellValue("Sheet1", "R"+strconv.Itoa(i+1), Ref) 
                fmt.Println("Ref with A_constant Optimized :" + Ref)
                //time.Sleep(5*time.Second)
            }
            err = f.Save()
            if err != nil {
                fmt.Println(err)
                os.Exit(1)
            }
        }
    }


}


func Find_Send(wd selenium.WebDriver, ID string, key string){
   btn, err := wd.FindElement(selenium.ByID, ID)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(key)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
}

func ConvertStrSlice2Map(sl []string) map[string]struct{} {
    set := make(map[string]struct{}, len(sl))
    for _, v := range sl {
        set[v] = struct{}{}
    }
    return set
}


func InMap(m map[string]struct{}, s string) bool {
    _, ok := m[s]
    return ok
}


func Get_Ref(wd selenium.WebDriver, DataMap map[string]string)(Ref string) {
    IOL := DataMap["IOL"]
    for k,v := range DataMap{
        switch k {
        case "IOL","Ref_PostOP":
            continue
        default:
            Find_Send(wd,k,v)
        }
    }
   //time.Sleep(15*time.Second)
   Calc, err := wd.FindElement(selenium.ByID, "btnCalculate")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Calc.Click(); err != nil {
        panic(err)
    }

   //time.Sleep(3* time.Second)
   div,err :=wd.FindElement(selenium.ByCSSSelector, "#PnPred")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   t,err :=div.FindElement(selenium.ByCSSSelector, "tbody")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   //fmt.Println(Results)
   //time.Sleep(5* time.Second)
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(len(Power_Refs))
   //fmt.Println(Power_Refs)
   Power_Refs_Map := map[string](string){}
   Powers := []string{}
   for i,Power_Ref := range Power_Refs{
       if i<=5 {
            continue
        } else if i<=10 {
            _Power_Ref:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            //fmt.Println(_Power_Ref)
            Power := _Power_Ref[0]
            //fmt.Println(Power)
            Powers = append(Powers,Power)
            Ref:=_Power_Ref[1]
            Power_Refs_Map[Power] = Ref
            //fmt.Println(Ref)
        } 
   }
   /*
   for Power,Ref := range Power_Refs_Map{
    fmt.Println(Power,Ref)
   }
   */
   //fmt.Println(Powers)
   set := ConvertStrSlice2Map(Powers)
   if !InMap(set,IOL){
    fmt.Println("IOL not in Powers")
    Back, _ := wd.FindElement(selenium.ByID, "btnBack")
    if err := Back.Click(); err != nil {
        panic(err)
    }
    return "---"
   }else{
        fmt.Printf("IOL in Powers:%s\n",IOL)
        Ref = Power_Refs_Map[IOL]
        Back, _ := wd.FindElement(selenium.ByID, "btnBack")
        if err := Back.Click(); err != nil {
            panic(err)
        }
        return Ref
    }
}

4. Kane

Kane计算公式的网站反应太慢了,十分耗时!

4.1 Optimize.go

package main

import (
    "os"
    "fmt"
    "time"
    "strconv"
    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/tebeka/selenium"
    "./Kane"
)

const (
    chromeDriverPath = "./chromedriver"
    port             = 8080
)


func main() {
    f, err := excelize.OpenFile("Data.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get all the rows in the Sheet1.
    DataMap := make(map[string]string, 0)
    rows, err := f.GetRows("Sheet1")
    var n int
    fmt.Println("Please input from where to start,default input is 0:")
    fmt.Scanln(&n)

    // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        //selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    //selenium.SetDebug(true)
    selenium.SetDebug(false)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    if err != nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    if err != nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    // Navigate to the simple playground interface.
    err = wd.Get("https://www.iolformula.com/")
    if err != nil {
      fmt.Println("get page faild", err.Error())
      //return
   }
   Agree, err := wd.FindElement(selenium.ByCSSSelector, `div[class="btn btn-primary btn_agreement"]`)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Agree.Click(); err != nil {
        panic(err)
    }
   time.Sleep(5*time.Second)
   for i, row := range rows {
        if i>=n{
            if i == 0 {
                f.SetCellValue("Sheet1", "N1", "Optimized A_constant") 
                continue
            }else{
                fmt.Println("Processing patient:")
                fmt.Println(i)
                DataMap=map[string]string{
                            "Patient":row[0],
                            "Sex":row[14],
                            "A-Constant1":"118.80",
                            "al-right":row[1],
                            "k1-right":row[3],
                            "k2-right":row[5],
                            "acd-right":row[2],
                            "right-target":"0",
                            "lt-right":row[10],
                            //"cct-right":row[],
                            //"MainContent_WTW":row[9],
                            "IOL":row[11],
                            "Ref_PostOP":row[12],
                        }

                /*
                for k,v := range DataMap{
                    fmt.Println(k,v)
                }
                */
                A_constant := Kane.Get_A_constant(wd, DataMap)
                A_constant_ :=strconv.FormatFloat(A_constant, 'f', 4, 64)
                f.SetCellValue("Sheet1", "N"+strconv.Itoa(i+1), A_constant_) 
                fmt.Println("Optimized A_constant :" + A_constant_)
            }
            err = f.Save()
            if err != nil {
                fmt.Println(err)
                os.Exit(1)
            }
        }
    }


}

4.2 Kane

package Kane

import (
    "fmt"
    "strconv"
    "time"
    "math"
    "strings"
    "github.com/tebeka/selenium"
)



func Find_Send(wd selenium.WebDriver, ID string, key string){
   btn, err := wd.FindElement(selenium.ByID, ID)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }

   err =  btn.SendKeys(key)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
}

func ConvertStrSlice2Map(sl []string) map[string]struct{} {
    set := make(map[string]struct{}, len(sl))
    for _, v := range sl {
        set[v] = struct{}{}
    }
    return set
}


func InMap(m map[string]struct{}, s string) bool {
    _, ok := m[s]
    return ok
}

func FloatRound(f float64, n int) float64 {
    format := "%." + strconv.Itoa(n) + "f"
    res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
    return res
}

func Ajust(wd selenium.WebDriver, A_constant float64,IOL string,Ref_Post float64, Ref float64)(float64, float64){
   A_constant = FloatRound(A_constant + Ref_Post - Ref,3)
   Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
   Back, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(1)")
   if err := Back.Click(); err != nil {
        panic(err)
   }
   btn, err := wd.FindElement(selenium.ByID, "A-Constant1")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(strconv.FormatFloat(A_constant,'f', 3, 64))
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   time.Sleep(3 * time.Second)

   menu, _:= wd.FindElement(selenium.ByCSSSelector, `div[class="button_submit_block form-group submit row jq_class_1"]`)
   Calc, _ := menu.FindElement(selenium.ByCSSSelector, "div:nth-child(1)")
   if err := Calc.Click(); err != nil {
        panic(err)
   }
   time.Sleep(5 * time.Second)
   t,err :=wd.FindElement(selenium.ByCSSSelector, `div[class="res_nontoric"]`)
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   Power_Refs := strings.Split(Results,"\n")

   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
            Power_Ref_:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            Power_ := Power_Ref_[0]
            Ref_ := Power_Ref_[1]
            if Power_ == IOL{
                Ref,_ = strconv.ParseFloat(Ref_,64)
            }
        } 
   }
   return Ref, A_constant
}

func Micro_Ajust(wd selenium.WebDriver, A_constant float64, IOL string, _Ref float64, Ref float64,Step float64)(float64,float64, float64){
   A_constant =FloatRound(A_constant + Step,3)
   Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
   Back, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(1)")
   if err := Back.Click(); err != nil {
        panic(err)
   }
   btn, err := wd.FindElement(selenium.ByID, "A-Constant1")
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(strconv.FormatFloat(A_constant,'f', 3, 64))
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   time.Sleep(3 * time.Second)
   menu, _:= wd.FindElement(selenium.ByCSSSelector, `div[class="button_submit_block form-group submit row jq_class_1"]`)
   Calc, _ := menu.FindElement(selenium.ByCSSSelector, "div:nth-child(1)")
   if err := Calc.Click(); err != nil {
        panic(err)
   }
   time.Sleep(5 * time.Second)
   t,err :=wd.FindElement(selenium.ByCSSSelector, `div[class="res_nontoric"]`)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(Power_Refs)
   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
            Power_Ref_:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            Power_ := Power_Ref_[0]
            Ref_ := Power_Ref_[1]
            //fmt.Println(Power_, Ref_)
            if Power_ == IOL{
                Ref,_ = strconv.ParseFloat(Ref_,64)
            }
        } 
   }
   //fmt.Println(_Ref,Ref)
   if _Ref > Ref{
    _Step := FloatRound(math.Abs(Step)/2,3)
    Step = _Step
    fmt.Println("Upaward!")
    //fmt.Println(Step)
   }else if _Ref < Ref{
    _Step := FloatRound(math.Abs(Step)/2,3)
    Step = - _Step
    fmt.Println("Downaward!")
    //fmt.Println(Step)
   }else{
    fmt.Println("Keep original direction!")
    //fmt.Println(Step)
   }
   //fmt.Println(Ref, A_constant, Step)
   fmt.Printf("Refraction (SE): %.3f A_constant: %.3f next step: %.3f\n", Ref, A_constant, Step)
   return Ref, A_constant, Step

}

func Get_A_constant(wd selenium.WebDriver, DataMap map[string]string)(A_constant float64) {
   IOL := DataMap["IOL"]
   if len(IOL)<=2{IOL = IOL +".0"}
   Ref_PostOP := DataMap["Ref_PostOP"]
   A_constant, _ = strconv.ParseFloat(DataMap["A-Constant1"],64)
   for k,v := range DataMap{
    switch k {
    case "Sex","IOL","Ref_PostOP":
        continue
    default:
        Find_Send(wd,k,v)
    }
   }
   time.Sleep(time.Second)
   //time.Sleep(3* time.Second)
   Sex, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="btn-group radio-group h-gender"]`)
   male, _ := Sex.FindElement(selenium.ByCSSSelector, "label:nth-child(1)")
   female, _ := Sex.FindElement(selenium.ByCSSSelector, "label:nth-child(2)")
   if DataMap["Sex"] =="1"{
       if err := male.Click(); err != nil {
        panic(err)
       }
   }else {
       if err := female.Click(); err != nil {
        panic(err)
       }
   }
   Submit, _ := wd.FindElement(selenium.ByCSSSelector, `div[class ="button_submit_block form-group submit row jq_class_1"]`)
   Calc, _ := Submit.FindElement(selenium.ByCSSSelector, "div:nth-child(1)")
   if err := Calc.Click(); err != nil {
        panic(err)
    }
   time.Sleep(5*time.Second)
   t,err :=wd.FindElement(selenium.ByCSSSelector, `div[class="res_nontoric"]`)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   //fmt.Println(Results)
   //time.Sleep(5* time.Second)
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(len(Power_Refs))
   //fmt.Println(Power_Refs)
   Power_Refs_Map := map[string](string){}
   Powers := []string{}
   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
            _Power_Ref:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            //fmt.Println(_Power_Ref)
            Power := _Power_Ref[0]
            //fmt.Println(Power)
            Powers = append(Powers,Power)
            Ref:=_Power_Ref[1]
            Power_Refs_Map[Power] = Ref
            //fmt.Println(Ref)
        } 
   }
   /*
   for Power,Ref := range Power_Refs_Map{
    fmt.Println(Power,Ref)
   }
   */
   //fmt.Println(Powers)
   set := ConvertStrSlice2Map(Powers)
   if !InMap(set,IOL){
    fmt.Println("IOL not in Powers")
    Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
    NewPatient, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(3)")
    if err := NewPatient.Click(); err != nil {
        panic(err)
    }
    return 0
   }else{
        fmt.Printf("IOL in Powers:%s\n",IOL)
        fmt.Printf("Refraction post operation:%s\n",DataMap["Ref_PostOP"])
        Ref, _:= strconv.ParseFloat(Power_Refs_Map[IOL],64)
        Ref_Post, _ := strconv.ParseFloat(Ref_PostOP,64)
        //fmt.Println(Ref,Ref_Post)
        for {
                A_constant_ := FloatRound(A_constant + Ref_Post - Ref,3)
                //fmt.Println(A_constant_)
                if !(A_constant_ >=110 && A_constant_<=125){
                    fmt.Println("A_constant out of boundary!")
                    Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
                    NewPatient, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(3)")
                    if err := NewPatient.Click(); err != nil {
                        panic(err)
                    }
                    return 0
                    break
                }else{
                    _Ref_D :=FloatRound(Ref_Post - Ref,2)
                    Ref, A_constant = Ajust(wd, A_constant,IOL,Ref_Post, Ref)
                    Ref_D := FloatRound(Ref_Post - Ref,2)
                    //fmt.Println(_Ref_D,Ref_D)
                    if math.Abs(Ref_D)<=0.02{
                        if Ref_D ==0 {
                            fmt.Println("Micro_Ajust_UpAndDown!")
                            A_constant_max,A_constant_min :=A_constant,A_constant
                            Ref0 :=Ref
                            Step := 0.002
                            for{
                                _Ref := Ref
                                Ref, A_constant_max, Step = Micro_Ajust(wd, A_constant_max,IOL, _Ref, Ref, Step) 
                                if _Ref >Ref {
                                    break
                                }
                                
                            }
                            fmt.Println("A_constant_max:" +strconv.FormatFloat(A_constant_max, 'f', 3, 64))

                            Ref = Ref0
                            Step = -0.002
                            for{
                                _Ref := Ref
                                Ref, A_constant_min, Step = Micro_Ajust(wd, A_constant_min,IOL, _Ref, Ref, Step)
                                if _Ref < Ref {
                                    break
                                }
                            }
                            fmt.Println("A_constant_min:" + strconv.FormatFloat(A_constant_min, 'f', 3, 64))
                            A_constant = FloatRound ((A_constant_max+A_constant_min)/2,4)
                            break
                        }else if _Ref_D*Ref_D<0 {

                            if Ref_D >0 {
                                fmt.Println("Micro_Ajust_Up")
                                Step := 0.002
                                for{
                                    _Ref := Ref
                                    _A_constant := A_constant
                                    Ref, A_constant, Step = Micro_Ajust(wd, A_constant,IOL, _Ref, Ref, Step)
                                    Ref_D = FloatRound(Ref_Post - Ref,2)
                                    if Ref_D ==0 {
                                        break
                                    }
                                    if _Ref > Ref {
                                        fmt.Println(_A_constant,A_constant)
                                        A_constant = FloatRound ((_A_constant +A_constant)/2,4)
                                        break
                                    }
                                }
                                break
                            }
                            if Ref_D <0{
                                fmt.Println("Micro_Ajust_Down")
                                Step :=-0.002
                                for{
                                    _Ref := Ref
                                    _A_constant := A_constant
                                    Ref, A_constant, Step = Micro_Ajust(wd, A_constant,IOL, _Ref, Ref, Step)
                                    Ref_D = FloatRound(Ref_Post - Ref,2)
                                    if Ref_D ==0 {
                                        break
                                    }
                                    if _Ref < Ref {
                                        fmt.Println(_A_constant,A_constant)
                                        A_constant = FloatRound ((_A_constant +A_constant)/2,4)
                                        break
                                    }
                                }
                                break
                            }

                        }   
                    }
                    
                }
            //time.Sleep(10* time.Second)
        }
        
    }
   //time.Sleep(15 * time.Second)
   //wd.Quit()
   Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
   NewPatient, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(3)")
   if err := NewPatient.Click(); err != nil {
        panic(err)
   }
   //time.Sleep(5*time.Second)
   return A_constant
}

4.3 GetRef.go

package main

import (
    "os"
    "fmt"
    "strconv"
    "time"
    "strings"
    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/tebeka/selenium"
)

const (
    chromeDriverPath = "./chromedriver"
    port             = 8080
)

func main() {
    var n int
    fmt.Println("Please input from where to start,default input is 0:")
    fmt.Scanln(&n)

    // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        //selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    //selenium.SetDebug(true)
    selenium.SetDebug(false)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    if err != nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    if err != nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    // Navigate to the simple playground interface.
    err = wd.Get("https://www.iolformula.com/")
    if err != nil {
      fmt.Println("get page faild", err.Error())
      //return
   }
   Agree, err := wd.FindElement(selenium.ByCSSSelector, `div[class="btn btn-primary btn_agreement"]`)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   if err := Agree.Click(); err != nil {
        panic(err)
    }
   time.Sleep(5*time.Second)

    f, err := excelize.OpenFile("Data.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get all the rows in the Sheet1.
    DataMap := make(map[string]string, 0)
    rows, err := f.GetRows("Sheet1")

    for i, row := range rows {
        if i>=n{
            if i == 0 {
                f.SetCellValue("Sheet1", "R1", "Ref with A_constant Optimized ") 
                continue
            }else{

                fmt.Println("Processing patient:")
                fmt.Println(i)
                DataMap=map[string]string{
                            "Patient":row[0],
                            "Sex":row[14],
                            "A-Constant1":"118.80",
                            "al-right":row[1],
                            "k1-right":row[3],
                            "k2-right":row[5],
                            "acd-right":row[2],
                            "right-target":"0",
                            "lt-right":row[10],
                            //"cct-right":row[],
                            "IOL":row[11],
                            "Ref_PostOP":row[12],
                        }

                /*
                for k,v := range DataMap{
                    fmt.Println(k,v)
                }
                */
                Ref := Get_Ref(wd, DataMap)
                
                f.SetCellValue("Sheet1", "R"+strconv.Itoa(i+1), Ref) 
                fmt.Println("Ref with A_constant Optimized :" + Ref)
                //time.Sleep(5*time.Second)
            }
            err = f.Save()
            if err != nil {
                fmt.Println(err)
                os.Exit(1)
            }
        }
    }


}


func Find_Send(wd selenium.WebDriver, ID string, key string){
   btn, err := wd.FindElement(selenium.ByID, ID)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.Clear()
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   err =  btn.SendKeys(key)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
}

func ConvertStrSlice2Map(sl []string) map[string]struct{} {
    set := make(map[string]struct{}, len(sl))
    for _, v := range sl {
        set[v] = struct{}{}
    }
    return set
}


func InMap(m map[string]struct{}, s string) bool {
    _, ok := m[s]
    return ok
}


func Get_Ref(wd selenium.WebDriver, DataMap map[string]string)(Ref string) {
   IOL := DataMap["IOL"]
   if len(IOL)<=2{IOL = IOL +".0"}
   //fmt.Println(IOL)
   for k,v := range DataMap{
    switch k {
    case "Sex","IOL","Ref_PostOP":
        continue
    default:
        Find_Send(wd,k,v)
    }
   }
   time.Sleep(time.Second)
   //time.Sleep(3* time.Second)
   Sex, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="btn-group radio-group h-gender"]`)
   male, _ := Sex.FindElement(selenium.ByCSSSelector, "label:nth-child(1)")
   female, _ := Sex.FindElement(selenium.ByCSSSelector, "label:nth-child(2)")
   if DataMap["Sex"] =="1"{
       if err := male.Click(); err != nil {
        panic(err)
       }
   }else {
       if err := female.Click(); err != nil {
        panic(err)
       }
   }
   Submit, _ := wd.FindElement(selenium.ByCSSSelector, `div[class ="button_submit_block form-group submit row jq_class_1"]`)
   Calc, _ := Submit.FindElement(selenium.ByCSSSelector, "div:nth-child(1)")
   if err := Calc.Click(); err != nil {
        panic(err)
    }
   time.Sleep(5*time.Second)
   t,err :=wd.FindElement(selenium.ByCSSSelector, `div[class="res_nontoric"]`)
   if err != nil {
      //panic(err)
      fmt.Println(err)
   }
   Results,err :=t.Text()
   if err != nil {
     panic(err)
   }
   //fmt.Println(Results)
   //time.Sleep(5* time.Second)
   Power_Refs := strings.Split(Results,"\n")
   //fmt.Println(len(Power_Refs))
   //fmt.Println(Power_Refs)
   Power_Refs_Map := map[string](string){}
   Powers := []string{}
   for i,Power_Ref := range Power_Refs{
       if i==0 {
            continue
        } else {
            _Power_Ref:=strings.Split(strings.TrimSpace(Power_Ref)," ")
            //fmt.Println(_Power_Ref)
            Power := _Power_Ref[0]
            //fmt.Println(Power)
            Powers = append(Powers,Power)
            Ref:=_Power_Ref[1]
            Power_Refs_Map[Power] = Ref
            //fmt.Println(Ref)
        } 
   }
   /*
   for Power,Ref := range Power_Refs_Map{
    fmt.Println(Power,Ref)
   }
   */
   //fmt.Println(Powers)
   set := ConvertStrSlice2Map(Powers)
   if !InMap(set,IOL){
    fmt.Println("IOL not in Powers")
    Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
    NewPatient, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(3)")
    if err := NewPatient.Click(); err != nil {
        panic(err)
    }
    return "---"
   }else{
        fmt.Printf("IOL in Powers:%s\n",IOL)
        Ref = Power_Refs_Map[IOL]
        Menu, _ := wd.FindElement(selenium.ByCSSSelector, `div[class="form-group submit row"]`)
        NewPatient, _ := Menu.FindElement(selenium.ByCSSSelector, "div:nth-child(3)")
        if err := NewPatient.Click(); err != nil {
            panic(err)
        }
        return Ref
    }
}

  1. http://chromedriver.storage.googleapis.com/index.html ↩︎

  2. Data.xlsx更换为自己的数据,请勿调整列顺序,增删列;Sex:1代表男,2代表女。Optimize.go里DataMap的初始A常数根据对应的晶体稍作调整,cmd窗口运行可保存运行记录,go run optimize.go > ./runlog.txt或者optimize.exe >./runlog.txt,即标准输出重定向到runlog.txt,终端将不可见输出,但需要输入0或者上次停止的位置以启动程序。GetRef.go 为得到优化A常数后,用优化后的A常数的平均值代入在线计算器以计算每个晶体的预留度数,请自行修改对应的A常数值 ↩︎

  3. 项目下载链接1:https://gitee.com/niu_xiao_long/Optimize_A_constant ↩︎

  4. 项目下载链接2:https://github.com/Lemeilleurprince/Optimize_A_constant ↩︎文章来源地址https://www.toymoban.com/news/detail-401354.html

到了这里,关于人工晶状体在线公式A常数优化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包