def export_as_cpo_s(model, model_name: Optional[str] = None, local_root: Optional[str] = None, copy_to_csv: bool = False, **kwargs) -> str: """Export .cpo file of model in the 'DSX_PROJECT_DIR.datasets' folder. It can write a copy as a .csv file, so it can be exported to a local machine. If not in DSX, it will write to the local file system in the 'local_root/datasets' directory. Args: model (docplex.cp.model): The CPLEX model to be exported model_name (str): name of .lp file. If none specified, will use the model.name. Specify if the model.name is not a valid file-name. local_root (str): name of local directory. Will write .lp file here, if not in DSX copy_to_csv (bool): If true, will create a copy of the file with the extension `.csv`. **kwargs: Passed to model.export_model Returns: path (str) path to cpo file Raises: ValueError if root directory can't be established. """ # Get model name: if model_name is None: model_name = model.name # Get root directory: sm = ScenarioManager(local_root=local_root) # Just to call the get_root_directory() # root_dir = sm.get_root_directory() datasets_dir = sm.get_data_directory() # Write regular cpo-file: cpo_file_name_1 = os.path.join(datasets_dir, model_name + '.cpo') model.export_model(cpo_file_name_1) # Writes the .cpo file # Copy to csv if copy_to_csv: cpo_file_name_2 = os.path.join(datasets_dir, model_name + '_to_csv.cpo') csv_file_name_2 = os.path.join(datasets_dir, model_name + '_cpo.csv') model.export_as_lp(cpo_file_name_2) os.rename(cpo_file_name_2, csv_file_name_2) # Return return cpo_file_name_1
def write_data_to_excel(self, excel_file_name: str = None) -> None: """Write inputs and/or outputs to an Excel file in datasets. The inputs and outputs as in the attributes `self.inputs` and `self.outputs` of the ScenarioManager If the excel_file_name is None, it will be generated from the model_name and scenario_name: MODEL_NAME + "_multi_output" Args: excel_file_name (str): The file name for the Excel file. """ if excel_file_name is None: if self.model_name is not None: excel_file_name = f"{self.model_name}_multi_output" else: raise ValueError( "The argument excel_file_name can only be 'None' if the model_name '{}' has been specified.".format( self.model_name)) # Save the regular Excel file: data_dir = self.get_data_directory() excel_file_path = os.path.join(data_dir, excel_file_name + '.xlsx') writer = pd.ExcelWriter(excel_file_path, engine='xlsxwriter') ScenarioManager.write_data_to_excel_s(writer, inputs=self.inputs, outputs=self.outputs) writer.save() if ScenarioManager.env_is_cpd25(): self.add_data_file_to_project(excel_file_path, excel_file_name + '.xlsx') return excel_file_path
def export_as_lp_s(model, model_name: Optional[str] = None, local_root: Optional[str] = None, copy_to_csv: bool = False) -> str: """Export .lp file of model in the 'DSX_PROJECT_DIR.datasets' folder. It can write a copy as a .csv file, so it can be exported to a local machine. If not in WSL, it will write to the local file system in the 'local_root/datasets' directory. Args: model (docplex.mp.model): The CPLEX model to be exported model_name (str): name of .lp file. If none specified, will use the model.name. Specify if the model.name is not a valid file-name. local_root (str): name of local directory. Will write .lp file here, if not in WSL copy_to_csv (bool): DEPRECATED. If true, will create a copy of the file with the extension `.csv`. Returns: path (str) path to lp file Raises: ValueError if root directory can't be established. """ # Get model name: if model_name is None: model_name = model.name # Get root directory: sm = ScenarioManager(local_root=local_root) # Just to call the get_root_directory() # root_dir = sm.get_root_directory() datasets_dir = sm.get_data_directory() # Write regular lp-file: # lp_file_name_1 = os.path.join(root_dir, 'datasets', model_name + '.lp') lp_file_name_1 = os.path.join(datasets_dir, model_name + '.lp') model.export_as_lp(lp_file_name_1) # Writes the .lp file ScenarioManager.add_file_as_data_asset_s(lp_file_name_1, model_name + '.lp') # platform = ScenarioManager.detect_platform() # if platform == platform.CPD40: # ScenarioManager.add_data_file_using_ws_lib_s(lp_file_name_1) # if platform == platform.CPD25: # ScenarioManager.add_data_file_to_project_s(lp_file_name_1, model_name + '.lp') # # Copy to csv (Not supported in CPD25. Not necessary.): # elif copy_to_csv: # # lp_file_name_2 = os.path.join(root_dir, 'datasets', model_name + '_to_csv.lp') # # csv_file_name_2 = os.path.join(root_dir, 'datasets', model_name + '_lp.csv') # lp_file_name_2 = os.path.join(datasets_dir, model_name + '_to_csv.lp') # csv_file_name_2 = os.path.join(datasets_dir, model_name + '_lp.csv') # model.export_as_lp(lp_file_name_2) # os.rename(lp_file_name_2, csv_file_name_2) # Return return lp_file_name_1
def get_root_directory(self) -> str: """Return the root directory of the file system. If system is WS, it will return the DSX root, otherwise the directory specified in the local_root. Raises: ValueError if root directory doesn't exist. """ if ScenarioManager.env_is_cpd25(): root_dir = '.' elif ScenarioManager.env_is_dsx(): # Note that this is False in DO! So don't run in DO root_dir = os.environ['DSX_PROJECT_DIR'] else: if self.local_root is None: raise ValueError('The local_root should be specified if loading from a file from outside of WS') root_dir = self.local_root # Assert that root_dir actually exists if not os.path.isdir(root_dir): raise ValueError("Root directory `{}` does not exist.".format(root_dir)) return root_dir
def get_data_directory(self) -> str: """Returns the path to the datasets folder. :return: path to the datasets folder """ if ScenarioManager.env_is_cpd40(): from ibm_watson_studio_lib import access_project_or_space wslib = access_project_or_space() data_dir = wslib.mount.get_base_dir() elif self.env_is_wscloud(): data_dir = '/home/dsxuser/work' # or use os.environ['PWD'] ? elif ScenarioManager.env_is_cpd25(): # Note that the data dir in CPD25 is not an actual real directory and is NOT in the hierarchy of the JupyterLab folder data_dir = '/project_data/data_asset' # Do NOT use the os.path.join! elif ScenarioManager.env_is_dsx(): data_dir = os.path.join(self.get_root_directory(), 'datasets') # Do we need to add an empty string at the end? else: # Local file system data_dir = os.path.join(self.get_root_directory(), 'datasets') return data_dir
def load_data_from_scenario(self, scenario_name): """TODO: see of by re-using a Client, this can be done faster""" sm = ScenarioManager(self.model_name, scenario_name, self.local_root, self.project_id, self.project_access_token, self.project) inputs, outputs = sm.load_data_from_scenario() return inputs, outputs