Пример #1
0
    def load(cls, path):
        # General
        try:
            with zipfile.ZipFile(path, "r") as zip:
                namelist = zip.namelist()

                # Load Settings
                settings_path = next(name for name in namelist
                                     if str.endswith(name, ".settings"))
                settings = None
                try:
                    settings = ProjectPreferences.from_json_file(
                        zip.extract(settings_path))
                except OSError:
                    print("[1/1] Loading of the settings file %s failed" %
                          settings_path)
                else:
                    print("[1/1] Successfully loaded the settings file %s" %
                          settings_path)

                # Load Patients
                patients_path = [
                    name for name in namelist
                    if str.endswith(name, ".patient")
                ]
                patients = []
                for i, patient_path in enumerate(patients_path):
                    try:
                        patients.append(
                            Patient.from_json_file(zip.extract(patient_path)))
                    except OSError:
                        print(
                            "[{0}/{1}] Loading of the patient file {2} failed".
                            format(i + 1, len(patients_path), patient_path))
                    else:
                        print(
                            "[{0}/{1}] Successfully loaded the patient file {2}"
                            .format(i + 1, len(patients_path), patient_path))
                patients = [
                    Patient.from_json_file(zip.extract(patient))
                    for patient in patients_path
                ]

                # Load Groups
                groups_path = [
                    name for name in namelist if str.endswith(name, ".group")
                ]
                groups = []
                for i, group_path in enumerate(groups_path):
                    try:
                        groups.append(
                            Group.from_json_file(zip.extract(group_path),
                                                 patients))
                    except OSError:
                        print("[{0}/{1}] Loading of the group file {2} failed".
                              format(i + 1, len(groups_path), group_path))
                    else:
                        print(
                            "[{0}/{1}] Successfully loaded the group file {2}".
                            format(i + 1, len(groups_path), group_path))

                # Load Protocols
                protocols_path = [
                    name for name in namelist if str.endswith(name, ".prov")
                ]
                protocols = []
                for i, protocol_path in enumerate(protocols_path):
                    try:
                        protocols.append(
                            Protocol.from_json_file(
                                zip.extract(protocol_path)))
                    except OSError:
                        print(
                            "[{0}/{1}] Loading of the protocol file {2} failed"
                            .format(i + 1, len(protocols_path), protocol_path))
                    else:
                        print(
                            "[{0}/{1}] Successfully loaded the protocol file {2}"
                            .format(i + 1, len(protocols_path), protocol_path))

                # Load Datasets
                datasets_path = [
                    name for name in namelist
                    if str.endswith(name, ".dataset")
                ]
                datasets = []
                for i, dataset_path in enumerate(datasets_path):
                    try:
                        datasets.append(
                            Dataset.from_json_file(zip.extract(dataset_path),
                                                   protocols, patients))
                    except OSError:
                        print(
                            "[{0}/{1}] Loading of the dataset file {2} failed".
                            format(i + 1, len(datasets_path), dataset_path))
                    else:
                        print(
                            "[{0}/{1}] Successfully loaded the dataset file {2}"
                            .format(i + 1, len(datasets_path), dataset_path))

                # Load Visualizations
                visualizations_path = [
                    name for name in namelist
                    if str.endswith(name, ".visualization")
                ]
                visualizations = []
                for i, visualization_path in enumerate(visualizations_path):
                    try:
                        visualizations.append(
                            Visualization.from_json_file(
                                zip.extract(visualization_path), patients,
                                datasets))
                    except OSError:
                        print(
                            "[{0}/{1}] Loading of the visualization file {2} failed"
                            .format(i + 1, len(visualizations_path),
                                    visualization_path))
                    else:
                        print(
                            "[{0}/{1}] Successfully loaded the visualization file {2}"
                            .format(i + 1, len(visualizations_path),
                                    visualization_path))

                print("Successfully loaded the project %s " % path)
                return cls(settings, patients, groups, protocols, datasets,
                           visualizations)
        except OSError:
            print("Loading of the project %s failed" % path)