Skip to content

[BT] This is all a really rough kludge to get this working #978

@github-actions

Description

@github-actions

[BT] This is all a really rough kludge to get this working

NOW. Need to come back and think how to do this properly.

# BACKLOG(BT): This is all a really rough kludge to get this working

        )

    def _convert_matrix_time_format(
        self,
        import_dir: pathlib.Path,
        export_dir: pathlib.Path,
        from_time_format: nd.core.TimeFormat = None,
        to_time_format: nd.core.TimeFormat = None,
    ) -> None:
        """Converts matrices between time formats"""
        # TODO(BT): This function just assumes there's time periods.
        #  Won't work otherwise
        conversion_factors = from_time_format.get_conversion_factors(to_time_format)

        # Build matrix naming templates
        template = self.running_segmentation.generate_template_file_name(
            file_desc='{matrix_format}',
            trip_origin=self.trip_origin,
            year=str(self.year),
            compressed=True,
        )

        if self.trip_origin == nd.core.TripOrigin.HB.value:
            matrix_formats = ["synthetic_od_from", "synthetic_od_to"]
        elif self.trip_origin == nd.core.TripOrigin.NHB.value:
            matrix_formats = ["synthetic_od"]
        else:
            raise ValueError(f"Trip origin '{self.trip_origin}' not recognised.")

        # Build the multiprocessing kwargs
        kwarg_list = list()

        # BACKLOG(BT): This is all a really rough kludge to get this working
        #  NOW. Need to come back and think how to do this properly.
        if self.running_segmentation.has_time_period_segments():
            iterator = itertools.product(self.running_segmentation, [-1])
            naming_order = self.running_segmentation.naming_order
            segment_types = self.running_segmentation.segment_types
        else:
            tps = [1, 2, 3, 4, 5, 6]
            iterator = itertools.product(self.running_segmentation, tps)
            naming_order = self.running_segmentation.naming_order + ['tp']
            segment_types = self.running_segmentation.segment_types | {"tp": int}

        for segment_params, tp in iterator:
            # Generate filenames
            tp_params = segment_params.copy()
            if "tp" not in tp_params:
                tp_params['tp'] = tp
            segment_str = nd.core.SegmentationLevel.generate_template_segment_str(
                naming_order=naming_order,
                segment_params=tp_params,
                segment_types=segment_types,
            )

            # Build the kwarg list
            for mx_format in matrix_formats:
                fname = template.format(segment_params=segment_str, matrix_format=mx_format)
                kwarg_list.append({
                    "input_path": import_dir / fname,
                    "output_path": export_dir / fname,
                    "factor": conversion_factors[tp_params["tp"]]
                })

        # MP running
        self._logger.info(
            f"Converting OD matrix time format from {from_time_format.value} "
            f"to {to_time_format.value}."
        )
        pbar_kwargs = {'desc': "Converting OD matrix time format"}
        multiprocessing.multiprocess(
            fn=mat_utils.apply_factor,
            kwargs=kwarg_list,
            process_count=self.process_count,
            pbar_kwargs=pbar_kwargs

        )

    def compile_to_assignment_format(
        self,
        from_time_format: nd.core.TimeFormat = None,
        to_time_format: nd.core.TimeFormat = None,
    ):
        """TfN Specific helper function to compile outputs into assignment format

        This should really be the job of NorMITs Matrix tools! Move there

9f5cfd3d14f4d77aa323a9e519cad7394409d019

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions