Skip to content

Commit

Permalink
abstracted SimpleSystem
Browse files Browse the repository at this point in the history
Signed-off-by: WithPrecedent <coreyrayburnyung@gmail.com>
  • Loading branch information
WithPrecedent committed Apr 7, 2020
1 parent f84d1fa commit 048c53c
Show file tree
Hide file tree
Showing 19 changed files with 2,166 additions and 1,766 deletions.
2 changes: 1 addition & 1 deletion examples/classification_cancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.path.insert(0, os.path.join('..', 'simplify'))
sys.path.insert(0, os.path.join('..', '..', 'simplify'))

from pathlib import pathlib.Path
import pathlib

import pandas as pd
import numpy as np
Expand Down
4 changes: 2 additions & 2 deletions simplify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from simplify.core.base import SimpleSystem
from simplify.core.base import SimpleCreator
from simplify.core.base import SimpleSimpleRepository
from simplify.core.base import SimpleContainer
from simplify.core.base import SimpleRepository
from simplify.core.base import SimpleComponent
from simplify.core.dataset import Dataset
from simplify.core.filer import Filer
Expand All @@ -26,7 +26,7 @@
'SimpleSystem',
'SimpleCreator',
'SimpleSimpleRepository',
'SimpleContainer',
'SimpleRepository',
'SimpleComponent',
'Dataset',
'Filer',
Expand Down
12 changes: 6 additions & 6 deletions simplify/analyst/analyst.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,20 @@ def add(self,

""" Technique Subclass and Decorator """

def numpy_shield(callable: Callable) -> Callable:
def numpy_shield(process: Callable) -> Callable:
"""
"""
@wraps(callable)
@wraps(process)
def wrapper(*args, **kwargs):
call_signature = signature(callable)
call_signature = signature(process)
arguments = dict(call_signature.bind(*args, **kwargs).arguments)
try:
x_columns = list(arguments['x'].columns.values)
result = callable(*args, **kwargs)
result = process(*args, **kwargs)
if isinstance(result, np.ndarray):
result = pd.DataFrame(result, columns = x_columns)
except KeyError:
result = callable(*args, **kwargs)
result = process(*args, **kwargs)
return result
return wrapper

Expand All @@ -204,7 +204,7 @@ class Tool(Technique):
module (Optional[str]): name of module where object to use is located
(can either be a siMpLify or non-siMpLify module). Defaults to
'simplify.core'.
algorithm (Optional[object]): callable object which executes the primary
algorithm (Optional[object]): process object which executes the primary
method of a class instance. Defaults to None.
parameters (Optional[Dict[str, Any]]): parameters to be attached to
'algorithm' when 'algorithm' is instanced. Defaults to an empty
Expand Down
142 changes: 142 additions & 0 deletions simplify/core/author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"""
.. module:: Worker
:synopsis: generic siMpLify manager
:publisher: Corey Rayburn Yung
:copyright: 2019-2020
:license: Apache-2.0
"""

import collections.abc
import dataclasses
import importlib
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union

from simplify.core import base
from simplify.core import utilities


@dataclasses.dataclass
class Author(base.SimpleHandler, base.SimpleProxy):
"""Generic subpackage controller class for siMpLify data projects.
Args:
name (Optional[str]): designates the name of the class instance used
for internal referencing throughout siMpLify. If the class
instance needs settings from the shared 'Idea' instance, 'name'
should match the appropriate section name in that 'Idea' instance.
When subclassing, it is a good idea to use the same 'name' attribute
as the base class for effective coordination between siMpLify
classes. Defaults to None or __class__.__name__.lower().
idea (Optional['Idea']): shared project configuration settings.
options (Optional['SimpleRepository']):
book (Optional['Book']):
auto_draft (Optional[bool]): whether to call the 'draft' method when
instanced. Defaults to True.
auto_publish (Optional[bool]): whether to call the 'publish' method when
instanced. Defaults to True.
auto_apply (Optional[bool]): whether to call the 'apply' method when
instanced. For auto_apply to have an effect, 'dataset' must also
be passed. Defaults to False.
"""
name: Optional[str] = None
idea: Optional['configuration.Idea'] = None
options: Optional['base.SimpleRepository'] = dataclasses.field(
default_factory = base.SimpleRepository)

def __post_init__(self) -> None:
"""Initializes class instance attributes."""
self.proxify(proxy = 'draft', attribute = 'apply')
return self

""" Core siMpLify Methods """

def outline(self) -> Dict[str, List[str]]:
"""Creates dictionary with techniques for each step.
Returns:
Dict[str, Dict[str, List[str]]]: dictionary with keys of steps and
values of lists of techniques.
"""
steps = self._get_settings(
section = self.name,
prefix = self.name,
suffix = 'steps')
return {
step: self._get_settings(
section = self.name,
prefix = step,
suffix = 'techniques')
for step in steps}

def draft(self) -> None:
"""Activates and instances select attributes."""
self.book = self.instructions.book()
# Drafts outline of methods to use.
if self.instructions.comparer:
self._draft_comparer()
else:
self._draft_sequencer()
return self

""" Private Methods """

def _get_settings(self,
section: str,
prefix: str,
suffix: str) -> List[str]:
"""Returns settings from 'idea' based on 'name' and 'suffix'.
Args:
section (str): outer key name in 'idea' section.
prefix (str); prefix for an inner key name.
suffix (str): suffix to inner key name in 'idea'.
Returns:
List[str]: names of matching workers, steps, or techniques.
"""
return utilities.listify(self.idea[section]['_'.join([prefix, suffix])])

def _draft_comparer(self) -> None:
"""Drafts 'Book' instance with a parallel chapter structure.
Args:
project ('Project'): an instance for a 'Book' instance to be
modified.
Returns:
'Project': with 'Book' instance modified.
"""
# Creates list of steps from 'project'.
steps = list(self.overview[self.name].keys())
# Creates 'possible' list of lists of 'techniques'.
possible = list(self.overview[self.name].values())
# Creates a list of lists of the Cartesian product of 'possible'.
combinations = list(map(list, itertools.product(*possible)))
# Creates Chapter instance for every combination of techniques.
for techniques in combinations:
step_pairs = tuple(zip(steps, techniques))
chapter = self.instructions.chapter(steps = step_pairs)
self.book.chapters.append(chapter)
return project

def _draft_sequencer(self) -> None:
"""Drafts 'Book' instance with a serial 'techniques' structure.
Args:
project ('Project'): an instance for a 'Book' instance to be
modified.
Returns:
'Project': with 'Book' instance modified.
"""
new_steps = []
for step, techniques in project.overview[self.worker.name].items():
for technique in techniques:
new_steps.append((step, technique))
project[self.worker.name].steps.extend(new_steps)
return project

0 comments on commit 048c53c

Please sign in to comment.