old_contents: import numpy as np
  from scipy.optimize import basinhopping
  
  from patient_state import PatientState
  
  
  def solve():
      starting_params = PatientState.schnider_params()
  
      params_array = convert_params_structure_to_vector(starting_params)
  
      result = basinhopping(find_lsq_for_all_patients, params_array)
      return result.x
  
  
  def get_patients():
      return [{
          'age': 60,
          'expected_result': 0,
          'weight': 70,
          'height': 180,
          'sex': 'm'
      }]
  
  
  def convert_vector_to_params_structure(params_vector):
      # ToDo: need to construct params object in a well-defined way
      pass
  
  
  def convert_params_structure_to_vector(params):
      # ToDo: order of dictionary items is not well defined
      params_list = [v for k, v in params.items()]
      return np.array(params_list)
  
  
  def find_lsq_for_all_patients(params_vector):
      params = convert_vector_to_params_structure(params_vector)
      total_lsq = 0
      for patient in get_patients():
          result = solve_for_patient(patient, params)
          residual = result - patient['expected_result']
          total_lsq += residual ** 2
  
  
  def solve_for_patient(patient, params):
      patient = PatientState(patient['age'], patient['weight'], patient['height'], patient['sex'], params)
      patient.give_drug(92.60001)
      for t in range(130):
          patient.wait_time(1)
      return patient.x1
  
  
  result = solve()
  print result
  
new_contents: import numpy as np
  from scipy.optimize import basinhopping
  
  from patient_state import PatientState
  
  
  def solve():
      starting_params = PatientState.schnider_params()
  
      params_array = convert_params_structure_to_vector(starting_params)
  
      result = basinhopping(find_lsq_for_all_patients, params_array)
      return result.x
  
  
  def get_patients():
      return [{
          'age': 60,
          'expected_result': 0,
          'weight': 70,
          'height': 180,
          'sex': 'm'
      }]
  
  
  def convert_vector_to_params_structure(params_vector):
      """Convert numpy array of parameters to a parameters dictionary"""
      params = {
          'k10a': params_vector[0],
          'k10b': params_vector[1],
          'k10c': params_vector[2],
          'k10d': params_vector[3],
          'k12a': params_vector[4],
          'k12b': params_vector[5],
          'k13':  params_vector[6],
          'k21a': params_vector[7],
          'k21b': params_vector[8],
          'k21c': params_vector[9],
          'k21d': params_vector[10],
          'k31':  params_vector[11],
          'v1':   params_vector[12],
          'v3':   params_vector[13],
          'age_offset': params_vector[14],
          'weight_offset': params_vector[15],
          'lbm_offset': params_vector[16],
          'height_offset': params_vector[17]
      }
      return params
  
  
  def convert_params_structure_to_vector(params):
      """Convert parameters dictionary to a numpy array of parameters, so it can be used in a solver"""
  
      return np.array([
          params['k10a'], #0
          params['k10b'], #1
          params['k10c'], #2
          params['k10d'], #3
          params['k12a'], #4
          params['k12b'], #5
          params['k13'],  #6
          params['k21a'], #7
          params['k21b'], #8
          params['k21c'], #9
          params['k21d'], #10
          params['k31'],  #11
          params['v1'],   #12
          params['v3'],   #13
          params['age_offset'], #14,
          params['weight_offset'], #15
          params['lbm_offset'], #16
          params['height_offset'] #17
      ])
  
  
  def find_lsq_for_all_patients(params_vector):
      params = convert_vector_to_params_structure(params_vector)
      total_lsq = 0
      patient_list = get_patients()
      for patient in patient_list:
          result = solve_for_patient(patient, params)
          residual = result - patient['expected_result']
          total_lsq += residual ** 2
      return total_lsq/len(patient_list)
  
  
  def solve_for_patient(patient, params):
      patient = PatientState(patient['age'], patient['weight'], patient['height'], patient['sex'], params)
      patient.give_drug(92.60001)
      for t in range(130):
          patient.wait_time(1)
      return patient.x1
  
  
  result = solve()
  print result
  
subject: Convert numpy solving array to/from parameters