Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/source/examples/example_conditional_grid.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1654,14 +1654,14 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "jMGyvpTqFklu"
},
"source": [
"### Execute PyExperimenter\n",
"All experiments are executed one after the other by the same `PyExperimenter` due to `max_experiments=-1`. If just a single one or a predifined number of experiments should be executed, the `-1` has to be replaced by the according amount. The `random_order` is especially important in case of parallel execution of multiple `PyExperimenter`, e.g. when doing it on a HPC, to avoid collusions of accessing the same row of the table. \n",
"\n",
"All experiments are executed one after the other by the same `PyExperimenter` due to `max_experiments=-1`. If just a single one or a predifined number of experiments should be executed, the `-1` has to be replaced by the according amount.\n",
"The first parameter, i.e. `run_svm`, relates to the actual method that should be executed with the given keyfields of the table. "
]
},
Expand Down Expand Up @@ -3170,7 +3170,7 @@
}
],
"source": [
"experimenter.execute(run_svm, max_experiments=-1, random_order=True)\n",
"experimenter.execute(run_svm, max_experiments=-1)\n",
"\n",
"# showing database table\n",
"experimenter.get_table() "
Expand Down
7 changes: 4 additions & 3 deletions docs/source/examples/example_general_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,14 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "jMGyvpTqFklu"
},
"source": [
"### Execute PyExperimenter\n",
"All experiments are executed one after the other by the same `PyExperimenter` due to `max_experiments=-1`. If just a single one or a predifined number of experiments should be executed, the `-1` has to be replaced by the according amount. The `random_order` is especially important in case of parallel execution of multiple `PyExperimenter`, e.g. when doing it on a HPC, to avoid collusions of accessing the same row of the table. \n",
"All experiments are executed one after the other by the same `PyExperimenter` due to `max_experiments=-1`. If just a single one or a predifined number of experiments should be executed, the `-1` has to be replaced by the according amount\n",
"\n",
"The first parameter, i.e. `run_ml`, relates to the actual method that should be executed with the given keyfields of the table. "
]
Expand Down Expand Up @@ -989,7 +990,7 @@
}
],
"source": [
"experimenter.execute(run_ml, max_experiments=-1, random_order=True)\n",
"experimenter.execute(run_ml, max_experiments=-1)\n",
"\n",
"# showing database table\n",
"experimenter.get_table() "
Expand Down Expand Up @@ -1849,7 +1850,7 @@
}
],
"source": [
"experimenter.execute(run_ml, max_experiments=-1, random_order=True)\n",
"experimenter.execute(run_ml, max_experiments=-1)\n",
"\n",
"# showing database table\n",
"experimenter.get_table() "
Expand Down
6 changes: 2 additions & 4 deletions docs/source/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ from py_experimenter.experimenter import PyExperimenter

experimenter = PyExperimenter()
experimenter.fill_table_from_config()
experimenter.execute(run_experiment, max_experiments=-1, random_order=True)
experimenter.execute(run_experiment, max_experiments=-1)
```

### Creating a PyExperimenter
Expand Down Expand Up @@ -158,14 +158,12 @@ An experiment can be executed easily with the following call:
```python
experimenter.execute(
experiment_function = run_experiment,
max_experiments = -1,
random_order = True
max_experiments = -1
)
```

- `experiment_function` is the [experiment funtion](#defining-the-experiment-function) described above.
- `max_experiments` determines how many experiments will be executed by this `PyExperimenter`. If set to `-1`, it will execute experiments in a sequential fashion until no more open experiments are available.
- `random_order` determines if the order in which experiments are selected for execution should be random. This is especially important to be turned on, if the execution is parallelized, e.g. on an HPC cluster.

### Reset Experiments

Expand Down
21 changes: 8 additions & 13 deletions py_experimenter/database_connector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
from datetime import datetime
import logging
from typing import Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -161,22 +162,20 @@ def _get_column_values():
def _get_existing_rows(self, column_names):
pass

def get_experiment_configuration(self, random_order: bool):
def get_experiment_configuration(self):
try:
experiment_id, description, values = self._pull_open_experiment(random_order)
experiment_id, description, values = self._pull_open_experiment()
except IndexError as e:
raise NoExperimentsLeftException("No experiments left to execute")
except Exception as e:
raise DatabaseConnectionError(f'error \n {e} raised. \n Please check if fill_table() was called correctly.')

return experiment_id, dict(zip([i[0] for i in description], *values))

def _execute_queries(self, connection, cursor, random_order) -> Tuple[int, List, List]:
if random_order:
order_by = self.__class__.random_order_string()
else:
order_by = "id"
time = utils.get_current_time()
def _execute_queries(self, connection, cursor) -> Tuple[int, List, List]:
order_by = "id"
time = datetime.now()
time = time.strftime("%m/%d/%Y, %H:%M:%S")

self.execute(cursor, f"SELECT id FROM {self.table_name} WHERE status = 'created' ORDER BY {order_by} LIMIT 1;")
experiment_id = self.fetchall(cursor)[0][0]
Expand All @@ -189,12 +188,8 @@ def _execute_queries(self, connection, cursor, random_order) -> Tuple[int, List,
description = cursor.description
return experiment_id, description, values

@abc.abstractstaticmethod
def random_order_string():
pass

@abc.abstractmethod
def _pull_open_experiment(self, random_order) -> Tuple[int, List, List]:
def _pull_open_experiment(self) -> Tuple[int, List, List]:
pass

def _write_to_database(self, keys, values) -> None:
Expand Down
8 changes: 2 additions & 6 deletions py_experimenter/database_connector_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def connect(self):
except Error as err:
raise DatabaseConnectionError(err)

def _pull_open_experiment(self, random_order) -> Tuple[int, List, List]:
def _pull_open_experiment(self) -> Tuple[int, List, List]:
with connect(**self.database_credentials) as connection:
try:
cursor = self.cursor(connection)
experiment_id, description, values = self._execute_queries(connection, cursor, random_order)
experiment_id, description, values = self._execute_queries(connection, cursor)
except Exception as err:
connection.rollback()
raise err
Expand All @@ -57,10 +57,6 @@ def _table_has_correct_structure(self, cursor, typed_fields) -> List[str]:
config_columns = [k[0] for k in typed_fields]
return set(columns) == set(config_columns)

@staticmethod
def random_order_string():
return 'RANDOM()'

def _get_existing_rows(self, column_names):
def _remove_string_markers(row):
return row.replace("'", "")
Expand Down
8 changes: 2 additions & 6 deletions py_experimenter/database_connector_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,19 @@ def _table_has_correct_structure(self, cursor, typed_fields):
config_columns = [k[0] for k in typed_fields]
return set(columns) == set(config_columns)

def _pull_open_experiment(self, random_order) -> Tuple[int, List, List]:
def _pull_open_experiment(self) -> Tuple[int, List, List]:
try:
connection = self.connect()
cursor = self.cursor(connection)
self._start_transaction(connection, readonly=False)
experiment_id, description, values = self._execute_queries(connection, cursor, random_order)
experiment_id, description, values = self._execute_queries(connection, cursor)
except Exception as err:
connection.rollback()
raise err
self.close_connection(connection)

return experiment_id, description, values

@staticmethod
def random_order_string():
return 'RAND()'

def _get_existing_rows(self, column_names):
def _remove_double_whitespaces(existing_rows):
return [' '.join(row.split()) for row in existing_rows]
Expand Down
Loading