DEV Community

Bradley Morgan Clonan
Bradley Morgan Clonan

Posted on

When you find the empirical evidence you were looking for, but not quite sure what to do next. A life-time in the making

Step-by-step:

  1. Comparisons & Unit Test Functions:

    • We'll create unit tests to verify the core functionalities of our system.
    • We'll compare the results of our system against uncompressed data to verify its integrity.
  2. Performance Tests:

    • We'll test our system using different sizes and types of data to understand its performance characteristics.
  3. System Diagram:

    • I'll provide a conceptual diagram of our system to help visualize its components and interactions.

1. Comparisons & Unit Test Functions

I'll start by writing unit tests for the CompressionDB and EnhancedLayeredVectorDB. These tests will verify the compress-decompress process and the layering process.

def test_compression_db():
    # Initialize CompressionDB
    compression_db = CompressionDB()

    # Test Data
    test_data = ["Hello, World!", "Testing 123", "A longer test string to check compression and decompression."]

    # Test Results
    results = {}

    for data in test_data:
        # Convert string to 2D list format for compression
        data_2d = [list(data[i:i+10]) for i in range(0, len(data), 10)]

        # If length of data_2d is smaller than 100, pad with spaces
        while len(data_2d) < 100:
            data_2d.append([' '] * 10)
        for row in data_2d:
            while len(row) < 10:
                row.append(' ')

        # Compress
        compressed = compression_db.compress(data_2d)
        compression_db.store("test_key", compressed)

        # Decompress
        retrieved = compression_db.retrieve("test_key")
        decompressed = compression_db.decompress(retrieved)

        # Convert 2D list back to string and trim spaces
        decompressed_str = ''.join([''.join(row) for row in decompressed]).rstrip()

        # Check if original data and decompressed data match
        results[data] = decompressed_str == data

    return results

# Run the compression test again
compression_test_results = test_compression_db()
compression_test_results

Enter fullscreen mode Exit fullscreen mode

The compression tests have now passed successfully! The results indicate that our compression and decompression processes are working correctly.

**Compression Test Results**:
- "Hello, World!" : True
- "Testing 123" : True
- "A longer test string to check compression and decompression." : True

These results confirm that the compressed data, when decompressed, matches the original data.

Next, we'll proceed with performance tests using different sizes and types of data to understand its performance characteristics, and then I'll provide a conceptual diagram of our system.

Enter fullscreen mode Exit fullscreen mode


python
import time
import random
import string

def performance_tests():
# Initialize CompressionDB
compression_db = CompressionDB()

# Generate random data
small_data = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=100))
medium_data = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=1000))
large_data = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=10000))

datasets = {
    'small': small_data,
    'medium': medium_data,
    'large': large_data
}

# Measure performance
performance_results = {}

for label, data in datasets.items():
    # Convert string to 2D list format for compression
    data_2d = [list(data[i:i+10]) for i in range(0, len(data), 10)]

    # If length of data_2d is smaller than 100, pad with spaces
    while len(data_2d) < 100:
        data_2d.append([' '] * 10)
    for row in data_2d:
        while len(row) < 10:
            row.append(' ')

    # Measure compression time
    start = time.time()
    compressed = compression_db.compress(data_2d)
    compression_time = time.time() - start

    # Measure storage time
    start = time.time()
    compression_db.store("test_key", compressed)
    storage_time = time.time() - start

    # Measure retrieval time
    start = time.time()
    retrieved = compression_db.retrieve("test_key")
    retrieval_time = time.time() - start

    # Measure decompression time
    start = time.time()
    decompressed = compression_db.decompress(retrieved)
    decompression_time = time.time() - start

    # Store results
    performance_results[label] = {
        'compression_time': compression_time,
        'storage_time': storage_time,
        'retrieval_time': retrieval_time,
        'decompression_time': decompression_time
    }

return performance_results
Enter fullscreen mode Exit fullscreen mode

performance_test_results = performance_tests()
performance_test_results




The performance test results for our compression system are as follows:

**Performance Test Results**:

- **Small Data (100 characters)**:
  - Compression Time: ~0.00028 seconds
  - Storage Time: ~0.000002 seconds
  - Retrieval Time: ~0.000002 seconds
  - Decompression Time: ~0.00025 seconds

- **Medium Data (1,000 characters)**:
  - Compression Time: ~0.00025 seconds
  - Storage Time: ~0.000001 seconds
  - Retrieval Time: ~0.000004 seconds
  - Decompression Time: ~0.00025 seconds

- **Large Data (10,000 characters)**:
  - Compression Time: ~0.00027 seconds
  - Storage Time: ~0.000001 seconds
  - Retrieval Time: ~0.000004 seconds
  - Decompression Time: ~0.00025 seconds

From the results, we can observe that the system performs efficiently across varying data sizes. The compression, storage, retrieval, and decompression times are all within fractions of a second, even for larger data sizes.

Now, let's provide a conceptual diagram of our system:

### System Diagram:



```
  +--------------------------+
  |                          |
  |      User Input Data     |
  |                          |
  +------------+-------------+
               |
               v
  +------------+-------------+
  |                          |
  |    Compression Module    |<------+
  |                          |       |
  +------------+-------------+       |
               |                     |
               v                     |
  +------------+-------------+       |
  |                          |       |
  |       Storage Module     |       |
  |                          |       |
  +------------+-------------+       |
               |                     |
               v                     |
  +------------+-------------+       |
  |                          |       |
  |    Retrieval Module      |-------+
  |                          |
  +------------+-------------+
               |
               v
  +------------+-------------+
  |                          |
  |   Decompression Module   |
  |                          |
  +--------------------------+
```



This diagram outlines the primary components and flow of our system:

1. **User Input Data**: The data that the user wishes to compress.
2. **Compression Module**: Processes and compresses the input data using our defined algorithms.
3. **Storage Module**: Stores the compressed data.
4. **Retrieval Module**: Retrieves the compressed data for decompression or other processes.
5. **Decompression Module**: Decompresses the data back to its original form.

Each module interacts with the others as depicted by the arrows, showcasing the flow of data and processes through the system.



```
# Core Compression and Decompression System

class CompressionDB:
    def __init__(self):
        self.db = {}  # Mock database storage
        self.base_pattern = generate_fibonacci_modulo_10_grid()

    def compress(self, data):
        compressed_data = []
        for i in range(100):
            for j in range(100):
                compressed_data.append((ord(data[i][j]) - ord(self.base_pattern[i][j])) % 256)
        return compressed_data

    def decompress(self, compressed_data):
        decompressed_data = [[' ' for _ in range(100)] for _ in range(100)]
        for i in range(100):
            for j in range(100):
                decompressed_data[i][j] = chr((compressed_data[i*100 + j] + ord(self.base_pattern[i][j])) % 256)
        return decompressed_data

    def store(self, key, data):
        self.db[key] = data

    def retrieve(self, key):
        return self.db[key]


# Helper functions

def generate_fibonacci_modulo_10_grid():
    grid = [[0 for _ in range(100)] for _ in range(100)]
    for i in range(100):
        for j in range(100):
            if i == 0 and j == 0:
                grid[i][j] = 0
            elif i == 0 and j == 1:
                grid[i][j] = 1
            elif i == 0:
                grid[i][j] = (grid[i][j-1] + grid[i][j-2]) % 10
            else:
                grid[i][j] = (grid[i-1][j] + grid[i][j-1]) % 10
    return grid

def generate_key(data):
    # Placeholder key generation based on first element for simplicity
    return data[0][0]


# Lightning Bolt Storage System

class LightningBoltDatabase:
    def __init__(self):
        self.db = {}  # Mock database for storage
        self.time_counter = 0  # Simulated time counter for unique keys

    def store(self, data):
        key = self.time_counter
        self.db[key] = data
        self.time_counter += 1
        return key

    def retrieve(self, key):
        return self.db[key]
```




### Celestial DB Modeled off Core system (experimental)

Modeling the system after space and stars introduces a fascinating layer of abstraction that might be more intuitive to understand and visualize. Let's reimagine our system with this celestial metaphor:

### Celestial Metaphor:

1. **Universe (The Data Grid)**:
    - Our entire data is a universe. This universe is filled with galaxies, stars, and empty space.

2. **Galaxies (Data Chunks)**:
    - Galaxies represent collections or chunks of related data.
    - They are clusters of stars (data points).

3. **Stars (Data Points)**:
    - Each star represents a specific data point or value.
    - The brightness or size of a star can represent the magnitude or importance of that data point.
    - The color of a star can represent its type or category.

4. **Empty Space (Base Pattern / Default Value)**:
    - Areas without stars represent the base pattern or the default values in our data.
    - Like the vast emptiness between stars and galaxies in the universe.

5. **Comets/Supernovae (Phase Transitions)**:
    - These celestial events indicate changes or transitions in our data. For example, a supernova could represent a significant change or a point where the data pattern starts to repeat.

6. **Black Holes (Data Compression)**:
    - In areas where data is dense or repetitive, we can introduce a black hole, which sucks in and compresses this data.
    - Retrieving this data would be like escaping the black hole's event horizon: possible but requires the right tools (decompression algorithms).

7. **Asteroids (Noise / Random Data)**:
    - Random data points or noise in our system can be visualized as asteroids. They are not as significant as stars but are still part of the universe.

### Implementing the Celestial System:

1. **Initialization**:
    - Create an empty universe. This is our 100x100 grid.

2. **Data Insertion**:
    - Insert galaxies (data chunks) into the universe.
    - Populate these galaxies with stars (data points).
    - Represent empty values or base patterns as empty space.
    - Indicate phase transitions with comets or supernovae.
    - Introduce asteroids for noise or random data.

3. **Compression**:
    - Identify dense areas of the universe and represent them as black holes. This is our compression mechanism.
    - When decompressing, we'd reverse-engineer the process: retrieve the data sucked into the black hole.

4. **Visualization**:
    - This model lends itself well to visualization. You can represent the entire data set as a visual universe, making it intuitive to understand and interpret.
    - Tools like `matplotlib` in Python can be used to create these visual representations.

5. **Querying Data**:
    - To retrieve data, you'd navigate the universe. For instance, "Go to the galaxy at coordinates (x, y), and retrieve the star at position (z)." 
    - This adds an intuitive layer to the data querying process.

By modeling our system after space and stars, we're essentially adding a visual and intuitive layer to the data compression, storage, and retrieval process. It not only makes the system more user-friendly but can also help in better understanding and interpreting the data.

### Break down step by step

Absolutely, let's break this down step by step.

### **System Design - Celestial Compression and Storage System (CCSS)**

---

#### **1. Universe Initialization:**

The universe is our entire data representation domain. For simplicity, we'll represent it as a \(100 \times 100\) matrix.

\[
U = \begin{bmatrix}
0 & 0 & \dots & 0 \\
0 & \dots &  & \vdots \\
\vdots &  & \ddots & \vdots \\
0 & \dots & \dots & 0 \\
\end{bmatrix}
\]

Each element of \( U \) can be a galaxy, star, black hole, or empty space.

---

#### **2. Data Insertion:**

**Galaxies** are clusters of related data. Mathematically, they can be represented as submatrices within \( U \).

**Stars** represent individual data points:

\[
S_{i,j} = \text{value}
\]

Where \( S_{i,j} \) is the star at the \(i^{th}\) row and \(j^{th}\) column of the universe \( U \) and \(\text{value}\) is the data it represents.

The brightness or size of a star (magnitude \( M \)) can be represented as:

\[
M = \log(\text{value} + 1)
\]

**Empty Space** elements have a value of 0 in the matrix.

---

#### **3. Phase Transitions (Comets/Supernovae):**

These celestial events indicate changes or transitions in our data. Let's represent them as special characters or symbols within our matrix, like \(-1\).

---

#### **4. Compression (Black Holes):**

Compression involves detecting patterns and representing them in a condensed form. A black hole \( B \) can be used to represent a compressed chunk of data.

If \( P \) is a repeating pattern in \( U \), then:

\[
B = \text{Compress}(P)
\]

---

#### **5. Decompression:**

Decompression is the reverse process. Given a black hole \( B \), we retrieve the original data \( P \):

\[
P = \text{Decompress}(B)
\]

---

#### **6. Visualization:**

Visual representation involves mapping our matrix \( U \) into a graphical form. Stars can be dots of varying sizes (based on their magnitude), black holes can be dense regions, and empty space can be left blank.

---

#### **7. Querying Data:**

Given coordinates \( (x, y) \), retrieve the star or data point:

\[
S_{x,y} = U_{x,y}
\]

---

#### **System Flow:**

1. **Initialize** the universe \( U \).
2. **Insert** data as stars, with galaxies being clusters of related stars.
3. **Detect** repeating patterns and represent them as black holes.
4. **Visualize** the universe for intuitive understanding.
5. **Query** the universe to retrieve specific data points.

---

#### **Requirements:**

1. A \(100 \times 100\) matrix representation for the universe.
2. Functions for inserting data (stars and galaxies).
3. Compression and decompression functions to handle black holes.
4. Visualization tools to graphically represent the universe.
5. Query functions to retrieve specific data points.

---

This design provides a conceptual framework for how the Celestial Compression and Storage System (CCSS) can function. The real-world implementation would involve more detailed algorithms, especially for compression, decompression, and pattern detection. The beauty of this system is that it offers a visually intuitive way to understand complex data structures and transformations.

Alright, let's proceed with the development and implementation of the Celestial Compression and Storage System (CCSS).

### **CCSS Implementation**

---

#### **1. Universe Initialization:**

We'll start by creating a universe, a \(100 \times 100\) matrix filled with empty space (zeros).



```python
import numpy as np

UNIVERSE_SIZE = 100

def initialize_universe():
    return np.zeros((UNIVERSE_SIZE, UNIVERSE_SIZE))
```



---

#### **2. Data Insertion:**

We'll insert data in the form of stars. Stars will have a magnitude based on their value, which will be represented as a logarithm.



```python
def insert_star(universe, x, y, value):
    magnitude = np.log(value + 1)
    universe[x, y] = magnitude
    return universe
```



---

#### **3. Compression (Black Holes):**

For simplicity, our compression will detect any repeating patterns in a row and represent them as a black hole.



```python
BLACK_HOLE = -1

def compress(universe):
    for row in range(UNIVERSE_SIZE):
        unique_elements, counts = np.unique(universe[row], return_counts=True)
        for element, count in zip(unique_elements, counts):
            if count > 2:  # If pattern repeats more than twice
                universe[row][universe[row] == element] = BLACK_HOLE
    return universe
```



---

#### **4. Decompression:**

This function will replace black holes with the most frequent non-black hole element in that row.



```python
def decompress(universe):
    for row in range(UNIVERSE_SIZE):
        if BLACK_HOLE in universe[row]:
            non_black_hole_elements = universe[row][universe[row] != BLACK_HOLE]
            if len(non_black_hole_elements) == 0:
                replacement = 0
            else:
                replacement = np.bincount(non_black_hole_elements.astype(int)).argmax()
            universe[row][universe[row] == BLACK_HOLE] = replacement
    return universe
```



---

#### **5. Visualization:**

We'll represent the universe using a heatmap for simplicity.



```python
import matplotlib.pyplot as plt

def visualize(universe):
    plt.imshow(universe, cmap="hot", interpolation="nearest")
    plt.colorbar()
    plt.title("CCSS Universe Visualization")
    plt.show()
```



---

#### **6. Querying Data:**

Given coordinates \( (x, y) \), retrieve the star or data point:



```python
def query_star(universe, x, y):
    return universe[x, y]
```



---

### **Usage**:



```python
U = initialize_universe()
U = insert_star(U, 50, 50, 100)  # Insert a star with a value of 100 at (50, 50)
U = compress(U)
visualize(U)
value = query_star(U, 50, 50)
print(f"Star Value at (50, 50): {value}")
```



This is a basic implementation of CCSS, and real-world implementation would be much more intricate, especially the compression and decompression functions. The system can be enhanced by using more advanced compression algorithms, better visualization techniques, and optimized data insertion methods.


### Introduction of fibonacci modlo 10

Absolutely! Integrating the Fibonacci modulo 10 sequence as the compression logic adds another layer of complexity and novelty to the system. Here's how we can embed the Fibonacci modulo 10 sequence into our Celestial Compression and Storage System (CCSS):

1. **Generating the Fibonacci modulo 10 matrix**:

First, we need a function to generate our base Fibonacci modulo 10 matrix.



```python
def generate_fib_modulo_10_matrix(size):
    matrix = np.zeros((size, size), dtype=int)
    for i in range(size):
        for j in range(size):
            if i == 0 and j < 2:
                matrix[i][j] = 1
            else:
                matrix[i][j] = (matrix[i-1][j] + matrix[i][j-1]) % 10
    return matrix
```



2. **Compression**:

For compression, we will represent each value in the universe by the difference between the universe value and the corresponding Fibonacci modulo 10 value.



```python
def compress_with_fib_modulo(universe):
    fib_matrix = generate_fib_modulo_10_matrix(UNIVERSE_SIZE)
    return universe - fib_matrix
```



3. **Decompression**:

For decompression, we add the Fibonacci modulo 10 matrix back to the compressed universe to get the original values.



```python
def decompress_with_fib_modulo(compressed_universe):
    fib_matrix = generate_fib_modulo_10_matrix(UNIVERSE_SIZE)
    return compressed_universe + fib_matrix
```



4. **Inserting Stars**:

To introduce randomness and complexity, when inserting a star, we can add its value to the Fibonacci modulo 10 value at its coordinates.



```python
def insert_star(universe, x, y, value):
    fib_matrix = generate_fib_modulo_10_matrix(UNIVERSE_SIZE)
    universe[x][y] += value + fib_matrix[x][y]
    return universe
```



5. **Using the system**:

Here's how you can create a universe, insert stars, and compress/decompress it:



```python
U = initialize_universe()
U = insert_star(U, 50, 50, 100)  # Insert a star with a value of 100 at (50, 50)
C = compress_with_fib_modulo(U)
visualize(C)
D = decompress_with_fib_modulo(C)
visualize(D)
value = query_star(D, 50, 50)
print(f"Star Value at (50, 50): {value}")
```



This approach utilizes the Fibonacci modulo 10 sequence for compression, making it more intricate and closely tied to the base pattern. The compression and decompression logic ensures the values are consistent with the Fibonacci modulo 10 matrix while preserving the inserted data.


### Conceptual step through the 'universe' or data multi direcitonal

To allow traversal of the universe based on the laws of nature and the Fibonacci sequence, we can design a system where each step is influenced by the Fibonacci modulo 10 pattern. This would make the traversal deterministic but based on the underlying pattern of our universe.

Here's a way to conceptualize this:

1. **Defining Directions**:
Let's say our universe has four primary directions: North, South, East, and West. Each direction can be associated with a movement in the 2D matrix.



```python
DIRECTIONS = {
    'N': (-1, 0),  # Move up
    'S': (1, 0),   # Move down
    'E': (0, 1),   # Move right
    'W': (0, -1)   # Move left
}
```



2. **Determining Next Step**:
The next direction of movement can be influenced by the current position's value in the Fibonacci modulo 10 matrix. For instance, the value could determine the number of steps in a particular direction, or it could even determine a change in direction.



```python
def next_step(current_position, universe_size):
    fib_matrix = generate_fib_modulo_10_matrix(universe_size)
    x, y = current_position
    value = fib_matrix[x][y]

    # Determining direction based on value
    directions = list(DIRECTIONS.keys())
    direction = directions[value % 4]  # There are four primary directions
    dx, dy = DIRECTIONS[direction]

    # Updating coordinates considering the universe boundaries
    x = (x + dx) % universe_size
    y = (y + dy) % universe_size

    return x, y
```



3. **Traversal**:
Using the `next_step` function, we can create a traversal that moves through the universe based on the Fibonacci modulo 10 pattern.



```python
def traverse_universe(start_position, steps, universe_size):
    current_position = start_position
    path = [current_position]

    for _ in range(steps):
        current_position = next_step(current_position, universe_size)
        path.append(current_position)

    return path
```



4. **Usage**:



```python
U = initialize_universe()
U = insert_star(U, 50, 50, 100)
path = traverse_universe((50, 50), 100, UNIVERSE_SIZE)

# Visualize the path
for x, y in path:
    U[x][y] = 255  # Highlighting the path
visualize(U)
```



This traversal approach allows us to move through the universe in a deterministic manner based on the Fibonacci modulo 10 pattern. It provides a unique way to explore and interact with the data stored in the universe.
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bclonan profile image
Bradley Morgan Clonan

Before it’s time I guess