Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented a function to calculate the monotonicity of a curve file …
…in a specific format. Fixed the logger creating a handler every time it is called.
  • Loading branch information
pdz10001 committed Feb 16, 2018
1 parent 21835c5 commit 8377d07
Show file tree
Hide file tree
Showing 6 changed files with 1,423 additions and 3 deletions.
25 changes: 24 additions & 1 deletion cli_application.py
Expand Up @@ -7,9 +7,10 @@ import cmd
import os
import sys

from user_interface.curve_parser import parse_curve_file
from user_interface.curve_parser import parse_curve_file, parse_subdivided_curve_file
from logger import get_logger
from data_objects.subdivision_generator import generate_curve_values
from data_objects.monotonicity_calculator import calculate_monotonicity


class CliApplication(cmd.Cmd):
Expand Down Expand Up @@ -39,6 +40,21 @@ class CliApplication(cmd.Cmd):
self.generate_m_value(curve_path)
else:
self.logger.info("The curve file doesn't exist. Please choose an appropriate file.")

elif args.calculate_monotonicity:
self.logger.debug(args.calculate_monotonicity)
if os.path.exists(args.calculate_monotonicity):
if os.path.isdir(args.calculate_monotonicity):
self.logger.error("Please input a filename not a directory name.")
else:
self.calculate_monotonicity(args.calculate_monotonicity)
else:
curve_path = os.path.join(self.test_curves_path, args.calculate_monotonicity)
if os.path.exists(curve_path):
self.generate_m_value(curve_path)
else:
self.logger.info("The curve file doesn't exist. Please choose an appropriate file.")

else:
raise Exception("There were no arguments when running the application")

Expand All @@ -47,6 +63,12 @@ class CliApplication(cmd.Cmd):
generate_curve_values(knot_curve.get_points_as_np_arrays())
pass

def calculate_monotonicity(self, subdivided_file="subdivided_test.curve"):
subdivided_knot_curve_list = parse_subdivided_curve_file(subdivided_file)
for subdivided_knot_curve in subdivided_knot_curve_list:
calculate_monotonicity(subdivided_knot_curve)
pass


def main():
"""The main function that runs the Subdivision Generator application"""
Expand All @@ -58,6 +80,7 @@ def main():
description='Generates the required number of subdivisions for a Bezier curve to match the stick knot it was created from')
argument_parser.add_argument("-g", "--generate_m_value", help="generates the minimum number of iterations needed for the supplied \
stick knot's Bezier cure to match the stick knot", default=None, action="store")
argument_parser.add_argument("-m", "--calculate_monotonicity", help="calculates whether the coordinates of the subdivided curves in the file are monotonically increasing or decreasing", default=None, action="store")
arguments = argument_parser.parse_args()

get_logger().debug("Subdivision-Generator Starting Up")
Expand Down
18 changes: 18 additions & 0 deletions data_objects/monotonically_formatted_curve.py
@@ -0,0 +1,18 @@
#!/usr/bin/env python
"""
@author Peter Zaffetti 2018
"""
from stick_knot import StickKnot


class MonotonicallyFormattedCurve:

def __init__(self, knot_name, stick_knot):
self.knot_name = knot_name
self.stick_knot = stick_knot

def get_name(self):
return self.knot_name

def get_stick_knot(self):
return self.stick_knot
69 changes: 69 additions & 0 deletions data_objects/monotonicity_calculator.py
@@ -0,0 +1,69 @@
from monotonically_formatted_curve import MonotonicallyFormattedCurve
from logger import get_logger


# TODO: Refactor the hardcoded coordinate location: 0 should be a variable x, 1 should be y, and 2 should be z
def calculate_monotonicity(monotonically_formatted_curve):
curve_name = monotonically_formatted_curve.get_name()
stick_knot_points = monotonically_formatted_curve.get_stick_knot().get_points()

# PDZ- This will hold the monotonic status for all the coordinates. 0 means no monotonicity, 1 mean monotonically increasing, and -1 mean monotonically decreasing
coordinate_monotonic_status = [0, 0, 0]
for i in range(1, len(stick_knot_points) - 1):
if i == 1:
coordinate_monotonic_status[0] = calculate_coordinate_monotonicity(0, stick_knot_points[i - 1], stick_knot_points[i])
coordinate_monotonic_status[1] = calculate_coordinate_monotonicity(1, stick_knot_points[i - 1],
stick_knot_points[i])
coordinate_monotonic_status[2] = calculate_coordinate_monotonicity(2, stick_knot_points[i - 1],
stick_knot_points[i])

else:
# PDZ- Check each monotonicity status: if it differs then just set the coordinate to 0. Otherwise, leave it alone. At the end, any that are 0 aren't monotonic
if calculate_coordinate_monotonicity(0, stick_knot_points[i - 1], stick_knot_points[i]) != coordinate_monotonic_status[0]:
coordinate_monotonic_status[0] = 0 # PDZ- X coordinate was checked

if calculate_coordinate_monotonicity(1, stick_knot_points[i - 1], stick_knot_points[i]) != coordinate_monotonic_status[1]:
coordinate_monotonic_status[1] = 0 # PDZ- Y coordinate was checked

if calculate_coordinate_monotonicity(2, stick_knot_points[i - 1], stick_knot_points[i]) != coordinate_monotonic_status[2]:
coordinate_monotonic_status[2] = 0 # PDZ- Z coordinate was checked

logger = get_logger(calculate_monotonicity.__name__)

logger.info("%s, x: %s" % (curve_name, convert_int_to_monotonicity_str(coordinate_monotonic_status[0])))
logger.info("%s, y: %s" % (curve_name, convert_int_to_monotonicity_str(coordinate_monotonic_status[1])))
logger.info("%s, z: %s" % (curve_name, convert_int_to_monotonicity_str(coordinate_monotonic_status[2])))
logger.info("End of curve\n")


def convert_int_to_monotonicity_str(value):
if value == 0:
return "no"
if value == 1:
return "Strictly Mono Increasing"
if value == -1:
return "Strictly Mono Decreasing"

return "Something wasn't quite right"


def calculate_coordinate_monotonicity(index, stick_knot_point_behind, stick_knot_point_current):
prev_coord_val = 0
current_coord_val = 0

if index == 0:
prev_coord_val = stick_knot_point_behind.get_x()
current_coord_val = stick_knot_point_current.get_x()
elif index == 1:
prev_coord_val = stick_knot_point_behind.get_y()
current_coord_val = stick_knot_point_current.get_y()
elif index == 2:
prev_coord_val = stick_knot_point_behind.get_z()
current_coord_val = stick_knot_point_current.get_z()

if prev_coord_val < current_coord_val: # PDZ- Increasing since the point is bigger
return 1
elif prev_coord_val > current_coord_val: # PDZ- Decreasing
return -1
else: # PDZ- Same point
return 0
6 changes: 4 additions & 2 deletions logger.py
Expand Up @@ -29,12 +29,14 @@ def get_logger(classname=None):
file_handler = logging.FileHandler("logs/generator_log.txt")
file_handler.setFormatter(log_format)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
if len(logger.handlers) == 0: # PDZ- only add if there are no file handlers to begin with
logger.addHandler(file_handler)

console_handler = logging.StreamHandler()
console_handler.setFormatter(log_format)
console_handler.setLevel(logging.INFO)
logger.addHandler(console_handler)
if len(logger.handlers) == 1: # PDZ- only add if there is a file handler- this was causing a duplicate handler to be added every time a logger was gotten
logger.addHandler(console_handler)

logger.setLevel(logging.DEBUG)

Expand Down

0 comments on commit 8377d07

Please sign in to comment.