"""Functions to help working with local project data and information files."""importjsonimportpandasaspdimportosimportwarnings
[docs]deffile_df_name(projectId):""" Generate the default file name for project data file. :param projectId: The project's ID as found on PhotosynQ. If not provided, an Exception will be raised. :type projectId: int or str :return: The default file name for the project data frame. :rtype: str :raises ValueError: If no project ID is provided. """ifprojectIdisNone:raiseValueError("No Project ID provided to generate a filename")returnstr("PhotosynQ_"+str(projectId)+".pickle")
[docs]deffile_info_name(projectId):""" Generate the default file name for project info file. :param projectId: The project's ID as found on PhotosynQ. If not provided, an Exception will be raised. :type projectId: int or str :return: The default file name for the project information. :rtype: str :raises ValueError: If no project ID is provided. """ifprojectIdisNone:raiseValueError("No Project ID provided to generate a filename")returnstr("PhotosynQ_"+str(projectId)+".json")
[docs]deffile_exists(filename="",directory='.'):""" Check if local file exists. :param filename: The filename of the file. Defaults to an empty string. :type filename: str :param directory: The file location. Defaults to the current directory ("."). :type directory: str :return: True if the file exists, False otherwise. :rtype: bool :raises ValueError: If filename or directory are not strings. """ifnotisinstance(filename,str):raiseValueError("Provided filename needs to be a string")ifnotisinstance(directory,str):raiseValueError("Provided directory needs to be a string")returnos.path.isfile(os.path.join(directory,filename))
[docs]defsave(projectId,directory='.',df=None,info=None):""" Save project data and information to local files. For the data a file named like ``PhotosynQ_xxx.pickle`` will be saved. The content is compressed using the ``zip`` algorithm. For the project information a file in the ``JSON`` format is saved. :param projectId: The project's ID as found on PhotosynQ. If not provided, an Exception will be raised. :type projectId: int or str :param directory: The file location. Defaults to the current directory ("."). :type directory: str :param df: List of dataframes as returned by :func:`~jii_multispeq.project.get` :type df: list[dataframe] :param info: Project information as returned by :func:`~jii_multispeq.project.get` :type info: dict :return: None :rtype: NoneType :raises ValueError: If no project ID is provided. :raises ValueError: If directory is not a string. """ifprojectIdisNone:raiseValueError("No Project ID provided to generate a filename")ifnotisinstance(directory,str):raiseValueError("Provided directory needs to be a string")ifdfisnotNone:file=os.path.join(directory,file_df_name(projectId))pd.to_pickle(df,file,compression='zip')ifinfoisnotNone:file=os.path.join(directory,file_info_name(projectId))withopen(file,'w',encoding='utf-8')asf:json.dump(info,f,ensure_ascii=False,indent=2)
[docs]defload(projectId,directory='.'):""" Load a project's data and information based on the project's ID from the local drive. :param project_id: The project's ID as found on PhotosynQ. If not provided, a ValueError will be raised. :type project_id: int or str :param directory: The file location. Defaults to the current directory ("."). :type directory: str :return: A list of DataFrames representing the project's data and the project's information. Each will be set to None if the corresponding file is not found. :rtype: list[pandas.DataFrame], dict """# Generate file name for data frame(s)df_file=file_df_name(projectId)# Generate file name for project fileproject_file=file_info_name(projectId)# Check if data file existsifnotfile_exists(df_file,directory):warnings.warn("Project Data File not found (PhotosynQ_xxx.pickle)")df=None# Load data frameselse:df=pd.read_pickle(os.path.join(directory,df_file),compression='zip')# Check if project file existsifnotfile_exists(project_file,directory):warnings.warn("Project Information File not found (PhotosynQ_xxx.json)")info=None# Load project informationelse:withopen(os.path.join(directory,project_file),'r')asfile:info=json.load(file)returndf,info