Swap chunks or slices using a certain bounds reference.
(
self,
what: Literal["chunks", "slices"],
mode: Literal["start", "stop", "median"] | None = None,
)
| 1063 | print(f"time: {clock() - t1:.4f}. clock: {cpuclock() - c1:.4f}") |
| 1064 | |
| 1065 | def swap( |
| 1066 | self, |
| 1067 | what: Literal["chunks", "slices"], |
| 1068 | mode: Literal["start", "stop", "median"] | None = None, |
| 1069 | ) -> bool: |
| 1070 | """Swap chunks or slices using a certain bounds reference.""" |
| 1071 | # Thresholds for avoiding continuing the optimization |
| 1072 | # thnover = 4 * self.slicesize # minimum number of overlapping |
| 1073 | # # elements |
| 1074 | thnover = 40 |
| 1075 | thmult = 0.1 # minimum ratio of multiplicity (a 10%) |
| 1076 | thtover = 0.01 # minimum overlaping index for slices (a 1%) |
| 1077 | |
| 1078 | if self.verbose: |
| 1079 | t1 = clock() |
| 1080 | c1 = cpuclock() |
| 1081 | if what == "chunks": |
| 1082 | self.swap_chunks(mode) |
| 1083 | elif what == "slices": |
| 1084 | self.swap_slices(mode) |
| 1085 | if mode: |
| 1086 | message = f"swap_{what}({mode})" |
| 1087 | else: |
| 1088 | message = f"swap_{what}" |
| 1089 | nover, mult, tover = self.compute_overlaps( |
| 1090 | self.tmp, message, self.verbose |
| 1091 | ) |
| 1092 | rmult = len(mult.nonzero()[0]) / len(mult) |
| 1093 | if self.verbose: |
| 1094 | print(f"time: {clock() - t1:.4f}. clock: {cpuclock() - c1:.4f}") |
| 1095 | # Check that entropy is actually decreasing |
| 1096 | if what == "chunks" and self.last_tover > 0 and self.last_nover > 0: |
| 1097 | tover_var = (self.last_tover - tover) / self.last_tover |
| 1098 | nover_var = (self.last_nover - nover) / self.last_nover |
| 1099 | if tover_var < 0.05 and nover_var < 0.05: |
| 1100 | # Less than a 5% of improvement is too few |
| 1101 | return True |
| 1102 | self.last_tover = tover |
| 1103 | self.last_nover = nover |
| 1104 | # Check if some threshold has met |
| 1105 | if nover < thnover: |
| 1106 | return True |
| 1107 | if rmult < thmult: |
| 1108 | return True |
| 1109 | # Additional check for the overlap ratio |
| 1110 | if 0 <= tover < thtover: |
| 1111 | return True |
| 1112 | return False |
| 1113 | |
| 1114 | def create_temp(self) -> None: |
| 1115 | """Create some temporary objects for slice sorting purposes.""" |
no test coverage detected