Skip to content

Commit

Permalink
feat: add sweater swatching functions/docs, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
terriko committed Mar 15, 2023
1 parent 7698bcd commit c411f0e
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 7 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,54 @@ jupyter-lab

## Using PyKnit

```python
import pyknit

# Sweater fit check math. My gauge doesn't match the pattern but I like the
# fabric I got in my swatch and don't want to change the needle I'm using.
# What size should I knit?

pattern_g = pyknit.GaugeSwatch(
stitch_count=27.5, stitch_measure=10, row_count=40, row_measure=4, units="in"
)
my_g = pyknit.GaugeSwatch(
stitch_count=23.5, stitch_measure=10, row_count=33, row_measure=4, units="in"
)

# The closest size to my measurements is the 42in chest one, let's convert that

size3 = pyknit.convert_stitch_measure(42, pattern_g, my_g)
print(f"{size3=}")
```

Running that gives a new size of 49 inches. This sweater was designed for +/- 2 inches (it says so in the pattern), so that's too big unless I love a really baggy sweater.


```python
size2 = pyknit.convert_stitch_measure(38, pattern_g, my_g)
size1 = pyknit.convert_stitch_measure(34, pattern_g, my_g)
print(f"{size2=}")
print(f"{size1=}")
```

Those give me 44.25 inches and 40 inches respectively (rounded to the nearest quarter inch, anyhow)

If I was aiming for exactly 42 inches chest circumference, I'd have the option
of either knitting the 44 inch one and having 2 inches positive ease (i.e. the
sweater would be a little loose) or the 40 inch one with -2 inches positive
ease (i.e. the sweater would stretch to fit me), and I would choose whichever
one I think I'd like better assuming my swatch is sufficiently stretchy.

This [SweaterFit Example file](https://github.com/terriko/pyknit/blob/main/documentation/SweaterFit.py) is here if you want to sub in your own swatch numbers.


[Here's an example of how to calculate sweater sleeve decreases using pyknit](https://github.com/terriko/pyknit/blob/main/documentation/SleeveDecreases.md) to get you started.

For those using Jupyter, there are also several full interactive notebooks available:

* [Sweater Sleeve Decreases](https://github.com/terriko/pyknit/blob/main/documentation/SleeveDecreases.ipynb)
* [Triangle Hat interactive hat pattern](https://github.com/terriko/pyknit/blob/main/documentation/TriangleHat.ipynb)
* [Sweater Fit from gauge swatches](https://github.com/terriko/pyknit/blob/main/documentation/SweaterFit.ipynb)

## Why pyKnit?

Expand Down
129 changes: 129 additions & 0 deletions documentation/SweaterFit.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "79c68bfb-df11-41d8-903c-595e35e89dd3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"49.191489361702125"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pyknit\n",
"\n",
"# Sweater fit check math. My gauge doesn't match the pattern but I like the fabric I got in my swatch and don't want to\n",
"# change the needle I'm using. What size should I knit?\n",
"\n",
"pattern_g = pyknit.GaugeSwatch(stitch_count = 27.5, stitch_measure = 4, row_count = 40, row_measure = 4, units = \"in\") \n",
"my_g = pyknit.GaugeSwatch(stitch_count = 23.5, stitch_measure = 4, row_count = 33, row_measure = 4, units = \"in\") \n",
"\n",
"# The closest size to my measurements is the 42in chest one, let's convert that\n",
"\n",
"pyknit.convert_stitch_measure(42, pattern_g, my_g)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e148ad75-18ee-43e2-9b67-4bf073eea761",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"44.42553191489362"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Okay, that's going to be rather big... what about the next size down?\n",
"\n",
"pyknit.convert_stitch_measure(38, pattern_g, my_g)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "829b714d-9dca-4440-abb7-e71a48475238",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"39.829787234042556"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# closer...\n",
"\n",
"pyknit.convert_stitch_measure(34, pattern_g, my_g)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "eba5a902-4585-410d-9391-b6f33964164b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# So if my chest measurement was exactly 42, I could either knit the 40in one and have -2 ease \n",
"# (i.e. the sweater will stretch over my chest) or 44 and have +2 ease (i.e. it'll be a bit loose)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "081deaa3-5aba-44fe-8583-89dcc1899fd6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
22 changes: 22 additions & 0 deletions documentation/SweaterFit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pyknit

# Sweater fit check math. My gauge doesn't match the pattern but I like the
# fabric I got in my swatch and don't want to change the needle I'm using.
# What size should I knit?

pattern_g = pyknit.GaugeSwatch(
stitch_count=27.5, stitch_measure=10, row_count=40, row_measure=4, units="in"
)
my_g = pyknit.GaugeSwatch(
stitch_count=23.5, stitch_measure=10, row_count=33, row_measure=4, units="in"
)

# The closest size to my measurements is the 42in chest one, let's convert that

size3 = pyknit.convert_stitch_measure(42, pattern_g, my_g)
print(f"{size3=}")

size2 = pyknit.convert_stitch_measure(38, pattern_g, my_g)
size1 = pyknit.convert_stitch_measure(34, pattern_g, my_g)
print(f"{size2=}")
print(f"{size1=}")
43 changes: 38 additions & 5 deletions pyknit/GaugeSwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@

class GaugeSwatch(BaseModel):
"""Information from a gauge swatch"""

row_count: PositiveFloat
row_measure: PositiveFloat
stitch_count: PositiveFloat
stitch_measure: PositiveFloat
units: Literal['cm', 'in']
units: Literal["cm", "in"]
# TODO: add yardage/weight for calculations?

def row_gauge(self) -> float:
""" return rows per unit (e.g. cm, inch) number """
"""return rows per unit (e.g. cm, inch) number"""
return self.row_count / self.row_measure

def stitch_gauge(self) -> float:
""" return stitches per unit (e.g. cm, inch) number """
"""return stitches per unit (e.g. cm, inch) number"""
return self.stitch_count / self.stitch_measure

@validate_arguments
Expand All @@ -50,12 +51,12 @@ def measurement_to_rows(self, measurement: PositiveFloat) -> int:

@validate_arguments
def rows_to_measurement(self, rows: PositiveInt) -> float:
""" figure out how long a number of rows will be """
"""figure out how long a number of rows will be"""
return rows / self.row_gauge()

@validate_arguments
def stitches_to_measurement(self, stitches: PositiveInt) -> float:
""" figure out how wide a number of stitches will be """
"""figure out how wide a number of stitches will be"""
return stitches / self.stitch_gauge()


Expand All @@ -68,3 +69,35 @@ def stitch_count(stitch_array: Set[str], legend: Set[str]) -> int:
return len(stitch_array)

# otherwise, assume every stitch has width=1

@validate_arguments
def convert_stitch_measure(
measurement: PositiveFloat, oldGauge: GaugeSwatch, newGauge: GaugeSwatch
) -> float:
"""
Given a masurement in the original gauge, find out what it would
be in the new gauge. e.g. if the sweater was going to be 40 inches
in pattern gauge, how much would it be in my gauge?
"""
# Convert my measurement to stitches in original gauge, then
# use the new gauge to convert the stitch count back to a measurement
return newGauge.stitches_to_measurement(
oldGauge.measurement_to_stitches(measurement)
)

@validate_arguments
def convert_row_measure(
measurement: PositiveFloat, oldGauge: GaugeSwatch, newGauge: GaugeSwatch
) -> float:
"""
Given a masurement in the original gauge, find out what it would
be in the new gauge. e.g. if the sweater was going to be 40 inches
in pattern gauge, how much would it be in my gauge?
"""
# Convert my measurement to stitches in original gauge, then
# use the new gauge to convert the stitch count back to a measurement
return newGauge.rows_to_measurement(
oldGauge.measurement_to_rows(measurement)
)


2 changes: 1 addition & 1 deletion pyknit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
root={"handlers": ["console"], "level": logging.DEBUG},
)

VERSION = "pyKnit 0.0.6"
VERSION = "pyKnit 0.0.7"

# Increase and decrease functions

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyknit"
version = "0.0.6"
version = "0.0.7"
authors = [
{ name="Terri Oda", email="terri@toybox.ca" },
]
Expand All @@ -15,6 +15,9 @@ classifiers = [
"Operating System :: OS Independent",
]

[project.scripts]
my-script = "pyknit.pyknit:main"

[project.urls]
"Homepage" = "https://github.com/terriko/pyknit"
"Bug Tracker" = "https://github.com/terriko/pyknit/issues"
Expand Down

0 comments on commit c411f0e

Please sign in to comment.