Skip to content

Commit

Permalink
Add .zip format for artifact upload (#93)
Browse files Browse the repository at this point in the history
* add .zip format

* lint and fixed bug

* fix local service

* fix local client load
  • Loading branch information
coruscating committed Feb 2, 2024
1 parent 213089e commit dc5bbd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
4 changes: 3 additions & 1 deletion qiskit_ibm_experiment/client/experiment_rest_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,7 @@ def file_download(
if result.status_code == 200:
if file_name.endswith(".yaml"):
return yaml.safe_load(result.content)
return result.json(cls=json_decoder)
elif file_name.endswith(".json"):
return result.json(cls=json_decoder)
return result.content
return result
11 changes: 9 additions & 2 deletions qiskit_ibm_experiment/client/local_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# pylint treats the dataframes as JsonReader for some reason
# pylint: disable=no-member

import io
import logging
import os
import uuid
Expand Down Expand Up @@ -767,9 +768,13 @@ def experiment_file_upload(
self._files_list[experiment_id] = []
if experiment_id not in self._files:
self._files[experiment_id] = {}
if isinstance(file_data, io.BytesIO):
size = len(file_data.getvalue())
else:
size = len(file_data)
new_file_element = {
"Key": file_name,
"Size": len(file_data),
"Size": size,
"LastModified": str(datetime.now()),
}
self._files_list[experiment_id].append(new_file_element)
Expand Down Expand Up @@ -798,4 +803,6 @@ def experiment_file_download(
raise IBMExperimentEntryNotFound
if file_name.endswith(".yaml"):
return yaml.safe_load(self._files[experiment_id][file_name])
return json.loads(self._files[experiment_id][file_name], cls=json_decoder)
elif file_name.endswith(".json"):
return json.loads(self._files[experiment_id][file_name], cls=json_decoder)
return self._files[experiment_id][file_name].read()
23 changes: 16 additions & 7 deletions qiskit_ibm_experiment/service/ibm_experiment_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,19 +1692,24 @@ def file_upload(
json_encoder: Custom encoder to use to encode the experiment.
Additional info:
The filename is expected to end with ".json" (otherwise it will be added)
The filename is expected to end with ".json", ".yaml", or ".zip", otherwise
".json" will be added,
and the data itself should be either a dictionary or a JSON serialization
with the default encoder.
"""
# currently the resultdb enforces files to end with .json or .yaml
# currently resultdb enforces files to end with .json, .yaml, or .zip
# without suffix, we assume json formatting
if not (file_name.endswith(".json") or file_name.endswith(".yaml")):
if not (
file_name.endswith(".json")
or file_name.endswith(".yaml")
or file_name.endswith(".zip")
):
file_name += ".json"
if isinstance(file_data, dict):
# for now we avoid using custom encoder with yaml files
if file_name.endswith(".yaml"):
file_data = yaml.dump(file_data)
else:
elif file_name.endswith(".json"):
file_data = json.dumps(file_data, cls=json_encoder)
self._api_client.experiment_file_upload(experiment_id, file_name, file_data)

Expand All @@ -1722,10 +1727,14 @@ def file_download(
Returns:
The JSON deserialization of the data file
Additional info:
The filename is expected to end with ".json", otherwise
it will be added.
The filename is expected to end with ".json", ".yaml", or ".zip", otherwise
".json" will be added.
"""
if not (file_name.endswith(".json") or file_name.endswith(".yaml")):
if not (
file_name.endswith(".json")
or file_name.endswith(".yaml")
or file_name.endswith(".zip")
):
file_name += ".json"
# for now we avoid using custom decoder with yaml files
file_data = self._api_client.experiment_file_download(
Expand Down

0 comments on commit dc5bbd7

Please sign in to comment.